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 support for duration_seconds in the profile for AssumeRole. #1600

Merged
merged 1 commit into from
Dec 6, 2018
Merged
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
21 changes: 18 additions & 3 deletions botocore/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,16 +689,20 @@ def _get_credentials(self):

def _assume_role_kwargs(self):
"""Get the arguments for assume role based on current configuration."""
assume_role_kwargs = self._assume_kwargs
assume_role_kwargs = deepcopy(self._assume_kwargs)

mfa_serial = assume_role_kwargs.get('SerialNumber')

if mfa_serial is not None:
prompt = 'Enter MFA code for %s: ' % mfa_serial
token_code = self._mfa_prompter(prompt)

assume_role_kwargs = deepcopy(assume_role_kwargs)
assume_role_kwargs['TokenCode'] = token_code

duration_seconds = assume_role_kwargs.get('DurationSeconds')

if duration_seconds is not None:
assume_role_kwargs['DurationSeconds'] = duration_seconds

return assume_role_kwargs

def _create_client(self):
Expand Down Expand Up @@ -1235,6 +1239,10 @@ def _load_creds_via_assume_role(self, profile_name):
if mfa_serial is not None:
extra_args['SerialNumber'] = mfa_serial

duration_seconds = role_config.get('duration_seconds')
if duration_seconds is not None:
extra_args['DurationSeconds'] = duration_seconds

fetcher = AssumeRoleCredentialFetcher(
client_creator=self._client_creator,
source_credentials=source_credentials,
Expand Down Expand Up @@ -1267,6 +1275,7 @@ def _get_role_config(self, profile_name):
mfa_serial = profile.get('mfa_serial')
external_id = profile.get('external_id')
role_session_name = profile.get('role_session_name')
duration_seconds = profile.get('duration_seconds')

role_config = {
'role_arn': role_arn,
Expand All @@ -1277,6 +1286,12 @@ def _get_role_config(self, profile_name):
'credential_source': credential_source
}

if duration_seconds is not None:
try:
role_config['duration_seconds'] = int(duration_seconds)
except ValueError:
pass

# Either the credential source or the source profile must be
# specified, but not both.
if credential_source is not None and source_profile is not None:
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,51 @@ def test_external_id_provided(self):
client.assume_role.assert_called_with(
RoleArn='myrole', ExternalId='myid', RoleSessionName=mock.ANY)

def test_assume_role_with_duration(self):
self.fake_config['profiles']['development']['duration_seconds'] = 7200
response = {
'Credentials': {
'AccessKeyId': 'foo',
'SecretAccessKey': 'bar',
'SessionToken': 'baz',
'Expiration': self.some_future_time().isoformat(),
},
}
client_creator = self.create_client_creator(with_response=response)
provider = credentials.AssumeRoleProvider(
self.create_config_loader(), client_creator,
cache={}, profile_name='development')

# The credentials won't actually be assumed until they're requested.
provider.load().get_frozen_credentials()

client = client_creator.return_value
client.assume_role.assert_called_with(
RoleArn='myrole', RoleSessionName=mock.ANY,
DurationSeconds=7200)

def test_assume_role_with_bad_duration(self):
self.fake_config['profiles']['development']['duration_seconds'] = 'garbage value'
response = {
'Credentials': {
'AccessKeyId': 'foo',
'SecretAccessKey': 'bar',
'SessionToken': 'baz',
'Expiration': self.some_future_time().isoformat(),
},
}
client_creator = self.create_client_creator(with_response=response)
provider = credentials.AssumeRoleProvider(
self.create_config_loader(), client_creator,
cache={}, profile_name='development')

# The credentials won't actually be assumed until they're requested.
provider.load().get_frozen_credentials()

client = client_creator.return_value
client.assume_role.assert_called_with(
RoleArn='myrole', RoleSessionName=mock.ANY)

def test_assume_role_with_mfa(self):
self.fake_config['profiles']['development']['mfa_serial'] = 'mfa'
response = {
Expand Down