diff --git a/botocore/credentials.py b/botocore/credentials.py index 51e301f0bc..5f3cb3bf3a 100644 --- a/botocore/credentials.py +++ b/botocore/credentials.py @@ -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) @@ -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 diff --git a/tests/unit/test_credentials.py b/tests/unit/test_credentials.py index 796cdab9ae..19bf4dca2e 100644 --- a/tests/unit/test_credentials.py +++ b/tests/unit/test_credentials.py @@ -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', @@ -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):