def switch_to_session(session_name): if session_name: all_sessions = get_all_active_sessions() if session_name not in all_sessions: util.error_log(f'Session {session_name} unavailable') # replace the default profile in the AWS_CREDS file replace_config_section(config.AWS_CREDS_PATH, 'default', all_sessions[session_name]) print('INFO: Switched to => ' + util.green_text('{}'.format(session_name)))
def get_base_credentials_for_project(project_name): base_config = get_base_aws_config() base_profile_name = util.get_base_aws_profile_for_project(project_name) try: base_credentials_section = base_config[base_profile_name] key_id = base_credentials_section['aws_access_key_id'] access_key = base_credentials_section['aws_secret_access_key'] except NoSectionError and KeyError: util.error_log( "Credentials for profile '[%s]' is missing. " "You must add this section to your AWS credentials file." % (base_profile_name, )) except NoOptionError as e: util.error_log(e) return {'key_id': key_id, 'access_key': access_key}
def perform_reset(): if os.path.exists(config.AWS_ASSUME_CONFIG_PATH): try: ans = config_collector.ConfirmationDialog( f'This file => "{config.AWS_ASSUME_CONFIG_PATH}" will be deleted. ' f'Are you sure you want to perform a reset?: ').get_answer() if ans: os.remove(config.AWS_ASSUME_CONFIG_PATH) util.info_log( f'The file "{config.AWS_ASSUME_CONFIG_PATH}" is deleted') except Exception: util.error_log( f'The file "{config.AWS_ASSUME_CONFIG_PATH}" could not be deleted' ) else: util.error_log( f'The file "{config.AWS_ASSUME_CONFIG_PATH}" does not exist') pass
def configure(write_mode='w', check_file_existence=True): if check_file_existence and os.path.isfile(config.AWS_ASSUME_CONFIG_PATH): util.error_log( 'File %s already exists.' 'Run `aws-sessions-switcher projects add` if you want to add a new project configuration.' 'Type \'aws-sessions-switcher -h\' to see all the available sub-commands' % (config.AWS_ASSUME_CONFIG_PATH, )) else: _collector = config_collector.ConfigCollector() answers = _collector.collect() if not answers: return cfg_parser = configparser.ConfigParser() cfg_parser[ f"{answers['project_name']}-{answers['project_environment']}"] = answers with open(config.AWS_ASSUME_CONFIG_PATH, write_mode) as configfile: cfg_parser.write(configfile) print( util.yellow_text( f"Note: Make sure to put your security credentials under " f"\"{util.get_base_aws_profile_for_project(answers['project_name'])}\" " f"section of your AWS Credentials"))