-
Notifications
You must be signed in to change notification settings - Fork 10
Add support for DSQL iam authentication #919
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
base: main
Are you sure you want to change the base?
Changes from all commits
d22c555
b640243
7cc4801
37bfb90
da8f15c
b7338fa
f081739
7ce563e
5600acf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from aws_advanced_python_wrapper.iam_plugin import IamAuthPlugin | ||
from aws_advanced_python_wrapper.plugin import Plugin, PluginFactory | ||
from aws_advanced_python_wrapper.utils.dsql_token_utils import DSQLTokenUtils | ||
|
||
if TYPE_CHECKING: | ||
from aws_advanced_python_wrapper.plugin_service import PluginService | ||
from aws_advanced_python_wrapper.utils.properties import Properties | ||
|
||
|
||
class DsqlIamAuthPluginFactory(PluginFactory): | ||
def get_instance(self, plugin_service: PluginService, props: Properties) -> Plugin: | ||
return IamAuthPlugin(plugin_service, DSQLTokenUtils()) |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -31,6 +31,7 @@ | |||||||||||
from aws_advanced_python_wrapper.hostinfo import HostInfo | ||||||||||||
from aws_advanced_python_wrapper.pep249 import Connection | ||||||||||||
from aws_advanced_python_wrapper.plugin_service import PluginService | ||||||||||||
from aws_advanced_python_wrapper.utils.token_utils import TokenUtils | ||||||||||||
|
||||||||||||
import requests | ||||||||||||
|
||||||||||||
|
@@ -40,6 +41,7 @@ | |||||||||||
from aws_advanced_python_wrapper.utils.messages import Messages | ||||||||||||
from aws_advanced_python_wrapper.utils.properties import (Properties, | ||||||||||||
WrapperProperties) | ||||||||||||
from aws_advanced_python_wrapper.utils.rds_token_utils import RDSTokenUtils | ||||||||||||
from aws_advanced_python_wrapper.utils.rdsutils import RdsUtils | ||||||||||||
|
||||||||||||
logger = Logger(__name__) | ||||||||||||
|
@@ -51,12 +53,16 @@ class OktaAuthPlugin(Plugin): | |||||||||||
_rds_utils: RdsUtils = RdsUtils() | ||||||||||||
_token_cache: Dict[str, TokenInfo] = {} | ||||||||||||
|
||||||||||||
def __init__(self, plugin_service: PluginService, credentials_provider_factory: CredentialsProviderFactory, session: Optional[Session] = None): | ||||||||||||
def __init__(self, plugin_service: PluginService, | ||||||||||||
credentials_provider_factory: CredentialsProviderFactory, | ||||||||||||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
token_utils: TokenUtils, | ||||||||||||
session: Optional[Session] = None): | ||||||||||||
self._plugin_service = plugin_service | ||||||||||||
self._credentials_provider_factory = credentials_provider_factory | ||||||||||||
self._session = session | ||||||||||||
|
||||||||||||
self._region_utils = RegionUtils() | ||||||||||||
self._token_utils = token_utils | ||||||||||||
telemetry_factory = self._plugin_service.get_telemetry_factory() | ||||||||||||
self._fetch_token_counter = telemetry_factory.create_counter("okta.fetch_token.count") | ||||||||||||
self._cache_size_gauge = telemetry_factory.create_gauge("okta.token_cache.size", lambda: len(OktaAuthPlugin._token_cache)) | ||||||||||||
|
@@ -140,7 +146,7 @@ def _update_authentication_token(self, | |||||||||||
port: int = IamAuthUtils.get_port(props, host_info, self._plugin_service.database_dialect.default_port) | ||||||||||||
credentials: Optional[Dict[str, str]] = self._credentials_provider_factory.get_aws_credentials(region, props) | ||||||||||||
|
||||||||||||
token: str = IamAuthUtils.generate_authentication_token( | ||||||||||||
token: str = self._token_utils.generate_authentication_token( | ||||||||||||
self._plugin_service, | ||||||||||||
user, | ||||||||||||
host_info.host, | ||||||||||||
|
@@ -228,7 +234,7 @@ def get_saml_assertion(self, props: Properties): | |||||||||||
|
||||||||||||
class OktaAuthPluginFactory(PluginFactory): | ||||||||||||
def get_instance(self, plugin_service: PluginService, props: Properties) -> Plugin: | ||||||||||||
return OktaAuthPlugin(plugin_service, self.get_credentials_provider_factory(plugin_service, props)) | ||||||||||||
return OktaAuthPlugin(plugin_service, self.get_credentials_provider_factory(plugin_service, props), RDSTokenUtils()) | ||||||||||||
|
||||||||||||
def get_credentials_provider_factory(self, plugin_service: PluginService, props: Properties) -> OktaCredentialsProviderFactory: | ||||||||||||
return OktaCredentialsProviderFactory(plugin_service, props) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Dict, Optional | ||
|
||
from aws_advanced_python_wrapper.utils.log import Logger | ||
from aws_advanced_python_wrapper.utils.telemetry.telemetry import \ | ||
TelemetryTraceLevel | ||
from aws_advanced_python_wrapper.utils.token_utils import TokenUtils | ||
|
||
if TYPE_CHECKING: | ||
from aws_advanced_python_wrapper.plugin_service import PluginService | ||
from boto3 import Session | ||
|
||
import boto3 | ||
|
||
logger = Logger(__name__) | ||
|
||
|
||
class DSQLTokenUtils(TokenUtils): | ||
def generate_authentication_token( | ||
self, | ||
plugin_service: PluginService, | ||
user: Optional[str], | ||
host_name: Optional[str], | ||
port: Optional[int], | ||
region: Optional[str], | ||
credentials: Optional[Dict[str, str]] = None, | ||
client_session: Optional[Session] = None) -> str: | ||
telemetry_factory = plugin_service.get_telemetry_factory() | ||
context = telemetry_factory.open_telemetry_context("fetch authentication token", TelemetryTraceLevel.NESTED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to change this to "fetch DSQL authentication token" to distinguish it from regular IAM auth? Or should we leave it with the same name? |
||
|
||
try: | ||
session = client_session if client_session else boto3.Session() | ||
if credentials is not None: | ||
client = session.client( | ||
'dsql', | ||
region_name=region, | ||
aws_access_key_id=credentials.get('AccessKeyId'), | ||
aws_secret_access_key=credentials.get('SecretAccessKey'), | ||
aws_session_token=credentials.get('SessionToken') | ||
) | ||
else: | ||
client = session.client( | ||
'dsql', | ||
region_name=region | ||
) | ||
|
||
if user == "admin": | ||
token = client.generate_db_connect_admin_auth_token(host_name, region) | ||
else: | ||
token = client.generate_db_connect_auth_token(host_name, region) | ||
|
||
logger.debug("IamAuthUtils.GeneratedNewAuthToken", token) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In RdsTokenUtils we close the client before returning the token, should we do that here as well or no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is token credentials right? This is a security risk imo, I don't think it's a good idea even if it's debug logs. |
||
return token | ||
except Exception as ex: | ||
context.set_success(False) | ||
context.set_exception(ex) | ||
raise ex | ||
finally: | ||
context.close_context() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: