Skip to content

Commit

Permalink
call validator when executing trigger/action
Browse files Browse the repository at this point in the history
  • Loading branch information
a-leonardi committed Oct 1, 2024
1 parent 23e1e36 commit 5736f9f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
1 change: 1 addition & 0 deletions sekoia_automation/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 8 additions & 48 deletions sekoia_automation/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}
Expand All @@ -583,4 +544,3 @@ def validate(self):
timeout=30,
)
response.raise_for_status()

1 change: 1 addition & 0 deletions sekoia_automation/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def test_logs_url():
mock.assert_called_with(trigger.LOGS_URL_FILE_NAME)


def test_validation_callback_url():
trigger = DummyTrigger()
with patch.object(
Module, "load_config", return_value="validation_callback"
) as mock:
assert trigger.validation_callback_url == "validation_callback"
mock.assert_called_with(trigger.VALIDATION_CALLBACK_URL_FILE_NAME)


def test_trigger_configuration():
trigger = DummyTrigger()
with (
Expand Down

0 comments on commit 5736f9f

Please sign in to comment.