Skip to content

Commit cdb346c

Browse files
authored
fix: Skip invalid YAML files in Beats dist (#4865)
* Skip invalid YAML files but keep them in the branch * Typo fix * Patch version bump * Adding a schema generation command to `test_cli.bash` flow
1 parent 1fb60d6 commit cdb346c

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

detection_rules/beats.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
def _decompress_and_save_schema(url: str, release_name: str) -> None:
24-
print(f"Downloading beats {release_name}")
24+
print(f"Downloading beats {release_name}", url)
2525
response = requests.get(url, timeout=30)
2626

2727
print(f"Downloaded {len(response.content) / 1024.0 / 1024.0:.2f} MB release.")
@@ -34,26 +34,25 @@ def _decompress_and_save_schema(url: str, release_name: str) -> None:
3434
for name in archive.namelist():
3535
path = Path(name)
3636
if path.name in ("fields.yml", "fields.common.yml", "config.yml"):
37-
contents = archive.read(name)
38-
3937
# chop off the base directory name
4038
key = name[len(base_directory) :]
4139

4240
if key.startswith("x-pack"):
4341
key = key[len("x-pack") + 1 :]
4442

45-
try:
46-
decoded = yaml.safe_load(contents)
47-
except yaml.YAMLError as e:
48-
print(f"Error loading {name}")
49-
raise ValueError(f"Error loading {name}") from e
50-
5143
# create a hierarchical structure
5244
branch = fs
5345
directory, base_name = os.path.split(key)
5446
for limb in directory.split(os.path.sep):
5547
branch = branch.setdefault("folders", {}).setdefault(limb, {})
5648

49+
contents = archive.read(name)
50+
try:
51+
decoded = yaml.safe_load(contents)
52+
except yaml.YAMLError:
53+
print(f"Error loading {name}, not a valid YAML")
54+
decoded = None
55+
5756
branch.setdefault("files", {})[base_name] = decoded
5857

5958
# remove all non-beat directories

detection_rules/etc/test_cli.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ rm -rf tmp-export
2525
echo "Updating rule data schemas"
2626
python -m detection_rules dev schemas update-rule-data
2727

28+
echo "Generate Beats schemas"
29+
GITHUB_TOKEN="foo" python -m detection_rules dev schemas generate --schema beats
30+
2831
echo "Validating rule: execution_github_new_event_action_for_pat.toml"
2932
python -m detection_rules validate-rule rules_building_block/execution_github_new_event_action_for_pat.toml
3033

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.3.0"
3+
version = "1.3.1"
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)