From d109b0bd5cdad4c780a1e85a00ad9ee3d65e135d Mon Sep 17 00:00:00 2001 From: Ariel Date: Thu, 13 Mar 2025 14:38:14 -0600 Subject: [PATCH 1/2] Class name is rule id --- pypanther/base.py | 4 ++++ pypanther/generate.py | 2 +- tests/test_base.py | 13 +++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pypanther/base.py b/pypanther/base.py index 4086363b..559dbd66 100644 --- a/pypanther/base.py +++ b/pypanther/base.py @@ -242,6 +242,10 @@ def __init_subclass__(cls, **kwargs): child.tags.append("Foo") parent.tags.append("Foo") # not inherited by children of parent """ + # Set the id to the class name if it's not explicitly set + if "id" not in cls.__dict__ or not cls.__dict__["id"]: + cls.id = cls.__name__ + for attr in RULE_ALL_ATTRS: if attr not in cls.__dict__: try: diff --git a/pypanther/generate.py b/pypanther/generate.py index e7913971..3e562b2d 100644 --- a/pypanther/generate.py +++ b/pypanther/generate.py @@ -122,7 +122,7 @@ def convert_rule(filepath: Path, helpers: Set[str]) -> Optional[str]: ) value = ast.List(elts=log_type_elts) if k == "RuleID": - value = ast.Constant(value=v + ID_POSTFIX) + continue assignments.append( ast.Assign( diff --git a/tests/test_base.py b/tests/test_base.py index 8cb0d1cb..243edd3c 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -353,20 +353,17 @@ def rule(self, event): class TestValidation: - def test_rule_missing_id(self): - class rule(Rule): + def test_rule_id_is_class_name(self): + """Test that a rule's id is automatically set to its class name.""" + class TestRule(Rule): default_severity = Severity.INFO log_types = ["test"] def rule(self, event): return False - with pytest.raises(ValidationError) as e: - rule.validate() - errors = e.value.errors() - assert len(errors) == 1 - assert errors[0]["loc"] == ("id",) - assert errors[0]["msg"] == "Field required" + # Verify that id is set to the class name + assert TestRule.id == "TestRule" def test_create_rule_missing_method(self) -> None: class rule(Rule): From f5d9d1c67105bf525ee6b32ea0af7de7367da232 Mon Sep 17 00:00:00 2001 From: Ariel Date: Thu, 13 Mar 2025 14:42:24 -0600 Subject: [PATCH 2/2] lint --- tests/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_base.py b/tests/test_base.py index 243edd3c..3c634bc8 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -13,7 +13,6 @@ TRUNCATED_STRING_SUFFIX, TYPE_RULE, ) -from pydantic import ValidationError from pypanther.base import RULE_ALL_ATTRS, Rule, RuleModel, panther_managed from pypanther.cache import data_model_cache @@ -355,6 +354,7 @@ def rule(self, event): class TestValidation: def test_rule_id_is_class_name(self): """Test that a rule's id is automatically set to its class name.""" + class TestRule(Rule): default_severity = Severity.INFO log_types = ["test"]