Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_safe_print_color(open: MagicMock, stderr: MagicMock, yaml_load: MagicMock):
yaml_load.return_value = {'colors': True}
with patch.object(os, 'name', 'nt'):
safe_print('Text', color=colorama.Fore.RED)
assert colorama.Fore.RED not in stderr.getvalue()
with patch.object(os, 'name', 'darwin'):
safe_print('Text', color=colorama.Fore.RED)
assert colorama.Fore.RED in stderr.getvalue()
def test_safe_print_color(open: MagicMock, stderr: MagicMock, yaml_load: MagicMock):
yaml_load.return_value = {'colors': True}
with patch.object(os, 'name', 'nt'):
safe_print('Text', color=colorama.Fore.RED)
assert colorama.Fore.RED not in stderr.getvalue()
with patch.object(os, 'name', 'darwin'):
safe_print('Text', color=colorama.Fore.RED)
assert colorama.Fore.RED in stderr.getvalue()
choice = principal_plus_role_arn
elif args.profile_name:
profile_role_arn = profiles.get(args.profile_name, {}).get('role_arn')
principal_arn = profiles.get(args.profile_name, {}).get('principal_arn')
if profile_role_arn is None or principal_arn is None:
raise exceptions.InvalidProfileError(args.profile_name, 'both role_arn and principal_arn are necessary for saml profiles')
principal_plus_profile_role_arn = ','.join([principal_arn, profile_role_arn])
if principal_plus_profile_role_arn in roles:
choice = principal_plus_profile_role_arn
else:
raise exceptions.SAMLRoleNotFoundError(principal_arn, profile_role_arn)
safe_print('Match: {}'.format(choice))
else:
for index, choice in enumerate(roles):
safe_print('{}) {}'.format(index, choice), color=colorama.Fore.LIGHTYELLOW_EX)
safe_print('Which role do you want to assume? > ', end='', color=colorama.Fore.LIGHTCYAN_EX)
response = input()
if response.isnumeric():
choice = roles[int(response)]
else:
choice = difflib.get_close_matches(response, roles, cutoff=0)[0]
role_arn = choice.split(',')[1]
principal_arn = choice.split(',')[0]
else:
role_arn = roles[0].split(',')[1]
principal_arn = roles[0].split(',')[0]
safe_print('Assuming role: {},{}'.format(principal_arn, role_arn), color=colorama.Fore.GREEN)
credentials = aws_lib.assume_role_with_saml(
role_arn,
principal_arn,
assertion,
region=None,
logger.debug('Parsing arguments')
args = argument_parser.parse_args(system_arguments)
logger.debug('Handling arguments')
if args.refresh_autocomplete:
autocomplete_file = Path('~/.awsume/autocomplete.json').expanduser()
result = self.plugin_manager.hook.get_profile_names(
config=self.config,
arguments=args,
)
profile_names = [y for x in result for y in x]
json.dump({'profile-names': profile_names}, open(autocomplete_file, 'w'))
raise exceptions.EarlyExit()
if args.list_plugins:
for plugin_name, _ in self.plugin_manager.list_name_plugin():
if 'default_plugins' not in plugin_name:
safe_print(plugin_name, color=colorama.Fore.LIGHTCYAN_EX)
raise exceptions.EarlyExit()
self.plugin_manager.hook.post_add_arguments(
config=self.config,
arguments=args,
parser=argument_parser,
)
args.system_arguments = system_arguments
return args
def get_mfa_token() -> str:
token_pattern = re.compile('^[0-9]{6}$')
safe_print('Enter MFA token: ', colorama.Fore.CYAN, end='')
while True:
mfa_token = input()
if token_pattern.match(mfa_token):
return mfa_token
else:
safe_print('Please enter a valid MFA token: ', colorama.Fore.CYAN, end='')
raise exceptions.ConfigOperationException('Must supply value to set {} to'.format(operations[1]))
logger.debug('Setting {} to {}'.format(operations[1], operations[2]))
value = get_value_from_args(operations[2:])
config = update_dict_parts(config, operations[1], value)
if operations[0].lower() in ['reset']:
default_value = get_dict_parts(defaults, operations[1])
if default_value is None:
raise exceptions.ConfigOperationException('Key does not have a default: {}'.format(operations[1]), colorama.Fore.YELLOW)
config = update_dict_parts(config, operations[1], default_value)
safe_print('Reset key {} to {}'.format(operations[1], default_value), colorama.Fore.YELLOW)
if operations[0].lower() in ['clear']:
config, deleted = delete_dict_value_parts(config, operations[1])
if deleted:
safe_print('Deleted key {}'.format(operations[1]), colorama.Fore.YELLOW)
write_config(config)
def write_config(config: dict):
if not os.path.exists(str(constants.AWSUME_DIR)):
os.makedirs(str(constants.AWSUME_DIR))
if not os.path.isfile(str(constants.AWSUME_CONFIG)):
open(str(constants.AWSUME_CONFIG), 'a').close()
try:
yaml.safe_dump(config, open(str(constants.AWSUME_CONFIG), 'w'), width=1000)
except Exception as e:
safe_print('Unable to write config: {}'.format(e), colorama.Fore.RED)
def get_mfa_token() -> str:
token_pattern = re.compile('^[0-9]{6}$')
safe_print('Enter MFA token: ', colorama.Fore.CYAN, end='')
while True:
mfa_token = input()
if token_pattern.match(mfa_token):
return mfa_token
else:
safe_print('Please enter a valid MFA token: ', colorama.Fore.CYAN, end='')