Skip to content

Commit ade05d1

Browse files
Add date and maturity logic
1 parent 7dd5665 commit ade05d1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

detection_rules/cli_utils.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ def get_collection(*args, **kwargs):
9595
if len(rules) == 0:
9696
client_error("No rules found")
9797

98-
# TODO add check here for rule directory path
99-
# Either fix or warn if the path is not correct
98+
# Warn that if the path does not match the expected path, it will be saved to the expected path
10099
for rule in rules:
101100
threat = rule.contents.data.get("threat")
102101
first_tactic = threat[0].tactic.name if threat else ""
103102
rule_name = rulename_to_filename(rule.contents.data.name, tactic_name=first_tactic)
104103
if rule.path.name != rule_name:
105-
click.secho(f"WARNING: Rule path does not match expected path: {rule.path.name} != {rule_name}", fg="yellow")
104+
click.secho(
105+
f"WARNING: Rule path does not match required path: {rule.path.name} != {rule_name}", fg="yellow"
106+
)
106107

107108
kwargs["rules"] = rules
108109
return f(*args, **kwargs)
@@ -209,7 +210,24 @@ def rule_prompt(path=None, rule_type=None, required_only=True, save=True, verbos
209210
# DEFAULT_PREBUILT_RULES_DIRS[0] is a required directory just as a suggestion
210211
suggested_path = Path(DEFAULT_PREBUILT_RULES_DIRS[0]) / contents['name']
211212
path = Path(path or input(f'File path for rule [{suggested_path}]: ') or suggested_path).resolve()
212-
meta = {'creation_date': creation_date, 'updated_date': creation_date, 'maturity': 'development'}
213+
# NOTE we may want to remove the date logic, should the date match Kibana or match rules repo?
214+
# Inherit maturity and dates from the rule already exists
215+
maturity = "development"
216+
updated_date = None
217+
created_date = None
218+
if path.exists():
219+
rules = RuleCollection()
220+
rules.load_file(path)
221+
if rules:
222+
maturity = rules.rules[0].contents.metadata.maturity
223+
updated_date = rules.rules[0].contents.metadata.updated_at
224+
created_date = rules.rules[0].contents.metadata.created_at
225+
226+
meta = {
227+
"creation_date": created_date or creation_date,
228+
"updated_date": updated_date or creation_date,
229+
"maturity": maturity,
230+
}
213231

214232
try:
215233
rule = TOMLRule(path=Path(path), contents=TOMLRuleContents.from_dict({'rule': contents, 'metadata': meta}))

detection_rules/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
ROOT_DIR = Path(__file__).parent.parent
2121
CUSTOM_RULES_DIR = os.getenv('CUSTOM_RULES_DIR', None)
22+
CUSTOM_RULES_DIR = "/home/forteea1/Code/dac_demo/detection-rules/demo"
2223

2324

2425
@dataclass

0 commit comments

Comments
 (0)