Skip to content

Commit b09fc94

Browse files
authored
Merge branch 'main' into cleanup_mitre_actors_and_techniques
2 parents 07a92c1 + 030ce2f commit b09fc94

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

contentctl/objects/abstract_security_content_objects/detection_abstract.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,21 +383,17 @@ def providing_technologies(self) -> List[ProvidingTechnology]:
383383
@computed_field
384384
@property
385385
def risk(self) -> list[dict[str, Any]]:
386-
risk_objects: list[dict[str, str | int]] = []
387-
388-
for entity in self.rba.risk_objects:
389-
risk_object: dict[str, str | int] = dict()
390-
risk_object["risk_object_type"] = entity.type
391-
risk_object["risk_object_field"] = entity.field
392-
risk_object["risk_score"] = entity.score
393-
risk_objects.append(risk_object)
394-
395-
for entity in self.rba.threat_objects:
396-
threat_object: dict[str, str] = dict()
397-
threat_object["threat_object_field"] = entity.field
398-
threat_object["threat_object_type"] = entity.type
399-
risk_objects.append(threat_object)
400-
return risk_objects
386+
if self.rba is None:
387+
raise Exception(
388+
f"Attempting to serialize rba section of [{self.name}], however RBA section is None"
389+
)
390+
"""
391+
action.risk.param._risk
392+
of the conf file only contains a list of dicts. We do not eant to
393+
include the message here, so we do not return it.
394+
"""
395+
rba_dict = self.rba.model_dump()
396+
return rba_dict["risk_objects"] + rba_dict["threat_objects"]
401397

402398
@computed_field
403399
@property

contentctl/objects/rba.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from enum import Enum
2-
from pydantic import BaseModel, computed_field, Field
1+
from __future__ import annotations
2+
33
from abc import ABC
4-
from typing import Set, Annotated
5-
from contentctl.objects.enums import RiskSeverity
4+
from enum import Enum
5+
from typing import Annotated, Set
66

7+
from pydantic import BaseModel, Field, computed_field, model_serializer
8+
9+
from contentctl.objects.enums import RiskSeverity
710

811
RiskScoreValue_Type = Annotated[int, Field(ge=1, le=100)]
912

@@ -51,6 +54,28 @@ class RiskObject(BaseModel):
5154
def __hash__(self):
5255
return hash((self.field, self.type, self.score))
5356

57+
def __lt__(self, other: RiskObject) -> bool:
58+
if (
59+
f"{self.field}{self.type}{self.score}"
60+
< f"{other.field}{other.type}{other.score}"
61+
):
62+
return True
63+
return False
64+
65+
@model_serializer
66+
def serialize_risk_object(self) -> dict[str, str | int]:
67+
"""
68+
We define this explicitly for two reasons, even though the automatic
69+
serialization works correctly. First we want to enforce a specific
70+
field order for reasons of readability. Second, some of the fields
71+
actually have different names than they do in the object.
72+
"""
73+
return {
74+
"risk_object_field": self.field,
75+
"risk_object_type": self.type,
76+
"risk_score": self.score,
77+
}
78+
5479

5580
class ThreatObject(BaseModel):
5681
field: str
@@ -59,6 +84,24 @@ class ThreatObject(BaseModel):
5984
def __hash__(self):
6085
return hash((self.field, self.type))
6186

87+
def __lt__(self, other: ThreatObject) -> bool:
88+
if f"{self.field}{self.type}" < f"{other.field}{other.type}":
89+
return True
90+
return False
91+
92+
@model_serializer
93+
def serialize_threat_object(self) -> dict[str, str]:
94+
"""
95+
We define this explicitly for two reasons, even though the automatic
96+
serialization works correctly. First we want to enforce a specific
97+
field order for reasons of readability. Second, some of the fields
98+
actually have different names than they do in the object.
99+
"""
100+
return {
101+
"threat_object_field": self.field,
102+
"threat_object_type": self.type,
103+
}
104+
62105

63106
class RBAObject(BaseModel, ABC):
64107
message: str
@@ -94,3 +137,11 @@ def severity(self) -> RiskSeverity:
94137
raise Exception(
95138
f"Error getting severity - risk_score must be between 0-100, but was actually {self.risk_score}"
96139
)
140+
141+
@model_serializer
142+
def serialize_rba(self) -> dict[str, str | list[dict[str, str | int]]]:
143+
return {
144+
"message": self.message,
145+
"risk_objects": [obj.model_dump() for obj in sorted(self.risk_objects)],
146+
"threat_objects": [obj.model_dump() for obj in sorted(self.threat_objects)],
147+
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "contentctl"
3-
version = "5.0.0"
3+
version = "5.0.1"
44

55
description = "Splunk Content Control Tool"
66
authors = ["STRT <[email protected]>"]

0 commit comments

Comments
 (0)