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_get_aws_files():
args = argparse.Namespace(config_file=None, credentials_file=None)
config = {}
config_file, credentials_file = aws_files.get_aws_files(args, config)
assert config_file == str(Path(constants.DEFAULT_CONFIG_FILE))
assert credentials_file == str(Path(constants.DEFAULT_CREDENTIALS_FILE))
def test_get_aws_files():
args = argparse.Namespace(config_file=None, credentials_file=None)
config = {}
config_file, credentials_file = aws_files.get_aws_files(args, config)
assert config_file == str(Path(constants.DEFAULT_CONFIG_FILE))
assert credentials_file == str(Path(constants.DEFAULT_CREDENTIALS_FILE))
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 load_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()
options = None
try:
options = yaml.safe_load(open(str(constants.AWSUME_CONFIG), 'r'))
except Exception as e:
raise exceptions.ConfigParseException(constants.AWSUME_CONFIG, message='Cannot parse config file', error=e)
if options is None:
options = defaults
write_config(options)
return options
config_file = os.environ.get('AWS_CONFIG_FILE')
elif args and args.config_file:
config_file = args.config_file
elif config and config.get('config-file'):
config_file = config.get('config-file')
else:
config_file = constants.DEFAULT_CONFIG_FILE
if os.environ.get('AWS_SHARED_CREDENTIALS_FILE'):
credentials_file = os.environ.get('AWS_SHARED_CREDENTIALS_FILE')
elif args and args.credentials_file:
credentials_file = args.credentials_file
elif config and config.get('credentials-file'):
credentials_file = config.get('credentials-file')
else:
credentials_file = constants.DEFAULT_CREDENTIALS_FILE
return str(Path(config_file)), str(Path(credentials_file))
def ensure_cache_dir():
cache_dir = str(constants.AWSUME_CACHE_DIR)
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
os.chmod(cache_dir, 0o700) #ensure directory is secure.
def load_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()
options = None
try:
options = yaml.safe_load(open(str(constants.AWSUME_CONFIG), 'r'))
except Exception as e:
raise exceptions.ConfigParseException(constants.AWSUME_CONFIG, message='Cannot parse config file', error=e)
if options is None:
options = defaults
write_config(options)
return options
def get_aws_files(args: argparse.Namespace, config: dict) -> tuple:
if os.environ.get('AWS_CONFIG_FILE'):
config_file = os.environ.get('AWS_CONFIG_FILE')
elif args and args.config_file:
config_file = args.config_file
elif config and config.get('config-file'):
config_file = config.get('config-file')
else:
config_file = constants.DEFAULT_CONFIG_FILE
if os.environ.get('AWS_SHARED_CREDENTIALS_FILE'):
credentials_file = os.environ.get('AWS_SHARED_CREDENTIALS_FILE')
elif args and args.credentials_file:
credentials_file = args.credentials_file
elif config and config.get('credentials-file'):
credentials_file = config.get('credentials-file')
else:
credentials_file = constants.DEFAULT_CREDENTIALS_FILE
return str(Path(config_file)), str(Path(credentials_file))
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 write_aws_cache(cache_file_name: str, session: dict) -> dict:
ensure_cache_dir()
cache_path = str(constants.AWSUME_CACHE_DIR) + '/' + cache_file_name
if os.path.exists(cache_path):
os.chmod(cache_path, 0o600)
else:
open(cache_path, 'a').close()
os.chmod(cache_path, 0o600)
logger.debug('Cache file path: ' + cache_path)
expiration = session['Expiration'].astimezone(dateutil.tz.tzlocal())
expiration = expiration.strftime('%Y-%m-%d %H:%M:%S')
try:
json.dump({
**session,
'Expiration': expiration,
}, open(cache_path, 'w'), indent=2, default=str)
except:
logger.debug('There was an error writing to the cache file', exc_info=True)
session['Expiration'] = datetime.strptime(expiration, '%Y-%m-%d %H:%M:%S')