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

Add operator to SigmaStatus and SigmaLevel #194

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install Poetry
run: pipx install poetry
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: poetry
Expand Down
66 changes: 63 additions & 3 deletions sigma/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,41 @@ def __str__(self) -> str:


class SigmaStatus(EnumLowercaseStringMixin, Enum):
STABLE = auto()
UNSUPPORTED = auto()
DEPRECATED = auto()
EXPERIMENTAL = auto()
TEST = auto()
DEPRECATED = auto()
UNSUPPORTED = auto()
STABLE = auto()

def __eq__(self, other):
if isinstance(other, SigmaStatus):
return self.value == other.value
return False

def __ge__(self, other):
if isinstance(other, SigmaStatus):
return self.value >= other.value
return False

def __gt__(self, other):
if isinstance(other, SigmaStatus):
return self.value > other.value
return False

def __ne__(self, other):
if isinstance(other, SigmaStatus):
return self.value != other.value
return False

def __le__(self, other):
if isinstance(other, SigmaStatus):
return self.value <= other.value
return False

def __lt__(self, other):
if isinstance(other, SigmaStatus):
return self.value < other.value
return False


class SigmaLevel(EnumLowercaseStringMixin, Enum):
Expand All @@ -51,6 +81,36 @@ class SigmaLevel(EnumLowercaseStringMixin, Enum):
HIGH = auto()
CRITICAL = auto()

def __eq__(self, other):
if isinstance(other, SigmaLevel):
return self.value == other.value
return False

def __ge__(self, other):
if isinstance(other, SigmaLevel):
return self.value >= other.value
return False

def __gt__(self, other):
if isinstance(other, SigmaLevel):
return self.value > other.value
return False

def __ne__(self, other):
if isinstance(other, SigmaLevel):
return self.value != other.value
return False

def __le__(self, other):
if isinstance(other, SigmaLevel):
return self.value <= other.value
return False

def __lt__(self, other):
if isinstance(other, SigmaLevel):
return self.value < other.value
return False


class SigmaRelatedType(EnumLowercaseStringMixin, Enum):
OBSOLETES = auto()
Expand Down
36 changes: 36 additions & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,46 @@ def test_sigmalevel_str():
assert str(SigmaLevel.MEDIUM) == "medium"


def test_sigmalevel_comparation():
assert SigmaLevel.HIGH == SigmaLevel.HIGH
assert SigmaLevel.HIGH >= SigmaLevel.LOW
assert SigmaLevel.HIGH > SigmaLevel.LOW
assert SigmaLevel.HIGH != SigmaLevel.LOW
assert SigmaLevel.LOW <= SigmaLevel.HIGH
assert SigmaLevel.LOW < SigmaLevel.HIGH


def test_sigmalevel_comparation_invalid():
assert not (SigmaLevel.HIGH == "HIGH")
assert not (SigmaLevel.HIGH >= "LOW")
assert not (SigmaLevel.HIGH > "LOW")
assert not (SigmaLevel.HIGH != "LOW")
assert not (SigmaLevel.LOW <= "HIGH")
assert not (SigmaLevel.LOW < "HIGH")


def test_sigmastatus_str():
assert str(SigmaStatus.STABLE) == "stable"


def test_sigmastatus_comparation():
assert SigmaStatus.STABLE == SigmaStatus.STABLE
assert SigmaStatus.STABLE >= SigmaStatus.EXPERIMENTAL
assert SigmaStatus.STABLE > SigmaStatus.EXPERIMENTAL
assert SigmaStatus.STABLE != SigmaStatus.EXPERIMENTAL
assert SigmaStatus.EXPERIMENTAL <= SigmaStatus.STABLE
assert SigmaStatus.EXPERIMENTAL < SigmaStatus.STABLE


def test_sigmastatus_comparation_invalid():
assert not (SigmaStatus.STABLE == "STABLE")
assert not (SigmaStatus.STABLE >= "EXPERIMENTAL")
assert not (SigmaStatus.STABLE > "EXPERIMENTAL")
assert not (SigmaStatus.STABLE != "EXPERIMENTAL")
assert not (SigmaStatus.EXPERIMENTAL <= "STABLE")
assert not (SigmaStatus.EXPERIMENTAL < "STABLE")


### SigmaRuleTag tests ###
def test_sigmaruletag_fromstr():
assert SigmaRuleTag.from_str("namespace.name") == SigmaRuleTag("namespace", "name")
Expand Down