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

Fix typos and type hints in SigmaCollection #187

Merged
merged 3 commits into from
Jan 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
23 changes: 14 additions & 9 deletions sigma/collection.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
from dataclasses import dataclass, field
from typing import Callable, Dict, Iterable, List, Optional, Union, IO
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Union, IO
from uuid import UUID
from sigma.correlations import SigmaCorrelationRule

from sigma.rule import SigmaRule, SigmaRuleBase
import yaml

from sigma.correlations import SigmaCorrelationRule
from sigma.exceptions import (
SigmaCollectionError,
SigmaError,
SigmaRuleLocation,
SigmaRuleNotFoundError,
)
import yaml
from sigma.rule import SigmaRule, SigmaRuleBase


@dataclass
class SigmaCollection:
"""Collection of Sigma rules"""

rules: List[SigmaRule]
rules: List[SigmaRuleBase]
errors: List[SigmaError] = field(default_factory=list)
ids_to_rules: Dict[UUID, SigmaRule] = field(init=False, repr=False, hash=False, compare=False)
names_to_rules: Dict[str, SigmaRule] = field(init=False, repr=False, hash=False, compare=False)
ids_to_rules: Dict[UUID, SigmaRuleBase] = field(
init=False, repr=False, hash=False, compare=False
)
names_to_rules: Dict[str, SigmaRuleBase] = field(
init=False, repr=False, hash=False, compare=False
)

def __post_init__(self):
"""
Expand Down Expand Up @@ -160,7 +165,7 @@ def load_ruleset(

:param inputs: List of strings and :class:`pathlib.Path` objects that reference files or
directories that should be loaded.
:param collect_errors: parse or verification errors are collected in :class:`SigmaRule`
:param collect_errors: parse or verification errors are collected in :class:`SigmaRuleBase`
objects instead of raising them immediately. Defaults to ``False``.
:param on_beforeload: Optional function that is called for each path to a Sigma rule before the parsing and
construction of the :class:`SigmaCollection` object is done. The path returned by this function is
Expand Down Expand Up @@ -212,7 +217,7 @@ 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]:
def get_unreferenced_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)

Expand Down
7 changes: 4 additions & 3 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path
from uuid import UUID

import pytest

from sigma.collection import SigmaCollection, deep_dict_update
from sigma.correlations import (
SigmaCorrelationCondition,
Expand All @@ -10,15 +12,14 @@
SigmaCorrelationType,
SigmaRuleReference,
)
from sigma.rule import SigmaRule, SigmaLogSource
from sigma.types import SigmaString
from sigma.exceptions import (
SigmaCollectionError,
SigmaModifierError,
SigmaRuleLocation,
SigmaError,
SigmaRuleNotFoundError,
)
from sigma.rule import SigmaRule, SigmaLogSource


def test_single_rule():
Expand Down Expand Up @@ -384,7 +385,7 @@ def test_get_output_rules(rules_with_correlation):


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

Expand Down