Skip to content

Commit 9e04ea9

Browse files
committed
Support AWS RDS IAM Authentication for Redash database
1 parent 85f0019 commit 9e04ea9

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ compose_build: .env
44
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose build
55

66
up:
7-
docker compose up -d redis postgres --remove-orphans
8-
docker compose exec -u postgres postgres psql postgres --csv \
9-
-1tqc "SELECT table_name FROM information_schema.tables WHERE table_name = 'organizations'" 2> /dev/null \
10-
| grep -q "organizations" || make create_database
11-
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose up -d --build --remove-orphans
7+
# docker compose up -d redis postgres --remove-orphans
8+
# docker compose exec -u postgres postgres psql postgres --csv \
9+
# -1tqc "SELECT table_name FROM information_schema.tables WHERE table_name = 'organizations'" 2> /dev/null \
10+
# | grep -q "organizations" || make create_database
11+
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose up --build --remove-orphans
1212

1313
test_db:
1414
@for i in `seq 1 5`; do \

compose.yaml

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ x-redash-environment: &redash-environment
1313
REDASH_HOST: http://localhost:5001
1414
REDASH_LOG_LEVEL: "INFO"
1515
REDASH_REDIS_URL: "redis://redis:6379/0"
16-
REDASH_DATABASE_URL: "postgresql://postgres@postgres/postgres"
16+
# REDASH_DATABASE_URL: "postgresql://postgres@postgres/postgres"
1717
REDASH_RATELIMIT_ENABLED: "false"
1818
REDASH_MAIL_DEFAULT_SENDER: "[email protected]"
1919
REDASH_MAIL_SERVER: "email"
@@ -26,7 +26,7 @@ services:
2626
<<: *redash-service
2727
command: dev_server
2828
depends_on:
29-
- postgres
29+
# - postgres
3030
- redis
3131
ports:
3232
- "5001:5000"
@@ -52,17 +52,17 @@ services:
5252
redis:
5353
image: redis:7-alpine
5454
restart: unless-stopped
55-
postgres:
56-
image: pgautoupgrade/pgautoupgrade:latest
57-
ports:
58-
- "15432:5432"
59-
# The following turns the DB into less durable, but gains significant performance improvements for the tests run (x3
60-
# improvement on my personal machine). We should consider moving this into a dedicated Docker Compose configuration for
61-
# tests.
62-
command: "postgres -c fsync=off -c full_page_writes=off -c synchronous_commit=OFF"
63-
restart: unless-stopped
64-
environment:
65-
POSTGRES_HOST_AUTH_METHOD: "trust"
55+
# postgres:
56+
# image: pgautoupgrade/pgautoupgrade:latest
57+
# ports:
58+
# - "15432:5432"
59+
# # The following turns the DB into less durable, but gains significant performance improvements for the tests run (x3
60+
# # improvement on my personal machine). We should consider moving this into a dedicated Docker Compose configuration for
61+
# # tests.
62+
# command: "postgres -c fsync=off -c full_page_writes=off -c synchronous_commit=OFF"
63+
# restart: unless-stopped
64+
# environment:
65+
# POSTGRES_HOST_AUTH_METHOD: "trust"
6666
email:
6767
image: maildev/maildev
6868
ports:

redash/models/base.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import boto3
12
import functools
23

34
from flask_sqlalchemy import BaseQuery, SQLAlchemy
45
from sqlalchemy.dialects.postgresql import UUID
6+
from sqlalchemy.engine import Engine
7+
from sqlalchemy.event import listens_for
58
from sqlalchemy.orm import object_session
69
from sqlalchemy.pool import NullPool
710
from sqlalchemy_searchable import SearchQueryMixin, make_searchable, vectorizer
@@ -41,6 +44,18 @@ def apply_pool_defaults(self, app, options):
4144
# and indexes for the full text search
4245
make_searchable(db.metadata, options={"regconfig": "pg_catalog.simple"})
4346

47+
if settings.REDASH_DATABASE_IAM_AUTH:
48+
49+
@listens_for(Engine, "do_connect")
50+
def db_connect_hook(dialect, conn_rec, cargs, cparams):
51+
rds_client = boto3.client("rds")
52+
auth_token = rds_client.generate_db_auth_token(
53+
DBHostname=cparams["host"],
54+
Port=cparams["port"],
55+
DBUsername=cparams["user"],
56+
)
57+
cparams["password"] = auth_token
58+
4459

4560
class SearchBaseQuery(BaseQuery, SearchQueryMixin):
4661
"""

redash/settings/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,6 @@ def email_server_is_configured():
459459

460460
# Email blocked domains, use delimiter comma to separated multiple domains
461461
BLOCKED_DOMAINS = set_from_string(os.environ.get("REDASH_BLOCKED_DOMAINS", "qq.com"))
462+
463+
# AWS
464+
REDASH_DATABASE_IAM_AUTH = parse_boolean(os.environ.get("REDASH_DATABASE_IAM_AUTH", "false"))

0 commit comments

Comments
 (0)