Skip to content

Instantly share code, notes, and snippets.

@jappievw
Last active July 31, 2024 12:05
Show Gist options
  • Save jappievw/2c54fd3150fd6e80cc05a7b4cdea60f6 to your computer and use it in GitHub Desktop.
Save jappievw/2c54fd3150fd6e80cc05a7b4cdea60f6 to your computer and use it in GitHub Desktop.
Boto3 Management Session with Refreshable Assume Role
from os import getenv
from boto3 import Session
from util import make_refreshable_assume_role_session
def example():
management_session = Session(aws_access_key_id=getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=getenv('AWS_SECRET_ACCESS_KEY'))
assume_role_params = dict(
RoleArn=getenv('AWS_ROLE_ARN'),
RoleSessionName='ExampleSessionName',
)
assumed_session = make_refreshable_assume_role_session(management_session, assume_role_params)
print(assumed_session.client('sts').get_caller_identity())
if __name__ == '__main__':
example()
MIT License
Copyright (c) 2018 Jasper van Wanrooy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
from boto3 import Session
from botocore.credentials import CredentialProvider, CredentialResolver, Credentials, \
RefreshableCredentials, create_assume_role_refresher
from botocore.session import get_session
def make_refreshable_assume_role_session(management_session: Session, assume_role_params: dict) -> Session:
provider = ManagementSessionWithRefreshableAssumeRoleProvider(management_session, assume_role_params)
resolver = CredentialResolver(providers=[provider])
botocore_session = get_session()
botocore_session.register_component('credential_provider', resolver)
return Session(botocore_session=botocore_session, region_name=management_session.region_name)
class ManagementSessionWithRefreshableAssumeRoleProvider(CredentialProvider):
METHOD = 'management-assume-role'
def __init__(self, management_session: Session, assume_role_params: dict):
assert isinstance(management_session, Session)
self._management_session = management_session
assert isinstance(assume_role_params, dict)
self._assume_role_params = assume_role_params
super().__init__()
def load(self) -> Credentials:
refresh = create_assume_role_refresher(self._management_session.client('sts'),
self._assume_role_params)
return RefreshableCredentials.create_from_metadata(
metadata=refresh(),
refresh_using=refresh,
method=self.METHOD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment