Skip to content

Commit 09c4d90

Browse files
Add synthetic properties check
1 parent 29d4aeb commit 09c4d90

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

detection_rules/index_mappings.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,16 @@ def prune_mappings_of_unsupported_types(
173173
delete_nested_key_from_dict(stream_mappings, field_name)
174174
nested_flattened_fields = find_flattened_fields_with_subfields(stream_mappings)
175175
for field in nested_flattened_fields:
176+
# Remove both .fields and .properties entries for flattened fields
177+
# properties entries can occur when being merged with non-ecs or custom schemas
176178
field_name = str(field).split(".fields.")[0].replace(".", ".properties.") + ".fields"
179+
property_name = str(field).split(".fields.")[0].replace(".", ".properties.") + ".properties"
177180
log(
178181
f"Warning: flattened field `{field}` found in `{integration}-{stream}` with sub fields. "
179182
f"Removing parent field from schema for ES|QL validation."
180183
)
181184
delete_nested_key_from_dict(stream_mappings, field_name)
185+
delete_nested_key_from_dict(stream_mappings, property_name)
182186
return stream_mappings
183187

184188

@@ -246,12 +250,13 @@ def get_index_to_package_lookup(indices: list[str], index_lookup: dict[str, Any]
246250
return index_lookup_indices
247251

248252

249-
def get_filtered_index_schema(
253+
def get_filtered_index_schema( # noqa: PLR0913
250254
indices: list[str],
251255
index_lookup: dict[str, Any],
252256
ecs_schema: dict[str, Any],
253257
non_ecs_mapping: dict[str, Any],
254258
custom_mapping: dict[str, Any],
259+
log: Callable[[str], None],
255260
) -> tuple[dict[str, Any], dict[str, Any]]:
256261
"""Check if the provided indices are known based on the integration format. Returns the combined schema."""
257262

@@ -304,7 +309,7 @@ def get_filtered_index_schema(
304309
# Need to use a merge here to not overwrite existing fields
305310
utils.combine_dicts(base, deepcopy(non_ecs_mapping.get(match, {})))
306311
utils.combine_dicts(base, deepcopy(custom_mapping.get(match, {})))
307-
filtered_index_lookup[match] = base
312+
filtered_index_lookup[match] = prune_mappings_of_unsupported_types("index", match, base, log)
308313
utils.combine_dicts(combined_mappings, deepcopy(base))
309314

310315
# Reduce the index lookup to only the matched indices (remote/Kibana schema validation source of truth)
@@ -413,6 +418,9 @@ def find_flattened_fields_with_subfields(mapping: dict[str, Any], path: str = ""
413418
# Check if the field is of type 'flattened' and has a 'fields' key
414419
if properties.get("type") == "flattened" and "fields" in properties: # type: ignore[reportUnknownVariableType]
415420
flattened_fields_with_subfields.append(current_path) # type: ignore[reportUnknownVariableType]
421+
# Check if the field is of type 'flattened' and has a 'properties' key
422+
if properties.get("type") == "flattened" and "properties" in properties: # type: ignore[reportUnknownVariableType]
423+
flattened_fields_with_subfields.append(current_path) # type: ignore[reportUnknownVariableType]
416424

417425
# Recurse into subfields
418426
if "properties" in properties:
@@ -506,7 +514,7 @@ def prepare_mappings( # noqa: PLR0913
506514

507515
# Filter combined mappings based on the provided indices
508516
combined_mappings, index_lookup = get_filtered_index_schema(
509-
indices, index_lookup, ecs_schema, non_ecs_mapping, custom_mapping
517+
indices, index_lookup, ecs_schema, non_ecs_mapping, custom_mapping, log
510518
)
511519

512520
index_lookup.update({"rule-ecs-index": ecs_schema})

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.5.8"
3+
version = "1.5.9"
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)