From bb78c4407f09cec7fa4e82012c8567b725be7445 Mon Sep 17 00:00:00 2001 From: Atte Niemi <4998544+hur@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:19:28 +0000 Subject: [PATCH] Fixes attribute errors in the Custom IOA module (#218) * Fixes uninitialized variables in the Custom IOA module * Fix linting * Fix double initialization * Fix missing cloud check and update code comment --- caracara/modules/custom_ioa/rules.py | 42 +++++++++++++++++++++++----- tests/unit_tests/test_custom_ioas.py | 26 +++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/caracara/modules/custom_ioa/rules.py b/caracara/modules/custom_ioa/rules.py index 881636a..e552fad 100644 --- a/caracara/modules/custom_ioa/rules.py +++ b/caracara/modules/custom_ioa/rules.py @@ -23,9 +23,8 @@ class IoaRuleGroup: rules_to_delete: List[CustomIoaRule] group: IoaRuleGroup - # The fields below will only be populated if `exists_in_cloud()` returns `True`, with the - # exception of `id` which should be initialised to `None` if this object does not exist on the - # cloud + # The fields below will only be populated if `exists_in_cloud()` returns `True`, + # otherwise they will be `None` id_: str comment: str committed_on: datetime @@ -60,7 +59,19 @@ def __init__(self, name: str, description: str, platform: str): self.rules = [] self.rules_to_delete = [] self.group = None + self.id_ = None + self.comment = None + self.committed_on = None + self.created_by = None + self.created_on = None + self.customer_id = None + self.deleted = None + self.enabled = None + self.modified_by = None + self.modified_on = None + self.rule_ids = None + self.version = None def __repr__(self): """Return an unambiguous string representation of the IOA and its ID, platform and name.""" @@ -301,9 +312,8 @@ class CustomIoaRule: # This field should exist if this object has been added to a group group: IoaRuleGroup - # All these fields should exist if `exists_in_cloud()` returns True, with the exception of - # instance_id which should exist and initialised to `None` if this object does not exist in the - # cloud + # The fields below will only be populated if `exists_in_cloud()` returns `True`, + # otherwise they will be `None` instance_id: str action_label: str comment: str @@ -353,13 +363,29 @@ def __init__( self.severity = severity self.rule_type = rule_type self.group = None - self.instance_id = None self.fields = {} for field_type in rule_type.fields: field = field_type.to_concrete_field() self.fields[(field["name"], field["type"])] = field + self.instance_id = None + self.action_label = None + self.comment = None + self.committed_on = None + self.created_by = None + self.created_on = None + self.customer_id = None + self.deleted = None + self.disposition_id = None + self.enabled = None + self.instance_version = None + self.magic_cookie = None + self.modified_by = None + self.modified_on = None + self.version_ids = None + self.pattern_id = None + def __repr__(self): """Return an unambiguous string representation of the CustomIoaRule and its properties. @@ -583,6 +609,8 @@ def dump(self) -> dict: This object model is defined in the CrowdStrike API Swagger document. """ + if not self.exists_in_cloud(): + raise ValueError("This group does not exist in the cloud!") return { "customer_id": self.customer_id, "instance_id": self.instance_id, diff --git a/tests/unit_tests/test_custom_ioas.py b/tests/unit_tests/test_custom_ioas.py index 33a936b..0312283 100644 --- a/tests/unit_tests/test_custom_ioas.py +++ b/tests/unit_tests/test_custom_ioas.py @@ -849,3 +849,29 @@ def mock_create_rule(body): } ) assert len([rule for rule in new_group.rules if rule.exists_in_cloud()]) == len(group.rules) + + +def test_ioa_rule_group_repr(): + """Tests the `__repr__` function of an IoaRuleGroup.""" + irg = IoaRuleGroup("name", "desc", "platform") + assert ( + str(irg) == "" + ) + + +def test_ioa_rule_repr(simple_rule_type: RuleType): + """Tests the `__repr__` function of an CustomIoaRule.""" + ir = CustomIoaRule("name", "desc", "informational", simple_rule_type) + assert str(ir) == ( + ", ...)>" + ) + + +def test_rule_type_repr(simple_rule_type: RuleType): + """Tests the `__repr__` function of an RuleType.""" + assert ( + str(simple_rule_type) + == "" + )