Skip to content

Commit

Permalink
Simplify empty env var creds check
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesls committed Apr 16, 2019
1 parent f97578e commit 01790d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions botocore/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,9 @@ def load(self):
Search for credentials in explicit environment variables.
"""

access_key_var = self._mapping['access_key']
access_key = self.environ.get(self._mapping['access_key'], '')

if access_key_var in self.environ and not self.environ.get(access_key_var) == '':
if access_key:
logger.info('Found credentials in environment variables.')
fetcher = self._create_credentials_fetcher()
credentials = fetcher(require_expiry=False)
Expand Down Expand Up @@ -953,30 +953,32 @@ def _create_credentials_fetcher(self):
def fetch_credentials(require_expiry=True):
credentials = {}

access_key = environ.get(mapping['access_key'])
if access_key is None or access_key == '':
access_key = environ.get(mapping['access_key'], '')
if not access_key:
raise PartialCredentialsError(
provider=method, cred_var=mapping['access_key'])
credentials['access_key'] = access_key

secret_key = environ.get(mapping['secret_key'])
if secret_key is None or secret_key == '':
secret_key = environ.get(mapping['secret_key'], '')
if not secret_key:
raise PartialCredentialsError(
provider=method, cred_var=mapping['secret_key'])
credentials['secret_key'] = secret_key

token = None
credentials['token'] = None
for token_env_var in mapping['token']:
if token_env_var in environ and not environ[token_env_var] == '':
token = environ[token_env_var]
token = environ.get(token_env_var, '')
if token:
credentials['token'] = token
break
credentials['token'] = token

expiry_time = environ.get(mapping['expiry_time'])
if require_expiry and expiry_time is None:
credentials['expiry_time'] = None
expiry_time = environ.get(mapping['expiry_time'], '')
if expiry_time:
credentials['expiry_time'] = expiry_time
if require_expiry and not expiry_time:
raise PartialCredentialsError(
provider=method, cred_var=mapping['expiry_time'])
credentials['expiry_time'] = expiry_time

return credentials

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ def test_external_id_provided(self):
RoleArn='myrole', ExternalId='myid', RoleSessionName=mock.ANY)

def test_assume_role_with_duration(self):
self.fake_config['profiles']['development']['duration_seconds'] = 7200
self.fake_config['profiles']['development']['duration_seconds'] = 7200
response = {
'Credentials': {
'AccessKeyId': 'foo',
Expand All @@ -1864,7 +1864,7 @@ def test_assume_role_with_duration(self):

client = client_creator.return_value
client.assume_role.assert_called_with(
RoleArn='myrole', RoleSessionName=mock.ANY,
RoleArn='myrole', RoleSessionName=mock.ANY,
DurationSeconds=7200)

def test_assume_role_with_bad_duration(self):
Expand Down

0 comments on commit 01790d8

Please sign in to comment.