Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AWS RDS IAM Authentication for PostgreSQL data source #7304

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ tzlocal = "4.3.1"
pyodbc = "5.1.0"
debugpy = "^1.8.9"
paramiko = "3.4.1"
boto3 = "1.28.8"
botocore = "1.31.8"

[tool.poetry.group.all_ds]
optional = true

[tool.poetry.group.all_ds.dependencies]
atsd-client = "3.0.5"
azure-kusto-data = "0.0.35"
boto3 = "1.28.8"
botocore = "1.31.8"
cassandra-driver = "3.21.0"
certifi = ">=2019.9.11"
cmem-cmempy = "21.2.3"
Expand Down
38 changes: 26 additions & 12 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tempfile import NamedTemporaryFile
from uuid import uuid4

import boto3
import psycopg2
from psycopg2.extras import Range

Expand All @@ -23,13 +24,6 @@

logger = logging.getLogger(__name__)

try:
import boto3

IAM_ENABLED = True
except ImportError:
IAM_ENABLED = False

Copy link
Contributor Author

@winebarrel winebarrel Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following pull request moved boto3 to the main group, so remove the check for boto3.

types_map = {
20: TYPE_INTEGER,
21: TYPE_INTEGER,
Expand Down Expand Up @@ -167,6 +161,8 @@ def configuration_schema(cls):
"sslrootcertFile": {"type": "string", "title": "SSL Root Certificate"},
"sslcertFile": {"type": "string", "title": "SSL Client Certificate"},
"sslkeyFile": {"type": "string", "title": "SSL Client Key"},
"iamAuth": {"type": "boolean", "title": "IAM authentication"},
"awsRegion": {"type": "string", "title": "AWS Region"},
},
"order": ["host", "port", "user", "password"],
"required": ["dbname"],
Expand All @@ -176,6 +172,8 @@ def configuration_schema(cls):
"sslrootcertFile",
"sslcertFile",
"sslkeyFile",
"iamAuth",
"awsRegion",
],
}

Expand Down Expand Up @@ -251,11 +249,27 @@ def _get_tables(self, schema):

def _get_connection(self):
self.ssl_config = _get_ssl_config(self.configuration)

user = self.configuration.get("user")
password = self.configuration.get("password")
host = self.configuration.get("host")
port = self.configuration.get("port")

if self.configuration.get("iamAuth", False):
region_name = self.configuration.get("awsRegion")
rds_client = boto3.client("rds", region_name=region_name)
auth_token = rds_client.generate_db_auth_token(
DBHostname=host,
Port=port,
DBUsername=user,
)
password = auth_token

connection = psycopg2.connect(
user=self.configuration.get("user"),
password=self.configuration.get("password"),
host=self.configuration.get("host"),
port=self.configuration.get("port"),
user=user,
password=password,
host=host,
port=port,
dbname=self.configuration.get("dbname"),
async_=True,
**self.ssl_config,
Expand Down Expand Up @@ -421,7 +435,7 @@ def name(cls):

@classmethod
def enabled(cls):
return IAM_ENABLED
return True

def _login_method_selection(self):
if self.configuration.get("rolename"):
Expand Down
Loading