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

feat(assume_role): add support for custom session policies #274

Merged
merged 2 commits into from
Sep 10, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
__pycache__/
*.py[cod]
*$py.class
pyrightconfig.json

# C extensions
*.so
Expand Down
14 changes: 13 additions & 1 deletion awsume/awsumepy/default_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ def add_arguments(config: dict, parser: argparse.ArgumentParser):
metavar='session_name',
help='Set a custom role session name',
)
parser.add_argument('--session-policy',
action='store',
dest='session_policy',
metavar='session_policy',
help='Custom session policy JSON',
)
parser.add_argument('--role-duration',
action='store',
dest='role_duration',
Expand Down Expand Up @@ -339,7 +345,7 @@ def assume_role_from_cli(config: dict, arguments: dict, profiles: dict):
logger.debug('Session name: {}'.format(session_name))
if not arguments.source_profile:
logger.debug('Using current credentials to assume role')
role_session = aws_lib.assume_role({}, arguments.role_arn, session_name, region=region, external_id=arguments.external_id, role_duration=role_duration, tags=arguments.session_tags)
role_session = aws_lib.assume_role({}, arguments.role_arn, session_name, session_policy=arguments.session_policy, region=region, external_id=arguments.external_id, role_duration=role_duration, tags=arguments.session_tags)
else:
logger.debug('Using the source_profile from the cli to call assume_role')
source_profile = profiles.get(arguments.source_profile)
Expand All @@ -357,6 +363,7 @@ def assume_role_from_cli(config: dict, arguments: dict, profiles: dict):
source_session,
arguments.role_arn,
session_name,
session_policy=arguments.session_policy,
region=region,
external_id=arguments.external_id,
role_duration=role_duration,
Expand All @@ -370,6 +377,7 @@ def assume_role_from_cli(config: dict, arguments: dict, profiles: dict):
source_credentials,
arguments.role_arn,
session_name,
session_policy=arguments.session_policy,
region=region,
external_id=arguments.external_id,
role_duration=role_duration,
Expand All @@ -394,6 +402,7 @@ def assume_role_from_cli(config: dict, arguments: dict, profiles: dict):
source_session,
arguments.role_arn,
session_name,
session_policy=arguments.session_policy,
region=region,
external_id=arguments.external_id,
role_duration=role_duration,
Expand All @@ -413,6 +422,7 @@ def get_assume_role_credentials(config: dict, arguments: argparse.Namespace, pro
source_credentials,
target_profile.get('role_arn'),
profile_lib.get_session_name(config, arguments, profiles, target_profile_name),
session_policy=arguments.session_policy,
region=region,
external_id=external_id,
role_duration=role_duration,
Expand Down Expand Up @@ -458,6 +468,7 @@ def get_assume_role_credentials_mfa_required(config: dict, arguments: argparse.N
source_session,
target_profile.get('role_arn'),
profile_lib.get_session_name(config, arguments, profiles, target_profile_name),
session_policy=arguments.session_policy,
region=region,
external_id=external_id,
role_duration=role_duration,
Expand Down Expand Up @@ -488,6 +499,7 @@ def get_assume_role_credentials_mfa_required_large_custom_duration(config: dict,
source_session,
target_profile.get('role_arn'),
profile_lib.get_session_name(config, arguments, profiles, target_profile_name),
session_policy=arguments.session_policy,
region=region,
external_id=external_id,
role_duration=role_duration,
Expand Down
3 changes: 3 additions & 0 deletions awsume/awsumepy/lib/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def assume_role(
source_credentials: dict,
role_arn: str,
session_name: str,
session_policy: str = None,
external_id: str = None,
region: str = None,
role_duration: int = None,
Expand All @@ -53,6 +54,8 @@ def assume_role(
)
role_sts_client = boto_session.client('sts') # type: botostubs.STS
kwargs = { 'RoleSessionName': session_name, 'RoleArn': role_arn }
if session_policy:
kwargs['Policy'] = session_policy
if external_id:
kwargs['ExternalId'] = external_id
if role_duration:
Expand Down
13 changes: 13 additions & 0 deletions test/unit/awsume/awsumepy/test_default_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


def generate_namespace_with_defaults(
session_policy=None,
session_tags=None,
region=None,
source_profile=None,
Expand All @@ -26,6 +27,7 @@ def generate_namespace_with_defaults(
**kwargs
) -> argparse.Namespace:
return argparse.Namespace(
session_policy=session_policy,
session_tags=session_tags,
region=region,
source_profile=source_profile,
Expand Down Expand Up @@ -426,6 +428,7 @@ def test_assume_role_from_cli(aws_lib: MagicMock, profile_lib: MagicMock):
default_plugins.assume_role_from_cli(config, arguments, profiles)
aws_lib.assume_role.assert_called_with(
{}, arguments.role_arn, 'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
Expand Down Expand Up @@ -463,6 +466,7 @@ def test_assume_role_from_cli_source_profile(aws_lib: MagicMock, profile_lib: Ma
aws_lib.get_session_token.return_value,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
Expand Down Expand Up @@ -500,6 +504,7 @@ def test_assume_role_from_cli_source_profile_role_duration_mfa(aws_lib: MagicMoc
profile_lib.profile_to_credentials.return_value,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration='43200',
Expand Down Expand Up @@ -538,6 +543,7 @@ def test_assume_role_from_cli_source_profile_role_duration_no_mfa(aws_lib: Magic
profile_lib.profile_to_credentials.return_value,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration='43200',
Expand Down Expand Up @@ -582,6 +588,7 @@ def test_assume_role_from_cli_source_profile_no_role_duration_mfa(aws_lib: Magic
aws_lib.get_session_token.return_value,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
Expand Down Expand Up @@ -618,6 +625,7 @@ def test_assume_role_from_cli_source_profile_no_role_duration_no_mfa(aws_lib: Ma
profile_lib.profile_to_credentials.return_value,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
Expand Down Expand Up @@ -690,6 +698,7 @@ def test_get_credentials(aws_lib: MagicMock, create_autoawsume_profile: MagicMoc
aws_lib.get_session_token.return_value,
'myrolearn',
'mysessionname',
session_policy=arguments.session_policy,
region=None,
external_id='myexternalid',
role_duration=0,
Expand Down Expand Up @@ -742,6 +751,7 @@ def test_get_credentials_auto_refresh(aws_lib: MagicMock, create_autoawsume_prof
aws_lib.get_session_token.return_value,
'myrolearn',
'mysessionname',
session_policy=arguments.session_policy,
region=None,
external_id='myexternalid',
role_duration=0,
Expand Down Expand Up @@ -784,6 +794,7 @@ def test_get_credentials_role_duration(aws_lib: MagicMock, create_autoawsume_pro
{ 'AccessKeyId': 'AKIA...', 'SecretAccessKey': 'SECRET', 'SessionToken': None, 'Region': None },
'myrolearn',
'mysessionname',
session_policy=arguments.session_policy,
region=None,
external_id='myexternalid',
role_duration=43200,
Expand Down Expand Up @@ -898,6 +909,7 @@ def test_get_credentials_no_mfa_role(aws_lib: MagicMock, create_autoawsume_profi
{ 'AccessKeyId': 'AKIA...', 'SecretAccessKey': 'SECRET', 'SessionToken': None, 'Region': None },
'myrolearn',
'mysessionname',
session_policy=arguments.session_policy,
region=None,
external_id='myexternalid',
role_duration=0,
Expand Down Expand Up @@ -996,6 +1008,7 @@ def test_post_add_arguments_session_tags(aws_lib: MagicMock):
config,
arguments.role_arn,
'awsume-cli-role',
session_policy=arguments.session_policy,
external_id=arguments.external_id,
role_duration='43200',
tags=[
Expand Down
Loading