1919from types import SimpleNamespace
2020from typing import TYPE_CHECKING , Callable , Optional , Set , Tuple
2121
22- import boto3
2322from botocore .exceptions import ClientError , EndpointConnectionError
2423
24+ from aws_advanced_python_wrapper .aws_credentials_manager import \
25+ AwsCredentialsManager
2526from aws_advanced_python_wrapper .utils .cache_map import CacheMap
2627
2728if TYPE_CHECKING :
@@ -86,7 +87,7 @@ def connect(
8687 props : Properties ,
8788 is_initial_connection : bool ,
8889 connect_func : Callable ) -> Connection :
89- return self ._connect (props , connect_func )
90+ return self ._connect (host_info , props , connect_func )
9091
9192 def force_connect (
9293 self ,
@@ -96,16 +97,16 @@ def force_connect(
9697 props : Properties ,
9798 is_initial_connection : bool ,
9899 force_connect_func : Callable ) -> Connection :
99- return self ._connect (props , force_connect_func )
100+ return self ._connect (host_info , props , force_connect_func )
100101
101- def _connect (self , props : Properties , connect_func : Callable ) -> Connection :
102+ def _connect (self , host_info : HostInfo , props : Properties , connect_func : Callable ) -> Connection :
102103 token_expiration_sec : int = WrapperProperties .SECRETS_MANAGER_EXPIRATION .get_int (props )
103104 # if value is less than 0, default to one year
104105 if token_expiration_sec < 0 :
105106 token_expiration_sec = AwsSecretsManagerPlugin ._ONE_YEAR_IN_SECONDS
106107 token_expiration_ns = token_expiration_sec * 1_000_000_000
107108
108- secret_fetched : bool = self ._update_secret (token_expiration_ns = token_expiration_ns )
109+ secret_fetched : bool = self ._update_secret (host_info , props , token_expiration_ns = token_expiration_ns )
109110
110111 try :
111112 self ._apply_secret_to_properties (props )
@@ -116,7 +117,7 @@ def _connect(self, props: Properties, connect_func: Callable) -> Connection:
116117 raise AwsWrapperError (
117118 Messages .get_formatted ("AwsSecretsManagerPlugin.ConnectException" , e )) from e
118119
119- secret_fetched = self ._update_secret (token_expiration_ns = token_expiration_ns , force_refetch = True )
120+ secret_fetched = self ._update_secret (host_info , props , token_expiration_ns = token_expiration_ns , force_refetch = True )
120121
121122 if secret_fetched :
122123 try :
@@ -128,7 +129,7 @@ def _connect(self, props: Properties, connect_func: Callable) -> Connection:
128129 unhandled_error )) from unhandled_error
129130 raise AwsWrapperError (Messages .get_formatted ("AwsSecretsManagerPlugin.FailedLogin" , e )) from e
130131
131- def _update_secret (self , token_expiration_ns : int , force_refetch : bool = False ) -> bool :
132+ def _update_secret (self , host_info : HostInfo , props : Properties , token_expiration_ns : int , force_refetch : bool = False ) -> bool :
132133 """
133134 Called to update credentials from the cache, or from the AWS Secrets Manager service.
134135 :param token_expiration_ns: Expiration time in nanoseconds for secret stored in cache.
@@ -146,7 +147,7 @@ def _update_secret(self, token_expiration_ns: int, force_refetch: bool = False)
146147 endpoint = self ._secret_key [2 ]
147148 if not self ._secret or force_refetch :
148149 try :
149- self ._secret = self ._fetch_latest_credentials ()
150+ self ._secret = self ._fetch_latest_credentials (host_info , props )
150151 if self ._secret :
151152 AwsSecretsManagerPlugin ._secrets_cache .put (self ._secret_key , self ._secret , token_expiration_ns )
152153 fetched = True
@@ -177,26 +178,19 @@ def _update_secret(self, token_expiration_ns: int, force_refetch: bool = False)
177178 if context is not None :
178179 context .close_context ()
179180
180- def _fetch_latest_credentials (self ):
181+ def _fetch_latest_credentials (self , host_info : HostInfo , props : Properties ):
181182 """
182183 Fetches the current credentials from AWS Secrets Manager service.
183184
184185 :return: a Secret object containing the credentials fetched from the AWS Secrets Manager service.
185186 """
186- session = self ._session if self ._session else boto3 .Session ()
187-
188- client = session .client (
189- 'secretsmanager' ,
190- region_name = self ._secret_key [1 ],
191- endpoint_url = self ._secret_key [2 ],
192- )
187+ session = AwsCredentialsManager .get_session (host_info , props , self ._secret_key [1 ])
188+ client = AwsCredentialsManager .get_client ("secretsmanager" , session , host_info .host , self ._secret_key [1 ], self ._secret_key [2 ])
193189
194190 secret = client .get_secret_value (
195191 SecretId = self ._secret_key [0 ],
196192 )
197193
198- client .close ()
199-
200194 return loads (secret .get ("SecretString" ), object_hook = lambda d : SimpleNamespace (** d ))
201195
202196 def _apply_secret_to_properties (self , properties : Properties ):
0 commit comments