|
8 | 8 | import sys |
9 | 9 | from pathlib import Path |
10 | 10 | from typing import Iterable, List, Optional |
| 11 | +from datetime import datetime |
11 | 12 |
|
12 | 13 | import click |
13 | 14 |
|
@@ -237,10 +238,30 @@ def kibana_export_rules(ctx: click.Context, directory: Path, action_connectors_d |
237 | 238 | rule_resource["author"] = rule_resource.get("author") or default_author or [rule_resource.get("created_by")] |
238 | 239 | if isinstance(rule_resource["author"], str): |
239 | 240 | rule_resource["author"] = [rule_resource["author"]] |
240 | | - contents = TOMLRuleContents.from_rule_resource(rule_resource, maturity="production") |
241 | | - threat = contents.data.get("threat") |
242 | | - first_tactic = threat[0].tactic.name if threat else "" |
243 | | - rule_name = rulename_to_filename(contents.data.name, tactic_name=first_tactic) |
| 241 | + # NOTE we may want to remove the date logic, should the date match kibana or match rules repo? |
| 242 | + # Inherit maturity from the rule already exists |
| 243 | + maturity = "development" |
| 244 | + updated_date = None |
| 245 | + created_date = None |
| 246 | + threat = rule_resource.get("threat") |
| 247 | + first_tactic = threat[0].get("tactic").get("name") if threat else "" |
| 248 | + rule_name = rulename_to_filename(rule_resource.get("name"), tactic_name=first_tactic) |
| 249 | + # check if directory / f"{rule_name}" exists |
| 250 | + if (directory / f"{rule_name}").exists(): |
| 251 | + rules = RuleCollection() |
| 252 | + rules.load_file(directory / f"{rule_name}") |
| 253 | + if rules: |
| 254 | + maturity = rules.rules[0].contents.metadata.maturity |
| 255 | + updated_date = datetime.strptime(rule_resource.get("updated_at"), "%Y-%m-%dT%H:%M:%S.%fZ").strftime( |
| 256 | + "%Y/%m/%d" |
| 257 | + ) |
| 258 | + created_date = datetime.strptime(rule_resource.get("created_at"), "%Y-%m-%dT%H:%M:%S.%fZ").strftime( |
| 259 | + "%Y/%m/%d" |
| 260 | + ) |
| 261 | + |
| 262 | + contents = TOMLRuleContents.from_rule_resource( |
| 263 | + rule_resource, creation_date=created_date, updated_date=updated_date, maturity=maturity |
| 264 | + ) |
244 | 265 | rule = TOMLRule(contents=contents, path=directory / f"{rule_name}") |
245 | 266 | except Exception as e: |
246 | 267 | if skip_errors: |
|
0 commit comments