Skip to content

Commit beb14ee

Browse files
authored
Update set_credentials to no longer pass aws_account_id (#4586)
1 parent 8a46af6 commit beb14ee

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

boto3/session.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,21 @@ def __init__(
8181
if profile_name is not None:
8282
self._session.set_config_variable('profile', profile_name)
8383

84-
creds = (
85-
aws_access_key_id,
86-
aws_secret_access_key,
87-
aws_session_token,
88-
aws_account_id,
89-
)
90-
if any(creds):
91-
if self._account_id_set_without_credentials(
92-
aws_account_id, aws_access_key_id, aws_secret_access_key
93-
):
84+
credentials_kwargs = {
85+
"aws_access_key_id": aws_access_key_id,
86+
"aws_secret_access_key": aws_secret_access_key,
87+
"aws_session_token": aws_session_token,
88+
"aws_account_id": aws_account_id,
89+
}
90+
91+
if any(credentials_kwargs.values()):
92+
if self._account_id_set_without_credentials(**credentials_kwargs):
9493
raise NoCredentialsError()
95-
self._session.set_credentials(
96-
aws_access_key_id,
97-
aws_secret_access_key,
98-
aws_session_token,
99-
aws_account_id,
100-
)
94+
95+
if aws_account_id is None:
96+
del credentials_kwargs["aws_account_id"]
97+
98+
self._session.set_credentials(*credentials_kwargs.values())
10199

102100
if region_name is not None:
103101
self._session.set_config_variable('region', region_name)
@@ -332,8 +330,8 @@ def client(
332330
'aws_account_id': aws_account_id,
333331
}
334332
if aws_account_id is None:
335-
# Remove aws_account_id for lambda for arbitrary
336-
# botocore version mismatches.
333+
# Remove aws_account_id for arbitrary
334+
# botocore version mismatches in AWS Lambda.
337335
del create_client_kwargs['aws_account_id']
338336

339337
return self._session.create_client(
@@ -562,10 +560,15 @@ def _register_default_handlers(self):
562560
)
563561

564562
def _account_id_set_without_credentials(
565-
self, account_id, access_key, secret_key
563+
self,
564+
*,
565+
aws_account_id,
566+
aws_access_key_id,
567+
aws_secret_access_key,
568+
**kwargs,
566569
):
567-
if account_id is None:
570+
if aws_account_id is None:
568571
return False
569-
elif access_key is None or secret_key is None:
572+
elif aws_access_key_id is None or aws_secret_access_key is None:
570573
return True
571574
return False

tests/unit/test_session.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ def test_credentials_can_be_set(self):
7070

7171
assert self.bc_session_cls.called
7272
assert bc_session.set_credentials.called
73-
bc_session.set_credentials.assert_called_with(
74-
'key', 'secret', 'token', None
75-
)
73+
bc_session.set_credentials.assert_called_with('key', 'secret', 'token')
7674

7775
def test_credentials_can_be_set_with_account_id(self):
7876
bc_session = self.bc_session_cls.return_value

0 commit comments

Comments
 (0)