Skip to content

Commit b3ae212

Browse files
committed
Support AWS RDS IAM Authentication for PostgreSQK data soruce
1 parent e2437d8 commit b3ae212

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

poetry.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ debugpy = "^1.8.9"
9292
paramiko = "3.4.1"
9393
oracledb = "2.5.1"
9494
ibm-db = { version = "^3.2.7", markers = "platform_machine == 'x86_64' or platform_machine == 'AMD64'" }
95+
boto3 = "1.28.8"
96+
botocore = "1.31.8"
9597

9698
[tool.poetry.group.all_ds]
9799
optional = true
98100

99101
[tool.poetry.group.all_ds.dependencies]
100102
atsd-client = "3.0.5"
101103
azure-kusto-data = "5.0.1"
102-
boto3 = "1.28.8"
103-
botocore = "1.31.8"
104104
cassandra-driver = "3.29.3"
105105
certifi = ">=2019.9.11"
106106
cmem-cmempy = "21.2.3"

redash/query_runner/pg.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from tempfile import NamedTemporaryFile
66
from uuid import uuid4
77

8+
import boto3
89
import psycopg2
910
from psycopg2.extras import Range
1011

@@ -23,13 +24,6 @@
2324

2425
logger = logging.getLogger(__name__)
2526

26-
try:
27-
import boto3
28-
29-
IAM_ENABLED = True
30-
except ImportError:
31-
IAM_ENABLED = False
32-
3327
types_map = {
3428
20: TYPE_INTEGER,
3529
21: TYPE_INTEGER,
@@ -177,6 +171,8 @@ def configuration_schema(cls):
177171
"sslrootcertFile": {"type": "string", "title": "SSL Root Certificate"},
178172
"sslcertFile": {"type": "string", "title": "SSL Client Certificate"},
179173
"sslkeyFile": {"type": "string", "title": "SSL Client Key"},
174+
"awsIamAuth": {"type": "boolean", "title": "AWS IAM authentication"},
175+
"awsRegion": {"type": "string", "title": "AWS Region"},
180176
},
181177
"order": ["host", "port", "user", "password"],
182178
"required": ["dbname"],
@@ -186,6 +182,8 @@ def configuration_schema(cls):
186182
"sslrootcertFile",
187183
"sslcertFile",
188184
"sslkeyFile",
185+
"awsIamAuth",
186+
"awsRegion",
189187
],
190188
}
191189

@@ -255,11 +253,27 @@ def _get_tables(self, schema):
255253
def _get_connection(self):
256254
self.ssl_config = _get_ssl_config(self.configuration)
257255
self.dsn = _parse_dsn(self.configuration)
256+
257+
user = self.configuration.get("user")
258+
password = self.configuration.get("password")
259+
host = self.configuration.get("host")
260+
port = self.configuration.get("port", 5432)
261+
262+
if self.configuration.get("awsIamAuth", False):
263+
region_name = self.configuration.get("awsRegion")
264+
rds_client = boto3.client("rds", region_name=region_name)
265+
auth_token = rds_client.generate_db_auth_token(
266+
DBHostname=host,
267+
Port=port,
268+
DBUsername=user,
269+
)
270+
password = auth_token
271+
258272
connection = psycopg2.connect(
259-
user=self.configuration.get("user"),
260-
password=self.configuration.get("password"),
261-
host=self.configuration.get("host"),
262-
port=self.configuration.get("port"),
273+
user=user,
274+
password=password,
275+
host=host,
276+
port=port,
263277
dbname=self.configuration.get("dbname"),
264278
async_=True,
265279
**self.ssl_config,
@@ -426,7 +440,7 @@ def name(cls):
426440

427441
@classmethod
428442
def enabled(cls):
429-
return IAM_ENABLED
443+
return True
430444

431445
def _login_method_selection(self):
432446
if self.configuration.get("rolename"):

0 commit comments

Comments
 (0)