Skip to content

Commit cfeb4c8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent af9404e commit cfeb4c8

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/awx_plugins/credentials/aws_assumerole.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
'label': 'AWS ARN Role Name',
3838
'type': 'string',
3939
'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+
},
4142
],
4243
'metadata': [{'id': 'identifier',
4344
'label': 'Identifier',
@@ -51,20 +52,23 @@
5152

5253
def aws_assumerole_getcreds(access_key, secret_key, role_arn, external_id):
5354
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+
):
5557
# Connect using credentials in the EE
5658
connection = boto3.client(service_name='sts')
5759
else:
5860
# Connect to AWS using provided credentials
5961
connection = boto3.client(
6062
service_name='sts',
6163
aws_access_key_id=access_key,
62-
aws_secret_access_key=secret_key)
64+
aws_secret_access_key=secret_key,
65+
)
6366
try:
6467
response = connection.assume_role(
6568
RoleArn=role_arn,
6669
RoleSessionName='AAP_AWS_Role_Session1',
67-
ExternalId=external_id)
70+
ExternalId=external_id,
71+
)
6872
except ClientError as ce:
6973
raise ValueError(f'Got a bad client response from AWS: {ce.msg}.')
7074

@@ -74,7 +78,8 @@ def aws_assumerole_getcreds(access_key, secret_key, role_arn, external_id):
7478

7579

7680
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."""
7883
access_key = kwargs.get('access_key')
7984
secret_key = kwargs.get('secret_key')
8085
role_arn = kwargs.get('role_arn')
@@ -87,19 +92,24 @@ def aws_assumerole_backend(**kwargs):
8792
# multiple roles.
8893
#
8994
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+
)
9197
credential_key = credential_key_hash.hexdigest()
9298

9399
credentials = _aws_cred_cache.get(credential_key, None)
94100

95101
# If there are no credentials for this user/ARN *or* the credentials
96102
# we have in the cache have expired, then we need to contact AWS again.
97103
#
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+
):
100109

101110
credentials = aws_assumerole_getcreds(
102-
access_key, secret_key, role_arn, external_id)
111+
access_key, secret_key, role_arn, external_id,
112+
)
103113

104114
_aws_cred_cache[credential_key] = credentials
105115

@@ -114,4 +124,5 @@ def aws_assumerole_backend(**kwargs):
114124
aws_assumerole_plugin = CredentialPlugin(
115125
'AWS Assume Role Plugin',
116126
inputs=assume_role_inputs,
117-
backend=aws_assumerole_backend)
127+
backend=aws_assumerole_backend,
128+
)

tests/credential_plugins_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ def test_hashivault_handle_auth_not_enough_args():
133133

134134

135135
def test_aws_assumerole_with_accesssecret():
136-
'''
137-
Test that the aws_assumerole_backend function call returns a token given the access_key and secret_key.
138-
'''
136+
"""Test that the aws_assumerole_backend function call returns a token given
137+
the access_key and secret_key."""
139138
kwargs = {
140139
'access_key': 'my_access_key',
141140
'secret_key': 'my_secret_key',
@@ -154,7 +153,8 @@ def test_aws_assumerole_with_accesssecret():
154153
kwargs.get('access_key'),
155154
kwargs.get('secret_key'),
156155
kwargs.get('role_arn'),
157-
None)
156+
None,
157+
)
158158
assert token == 'the_access_token'
159159
kwargs['identifier'] = 'secret_key'
160160
method_mock.reset_mock()
@@ -169,9 +169,7 @@ def test_aws_assumerole_with_accesssecret():
169169

170170

171171
def test_aws_assumerole_with_arnonly():
172-
'''
173-
Test backend function with only the role ARN provided.
174-
'''
172+
"""Test backend function with only the role ARN provided."""
175173
kwargs = {
176174
'role_arn': 'the_arn',
177175
'identifier': 'access_token',
@@ -185,7 +183,8 @@ def test_aws_assumerole_with_arnonly():
185183
}
186184
token = aws_assumerole.aws_assumerole_backend(**kwargs)
187185
method_mock.assert_called_with(
188-
None, None, kwargs.get('role_arn'), None)
186+
None, None, kwargs.get('role_arn'), None,
187+
)
189188
assert token == 'the_access_token'
190189
kwargs['identifier'] = 'secret_key'
191190
method_mock.reset_mock()

0 commit comments

Comments
 (0)