Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise exceptions.EarlyExit()
if type(arguments.config) is list:
config_lib.handle_config(arguments.config)
raise exceptions.EarlyExit()
if arguments.kill:
kill(arguments)
raise exceptions.EarlyExit()
if arguments.with_saml:
if bool(arguments.role_arn) is not bool(arguments.principal_arn):
parser.error('both or neither --principal-arn and --role-arn must be specified with saml')
if not arguments.with_saml and arguments.principal_arn:
parser.error('--principal-arn can only be specified with --with-saml')
if arguments.role_arn and not arguments.role_arn.startswith('arn:'):
logger.debug('Using short-hand role arn syntax')
parts = arguments.role_arn.split(':')
if len(parts) == 2:
partition = 'aws'
account_id = parts[0]
role_name = parts[1]
elif len(parts) == 3:
partition = parts[0]
account_id = parts[1]
role_name = parts[2]
else:
parser.error('--role-arn must be a valid role arn or follow the format "::"')
if not account_id.isnumeric() or len(account_id) is not 12:
parser.error('--role-arn account id must be valid numeric account id of length 12')
arguments.role_arn = 'arn:{}:iam::{}:role/{}'.format(partition, account_id, role_name)
if arguments.principal_arn and not arguments.principal_arn.startswith('arn:'):
def main():
try:
if '--debug' in sys.argv:
logger.setLevel(logging.DEBUG)
logger.debug('Debug logs are visible')
elif '--info' in sys.argv:
logger.setLevel(logging.INFO)
logger.info('Info logs are visible')
logger.debug('Executing awsume')
run_awsume(sys.argv[1:])
except KeyboardInterrupt:
pass
def kill_autoawsume():
logger.debug('Killing autoawsume')
for proc in psutil.process_iter():
try:
for command_string in proc.cmdline():
if 'autoawsume' in command_string:
proc.kill()
except Exception:
pass
def kill(arguments: argparse.Namespace):
_, credentials_file = get_aws_files(None, None)
if arguments.profile_name:
logger.debug('Stoping auto-refresh of profile {}'.format(arguments.profile_name))
delete_section('autoawsume-{}'.format(arguments.profile_name), credentials_file)
profiles = read_aws_file(credentials_file)
profile_names = [_ for _ in profiles]
if any(['autoawsume-' in _ for _ in profile_names]):
print('Stop {}'.format(arguments.profile_name))
return
else:
logger.debug('There were not more autoawsume profiles, stopping autoawsume')
print('Kill')
kill_autoawsume()
else:
logger.debug('Stopping all auto refreshing and removing autoawsume profiles')
kill_autoawsume()
profiles = read_aws_file(credentials_file)
for profile in profiles:
if 'autoawsume-' in profile:
delete_section(profile, credentials_file)
print('Kill')
def parse_args(self, system_arguments: list) -> argparse.Namespace:
logger.debug('Gathering arguments')
epilog = """Thank you for using AWSume! Check us out at https://trek10.com"""
description="""Awsume - A cli that makes using AWS IAM credentials easy"""
argument_parser = argparse.ArgumentParser(
prog='awsume',
description=description,
epilog=epilog,
formatter_class=lambda prog: (argparse.RawDescriptionHelpFormatter(prog, max_help_position=80, width=80)), # pragma: no cover
)
self.plugin_manager.hook.pre_add_arguments(
config=self.config,
)
self.plugin_manager.hook.add_arguments(
config=self.config,
parser=argument_parser,
)
logger.debug('Parsing arguments')
def assume_role(
source_credentials: dict,
role_arn: str,
session_name: str,
external_id: str = None,
region: str = None,
role_duration: int = None,
mfa_serial: str = None,
mfa_token: str = None,
) -> dict:
if len(session_name) < 2:
session_name = session_name.center(2, '_')
logger.debug('Assuming role: {}'.format(role_arn))
logger.debug('Session name: {}'.format(session_name))
try:
boto_session = boto3.session.Session(
aws_access_key_id=source_credentials.get('AccessKeyId'),
aws_secret_access_key=source_credentials.get('SecretAccessKey'),
aws_session_token=source_credentials.get('SessionToken'),
region_name=region,
)
role_sts_client = boto_session.client('sts') # type: botostubs.STS
kwargs = { 'RoleSessionName': session_name, 'RoleArn': role_arn }
if external_id:
kwargs['ExternalId'] = external_id
if role_duration:
kwargs['DurationSeconds'] = int(role_duration)
if mfa_serial:
kwargs['SerialNumber'] = mfa_serial