def test_init_aws_api_configuration_policy(): configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "configuration_values.py") configuration.init_from_file() assert configuration.aws_api_s3_cache_dir == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/s3") assert configuration.aws_api_s3_buckets_cache_file == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/s3/buckets.json") assert configuration.aws_api_ec2_cache_dir == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/ec2") assert configuration.aws_api_ec2_security_groups_cache_file == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/ec2/network_security_groups.json") assert configuration.aws_api_lambda_cache_dir == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/lambda") assert configuration.aws_api_lambdas_cache_file == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/lambda/lambdas.json") assert configuration.aws_api_cleanup_reports_dir == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/cleanup") assert configuration.aws_api_cleanups_lambda_file == os.path.join( os.path.dirname(os.path.abspath(__file__)), "ignore/cache/12345678910/cleanup/lambda.txt")
def put_secret_value(arguments) -> None: configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = arguments.configuration_file_full_path configuration.init_from_file() aws_api = AWSAPI(configuration) aws_api.put_secret_value(arguments.secret_name, arguments.secret_value)
def get_secret_file(arguments) -> None: if arguments.configuration_file_full_path != "None": configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = arguments.configuration_file_full_path configuration.init_from_file() aws_api = AWSAPI(configuration) else: aws_api = AWSAPI() aws_api.get_secret_file(arguments.secret_name, arguments.secret_file_path)
def init_and_cache(arguments) -> None: configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = arguments.configuration_file_full_path configuration.init_from_file() aws_api = AWSAPI(configuration) init_functions = {"interfaces": aws_api.init_network_interfaces} cache_files = { "interfaces": configuration.aws_api_ec2_network_interfaces_cache_file } objects = init_functions[arguments.target]() aws_api.cache_objects(objects, cache_files[arguments.target])
def cleanup(arguments) -> None: configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = arguments.configuration_file_full_path configuration.init_from_file() aws_api = AWSAPI(configuration) init_functions = {"interfaces": aws_api.init_network_interfaces} cache_files = { "interfaces": configuration.aws_api_ec2_network_interfaces_cache_file } output_files = { "interfaces": configuration.aws_api_cleanups_network_interfaces_report_file } init_functions[arguments.target](from_cache=True, cache_file=cache_files[arguments.target]) aws_api.cleanup_report_network_interfaces(output_files[arguments.target])
def __init__(self, configuration): aws_api_conf = AWSAPIConfigurationPolicy() aws_api_conf.configuration_file_full_path = configuration.aws_api_configuration_values_file_path aws_api_conf.init_from_file() self.aws_api = AWSAPI(configuration=aws_api_conf) jenkins_conf = JenkinsConfigurationPolicy() jenkins_conf.configuration_file_full_path = configuration.jenkins_manager_configuration_values_file_path jenkins_conf.init_from_file() self.jenkins_manager = JenkinsManager(configuration=jenkins_conf) self.jenkins_ec2_instance_type = configuration.jenkins_ec2_instance_type self.ssh_master_access_key_name = "jenkins-master-access-key" self.ssh_agent_access_key_name = "jenkins-agent-access-key" self.jenkins_mater_iam_role_name = "role-jenkins-master" self.jenkins_mater_iam_role_name = "policy-jenkins-master-spot-fleet-access" self.public_subnet_id = "" self.private_subnet_id = "" self.jenkins_master_security_group_name = "" self.ssh_agent_security_group_name = ""
def set_session_credentials(arguments, configs_dict) -> None: configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = "~/Desktop/tmp/configuration_values.py" configuration.init_from_file() accounts = CommonUtils.load_object_from_module(configuration.accounts_file, "main") AWSAccount.set_aws_account(accounts[configuration.aws_api_account]) session = SessionsManager.connect_session() credentials = session.get_credentials() credentials = credentials.get_frozen_credentials() ret = f"\n\n[{arguments.profile_name}]" ret += f"\naws_access_key_id = {credentials.access_key}" ret += f"\naws_secret_access_key = {credentials.secret_key}" ret += f"\naws_session_token = {credentials.token}" with open("~/.aws/credentials") as file_handler: contents = file_handler.read() if arguments.profile_name in contents: start_index = contents.index(f"[{arguments.profile_name}]") try: end_index = contents.index("[", start_index + 1) tail_string = "\n\n" + contents[end_index:].strip("\n") except ValueError: tail_string = "" new_contents = contents[:start_index].strip("\n") + ret + tail_string with open("~/.aws/credentials", "w+") as file_handler: file_handler.write(new_contents) else: with open("~/.aws/credentials", "a+") as file_handler: file_handler.write(ret)
from horey.aws_api.aws_services_entities.aws_lambda import AWSLambda from horey.aws_api.aws_services_entities.lambda_event_source_mapping import LambdaEventSourceMapping from horey.common_utils.common_utils import CommonUtils configuration_values_file_full_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "h_logger_configuration_values.py") logger = get_logger( configuration_values_file_full_path=configuration_values_file_full_path) configuration = AWSAPIConfigurationPolicy() configuration.configuration_file_full_path = os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", "ignore", "aws_api_configuration_values.py")) configuration.init_from_file() aws_api = AWSAPI(configuration=configuration) mock_values_file_path = os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ignore", "mock_values.py")) mock_values = CommonUtils.load_object_from_module(mock_values_file_path, "main") def test_add_managed_region(): aws_api.add_managed_region(Region.get_region("us-west-2")) def test_provision_certificate():