Ejemplo n.º 1
0
 def __init__(self, session, config_path=None):
     reset_config(filepath=config_path)
     try:
         control_config()
     except ValueError as e:
         logger.error(e)
         raise e
     self.config_path = config_path
     self.host = config.get(Sections.SERVER, "host")
     self.api_port = config.get(Sections.SERVER, "api_port")
     self.websocket_port = config.get(Sections.SERVER, "websocket_port")
     self.workspace = config.get(Sections.SERVER, "workspace")
     self.agent_token = config[Sections.TOKENS].get("agent", None)
     self.agent_name = config.get(Sections.AGENT, "agent_name")
     self.session = session
     self.websocket = None
     self.websocket_token = None
     executors_list_str = config[Sections.AGENT].get("executors", []).split(",")
     if "" in executors_list_str:
         executors_list_str.remove("")
     self.executors = {
         executor_name:
             Executor(executor_name, config) for executor_name in executors_list_str
     }
     ssl_cert_path = config[Sections.SERVER].get("ssl_cert", None)
     self.ws_ssl_enabled = self.api_ssl_enabled = config[Sections.SERVER].get("ssl", "False").lower() in ["t", "true"]
     self.api_kwargs = {"ssl": ssl.create_default_context(cafile=ssl_cert_path)} if self.api_ssl_enabled and ssl_cert_path else {}
     self.ws_kwargs = {"ssl": ssl.create_default_context(cafile=ssl_cert_path)} if self.ws_ssl_enabled and ssl_cert_path else {}
     self.execution_id = None
    def run(self):
        end = False

        def_value, choices = get_default_value_and_choices("Q", ["A", "E", "Q"])

        while not end:
            value = click.prompt("Do you want to edit the [A]gent or the [E]xecutors? Do you want to [Q]uit?",
                                 type=click.Choice(choices=choices, case_sensitive=False),
                                 default=def_value)
            if value.upper() == "A":
                process_agent()
            elif value.upper() == "E":
                self.process_executors()
            else:
                process_choice_errors(value)
                try:
                    if Sections.AGENT in config.instance.sections():
                        self.save_executors()
                        config.control_config()
                        end = True
                    else:
                        print(f"{Bcolors.FAIL}Add agent configuration{Bcolors.ENDC}")

                except ValueError as e:
                    print(f"{Bcolors.FAIL}{e}{Bcolors.ENDC}")

        config.save_config(self.config_filepath)
    def __init__(self, session, config_path=None):
        reset_config(filepath=config_path)
        try:
            verify()
            control_config()
        except ValueError as e:
            logger.error(e)
            raise e
        self.config_path = config_path
        self.host = config.get(Sections.SERVER, "host")
        self.api_port = config.get(Sections.SERVER, "api_port")
        self.websocket_port = config.get(Sections.SERVER, "websocket_port")
        self.agent_token = config[Sections.TOKENS].get(
            "agent", None) if Sections.TOKENS in config else None
        self.agent_name = config.get(Sections.AGENT, "agent_name")
        self.session = session
        self.websocket = None
        self.websocket_token = None
        self.workspaces = _parse_list(config.get(Sections.SERVER,
                                                 "workspaces"))

        self.executors = {
            executor_name: Executor(executor_name, config)
            for executor_name in _parse_list(config[Sections.AGENT].get(
                "executors", ""))
        }
        self.ws_ssl_enabled = self.api_ssl_enabled = config[
            Sections.SERVER].get("ssl", "False").lower() in [
                "t",
                "true",
            ]
        ssl_cert_path = config[Sections.SERVER].get("ssl_cert", None)
        if not Path(ssl_cert_path).exists():
            raise ValueError(
                f"SSL cert does not exist in path {ssl_cert_path}")
        self.api_kwargs: Dict[str, object] = ({
            "ssl":
            ssl.create_default_context(cafile=ssl_cert_path)
            if "HTTPS_PROXY" not in os.environ else False
        } if self.api_ssl_enabled and ssl_cert_path else {})
        if "HTTPS_PROXY" in os.environ:
            logger.info("HTTPS_PROXY is set; will not do SSL verify")
            ws_ssl_context = ssl.create_default_context()
            ws_ssl_context.check_hostname = False
            ws_ssl_context.verify_mode = ssl.CERT_NONE
        else:
            ws_ssl_context = ssl.create_default_context(cafile=ssl_cert_path)
        self.ws_kwargs = {"ssl": ws_ssl_context} if self.ws_ssl_enabled else {}
        self.execution_id = None
        self.executor_tasks: Dict[str, List[Task]] = {
            Dispatcher.TaskLabels.EXECUTOR: [],
            Dispatcher.TaskLabels.CONNECTION_CHECK: [],
        }
        self.sigterm_received = False
 def __init__(self, session, config_path=None):
     reset_config(filepath=config_path)
     try:
         control_config()
     except ValueError as e:
         logger.error(e)
         raise e
     self.config_path = config_path
     self.host = config.instance[Sections.SERVER]["host"]
     self.api_port = config.instance[Sections.SERVER]["api_port"]
     self.websocket_port = config.instance[Sections.SERVER]["websocket_port"]
     self.agent_token = (
         config.instance[Sections.TOKENS].get("agent") if Sections.TOKENS in config.instance else None
     )
     self.agent_name = config.instance[Sections.AGENT]["agent_name"]
     self.session = session
     self.websocket = None
     self.websocket_token = None
     self.workspaces = config.instance[Sections.SERVER]["workspaces"]
     self.executors = {
         executor_name: Executor(executor_name, executor_data)
         for executor_name, executor_data in config.instance[Sections.AGENT].get("executors", {}).items()
     }
     self.ws_ssl_enabled = self.api_ssl_enabled = config.instance[Sections.SERVER].get("ssl", False)
     ssl_cert_path = config.instance[Sections.SERVER].get("ssl_cert", None)
     ssl_ignore = config.instance[Sections.SERVER].get("ssl_ignore", False)
     if not Path(ssl_cert_path).exists():
         raise ValueError(f"SSL cert does not exist in path {ssl_cert_path}")
     if self.api_ssl_enabled:
         if ssl_cert_path:
             ssl_cert_context = ssl.create_default_context(cafile=ssl_cert_path)
             self.api_kwargs = {"ssl": ssl_cert_context}
             self.ws_kwargs = {"ssl": ssl_cert_context}
         else:
             if ssl_ignore or "HTTPS_PROXY" in os.environ:
                 ignore_ssl_context = ssl.create_default_context()
                 ignore_ssl_context.check_hostname = False
                 ignore_ssl_context.verify_mode = ssl.CERT_NONE
                 self.api_kwargs = {"ssl": ignore_ssl_context}
                 self.ws_kwargs = {"ssl": ignore_ssl_context}
             else:
                 self.api_kwargs: Dict[str, object] = {}
                 self.ws_kwargs: Dict[str, object] = {}
     else:
         self.api_kwargs: Dict[str, object] = {}
         self.ws_kwargs: Dict[str, object] = {}
     self.execution_id = None
     self.executor_tasks: Dict[str, List[Task]] = {
         Dispatcher.TaskLabels.EXECUTOR: [],
         Dispatcher.TaskLabels.CONNECTION_CHECK: [],
     }
     self.sigterm_received = False
