Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 4, 2024
1 parent af9404e commit cfeb4c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
31 changes: 21 additions & 10 deletions src/awx_plugins/credentials/aws_assumerole.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'label': 'AWS ARN Role Name',
'type': 'string',
'secret': True,
'help_text': _('The ARN Role Name to be assumed in AWS')},
'help_text': _('The ARN Role Name to be assumed in AWS'),

Check warning on line 40 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L40

Added line #L40 was not covered by tests
},
],
'metadata': [{'id': 'identifier',
'label': 'Identifier',
Expand All @@ -51,20 +52,23 @@

def aws_assumerole_getcreds(access_key, secret_key, role_arn, external_id):
if (access_key is None or len(access_key) == 0) and (
secret_key is None or len(secret_key) == 0):
secret_key is None or len(secret_key) == 0

Check warning on line 55 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L55

Added line #L55 was not covered by tests
):
# Connect using credentials in the EE
connection = boto3.client(service_name='sts')

Check warning on line 58 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L58

Added line #L58 was not covered by tests
else:
# Connect to AWS using provided credentials
connection = boto3.client(
service_name='sts',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
aws_secret_access_key=secret_key,

Check warning on line 64 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L61-L64

Added lines #L61 - L64 were not covered by tests
)
try:
response = connection.assume_role(
RoleArn=role_arn,
RoleSessionName='AAP_AWS_Role_Session1',
ExternalId=external_id)
ExternalId=external_id,

Check warning on line 70 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L66-L70

Added lines #L66 - L70 were not covered by tests
)
except ClientError as ce:
raise ValueError(f'Got a bad client response from AWS: {ce.msg}.')

Check warning on line 73 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L72-L73

Added lines #L72 - L73 were not covered by tests

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


def aws_assumerole_backend(**kwargs):
"""This backend function actually contacts AWS to assume a given role for the specified user"""
"""This backend function actually contacts AWS to assume a given role for
the specified user."""
access_key = kwargs.get('access_key')
secret_key = kwargs.get('secret_key')
role_arn = kwargs.get('role_arn')
Expand All @@ -87,19 +92,24 @@ def aws_assumerole_backend(**kwargs):
# multiple roles.
#
credential_key_hash = hashlib.sha256(
(str(access_key or '') + role_arn).encode('utf-8'))
(str(access_key or '') + role_arn).encode('utf-8'),

Check warning on line 95 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L95

Added line #L95 was not covered by tests
)
credential_key = credential_key_hash.hexdigest()

credentials = _aws_cred_cache.get(credential_key, None)

# If there are no credentials for this user/ARN *or* the credentials
# we have in the cache have expired, then we need to contact AWS again.
#
if (credentials is None) or (credentials['Expiration'] < datetime.datetime.now(
credentials['Expiration'].tzinfo)):
if (credentials is None) or (
credentials['Expiration'] < datetime.datetime.now(
credentials['Expiration'].tzinfo,

Check warning on line 106 in src/awx_plugins/credentials/aws_assumerole.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/credentials/aws_assumerole.py#L105-L106

Added lines #L105 - L106 were not covered by tests
)
):

credentials = aws_assumerole_getcreds(
access_key, secret_key, role_arn, external_id)
access_key, secret_key, role_arn, external_id,
)

_aws_cred_cache[credential_key] = credentials

Expand All @@ -114,4 +124,5 @@ def aws_assumerole_backend(**kwargs):
aws_assumerole_plugin = CredentialPlugin(
'AWS Assume Role Plugin',
inputs=assume_role_inputs,
backend=aws_assumerole_backend)
backend=aws_assumerole_backend,
)
15 changes: 7 additions & 8 deletions tests/credential_plugins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ def test_hashivault_handle_auth_not_enough_args():


def test_aws_assumerole_with_accesssecret():
'''
Test that the aws_assumerole_backend function call returns a token given the access_key and secret_key.
'''
"""Test that the aws_assumerole_backend function call returns a token given
the access_key and secret_key."""
kwargs = {
'access_key': 'my_access_key',
'secret_key': 'my_secret_key',
Expand All @@ -154,7 +153,8 @@ def test_aws_assumerole_with_accesssecret():
kwargs.get('access_key'),
kwargs.get('secret_key'),
kwargs.get('role_arn'),
None)
None,

Check warning on line 156 in tests/credential_plugins_test.py

View check run for this annotation

Codecov / codecov/patch

tests/credential_plugins_test.py#L153-L156

Added lines #L153 - L156 were not covered by tests
)
assert token == 'the_access_token'
kwargs['identifier'] = 'secret_key'
method_mock.reset_mock()
Expand All @@ -169,9 +169,7 @@ def test_aws_assumerole_with_accesssecret():


def test_aws_assumerole_with_arnonly():
'''
Test backend function with only the role ARN provided.
'''
"""Test backend function with only the role ARN provided."""
kwargs = {
'role_arn': 'the_arn',
'identifier': 'access_token',

Check warning on line 175 in tests/credential_plugins_test.py

View check run for this annotation

Codecov / codecov/patch

tests/credential_plugins_test.py#L174-L175

Added lines #L174 - L175 were not covered by tests
Expand All @@ -185,7 +183,8 @@ def test_aws_assumerole_with_arnonly():
}
token = aws_assumerole.aws_assumerole_backend(**kwargs)
method_mock.assert_called_with(
None, None, kwargs.get('role_arn'), None)
None, None, kwargs.get('role_arn'), None,

Check warning on line 186 in tests/credential_plugins_test.py

View check run for this annotation

Codecov / codecov/patch

tests/credential_plugins_test.py#L186

Added line #L186 was not covered by tests
)
assert token == 'the_access_token'
kwargs['identifier'] = 'secret_key'
method_mock.reset_mock()
Expand Down

0 comments on commit cfeb4c8

Please sign in to comment.