|
1 | 1 | import functools
|
2 | 2 |
|
| 3 | +import boto3 |
3 | 4 | from flask_sqlalchemy import BaseQuery, SQLAlchemy
|
4 | 5 | from sqlalchemy.dialects.postgresql import UUID
|
| 6 | +from sqlalchemy.engine import Engine |
| 7 | +from sqlalchemy.event import listens_for |
5 | 8 | from sqlalchemy.orm import object_session
|
6 | 9 | from sqlalchemy.pool import NullPool
|
7 | 10 | from sqlalchemy_searchable import SearchQueryMixin, make_searchable, vectorizer
|
@@ -42,6 +45,21 @@ def apply_pool_defaults(self, app, options):
|
42 | 45 | make_searchable(db.metadata, options={"regconfig": "pg_catalog.simple"})
|
43 | 46 |
|
44 | 47 |
|
| 48 | +# IAM database authentication for AWS RDS |
| 49 | +# See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html |
| 50 | +if settings.REDASH_DATABASE_IAM_AUTH: |
| 51 | + |
| 52 | + @listens_for(Engine, "do_connect") |
| 53 | + def db_connect_hook(dialect, conn_rec, cargs, cparams): |
| 54 | + rds_client = boto3.client("rds") |
| 55 | + auth_token = rds_client.generate_db_auth_token( |
| 56 | + DBHostname=cparams["host"], |
| 57 | + Port=cparams["port"], |
| 58 | + DBUsername=cparams["user"], |
| 59 | + ) |
| 60 | + cparams["password"] = auth_token |
| 61 | + |
| 62 | + |
45 | 63 | class SearchBaseQuery(BaseQuery, SearchQueryMixin):
|
46 | 64 | """
|
47 | 65 | The SQA query class to use when full text search is wanted.
|
|
0 commit comments