clear-skies-aws 2.0.3__py3-none-any.whl → 2.0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clear_skies_aws-2.0.3.dist-info → clear_skies_aws-2.0.5.dist-info}/METADATA +4 -2
- {clear_skies_aws-2.0.3.dist-info → clear_skies_aws-2.0.5.dist-info}/RECORD +20 -15
- clearskies_aws/__init__.py +2 -0
- clearskies_aws/backends/__init__.py +3 -3
- clearskies_aws/backends/dynamo_db_backend.py +1 -0
- clearskies_aws/backends/dynamo_db_condition_parser.py +1 -0
- clearskies_aws/backends/dynamo_db_parti_ql_backend.py +2 -0
- clearskies_aws/contexts/lambda_api_gateway_web_socket.py +2 -2
- clearskies_aws/contexts/lambda_invoke.py +2 -2
- clearskies_aws/contexts/lambda_sqs_standard.py +2 -2
- clearskies_aws/cursors/__init__.py +3 -0
- clearskies_aws/cursors/iam/__init__.py +7 -0
- clearskies_aws/cursors/iam/rds_mysql.py +125 -0
- clearskies_aws/cursors/port_forwarding/__init__.py +3 -0
- clearskies_aws/cursors/port_forwarding/ssm.py +137 -0
- clearskies_aws/di/aws_additional_config_auto_import.py +1 -1
- clearskies_aws/di/inject/boto3.py +1 -1
- clearskies_aws/endpoints/secrets_manager_rotation.py +1 -1
- {clear_skies_aws-2.0.3.dist-info → clear_skies_aws-2.0.5.dist-info}/WHEEL +0 -0
- {clear_skies_aws-2.0.3.dist-info → clear_skies_aws-2.0.5.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: clear-skies-aws
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.5
|
|
4
4
|
Summary: clearskies bindings for working in AWS
|
|
5
5
|
Project-URL: Repository, https://github.com/clearskies-py/clearskies-aws
|
|
6
6
|
Project-URL: Issues, https://github.com/clearskies-py/clearskies-aws/issues
|
|
@@ -17,10 +17,12 @@ Classifier: Programming Language :: Python :: 3
|
|
|
17
17
|
Requires-Python: <4.0,>=3.11
|
|
18
18
|
Requires-Dist: boto3<2.0.0,>=1.26.148
|
|
19
19
|
Requires-Dist: clear-skies<3.0.0,>=2.0.20
|
|
20
|
-
Requires-Dist: types-boto3[dynamodb,sns,sqs]<2.0.0,>=1.38.13
|
|
20
|
+
Requires-Dist: types-boto3[dynamodb,secretsmanager,ses,sns,sqs,ssm,stepfunctions]<2.0.0,>=1.38.13
|
|
21
21
|
Provides-Extra: akeyless
|
|
22
22
|
Requires-Dist: akeyless-cloud-id<0.5.0,>=0.2.3; extra == 'akeyless'
|
|
23
23
|
Requires-Dist: akeyless<6.0.0,>=5.0.0; extra == 'akeyless'
|
|
24
|
+
Provides-Extra: mysql
|
|
25
|
+
Requires-Dist: clear-skies[mysql]; extra == 'mysql'
|
|
24
26
|
Provides-Extra: ses
|
|
25
27
|
Requires-Dist: jinja2<4.0.0,>=3.1.2; extra == 'ses'
|
|
26
28
|
Description-Content-Type: text/markdown
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
clearskies_aws/__init__.py,sha256
|
|
1
|
+
clearskies_aws/__init__.py,sha256=o83_Jk53zPtFz-zYfJVtbwfw2NdATRziR0tvRijnxVs,396
|
|
2
2
|
clearskies_aws/actions/__init__.py,sha256=YsIi3ZTdByu4R7I77uOAXDE5hzB31J_tRrIoTxe_bjo,371
|
|
3
3
|
clearskies_aws/actions/action_aws.py,sha256=JMogBFIrN72h5oBQLbyMy0LmfVLrqvWCYLQDml1qO4M,4674
|
|
4
4
|
clearskies_aws/actions/assume_role.py,sha256=XNxbVju460aBMS8NQhY80eoNVQgZRJ6-OydsNOx3neA,4319
|
|
@@ -6,29 +6,34 @@ clearskies_aws/actions/ses.py,sha256=Cu8USID8vy2IZC6sFOIQRzv8a0LCMvYG15yn0l-fl5g
|
|
|
6
6
|
clearskies_aws/actions/sns.py,sha256=YS1TbEwtU-0lDbjG2HyTBs2J-ML5OL3ModAiGTMeK-c,2205
|
|
7
7
|
clearskies_aws/actions/sqs.py,sha256=r0z8njU87n09UgAq3l34JuNIbaBE85D_z8IE6ciIs9Q,3359
|
|
8
8
|
clearskies_aws/actions/step_function.py,sha256=Y6tGbQJIAD_IwV9ohwYfuv3vqtryTwpFTNGUFgdj_DQ,2689
|
|
9
|
-
clearskies_aws/backends/__init__.py,sha256=
|
|
9
|
+
clearskies_aws/backends/__init__.py,sha256=lmjWPoLN7Ebmdj3Pv5X1tJDcRWN3PsR8aGG9nDcqOrs,635
|
|
10
10
|
clearskies_aws/backends/backend.py,sha256=WpsoT0pZOdilvtOkbiNtk6DoOEzmjyWcCDfUCWOlToc,4316
|
|
11
|
-
clearskies_aws/backends/dynamo_db_backend.py,sha256=
|
|
12
|
-
clearskies_aws/backends/dynamo_db_condition_parser.py,sha256=
|
|
13
|
-
clearskies_aws/backends/dynamo_db_parti_ql_backend.py,sha256=
|
|
11
|
+
clearskies_aws/backends/dynamo_db_backend.py,sha256=0KfAow5pUXpiBg0rRWtG3j48mpHUtJP9q-C2FfpBeqA,29411
|
|
12
|
+
clearskies_aws/backends/dynamo_db_condition_parser.py,sha256=796BhrqqZy_lW4wVYbDKK85oVVKa0YnWbtl4Bb0RNF0,12637
|
|
13
|
+
clearskies_aws/backends/dynamo_db_parti_ql_backend.py,sha256=p0SdrRq467w0w5OxYscxPfqUOjrkZ41JLmJa3KQCXq8,46097
|
|
14
14
|
clearskies_aws/backends/sqs_backend.py,sha256=kHTzgBwpYzV31UcGaoSUcC_7eZEwm-GsCHvXFM1OLT0,2152
|
|
15
15
|
clearskies_aws/configs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
16
|
clearskies_aws/contexts/__init__.py,sha256=kBLUlkpIp88n5RgbE9gVi4rDnJou81vCE5g_aiv2v8Y,717
|
|
17
17
|
clearskies_aws/contexts/cli_web_socket_mock.py,sha256=XjsFcx0YlQqjb9Hh8q2NUW7T2Exu7_FHx9v_Am0Uq0g,784
|
|
18
18
|
clearskies_aws/contexts/lambda_alb.py,sha256=hNMWYePN_moC4waXdydAp7dJkGpSowxDZZzU39Sxc7I,3426
|
|
19
19
|
clearskies_aws/contexts/lambda_api_gateway.py,sha256=HWH-CAX_aYiDhQr8rBhR5TW99-wx2ODsyI_dqGJkcdw,3354
|
|
20
|
-
clearskies_aws/contexts/lambda_api_gateway_web_socket.py,sha256=
|
|
21
|
-
clearskies_aws/contexts/lambda_invoke.py,sha256=
|
|
20
|
+
clearskies_aws/contexts/lambda_api_gateway_web_socket.py,sha256=0zrBF3ShxKcBoG9UFGENUrjYa9-F-f4iywYwa9Z6lc0,4583
|
|
21
|
+
clearskies_aws/contexts/lambda_invoke.py,sha256=hvW05FtyiraAvhQiP5DX77LhYoRMgFTY4Tv6HhHH9C8,4872
|
|
22
22
|
clearskies_aws/contexts/lambda_sns.py,sha256=_TyPqRWosfaJzreYGNabvR-XTLYYO4eVWMVINSIY-OE,4129
|
|
23
|
-
clearskies_aws/contexts/lambda_sqs_standard.py,sha256=
|
|
23
|
+
clearskies_aws/contexts/lambda_sqs_standard.py,sha256=sekaPlCRvuHk-E8ohfFQkC2gAfO-DnTC7uLiIKfGY0E,5293
|
|
24
|
+
clearskies_aws/cursors/__init__.py,sha256=vy-WYxNEy5nd9fqY0En3T6WhE3omoV1tE-A5Uz26cCk,94
|
|
25
|
+
clearskies_aws/cursors/iam/__init__.py,sha256=dY410gfPoMXB42jhmamqD5IiCeopDnxr5wIibpqaYcY,127
|
|
26
|
+
clearskies_aws/cursors/iam/rds_mysql.py,sha256=bdAYTLg9t8DsoSi0otY9bmIgLCbrezczxd_3hWuEkeE,5639
|
|
27
|
+
clearskies_aws/cursors/port_forwarding/__init__.py,sha256=LBcFYeIIfmGhxf3Ezn1KChOUdkpF5AjJL1xE_ak-x3s,78
|
|
28
|
+
clearskies_aws/cursors/port_forwarding/ssm.py,sha256=RJXU6meeX9GNSbcxTGf6c9LcI4I0_wvYHnS_DzRSEXM,4498
|
|
24
29
|
clearskies_aws/di/__init__.py,sha256=pLHSIKxS1oELOgttRuwM0yXdJRxjZKXQ6tPxme2db0U,222
|
|
25
|
-
clearskies_aws/di/aws_additional_config_auto_import.py,sha256=
|
|
30
|
+
clearskies_aws/di/aws_additional_config_auto_import.py,sha256=94h_YsPBcdwMhqn0VAAfId1jLL5vCsk76kUrr-6ET_U,1275
|
|
26
31
|
clearskies_aws/di/inject/__init__.py,sha256=5_x5_BBQwC6J4k5YLdTm1DfIDM-95zXz1L5a1nMrlrY,186
|
|
27
|
-
clearskies_aws/di/inject/boto3.py,sha256=
|
|
32
|
+
clearskies_aws/di/inject/boto3.py,sha256=yUDiEpR2Si6pKcLrqMOlQEUU0pi6MS1tXNdoyC2mjwk,408
|
|
28
33
|
clearskies_aws/di/inject/boto3_session.py,sha256=11UYHz5kgrrx5lawoYaOFBm-QIoa45YUCMAOn4gT8Jo,383
|
|
29
34
|
clearskies_aws/di/inject/parameter_store.py,sha256=g0uAVwQEywLO9bCcYLbDKuyYnYgVyrfcYsOBJWYGads,475
|
|
30
35
|
clearskies_aws/endpoints/__init__.py,sha256=OUL_nhtuNs62BvQeVtC9xP_e9Hs_-qjANvb81vdLdrc,61
|
|
31
|
-
clearskies_aws/endpoints/secrets_manager_rotation.py,sha256=
|
|
36
|
+
clearskies_aws/endpoints/secrets_manager_rotation.py,sha256=c__4N-Z4qZ9XkzMMyIcxyPjOIxdO801FAJp6oX6n0a4,7761
|
|
32
37
|
clearskies_aws/endpoints/simple_body_routing.py,sha256=B3fnfxUEMuNpzc26Pzly6618DFU9fpaCk8KhTGSaptE,1181
|
|
33
38
|
clearskies_aws/input_outputs/__init__.py,sha256=RTDFwhPWZ2S0tZQiIPH0Tkj2xF-9qBjZte_CA2cmGt8,743
|
|
34
39
|
clearskies_aws/input_outputs/cli_web_socket_mock.py,sha256=cp0MaJjVnsXE1rx5K44lpN9uHKo3MOAsNxVQ3AsJOi4,547
|
|
@@ -57,7 +62,7 @@ clearskies_aws/secrets/additional_configs/iam_db_auth.py,sha256=PwyiLaacpRfhBKzQ
|
|
|
57
62
|
clearskies_aws/secrets/additional_configs/iam_db_auth_with_ssm.py,sha256=ABY29X-YvrE6vvNo6kVdf4DqyRNq5cFR5SfK7MNkltE,3463
|
|
58
63
|
clearskies_aws/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py,sha256=mLaplwvJLSbGh6oXgdOKL9Mv-6hLv5OUYCfEwHbHvLE,3700
|
|
59
64
|
clearskies_aws/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssm_bastion.py,sha256=2VHOwto4I9gBwrpd2HGpL-Wr0T2S-jFjUhe2Ib8hNJ8,6596
|
|
60
|
-
clear_skies_aws-2.0.
|
|
61
|
-
clear_skies_aws-2.0.
|
|
62
|
-
clear_skies_aws-2.0.
|
|
63
|
-
clear_skies_aws-2.0.
|
|
65
|
+
clear_skies_aws-2.0.5.dist-info/METADATA,sha256=CXZjCcG1-cVFBHHXsXu0Sn3u3Bf5gMpmfGzaPh_OLPc,9084
|
|
66
|
+
clear_skies_aws-2.0.5.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
67
|
+
clear_skies_aws-2.0.5.dist-info/licenses/LICENSE,sha256=MkEX8JF8kZxdyBpTTcB0YTd-xZpWnHvbRlw-pQh8u58,1069
|
|
68
|
+
clear_skies_aws-2.0.5.dist-info/RECORD,,
|
clearskies_aws/__init__.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from clearskies_aws.backends.backend import Backend
|
|
4
|
-
from clearskies_aws.backends.dynamo_db_backend import DynamoDBBackend
|
|
5
|
-
from clearskies_aws.backends.dynamo_db_condition_parser import DynamoDBConditionParser
|
|
6
|
-
from clearskies_aws.backends.dynamo_db_parti_ql_backend import (
|
|
4
|
+
from clearskies_aws.backends.dynamo_db_backend import DynamoDBBackend # type: ignore
|
|
5
|
+
from clearskies_aws.backends.dynamo_db_condition_parser import DynamoDBConditionParser # type: ignore
|
|
6
|
+
from clearskies_aws.backends.dynamo_db_parti_ql_backend import ( # type: ignore
|
|
7
7
|
DynamoDBPartiQLBackend,
|
|
8
8
|
DynamoDBPartiQLCursor,
|
|
9
9
|
)
|
|
@@ -73,7 +73,7 @@ class LambdaApiGatewayWebSocket(Context):
|
|
|
73
73
|
|
|
74
74
|
"""
|
|
75
75
|
|
|
76
|
-
def __call__(
|
|
76
|
+
def __call__( # type: ignore[override]
|
|
77
77
|
self, event: dict[str, Any], context: dict[str, Any], url: str = "", request_method: str = ""
|
|
78
|
-
) -> dict[str, Any]:
|
|
78
|
+
) -> dict[str, Any]:
|
|
79
79
|
return self.execute_application(LambdaApiGatewayWebSocketInputOutput(event, context, url))
|
|
@@ -125,9 +125,9 @@ class LambdaInvoke(Context):
|
|
|
125
125
|
|
|
126
126
|
"""
|
|
127
127
|
|
|
128
|
-
def __call__(
|
|
128
|
+
def __call__( # type: ignore[override]
|
|
129
129
|
self, event: dict[str, Any], context: dict[str, Any], request_method: str = "", url: str = ""
|
|
130
|
-
) -> dict[str, Any]:
|
|
130
|
+
) -> dict[str, Any]:
|
|
131
131
|
return self.execute_application(
|
|
132
132
|
LambdaInvokeInputOutput(
|
|
133
133
|
event,
|
|
@@ -120,9 +120,9 @@ class LambdaSqsStandard(Context):
|
|
|
120
120
|
|
|
121
121
|
"""
|
|
122
122
|
|
|
123
|
-
def __call__(
|
|
123
|
+
def __call__( # type: ignore[override]
|
|
124
124
|
self, event: dict[str, Any], context: dict[str, Any], url: str = "", request_method: str = ""
|
|
125
|
-
) -> dict[str, Any]:
|
|
125
|
+
) -> dict[str, Any]:
|
|
126
126
|
item_failures = []
|
|
127
127
|
for record in event["Records"]:
|
|
128
128
|
try:
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""
|
|
2
|
+
RdsMySql: MySQL cursor with AWS RDS IAM authentication.
|
|
3
|
+
|
|
4
|
+
This class provides a MySQL cursor that uses AWS RDS IAM DB authentication.
|
|
5
|
+
It loads connection parameters from environment variables and generates a temporary
|
|
6
|
+
IAM authentication token for secure database access.
|
|
7
|
+
|
|
8
|
+
Configuration fields:
|
|
9
|
+
- boto3: Injected boto3 provider for AWS API access.
|
|
10
|
+
- environment: Injected environment variable provider.
|
|
11
|
+
- hostname_environment_key: Environment variable for DB host (default: "DATABASE_HOST").
|
|
12
|
+
- username_environment_key: Environment variable for DB user (default: "DATABASE_USERNAME").
|
|
13
|
+
- database_environment_key: Environment variable for DB name (default: "DATABASE_NAME").
|
|
14
|
+
- port_environment_key: Environment variable for DB port (default: "DATABASE_PORT").
|
|
15
|
+
- cert_path_environment_key: Environment variable for SSL CA cert (default: "DATABASE_CERT_PATH").
|
|
16
|
+
- autocommit_environment_key: Environment variable for autocommit (default: "DATABASE_AUTOCOMMIT").
|
|
17
|
+
- connect_timeout_environment_key: Environment variable for connect timeout (default: "DATABASE_CONNECT_TIMEOUT").
|
|
18
|
+
- database_region_key: Environment variable for AWS region (default: "DATABASE_REGION").
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
import clearskies_aws.cursors.iam.rds_mysql
|
|
22
|
+
|
|
23
|
+
cursor = clearskies_aws.cursors.iam.rds_mysql.RdsMySql()
|
|
24
|
+
cursor.execute("SELECT 1")
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import os
|
|
28
|
+
from typing import Any
|
|
29
|
+
|
|
30
|
+
import clearskies
|
|
31
|
+
from clearskies import decorators
|
|
32
|
+
from clearskies.cursors import Mysql
|
|
33
|
+
|
|
34
|
+
from clearskies_aws.di import inject
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RdsMysql(Mysql):
|
|
38
|
+
"""MySQL cursor with AWS RDS IAM DB authentication."""
|
|
39
|
+
|
|
40
|
+
"""Injected boto3 provider for AWS API access."""
|
|
41
|
+
boto3 = inject.Boto3()
|
|
42
|
+
|
|
43
|
+
"""Injected environment variable provider."""
|
|
44
|
+
environment = clearskies.di.inject.Environment()
|
|
45
|
+
|
|
46
|
+
"""Environment variable for DB host (default: "DATABASE_HOST")."""
|
|
47
|
+
hostname_environment_key = clearskies.configs.String(default="DATABASE_HOST")
|
|
48
|
+
|
|
49
|
+
"""Environment variable for DB user (default: "DATABASE_USERNAME")."""
|
|
50
|
+
username_environment_key = clearskies.configs.String(default="DATABASE_USERNAME")
|
|
51
|
+
|
|
52
|
+
"""Environment variable for DB name (default: "DATABASE_NAME")."""
|
|
53
|
+
database_environment_key = clearskies.configs.String(default="DATABASE_NAME")
|
|
54
|
+
|
|
55
|
+
"""Environment variable for DB port (default: "DATABASE_PORT")."""
|
|
56
|
+
port_environment_key = clearskies.configs.String(default="DATABASE_PORT")
|
|
57
|
+
|
|
58
|
+
"""Environment variable for SSL CA cert (default: "DATABASE_CERT_PATH")."""
|
|
59
|
+
cert_path_environment_key = clearskies.configs.String(default="DATABASE_CERT_PATH")
|
|
60
|
+
|
|
61
|
+
"""Environment variable for autocommit (default: "DATABASE_AUTOCOMMIT")."""
|
|
62
|
+
autocommit_environment_key = clearskies.configs.String(default="DATABASE_AUTOCOMMIT")
|
|
63
|
+
|
|
64
|
+
"""Environment variable for connect timeout (default: "DATABASE_CONNECT_TIMEOUT")."""
|
|
65
|
+
connect_timeout_environment_key = clearskies.configs.String(default="DATABASE_CONNECT_TIMEOUT")
|
|
66
|
+
|
|
67
|
+
"""Environment variable for AWS region (default: "DATABASE_REGION")."""
|
|
68
|
+
database_region_key = clearskies.configs.String(default="DATABASE_REGION")
|
|
69
|
+
|
|
70
|
+
@decorators.parameters_to_properties
|
|
71
|
+
def __init__(
|
|
72
|
+
self,
|
|
73
|
+
hostname_environment_key: str | None = None,
|
|
74
|
+
username_environment_key: str | None = None,
|
|
75
|
+
database_environment_key: str | None = None,
|
|
76
|
+
port_environment_key: str | None = None,
|
|
77
|
+
cert_path_environment_key: str | None = None,
|
|
78
|
+
autocommit_environment_key: str | None = None,
|
|
79
|
+
database_region_key: str | None = None,
|
|
80
|
+
connect_timeout_environment_key: str | None = None,
|
|
81
|
+
port_forwarding: Any | None = None,
|
|
82
|
+
):
|
|
83
|
+
self.finalize_and_validate_configuration()
|
|
84
|
+
|
|
85
|
+
def build_connection_kwargs(self) -> dict:
|
|
86
|
+
"""
|
|
87
|
+
Build the connection kwargs for the MySQL client, using IAM DB authentication.
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
dict
|
|
92
|
+
Connection parameters for the MySQL client.
|
|
93
|
+
"""
|
|
94
|
+
connection_kwargs = {
|
|
95
|
+
"user": self.environment.get(self.username_environment_key),
|
|
96
|
+
"host": self.environment.get(self.hostname_environment_key),
|
|
97
|
+
"database": self.environment.get(self.database_environment_key),
|
|
98
|
+
"port": int(self.environment.get(self.port_environment_key, silent=True) or self.port),
|
|
99
|
+
"ssl_ca": self.environment.get(self.cert_path_environment_key, silent=True),
|
|
100
|
+
"autocommit": self.environment.get(self.autocommit_environment_key, silent=True),
|
|
101
|
+
"connect_timeout": int(
|
|
102
|
+
self.environment.get(self.connect_timeout_environment_key, silent=True) or self.connect_timeout
|
|
103
|
+
),
|
|
104
|
+
}
|
|
105
|
+
region: str = self.environment.get(self.database_region_key, True) or self.environment.get("AWS_REGION", True)
|
|
106
|
+
if not region:
|
|
107
|
+
raise ValueError(
|
|
108
|
+
"To use RDS IAM DB auth you must set DATABASE_REGION or AWS_REGION in the .env file or an environment variable"
|
|
109
|
+
)
|
|
110
|
+
os.environ["LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN"] = "1"
|
|
111
|
+
|
|
112
|
+
rds_api = self.boto3.Session().client("rds")
|
|
113
|
+
rds_token = rds_api.generate_db_auth_token(
|
|
114
|
+
DBHostname=connection_kwargs.get("host"),
|
|
115
|
+
Port=connection_kwargs.get("port", 3306),
|
|
116
|
+
DBUsername=connection_kwargs.get("user"),
|
|
117
|
+
Region=region,
|
|
118
|
+
)
|
|
119
|
+
connection_kwargs["password"] = rds_token
|
|
120
|
+
|
|
121
|
+
for kwarg in ["autocommit", "connect_timeout", "port", "ssl_ca"]:
|
|
122
|
+
if not connection_kwargs[kwarg]:
|
|
123
|
+
del connection_kwargs[kwarg]
|
|
124
|
+
|
|
125
|
+
return {**super().build_connection_kwargs(), **connection_kwargs}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import time
|
|
2
|
+
|
|
3
|
+
import clearskies.configs
|
|
4
|
+
from clearskies import decorators
|
|
5
|
+
from clearskies.cursors.port_forwarding.port_forwarder import PortForwarder
|
|
6
|
+
|
|
7
|
+
from clearskies_aws.di import inject
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Ssm(PortForwarder):
|
|
11
|
+
"""
|
|
12
|
+
Port forwarder using AWS SSM Session Manager.
|
|
13
|
+
|
|
14
|
+
This class sets up a local port forwarding tunnel to a remote host using AWS SSM.
|
|
15
|
+
If instance_id is not provided, it will search for a running instance by Name tag.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
"""
|
|
19
|
+
The EC2 instance ID to connect to. If not provided, instance_name will be used to look up the instance.
|
|
20
|
+
"""
|
|
21
|
+
instance_id = clearskies.configs.String(default=None)
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
The Name tag of the EC2 instance to search for if instance_id is not provided.
|
|
25
|
+
"""
|
|
26
|
+
instance_name = clearskies.configs.String(default=None)
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
The remote port to forward to.
|
|
30
|
+
"""
|
|
31
|
+
remote_port = clearskies.configs.Integer()
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
The local port to bind for the forwarding tunnel (default: 0, auto-selects a free port).
|
|
35
|
+
"""
|
|
36
|
+
local_port = clearskies.configs.Integer(default=0)
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
AWS region.
|
|
40
|
+
"""
|
|
41
|
+
region = clearskies.configs.String(default=None)
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
AWS CLI profile.
|
|
45
|
+
"""
|
|
46
|
+
profile = clearskies.configs.String(default=None)
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
Boto3 session or client provider
|
|
50
|
+
"""
|
|
51
|
+
boto3 = inject.Boto3()
|
|
52
|
+
|
|
53
|
+
@decorators.parameters_to_properties
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
instance_id=None,
|
|
57
|
+
instance_name=None,
|
|
58
|
+
remote_port=None,
|
|
59
|
+
local_port=0,
|
|
60
|
+
region=None,
|
|
61
|
+
profile=None,
|
|
62
|
+
):
|
|
63
|
+
self._proc = None
|
|
64
|
+
self.finalize_and_validate_configuration()
|
|
65
|
+
|
|
66
|
+
def setup(self, original_host: str, original_port: int) -> tuple[str, int]:
|
|
67
|
+
"""
|
|
68
|
+
Establish the port forwarding tunnel and return the local endpoint.
|
|
69
|
+
|
|
70
|
+
If instance_id is not set, searches for a running instance by Name tag.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
A tuple containing the local host and local port to connect to (e.g., ("localhost", 12345)).
|
|
74
|
+
"""
|
|
75
|
+
# Resolve instance_id if needed
|
|
76
|
+
if not self.instance_id and self.instance_name:
|
|
77
|
+
ec2_api = self.boto3.client("ec2", region_name=self.region)
|
|
78
|
+
running_instances = ec2_api.describe_instances(
|
|
79
|
+
Filters=[
|
|
80
|
+
{"Name": "tag:Name", "Values": [self.instance_name]},
|
|
81
|
+
{"Name": "instance-state-name", "Values": ["running"]},
|
|
82
|
+
]
|
|
83
|
+
)
|
|
84
|
+
instance_ids = []
|
|
85
|
+
for reservation in running_instances["Reservations"]:
|
|
86
|
+
for instance in reservation["Instances"]:
|
|
87
|
+
instance_ids.append(instance["InstanceId"])
|
|
88
|
+
if len(instance_ids) == 0:
|
|
89
|
+
raise ValueError("Failed to launch SSM tunnel! Cannot find bastion!")
|
|
90
|
+
self.instance_id = instance_ids.pop()
|
|
91
|
+
|
|
92
|
+
if self.local_port == 0:
|
|
93
|
+
self.local_port = self.pick_free_port("127.0.0.1")
|
|
94
|
+
|
|
95
|
+
if self.is_port_open("127.0.0.1", self.local_port):
|
|
96
|
+
return "127.0.0.1", self.local_port
|
|
97
|
+
|
|
98
|
+
ssm_cmd = [
|
|
99
|
+
"aws",
|
|
100
|
+
"ssm",
|
|
101
|
+
"start-session",
|
|
102
|
+
"--target",
|
|
103
|
+
self.instance_id,
|
|
104
|
+
"--document-name",
|
|
105
|
+
"AWS-StartPortForwardingSession",
|
|
106
|
+
"--parameters",
|
|
107
|
+
f"portNumber={self.remote_port},localPort={self.local_port}",
|
|
108
|
+
]
|
|
109
|
+
if self.region:
|
|
110
|
+
ssm_cmd += ["--region", self.region]
|
|
111
|
+
if self.profile:
|
|
112
|
+
ssm_cmd += ["--profile", self.profile]
|
|
113
|
+
|
|
114
|
+
self._proc = self.subprocess.Popen(ssm_cmd, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE)
|
|
115
|
+
|
|
116
|
+
start = time.time()
|
|
117
|
+
while True:
|
|
118
|
+
try:
|
|
119
|
+
test_sock = self.socket.socket(self.socket.AF_INET, self.socket.SOCK_STREAM)
|
|
120
|
+
test_sock.settimeout(0.2)
|
|
121
|
+
test_sock.connect(("127.0.0.1", self.local_port))
|
|
122
|
+
test_sock.close()
|
|
123
|
+
break
|
|
124
|
+
except Exception:
|
|
125
|
+
if self._proc is not None and self._proc.poll() is not None:
|
|
126
|
+
raise RuntimeError("SSM process exited unexpectedly")
|
|
127
|
+
if time.time() - start > 10:
|
|
128
|
+
raise TimeoutError(f"Timeout waiting for port {self.local_port} to open")
|
|
129
|
+
time.sleep(0.1)
|
|
130
|
+
|
|
131
|
+
return "127.0.0.1", self.local_port
|
|
132
|
+
|
|
133
|
+
def teardown(self):
|
|
134
|
+
if self._proc:
|
|
135
|
+
self._proc.terminate()
|
|
136
|
+
self._proc.wait()
|
|
137
|
+
self._proc = None
|
|
@@ -17,7 +17,7 @@ class AwsAdditionalConfigAutoImport(AdditionalConfigAutoImport):
|
|
|
17
17
|
This DI auto injects boto3, boto3 Session and the parameter store.
|
|
18
18
|
"""
|
|
19
19
|
|
|
20
|
-
def
|
|
20
|
+
def provide_boto3_sdk(self) -> ModuleType:
|
|
21
21
|
import boto3
|
|
22
22
|
|
|
23
23
|
return boto3
|
|
@@ -91,7 +91,7 @@ class SecretsManagerRotation(Endpoint):
|
|
|
91
91
|
pending_secret_data = json.loads(pending_secret["SecretString"])
|
|
92
92
|
except botocore.exceptions.ClientError as error:
|
|
93
93
|
if error.response["Error"]["Code"] == "ResourceNotFoundException":
|
|
94
|
-
pending_secret_data =
|
|
94
|
+
pending_secret_data = {}
|
|
95
95
|
else:
|
|
96
96
|
raise error
|
|
97
97
|
|
|
File without changes
|
|
File without changes
|