Skip to content

Commit 62feac3

Browse files
[Bug] Update Schema Prompt to include new_terms_fields (#4567)
* Update Schema Prompt to include new_terms_fields * Version Bump * Ensure list of strings * Update utils to support comma deliminated strings * Also remove excess quotes * Bump patch version * Remove Union * bump version
1 parent 6cb238b commit 62feac3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

detection_rules/cli_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
DEFAULT_PREBUILT_RULES_DIRS, RuleCollection,
2323
dict_filter)
2424
from .schemas import definitions
25-
from .utils import clear_caches, rulename_to_filename
25+
from .utils import clear_caches, ensure_list_of_strings, rulename_to_filename
2626
from .config import parse_rules_config
2727

2828
RULES_CONFIG = parse_rules_config()
@@ -195,7 +195,8 @@ def rule_prompt(path=None, rule_type=None, required_only=True, save=True, verbos
195195
if name == "new_terms":
196196
# patch to allow new_term imports
197197
result = {"field": "new_terms_fields"}
198-
result["value"] = schema_prompt("new_terms_fields", value=kwargs.pop("new_terms_fields"))
198+
new_terms_fields_value = schema_prompt("new_terms_fields", value=kwargs.pop("new_terms_fields", None))
199+
result["value"] = ensure_list_of_strings(new_terms_fields_value)
199200
history_window_start_value = kwargs.pop("history_window_start", None)
200201
result["history_window_start"] = [
201202
{

detection_rules/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ def dict_hash(obj: dict) -> str:
7474
return hashlib.sha256(raw_bytes).hexdigest()
7575

7676

77+
def ensure_list_of_strings(value: str | list) -> list[str]:
78+
"""Ensure or convert a value is a list of strings."""
79+
if isinstance(value, str):
80+
# Check if the string looks like a JSON list
81+
if value.startswith('[') and value.endswith(']'):
82+
try:
83+
# Attempt to parse the string as a JSON list
84+
parsed_value = json.loads(value)
85+
if isinstance(parsed_value, list):
86+
return [str(v) for v in parsed_value]
87+
except json.JSONDecodeError:
88+
pass
89+
# If it's not a JSON list, split by commas if present
90+
# Else return a list with the original string
91+
return list(map(lambda x: x.strip().strip('"'), value.split(',')))
92+
elif isinstance(value, list):
93+
return [str(v) for v in value]
94+
else:
95+
return []
96+
97+
7798
def get_json_iter(f):
7899
"""Get an iterator over a JSON file."""
79100
first = f.read(2)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "detection_rules"
3-
version = "1.0.12"
3+
version = "1.0.13"
44
description = "Detection Rules is the home for rules used by Elastic Security. This repository is used for the development, maintenance, testing, validation, and release of rules for Elastic Security’s Detection Engine."
55
readme = "README.md"
66
requires-python = ">=3.12"

0 commit comments

Comments
 (0)