Skip to content

Commit cdb5f4c

Browse files
committed
fixes
1 parent 2867e77 commit cdb5f4c

File tree

2 files changed

+16
-45
lines changed

2 files changed

+16
-45
lines changed

uncoder-core/app/translator/platforms/microsoft/parsers/microsoft_sentinel_rule.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MicrosoftSentinelRuleParser(MicrosoftSentinelQueryParser, JsonRuleMixin):
4747
mappings: MicrosoftSentinelMappings = microsoft_sentinel_rule_mappings
4848

4949
@staticmethod
50-
def __parse_timeframe(raw_timeframe: Optional[str]) -> Optional[timedelta]:
50+
def _parse_timeframe(raw_timeframe: Optional[str]) -> Optional[timedelta]:
5151
with suppress(ISO8601Error):
5252
return isodate.parse_duration(raw_timeframe)
5353

@@ -73,7 +73,7 @@ def parse_raw_query(self, text: str, language: str) -> RawQueryContainer:
7373
id_=parsed_description.get("rule_id"),
7474
title=rule.get("displayName"),
7575
description=parsed_description.get("description") or rule.get("description"),
76-
timeframe=self.__parse_timeframe(rule.get("queryFrequency", "")),
76+
timeframe=self._parse_timeframe(rule.get("queryFrequency", "")),
7777
severity=rule.get("severity", "medium"),
7878
mitre_attack=mitre_attack,
7979
author=parsed_description.get("author") or [rule.get("author")],
@@ -85,15 +85,10 @@ def parse_raw_query(self, text: str, language: str) -> RawQueryContainer:
8585

8686

8787
@parser_manager.register
88-
class MicrosoftSentinelYAMLRuleParser(MicrosoftSentinelQueryParser, YamlRuleMixin):
88+
class MicrosoftSentinelYAMLRuleParser(YamlRuleMixin, MicrosoftSentinelRuleParser):
8989
details: PlatformDetails = microsoft_sentinel_yaml_rule_details
9090
mappings: MicrosoftSentinelMappings = microsoft_sentinel_rule_mappings
9191

92-
@staticmethod
93-
def __parse_timeframe(raw_timeframe: Optional[str]) -> Optional[timedelta]:
94-
with suppress(ISO8601Error):
95-
return isodate.parse_duration(raw_timeframe)
96-
9792
def extract_tags(self, data: Union[dict, list, str]) -> list[str]:
9893
tags = []
9994
if isinstance(data, dict):
@@ -138,8 +133,8 @@ def parse_raw_query(self, text: str, language: str) -> RawQueryContainer:
138133
if isinstance(tag, str):
139134
tags.append(tag)
140135

141-
timeframe = self.__parse_timeframe(rule.get("queryFrequency", ""))
142-
query_period = self.__parse_timeframe(rule.get("queryPeriod", ""))
136+
timeframe = self._parse_timeframe(rule.get("queryFrequency", ""))
137+
query_period = self._parse_timeframe(rule.get("queryPeriod", ""))
143138

144139
return RawQueryContainer(
145140
query=rule["query"],
@@ -155,10 +150,10 @@ def parse_raw_query(self, text: str, language: str) -> RawQueryContainer:
155150
author=rule.get("metadata", {}).get("author", {}).get("name", "").split(","),
156151
tags=sorted(set(tags)),
157152
raw_metainfo_container=RawMetaInfoContainer(
158-
trigger_operator=rule.get("triggerOperator", ""),
159-
trigger_threshold=rule.get("triggerThreshold", ""),
160-
query_frequency=rule.get("queryFrequency", "") if not timeframe else None,
161-
query_period=rule.get("queryPeriod", "") if not query_period else None,
153+
trigger_operator=rule.get("triggerOperator"),
154+
trigger_threshold=rule.get("triggerThreshold"),
155+
query_frequency=rule.get("queryFrequency"),
156+
query_period=rule.get("queryPeriod"),
162157
),
163158
),
164159
)

uncoder-core/app/translator/platforms/microsoft/renders/microsoft_sentinel_rule.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
import copy
2121
import json
22-
from datetime import timedelta
2322
from typing import Optional
2423

24+
import isodate
25+
2526
from app.translator.core.custom_types.meta_info import SeverityType
2627
from app.translator.core.mapping import SourceMapping
2728
from app.translator.core.models.platform_details import PlatformDetails
@@ -71,41 +72,16 @@ def __create_mitre_threat(self, mitre_attack: MitreInfoContainer) -> tuple[list,
7172
return sorted(tactics), sorted(techniques)
7273

7374
@staticmethod
74-
def timedelta_to_iso8601(timedelta_: timedelta) -> str:
75-
days = timedelta_.days
76-
seconds = timedelta_.seconds
77-
microseconds = timedelta_.microseconds
78-
79-
hours, remainder = divmod(seconds, 3600)
80-
minutes, seconds = divmod(remainder, 60)
81-
82-
duration = "P"
83-
if days:
84-
duration += f"{days}D"
85-
86-
if hours or minutes or seconds or microseconds:
87-
duration += "T"
88-
if hours:
89-
duration += f"{hours}H"
90-
if minutes:
91-
duration += f"{minutes}M"
92-
if seconds or microseconds:
93-
# Handle the fractional part for seconds
94-
if microseconds:
95-
seconds += microseconds / 1_000_000
96-
duration += f"{seconds:.6f}S" if microseconds else f"{seconds}S"
97-
98-
return duration
99-
100-
def get_query_frequency(self, meta_info: MetaInfoContainer) -> Optional[str]:
75+
def get_query_frequency(meta_info: MetaInfoContainer) -> Optional[str]:
10176
if meta_info.timeframe:
102-
return self.timedelta_to_iso8601(meta_info.timeframe)
77+
return isodate.duration_isoformat(meta_info.timeframe)
10378
if meta_info.raw_metainfo_container:
10479
return meta_info.raw_metainfo_container.query_frequency
10580

106-
def get_query_period(self, meta_info: MetaInfoContainer) -> Optional[str]:
81+
@staticmethod
82+
def get_query_period(meta_info: MetaInfoContainer) -> Optional[str]:
10783
if meta_info.query_period:
108-
return self.timedelta_to_iso8601(meta_info.query_period)
84+
return isodate.duration_isoformat(meta_info.query_period)
10985
if meta_info.raw_metainfo_container:
11086
return meta_info.raw_metainfo_container.query_period
11187

0 commit comments

Comments
 (0)