Skip to content

Commit

Permalink
Improved test coverage and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspatzke committed Oct 29, 2023
1 parent 37910bf commit 126eb88
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
16 changes: 7 additions & 9 deletions sigma/correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum, auto
from typing import List, Optional
import sigma.exceptions as sigma_exceptions
from sigma.exceptions import SigmaRuleLocation
from sigma.exceptions import SigmaRuleLocation, SigmaTimespanError
from sigma.rule import EnumLowercaseStringMixin, SigmaRule, SigmaRuleBase


Expand Down Expand Up @@ -70,7 +70,7 @@ def to_dict(self) -> dict:
return {self.op.name.lower(): self.count}


def parse_timespan(timespan: str) -> int:
def parse_timespan(timespan: str, source: Optional[SigmaRuleLocation] = None) -> int:
"""
Parses a string representing a time span and returns the equivalent number of seconds.
Expand All @@ -97,7 +97,9 @@ def parse_timespan(timespan: str) -> int:
}[timespan[-1]]
)
except (ValueError, KeyError):
raise sigma_exceptions.SigmaTimespanError(f"Timespan '{ timespan }' is invalid.")
raise sigma_exceptions.SigmaTimespanError(
f"Timespan '{ timespan }' is invalid.", source=source
)


def seconds_to_timespan(seconds: int) -> str:
Expand Down Expand Up @@ -213,12 +215,8 @@ def from_dict(
if timespan is not None:
try:
timespan = parse_timespan(timespan)
except ValueError:
errors.append(
sigma_exceptions.SigmaCorrelationTypeError(
f"Sigma correlation rule with invalid timespan", source=source
)
)
except SigmaTimespanError as e:
errors.append(e)
else:
errors.append(
sigma_exceptions.SigmaCorrelationRuleError(
Expand Down
12 changes: 6 additions & 6 deletions sigma/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,25 @@ class SigmaPlaceholderError(SigmaValueError):
pass


class SigmaCorrelationTypeError(SigmaValueError):
"""Wrong Sigma correlation type."""
class SigmaCorrelationRuleError(SigmaValueError):
"""Error in Sigma correlation rule."""

pass


class SigmaCorrelationRuleError(SigmaValueError):
"""Error in Sigma correlation rule."""
class SigmaCorrelationTypeError(SigmaCorrelationRuleError):
"""Wrong Sigma correlation type."""

pass


class SigmaCorrelationConditionError(SigmaValueError):
class SigmaCorrelationConditionError(SigmaCorrelationRuleError):
"""Error in Sigma correlation condition."""

pass


class SigmaTimespanError(Exception):
class SigmaTimespanError(SigmaCorrelationRuleError):
"""Raised when the timespan for calculating sigma is invalid."""

pass
Expand Down
27 changes: 27 additions & 0 deletions tests/test_correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ def test_correlation_invalid_timespan():
)


def test_correlation_without_timespan():
with pytest.raises(SigmaCorrelationRuleError, match="Sigma correlation rule without timespan"):
SigmaCorrelationRule.from_dict(
{
"name": "Invalid time span",
"correlation": {
"type": "event_count",
"rules": "failed_login",
"group-by": ["user"],
"condition": {"gte": 10},
},
}
)


def test_correlation_invalid_ordered():
with pytest.raises(
SigmaCorrelationRuleError, match="Sigma correlation ordered definition must be boolean"
Expand Down Expand Up @@ -268,6 +283,18 @@ def test_correlation_without_condition():
)


def test_correlation_without_condition_post_init_check():
with pytest.raises(SigmaCorrelationRuleError, match="Sigma correlation rule without condition"):
SigmaCorrelationRule(
type=SigmaCorrelationType.EVENT_COUNT,
rules=[SigmaRuleReference("failed_login")],
timespan=600,
group_by=["user"],
ordered=False,
condition=None,
)


def test_correlation_to_dict():
rule = SigmaCorrelationRule.from_dict(
{
Expand Down

0 comments on commit 126eb88

Please sign in to comment.