Skip to content

Commit

Permalink
Fixes attribute errors in the Custom IOA module (#218)
Browse files Browse the repository at this point in the history
* Fixes uninitialized variables in the Custom IOA module

* Fix linting

* Fix double initialization

* Fix missing cloud check and update code comment
  • Loading branch information
hur authored Oct 28, 2024
1 parent fa5d3c1 commit bb78c44
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
42 changes: 35 additions & 7 deletions caracara/modules/custom_ioa/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions tests/unit_tests/test_custom_ioas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "<IoaRuleGroup(id_=None, version=None, platform='platform', name='name', ...)>"
)


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) == (
"<CustomIoaRule(group=None, instance_id=None, instance_version=None name='name', "
"ruletype=<RuleType(id_='test_rule_type_simple', "
"name='SimpleType', platform='windows', ...)>, ...)>"
)


def test_rule_type_repr(simple_rule_type: RuleType):
"""Tests the `__repr__` function of an RuleType."""
assert (
str(simple_rule_type)
== "<RuleType(id_='test_rule_type_simple', name='SimpleType', platform='windows', ...)>"
)

0 comments on commit bb78c44

Please sign in to comment.