Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Created December 15, 2019 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashbrowncipher/60b21356e580008dad1012c2c76bb5f5 to your computer and use it in GitHub Desktop.
Save hashbrowncipher/60b21356e580008dad1012c2c76bb5f5 to your computer and use it in GitHub Desktop.
subclass of botocore.session.Session that assumes roles and refreshes credentials
from botocore.session import Session
credentials_cache = dict()
class Session(Session):
def __init__(self, role_arn, *args, **kwargs):
super().__init__(*args, **kwargs)
self._config = dict(
profiles=dict(
default=dict(
credential_source="Ec2InstanceMetadata",
role_arn=role_arn,
)
)
)
self.get_component("credential_provider").get_provider(
"assume-role"
).cache = credentials_cache
@classmethod
def from_role(cls, *args, **kwargs):
arn = _role_arn(*args, **kwargs)
return cls(arn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment