Skip to content

Commit 2738fb4

Browse files
brokensound77github-actions[bot]
authored andcommitted
Add test that newly introduced build-time fields for a min_stack for … (#2262)
* add test that newly introduced build-time fields for a min_stack for applicable rules. * account for rules without min_stack_version * limit test to >= stack ver (cherry picked from commit d37eac8)
1 parent 89a3915 commit 2738fb4

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

detection_rules/rule.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
MIN_FLEET_PACKAGE_VERSION = '7.13.0'
3939

4040
BUILD_FIELD_VERSIONS = {
41+
"related_integrations": (Version('8.3'), None),
4142
"required_fields": (Version('8.3'), None),
4243
"setup": (Version("8.3"), None)
4344
}
@@ -250,6 +251,17 @@ def parsed_note(self) -> Optional[MarkoDocument]:
250251
def is_elastic_rule(self):
251252
return 'elastic' in [a.lower() for a in self.author]
252253

254+
def get_build_fields(self) -> {}:
255+
"""Get a list of build-time fields along with the stack versions which they will build within."""
256+
build_fields = {}
257+
rule_fields = {f.name: f for f in dataclasses.fields(self)}
258+
259+
for fld in BUILD_FIELD_VERSIONS:
260+
if fld in rule_fields:
261+
build_fields[fld] = BUILD_FIELD_VERSIONS[fld]
262+
263+
return build_fields
264+
253265

254266
class DataValidator:
255267
"""Additional validation beyond base marshmallow schema validation."""

tests/test_all_rules.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from detection_rules import attack
1616
from detection_rules.beats import parse_beats_from_index
17+
from detection_rules.packaging import current_stack_version
1718
from detection_rules.rule import QueryRuleData
1819
from detection_rules.rule_loader import FILE_PATTERN
1920
from detection_rules.schemas import definitions
@@ -382,8 +383,6 @@ def test_updated_date_newer_than_creation(self):
382383

383384
def test_deprecated_rules(self):
384385
"""Test that deprecated rules are properly handled."""
385-
from detection_rules.packaging import current_stack_version
386-
387386
versions = default_version_lock.version_lock
388387
deprecations = load_etc_dump('deprecated_rules.json')
389388
deprecated_rules = {}
@@ -698,6 +697,34 @@ def test_rule_backports_for_restricted_fields(self):
698697
self.fail(err_msg)
699698

700699

700+
class TestBuildTimeFields(BaseRuleTest):
701+
"""Test validity of build-time fields."""
702+
703+
def test_build_fields_min_stack(self):
704+
"""Test that newly introduced build-time fields for a min_stack for applicable rules."""
705+
current_stack_ver = Version(current_stack_version())
706+
invalids = []
707+
708+
for rule in self.production_rules:
709+
min_stack = rule.contents.metadata.min_stack_version
710+
build_fields = rule.contents.data.get_build_fields()
711+
712+
errors = []
713+
for build_field, field_versions in build_fields.items():
714+
start_ver, end_ver = field_versions
715+
if start_ver is not None and current_stack_ver >= start_ver:
716+
if min_stack is None or not Version(min_stack) >= start_ver:
717+
errors.append(f'{build_field} >= {start_ver}')
718+
719+
if errors:
720+
err_str = ', '.join(errors)
721+
invalids.append(f'{self.rule_str(rule)} uses a rule type with build fields requiring min_stack_versions'
722+
f' to be set: {err_str}')
723+
724+
if invalids:
725+
self.fail(invalids)
726+
727+
701728
class TestRiskScoreMismatch(BaseRuleTest):
702729
"""Test that severity and risk_score fields contain corresponding values"""
703730

0 commit comments

Comments
 (0)