Ejemplo n.º 5
0
    def __init__(self, config_filepath: Path):
        self.config_filepath = config_filepath

        try:
            config.reset_config(config_filepath)
        except ValueError as e:
            if e.args[1] or config_filepath.is_file():
                # the filepath is either a file, or a folder containing a file,
                # which can't be processed
                raise e
        config.control_config()
        self.executors_dict = {}
        self.load_executors()
Ejemplo n.º 6
0
    async def run(self):
        end = False
        ignore_changes = False
        def_value, choices = get_default_value_and_choices(
            "Q", ["A", "E", "Q"])

        while not end:
            value = click.prompt(
                "Do you want to edit the [A]gent or the [E]xecutors? Do you "
                "want to [Q]uit?",
                type=click.Choice(choices=choices, case_sensitive=False),
                default=def_value,
            )
            if value.upper() == "A":
                process_agent()
            elif value.upper() == "E":
                await self.process_executors()
            else:
                process_choice_errors(value)
                try:
                    if Sections.AGENT in config.instance:
                        click.echo(
                            self.status_report(sections=config.instance))
                        config.control_config()
                        end = True
                    else:
                        if confirm_prompt(
                                click.style(
                                    "File configuration not saved. Are you sure?",
                                    fg="yellow")):
                            click.echo(
                                self.status_report(
                                    sections=config.instance.sections()))
                            end = True
                            ignore_changes = True
                        else:
                            end = False

                except ValueError as e:
                    click.secho(f"{e}", fg="red")
        if not ignore_changes:
            config.save_config(self.config_filepath)