Skip to content
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Reporting An Issue/Feature
Codestyle
---------
This project uses `ruff <https://github.com/astral-sh/ruff>`__ to enforce
codstyle requirements. We've codified this process using a tool called
codestyle requirements. We've codified this process using a tool called
`pre-commit <https://pre-commit.com/>`__. pre-commit allows us to specify a
config file with all tools required for code linting, and surfaces either a
git commit hook, or single command, for enforcing these.
Expand Down
11 changes: 11 additions & 0 deletions botocore/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(
event_hooks=None,
include_builtin_handlers=True,
profile=None,
session_profiles=None,
):
"""
Create a new Session object.
Expand All @@ -125,6 +126,12 @@ def __init__(
session. Note that the profile can only be set when
the session is created.

:type session_profiles: dict
:param session_profiles: A dictionary that is used add or override
profile configuration asociated with this session. The
keys are profile names and the values are profile
configurations with the same structure as the config file.

"""
if event_hooks is None:
self._original_handler = HierarchicalEmitter()
Expand All @@ -144,6 +151,7 @@ def __init__(
self._credentials = None
self._auth_token = None
self._profile_map = None
self._session_profiles = {}
# This is a dict that stores per session specific config variable
# overrides via set_config_variable().
self._session_instance_vars = {}
Expand All @@ -157,6 +165,8 @@ def __init__(
self.session_var_map = SessionVarDict(self, self.SESSION_VARIABLES)
if session_vars is not None:
self.session_var_map.update(session_vars)
if session_profiles is not None:
self._session_profiles = session_profiles
invoke_initializers(self)

def _register_components(self):
Expand Down Expand Up @@ -458,6 +468,7 @@ def full_config(self):
self._config['profiles'][profile].update(cred_vars)
except ConfigNotFound:
pass
self._config['profiles'].update(self._session_profiles)
return self._config

def get_default_client_config(self):
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ def test_can_get_with_methods(self):
value = self.session.get_config_variable('region', methods=('env',))
self.assertEqual(value, 'env-var')

def test_session_profile_add(self):
session = create_session(
session_profiles={
"test": {
"role_arn": "arn:aws:iam::123456789012:role/test",
"credential_source": "Ec2InstanceMetadata",
}
}
)

self.assertTrue("test" in session.full_config["profiles"])
self.assertEqual(
session.full_config["profiles"]["test"]["role_arn"],
"arn:aws:iam::123456789012:role/test",
)
self.assertEqual(
session.full_config["profiles"]["test"]["credential_source"],
"Ec2InstanceMetadata",
)


class TestSessionPartitionFiles(BaseSessionTest):
def test_lists_partitions_on_disk(self):
Expand Down