33
44from odd_collector_aws .domain .plugin import AwsPlugin
55
6+ from logging import logger
7+
68
79class AwsClient :
810 session : Session
@@ -13,13 +15,32 @@ def __init__(self, config: AwsPlugin):
1315 self ._init_session ()
1416
1517 def _init_session (self ):
18+
1619 self .session = Session (
1720 aws_access_key_id = self ._config .aws_access_key_id ,
1821 aws_secret_access_key = self ._config .aws_secret_access_key ,
1922 aws_session_token = self ._config .aws_session_token ,
2023 region_name = self ._config .aws_region ,
2124 )
2225
26+ if self ._config .aws_role_arn and self ._config .aws_role_session_name :
27+ try :
28+ assumed_role_response = self .session .client ("sts" ).assume_role (
29+ RoleArn = self ._config .aws_role_arn ,
30+ RoleSessionName = self ._config .aws_role_session_name
31+ )
32+
33+ if assumed_role_response .Credentials :
34+ self .session = Session (
35+ aws_access_key_id = assumed_role_response .Credentials .AccessKeyId ,
36+ aws_secret_access_key = assumed_role_response .Credentials .SecretAccessKey ,
37+ aws_session_token = assumed_role_response .Credentials .SessionToken ,
38+ region_name = self ._config .aws_region
39+ )
40+ except Exception :
41+ logger .debug (
42+ "Error assuming AWS Role" , exc_info = True )
43+
2344 def get_client (self , service_name : str ) -> BaseClient :
2445 return self .session .client (service_name , endpoint_url = self ._config .endpoint_url )
2546
0 commit comments