예제 #1
0
    def configure_or_check_default_profile(self):
        if not os.path.exists(self.config_path):
            create_config_file_now = input(
                f"No configuration file was found at {self.config_path}"
                f"\nWould you like to create this file with a default profile name now?"
                f" [y/n]: ")
            default_profile = None
        else:
            print(f"A configuration file exists at {self.config_path}")
            default_profile = load_data_from_yaml_file(self.config_path,
                                                       "default-profile")
            if default_profile:
                print(
                    f'It specifies a default profile name of "{default_profile}".'
                )
            else:
                print(f"It does not contain a default profile name.")
            create_config_file_now = input(
                f"Would you like to specify a new default profile name for the"
                f" configuration file now? [y/n]: ")

        if not create_config_file_now.strip().lower().startswith("y"):
            return

        while True:
            default_profile = input(
                f"Enter the name of your default AWS profile: ").strip()

            if default_profile:
                create_or_update_yaml_file(
                    self.config_path, {"default-profile": default_profile})
                print(
                    f'A default profile name of "{default_profile}" has been saved.'
                )
                break
            else:
                print(
                    f"Enter your default profile's name, or hit ctrl-c to exit."
                )
                continue

        return
예제 #2
0
    def display_cloudgoat_help(self, command):
        if not command or len(command) == 1:
            return print(help_text.CLOUDGOAT)

        # Makes "help foo" equivalent to "foo help".
        command.remove("help")

        if command[0] == "config":
            if len(command) > 1 and command[1] == "argcomplete":
                return print(help_text.CONFIG_ARGCOMPLETE)
            else:
                return print(help_text.CONFIG)
        elif command[0] == "create":
            return print(help_text.CREATE)
        elif command[0] == "destroy":
            return print(help_text.DESTROY)
        elif command[0] == "list":
            return print(help_text.LIST)
        elif command[0] == "help":
            if all([word == "help" for word in command]):
                joined_help_texts = " ".join(
                    ["help text for" for word in command])
                return print(f"Displays {joined_help_texts} CloudGoat.")
        else:
            scenario_name = normalize_scenario_name(command[0])
            scenario_dir_path = find_scenario_dir(self.scenarios_dir,
                                                  scenario_name)
            if scenario_dir_path:
                scenario_help_text = load_data_from_yaml_file(
                    os.path.join(scenario_dir_path, "manifest.yml"),
                    "help").strip()
                return print(
                    f"[cloudgoat scenario: {scenario_name}]\n{scenario_help_text}"
                )

        return print(
            f'Unrecognized command or scenario name. Try "cloudgoat.py help" or'
            f' "cloudgoat.py list all"')
예제 #3
0
    def parse_and_execute_command(self, parsed_args):
        command = parsed_args.command
        profile = parsed_args.profile

        # Display help text. Putting this first makes validation simpler.
        if command[0] == "help" or (len(command) >= 2
                                    and command[-1] == "help"):
            return self.display_cloudgoat_help(command)

        # Validation
        if len(command) == 1:
            if command[0] == "config":
                print(
                    f'The {command[0]} currently must be used with "whitelist",'
                    f' "profile", or "help".')
                return
            elif command[0] == "create":
                print(
                    f"The {command[0]} command must be used with either a scenario name"
                    f' or "help".'
                    f"\nAll scenarios:\n    " +
                    "\n    ".join(self.scenario_names))
                return
            elif command[0] == "destroy":
                print(
                    f"The {command[0]} command must be used with a scenario name,"
                    f' "all", or "help".'
                    f"\nAll scenarios:\n    " +
                    "\n    ".join(self.scenario_names))
                return
            elif command[0] == "list":
                print(
                    f"The {command[0]} command must be used with a scenario name,"
                    f' "all", "deployed", "undeployed", or "help".'
                    f"\nAll scenarios:\n    " +
                    "\n    ".join(self.scenario_names))
                return

        if command[0] in ("create", "destroy", "list"):
            if command[1].lower() in self.cloudgoat_commands:
                print(
                    f"CloudGoat scenarios cannot be named after CloudGoat commands."
                )
                return
            if command[1] in self.non_scenario_instance_dirs:
                print(
                    f'The name "{command[1]}" is reserved for CloudGoat and may not be'
                    f" used with the {command[0]} command.")
                return

        if command[0] in ("create", "destroy"):
            if not profile:
                if os.path.exists(self.config_path):
                    profile = load_data_from_yaml_file(self.config_path,
                                                       "default-profile")
                if not profile:
                    print(
                        f"The {command[0]} command requires the use of the --profile"
                        f" flag, or a default profile defined in the config.yml file"
                        f' (try "config profile").')
                    return
                else:
                    print(
                        f'Using default profile "{profile}" from config.yml...'
                    )

        # Execution
        if command[0] == "config":
            if command[1] == "whitelist" or command[1] == "whitelist.txt":
                return self.configure_or_check_whitelist(auto=parsed_args.auto,
                                                         print_values=True)
            elif command[1] == "profile":
                return self.configure_or_check_default_profile()
            elif command[1] == "argcomplete":
                return self.configure_argcomplete()

        elif command[0] == "create":
            return self.create_scenario(command[1], profile)

        elif command[0] == "destroy":
            if command[1] == "all":
                return self.destroy_all_scenarios(profile)
            else:
                return self.destroy_scenario(command[1], profile)

        elif command[0] == "list":
            if command[1] == "all":
                return self.list_all_scenarios()
            elif command[1] == "deployed":
                return self.list_deployed_scenario_instances()
            elif command[1] == "undeployed":
                return self.list_undeployed_scenarios()
            else:
                return self.list_scenario_instance(command[1])

        print(f'Unrecognized command. Try "cloudgoat.py help"')
        return