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

added option to specify profile when connecting to redshift via IAM #2581

Merged
merged 7 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Next
- Added option to specify profile when connecting to Redshift via IAM


Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding this ### Next section at the top, can you add a ### Features section under the ## dbt 0.18.0 (Release TBD) line, and add your line there? Please also add a link to the issue/PR, like the other features listed!

Also, please add a new contributors section to the ## dbt 0.18.0 (Release TBD) section with your username and a link here. Check out lower down in the file for the exact formatting!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

## dbt 0.18.0 (Release TBD)

## dbt 0.18.0b1 (June 08, 2020)
Expand Down
23 changes: 20 additions & 3 deletions plugins/redshift/dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RedshiftCredentials(PostgresCredentials):
default=None,
metadata={'description': 'If using IAM auth, the name of the cluster'},
)
iam_profile: Optional[str] = None
iam_duration_seconds: int = 900
search_path: Optional[str] = None
keepalives_idle: int = 240
Expand All @@ -56,7 +57,12 @@ def type(self):

def _connection_keys(self):
keys = super()._connection_keys()
return keys + ('method', 'cluster_id', 'iam_duration_seconds')
return keys + (
'method',
'cluster_id',
'iam_profile',
'iam_duration_seconds'
)


class RedshiftConnectionManager(PostgresConnectionManager):
Expand Down Expand Up @@ -87,10 +93,20 @@ def fresh_transaction(self, name=None):

@classmethod
def fetch_cluster_credentials(cls, db_user, db_name, cluster_id,
duration_s, autocreate, db_groups):
iam_profile, duration_s, autocreate,
db_groups):
"""Fetches temporary login credentials from AWS. The specified user
must already exist in the database, or else an error will occur"""
boto_client = boto3.client('redshift')

if iam_profile is None:
boto_client = boto3.client('redshift')
else:
logger.debug("Connecting to Redshift using 'IAM'" +
f"with profile {iam_profile}")
boto_session = boto3.Session(
profile_name=iam_profile
)
boto_client = boto_session.client('redshift')

try:
return boto_client.get_cluster_credentials(
Expand Down Expand Up @@ -123,6 +139,7 @@ def get_tmp_iam_cluster_credentials(cls, credentials):
credentials.user,
credentials.database,
credentials.cluster_id,
credentials.iam_profile,
iam_duration_s,
credentials.autocreate,
credentials.db_groups,
Expand Down