-
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 1 commit
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,29 @@ | ||
# 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.utils.dsql_token_utils import DSQLTokenUtils | ||
from aws_advanced_python_wrapper.iam_plugin import IamAuthPlugin | ||
from aws_advanced_python_wrapper.plugin import Plugin, PluginFactory | ||
from aws_advanced_python_wrapper.utils.properties import (Properties) | ||
|
||
if TYPE_CHECKING: | ||
from aws_advanced_python_wrapper.plugin_service import PluginService | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 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 abc import ABC, abstractmethod | ||
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: | ||
client = boto3.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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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 abc import ABC, abstractmethod | ||
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 RDSTokenUtils(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) | ||
|
||
try: | ||
session = client_session if client_session else boto3.Session() | ||
|
||
if credentials is not None: | ||
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. Can any of this common client acquisition logic be factored out into the base class? |
||
client = session.client( | ||
'rds', | ||
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( | ||
'rds', | ||
region_name=region | ||
) | ||
|
||
token = client.generate_db_auth_token( | ||
DBHostname=host_name, | ||
Port=port, | ||
DBUsername=user | ||
) | ||
|
||
client.close() | ||
|
||
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. Same with this, we should remove it. Ik it was here before but not a good idea to display it as it's part of the credentials. |
||
return token | ||
except Exception as ex: | ||
context.set_success(False) | ||
context.set_exception(ex) | ||
raise ex | ||
finally: | ||
context.close_context() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 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 abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING, Dict, Optional | ||
|
||
if TYPE_CHECKING: | ||
from aws_advanced_python_wrapper.plugin_service import PluginService | ||
from boto3 import Session | ||
|
||
class TokenUtils(ABC): | ||
@abstractmethod | ||
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: | ||
pass |
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: move the self to row above similar to other methods