Skip to content

Commit

Permalink
Added convenience iteration methods to SigmaCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspatzke committed Jan 12, 2024
1 parent 37a563e commit 066d4bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sigma/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from uuid import UUID
from sigma.correlations import SigmaCorrelationRule

from sigma.rule import SigmaRule
from sigma.rule import SigmaRule, SigmaRuleBase
from sigma.exceptions import (
SigmaCollectionError,
SigmaError,
Expand Down Expand Up @@ -208,6 +208,14 @@ def merge(cls, collections: Iterable["SigmaCollection"]) -> "SigmaCollection":
errors=[error for collection in collections for error in collection.errors],
)

def get_output_rules(self) -> Iterable[SigmaRuleBase]:
"""Returns an iterator across all rules where the output property is set to true"""
return (rule for rule in self.rules if rule._output)

def get_unrefereced_rules(self) -> Iterable[SigmaRuleBase]:
"""Returns an iterator across all rules that are not referenced by any other rule"""
return (rule for rule in self.rules if not rule._backreferences)

def __iter__(self):
return iter(self.rules)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ def test_load_ruleset_with_correlation(rules_with_correlation):
assert correlation_rule.rules[0].rule == rules_with_correlation.rules[0]


def test_get_output_rules(rules_with_correlation):
output_rules = list(rules_with_correlation.get_output_rules())
assert len(output_rules) == 1
assert isinstance(output_rules[0], SigmaCorrelationRule)


def test_get_unreferenced_rules(rules_with_correlation):
output_rules = list(rules_with_correlation.get_unrefereced_rules())
assert len(output_rules) == 1
assert isinstance(output_rules[0], SigmaCorrelationRule)


def test_load_ruleset_with_correlation_referencing_nonexistent_rule():
with pytest.raises(SigmaRuleNotFoundError, match="Rule 'rule-2' not found in rule collection"):
SigmaCollection.from_yaml(
Expand Down

0 comments on commit 066d4bb

Please sign in to comment.