Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add account ID to boto3 session and client #4444

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions boto3/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Session:
:type profile_name: string
:param profile_name: The name of a profile to use. If not given, then
the default profile is used.
:type aws_account_id: string
:param aws_account_id: AWS account ID
"""

def __init__(
Expand All @@ -54,6 +56,7 @@ def __init__(
region_name=None,
botocore_session=None,
profile_name=None,
aws_account_id=None,
):
if botocore_session is not None:
self._session = botocore_session
Expand All @@ -74,9 +77,18 @@ def __init__(
if profile_name is not None:
self._session.set_config_variable('profile', profile_name)

if aws_access_key_id or aws_secret_access_key or aws_session_token:
creds = (
aws_access_key_id,
aws_secret_access_key,
aws_session_token,
aws_account_id,
)
if any(creds):
self._session.set_credentials(
aws_access_key_id, aws_secret_access_key, aws_session_token
aws_access_key_id,
aws_secret_access_key,
aws_session_token,
aws_account_id,
)

if region_name is not None:
Expand Down Expand Up @@ -224,6 +236,7 @@ def client(
aws_secret_access_key=None,
aws_session_token=None,
config=None,
aws_account_id=None,
):
"""
Create a low-level service client by name.
Expand Down Expand Up @@ -291,6 +304,10 @@ def client(
<https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html>`_
for more details.

:type aws_account_id: string
:param aws_account_id: The account id to use when creating
the client. Same semantics as aws_access_key_id above.

:return: Service client instance

"""
Expand All @@ -305,6 +322,7 @@ def client(
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
config=config,
aws_account_id=aws_account_id,
)

def resource(
Expand Down
26 changes: 25 additions & 1 deletion tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,38 @@ def test_credentials_can_be_set(self):

assert self.bc_session_cls.called
assert bc_session.set_credentials.called
bc_session.set_credentials.assert_called_with('key', 'secret', 'token')
bc_session.set_credentials.assert_called_with(
'key', 'secret', 'token', None
)

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

# Set values in constructor
Session(
aws_access_key_id='key',
aws_secret_access_key='secret',
aws_session_token='token',
aws_account_id='account',
)

assert self.bc_session_cls.called
assert bc_session.set_credentials.called
bc_session.set_credentials.assert_called_with(
'key', 'secret', 'token', 'account'
)

def test_can_get_credentials(self):
access_key = 'foo'
secret_key = 'bar'
token = 'baz'
account_id = 'bin'

creds = mock.Mock()
creds.access_key = access_key
creds.secret_key = secret_key
creds.token = token
creds.account_id = account_id

bc_session = self.bc_session_cls.return_value
bc_session.get_credentials.return_value = creds
Expand All @@ -89,12 +110,14 @@ def test_can_get_credentials(self):
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
aws_session_token=token,
aws_account_id=account_id,
)

credentials = session.get_credentials()
assert credentials.access_key == access_key
assert credentials.secret_key == secret_key
assert credentials.token == token
assert credentials.account_id == account_id

def test_profile_can_be_set(self):
bc_session = self.bc_session_cls.return_value
Expand Down Expand Up @@ -240,6 +263,7 @@ def test_create_client_with_args(self):
region_name='us-west-2',
api_version=None,
config=None,
aws_account_id=None,
)

def test_create_resource_with_args(self):
Expand Down
Loading