From 193a5a66ae18a63c2314c72e4955f69343064237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20L=C3=A9onardi?= Date: Tue, 1 Oct 2024 16:35:39 +0200 Subject: [PATCH] call validator when executing trigger/action --- sekoia_automation/action.py | 1 + sekoia_automation/module.py | 56 ++++++------------------------------ sekoia_automation/trigger.py | 1 + 3 files changed, 10 insertions(+), 48 deletions(-) diff --git a/sekoia_automation/action.py b/sekoia_automation/action.py index 48d7a53..6e8e961 100644 --- a/sekoia_automation/action.py +++ b/sekoia_automation/action.py @@ -97,6 +97,7 @@ def error_message(self) -> str | None: def execute(self) -> None: try: + self.validate_module_configuration() self._ensure_data_path_set() self.set_task_as_running() self._results = self.run(self.arguments) diff --git a/sekoia_automation/module.py b/sekoia_automation/module.py index 25e48d3..aa83bff 100644 --- a/sekoia_automation/module.py +++ b/sekoia_automation/module.py @@ -490,54 +490,15 @@ def stop_monitoring(self): Stops the background monitoring operations """ - def validate(self): - # Check if "validate_module_configuration" is in the docker parameters - main_py_path = self.__module_path / "main.py" - with open(main_py_path) as file: - content = file.read() - - tree = ast.parse(content) - - docker_params = [] - node: Any - for node in ast.walk(tree): - if ( - hasattr(node, "func") - and isinstance(node.func, ast.Attribute) - and node.func.attr == "register" - ): - docker_param: str | None = None - if len(node.args) > 1 and isinstance(node.args[1], ast.Constant): - # provided as positional arg - docker_param = node.args[1].s - - elif len(node.keywords) > 0: - # provided as keyword arg - docker_param = node.keywords[0].value.s - - docker_params.append(docker_param) - - if "validate_module_configuration" not in docker_params: - return - - # Call the actual validation procedure - status = self.do_validation() + @abstractmethod + def validator(self) -> bool: + """To define in subclasses. Validates the configuration of the module. - # Return result of validation to Symphony - data = {"validation_status": status} - validation_callback_url = self.load_config( - self.VALIDATION_CALLBACK_URL_FILE_NAME - ) - response = requests.request( - "POST", - validation_callback_url, - json=data, - headers={"Authorization": f"Bearer {self.token}"}, - timeout=30, - ) - response.raise_for_status() + Returns: + bool: True if the module is valid, False otherwise + """ - def validate(self): + def validate_module_configuration(self): # Check if "validate_module_configuration" is in the docker parameters main_py_path = self.__module_path / "main.py" with open(main_py_path) as file: @@ -568,7 +529,7 @@ def validate(self): return # Call the actual validation procedure - status = self.do_validation() + status = self.validator() # Return result of validation to Symphony data = {"validation_status": status} @@ -583,4 +544,3 @@ def validate(self): timeout=30, ) response.raise_for_status() - diff --git a/sekoia_automation/trigger.py b/sekoia_automation/trigger.py index 9033492..80fb671 100644 --- a/sekoia_automation/trigger.py +++ b/sekoia_automation/trigger.py @@ -176,6 +176,7 @@ def execute(self) -> None: self._secrets = self._get_secrets_from_server() self.module.set_secrets(self._secrets) self._logs_timer.start() + self.validate_module_configuration() try: while not self._stop_event.is_set(): try: