37
37
'label' : 'AWS ARN Role Name' ,
38
38
'type' : 'string' ,
39
39
'secret' : True ,
40
- 'help_text' : _ ('The ARN Role Name to be assumed in AWS' )},
40
+ 'help_text' : _ ('The ARN Role Name to be assumed in AWS' ),
41
+ },
41
42
],
42
43
'metadata' : [{'id' : 'identifier' ,
43
44
'label' : 'Identifier' ,
51
52
52
53
def aws_assumerole_getcreds (access_key , secret_key , role_arn , external_id ):
53
54
if (access_key is None or len (access_key ) == 0 ) and (
54
- secret_key is None or len (secret_key ) == 0 ):
55
+ secret_key is None or len (secret_key ) == 0
56
+ ):
55
57
# Connect using credentials in the EE
56
58
connection = boto3 .client (service_name = 'sts' )
57
59
else :
58
60
# Connect to AWS using provided credentials
59
61
connection = boto3 .client (
60
62
service_name = 'sts' ,
61
63
aws_access_key_id = access_key ,
62
- aws_secret_access_key = secret_key )
64
+ aws_secret_access_key = secret_key ,
65
+ )
63
66
try :
64
67
response = connection .assume_role (
65
68
RoleArn = role_arn ,
66
69
RoleSessionName = 'AAP_AWS_Role_Session1' ,
67
- ExternalId = external_id )
70
+ ExternalId = external_id ,
71
+ )
68
72
except ClientError as ce :
69
73
raise ValueError (f'Got a bad client response from AWS: { ce .msg } .' )
70
74
@@ -74,7 +78,8 @@ def aws_assumerole_getcreds(access_key, secret_key, role_arn, external_id):
74
78
75
79
76
80
def aws_assumerole_backend (** kwargs ):
77
- """This backend function actually contacts AWS to assume a given role for the specified user"""
81
+ """This backend function actually contacts AWS to assume a given role for
82
+ the specified user."""
78
83
access_key = kwargs .get ('access_key' )
79
84
secret_key = kwargs .get ('secret_key' )
80
85
role_arn = kwargs .get ('role_arn' )
@@ -87,19 +92,24 @@ def aws_assumerole_backend(**kwargs):
87
92
# multiple roles.
88
93
#
89
94
credential_key_hash = hashlib .sha256 (
90
- (str (access_key or '' ) + role_arn ).encode ('utf-8' ))
95
+ (str (access_key or '' ) + role_arn ).encode ('utf-8' ),
96
+ )
91
97
credential_key = credential_key_hash .hexdigest ()
92
98
93
99
credentials = _aws_cred_cache .get (credential_key , None )
94
100
95
101
# If there are no credentials for this user/ARN *or* the credentials
96
102
# we have in the cache have expired, then we need to contact AWS again.
97
103
#
98
- if (credentials is None ) or (credentials ['Expiration' ] < datetime .datetime .now (
99
- credentials ['Expiration' ].tzinfo )):
104
+ if (credentials is None ) or (
105
+ credentials ['Expiration' ] < datetime .datetime .now (
106
+ credentials ['Expiration' ].tzinfo ,
107
+ )
108
+ ):
100
109
101
110
credentials = aws_assumerole_getcreds (
102
- access_key , secret_key , role_arn , external_id )
111
+ access_key , secret_key , role_arn , external_id ,
112
+ )
103
113
104
114
_aws_cred_cache [credential_key ] = credentials
105
115
@@ -114,4 +124,5 @@ def aws_assumerole_backend(**kwargs):
114
124
aws_assumerole_plugin = CredentialPlugin (
115
125
'AWS Assume Role Plugin' ,
116
126
inputs = assume_role_inputs ,
117
- backend = aws_assumerole_backend )
127
+ backend = aws_assumerole_backend ,
128
+ )
0 commit comments