Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SigmaLogSource class #258

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ line-length = 100
[build-system]
requires = ["poetry-core>=1.8.1"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
filterwarnings = ["ignore::DeprecationWarning"]
15 changes: 15 additions & 0 deletions sigma/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class SigmaLogSource:
category: Optional[str] = field(default=None)
product: Optional[str] = field(default=None)
service: Optional[str] = field(default=None)
definition: Optional[str] = field(default=None)
source: Optional[SigmaRuleLocation] = field(default=None, compare=False)
custom_attributes: Optional[Dict[str, Any]] = field(default=None, compare=False)

Expand All @@ -225,6 +226,18 @@ def __post_init__(self):
raise sigma_exceptions.SigmaLogsourceError(
"Sigma log source can't be empty", source=self.source
)
if self.category and not isinstance(self.category, str):
raise sigma_exceptions.SigmaLogsourceError(
"Sigma log source category must be string", source=self.source
)
if self.product and not isinstance(self.product, str):
raise sigma_exceptions.SigmaLogsourceError(
"Sigma log source product must be string", source=self.source
)
if self.service and not isinstance(self.service, str):
raise sigma_exceptions.SigmaLogsourceError(
"Sigma log source service must be string", source=self.source
)

@classmethod
def from_dict(
Expand All @@ -234,10 +247,12 @@ def from_dict(
custom_attributes = {
k: v for k, v in logsource.items() if k not in set(cls.__dataclass_fields__.keys())
}

return cls(
logsource.get("category"),
logsource.get("product"),
logsource.get("service"),
logsource.get("definition"),
source,
custom_attributes if len(custom_attributes) > 0 else None,
)
Expand Down
1 change: 0 additions & 1 deletion sigma/validators/core/logsources.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def validate(self, rule: SigmaRule) -> List[SigmaValidationIssue]:
return [
FieldnameLogsourceIssue(rules=[rule], fieldname=name)
for name in rule.logsource.custom_attributes
if not name == "definition"
]
else:
return []
22 changes: 22 additions & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ def test_sigmalogsource_fromdict_no_service():
assert logsource == SigmaLogSource("category-id", "product-id", None)


def test_sigmalogsource_fromdict_definition():
logsource = SigmaLogSource.from_dict(
{"category": "category-id", "product": "product-id", "definition": "use it"}
)
assert logsource == SigmaLogSource("category-id", "product-id", None, "use it")


def test_sigmalogsource_fromdict_category_not_str():
with pytest.raises(sigma_exceptions.SigmaLogsourceError):
SigmaLogSource.from_dict({"category": 1234, "product": "product-id"})


def test_sigmalogsource_fromdict_product_not_str():
with pytest.raises(sigma_exceptions.SigmaLogsourceError):
SigmaLogSource.from_dict({"category": "category-id", "product": {"a": "b"}})


def test_sigmalogsource_fromdict_service_not_str():
with pytest.raises(sigma_exceptions.SigmaLogsourceError):
SigmaLogSource.from_dict({"category": "category-id", "service": ["1", "2", "3"]})


def test_sigmalogsource_empty():
with pytest.raises(sigma_exceptions.SigmaLogsourceError, match="can't be empty.*test.yml"):
SigmaLogSource(None, None, None, source=sigma_exceptions.SigmaRuleLocation("test.yml"))
Expand Down