Skip to content

Commit

Permalink
Merge branch 'main' into feature/sync_http_client
Browse files Browse the repository at this point in the history
  • Loading branch information
squioc committed Sep 11, 2024
2 parents 55fa946 + 3a94f6c commit a5ec84d
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 316 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:

- id: ruff
name: Format with Ruff
entry: poetry run ruff
entry: poetry run ruff check
language: system
types: [ python ]
args: [ --fix, --exit-non-zero-on-fix, . ]
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.14.1] - 2024-09-10

### Fixed

- For GenericAPIAction, handle when the response is a no content response
- Fix dependency issue

## [1.14.0] - 2024-08-30

Expand Down
625 changes: 319 additions & 306 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "sekoia-automation-sdk"

version = "1.14.0"
version = "1.14.1"
description = "SDK to create Sekoia.io playbook modules"
license = "MIT"
readme = "README.md"
Expand Down Expand Up @@ -52,6 +52,7 @@ aiobotocore = { version = "^2.5.2", optional = true }
aiofiles = { version = "^23.1.0", optional = true }
aiocsv = { version = "^1.2.4", optional = true }
loguru = { version = "^0.7.0", optional = true }
jsonschema = "^4.22.0"

[tool.poetry.group.dev.dependencies]
unittest-xml-reporting = "^3"
Expand All @@ -69,7 +70,6 @@ types-requests = "^2.31.0.1"
types-pyyaml = "^6.0.12.10"
types-python-slugify = "^8.0.0.2"
pre-commit = "^3.3.3"
jsonschema = "^4.22.0"
requests-oauthlib = "^2.0.0"

[tool.poetry.group.lint.dependencies]
Expand Down
8 changes: 6 additions & 2 deletions sekoia_automation/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
MissingActionArgumentFileError,
SendEventError,
)
from sekoia_automation.module import Module, ModuleItem
from sekoia_automation.module import LogLevelStr, Module, ModuleItem
from sekoia_automation.storage import UPLOAD_CHUNK_SIZE
from sekoia_automation.utils import chunks, returns

Expand Down Expand Up @@ -107,7 +107,11 @@ def execute(self) -> None:
self.send_results()

def log(
self, message: str, level: str = "debug", only_sentry: bool = True, **kwargs
self,
message: str,
level: LogLevelStr = "debug",
only_sentry: bool = True,
**kwargs,
) -> None:
"""Log a message with a specific level."""
self._logs.append(
Expand Down
10 changes: 8 additions & 2 deletions sekoia_automation/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABC, abstractmethod
from functools import cached_property
from pathlib import Path
from typing import Any, cast
from typing import Any, Literal, cast

import requests
import sentry_sdk
Expand All @@ -25,6 +25,8 @@
get_as_model,
)

LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"]


class Module:
MODULE_CONFIGURATION_FILE_NAME = "module_configuration"
Expand Down Expand Up @@ -380,7 +382,11 @@ def _ensure_data_path_set(self):
raise

def log(
self, message: str, level: str = "debug", only_sentry: bool = False, **kwargs
self,
message: str,
level: LogLevelStr = "debug",
only_sentry: bool = False,
**kwargs,
) -> None:
"""Log a message with a specific level."""
# Right now propagates to sentry only errors and warnings
Expand Down
4 changes: 2 additions & 2 deletions sekoia_automation/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
TriggerConfigurationError,
)
from sekoia_automation.metrics import PrometheusExporterThread, make_exporter
from sekoia_automation.module import Module, ModuleItem
from sekoia_automation.module import LogLevelStr, Module, ModuleItem
from sekoia_automation.timer import RepeatedTimer
from sekoia_automation.utils import (
capture_retry_error,
Expand Down Expand Up @@ -306,7 +306,7 @@ def log_exception(self, exception: Exception, **kwargs):
message = kwargs.get("message", "An exception occurred")
self.log(f"{message}\n{exception}", level="error", propagate=False)

def log(self, message: str, level: str = "info", *args, **kwargs) -> None:
def log(self, message: str, level: LogLevelStr = "info", *args, **kwargs) -> None:
if level == "critical" and self._critical_log_sent:
# Prevent sending multiple critical errors
level = "error"
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def mocked_load_config(module, file_name, type_="str", non_exist_ok=False):
elif "callback" in file_name:
return FAKE_URL
elif "sentry" in file_name:
return "http://1234@localhost/123"
return None
else:
return DEFAULT_ARGUMENTS

Expand Down
67 changes: 67 additions & 0 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# third parties
import pytest
from pydantic import BaseModel
from sentry_sdk import get_isolation_scope

# internal
from sekoia_automation.exceptions import CommandNotFoundError, ModuleConfigurationError
Expand Down Expand Up @@ -196,3 +197,69 @@ def test_configuration_setter_missing_required_secret():
with pytest.raises(expected_exception=ModuleConfigurationError):
module.configuration = {"not a secret": "some value"}
assert module.configuration == {secret_name: "some other value"}


def test_playbook_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.playbook_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_playbook_run_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.playbook_run_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_node_run_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.node_run_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_trigger_configuration_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert (
module.trigger_configuration_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
)


def test_connector_configuration_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert (
module.connector_configuration_uuid
== "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
)


def test_init_sentry():
module = Module()
module._community_uuid = "community"
module._playbook_uuid = "playbook"
module._playbook_run_uuid = "playbook_run"
module._node_run_uuid = "node_run"
module._trigger_configuration_uuid = "trigger_configuration"
module._connector_configuration_uuid = "connector_configuration"

with patch.object(
module, "_load_sentry_dsn", return_value="http://1234@localhost/1234"
):
module.init_sentry()
tags = get_isolation_scope()._tags
assert tags["community"] == "community"
assert tags["playbook_uuid"] == "playbook"
assert tags["playbook_run_uuid"] == "playbook_run"
assert tags["trigger_configuration_uuid"] == "trigger_configuration"
assert tags["connector_configuration_uuid"] == "connector_configuration"

0 comments on commit a5ec84d

Please sign in to comment.