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

Fixes attribute errors in the Custom IOA module #218

Merged
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
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', ...)>"
)