|
14 | 14 |
|
15 | 15 | from detection_rules import attack |
16 | 16 | from detection_rules.beats import parse_beats_from_index |
| 17 | +from detection_rules.packaging import current_stack_version |
17 | 18 | from detection_rules.rule import QueryRuleData |
18 | 19 | from detection_rules.rule_loader import FILE_PATTERN |
19 | 20 | from detection_rules.schemas import definitions |
@@ -382,8 +383,6 @@ def test_updated_date_newer_than_creation(self): |
382 | 383 |
|
383 | 384 | def test_deprecated_rules(self): |
384 | 385 | """Test that deprecated rules are properly handled.""" |
385 | | - from detection_rules.packaging import current_stack_version |
386 | | - |
387 | 386 | versions = default_version_lock.version_lock |
388 | 387 | deprecations = load_etc_dump('deprecated_rules.json') |
389 | 388 | deprecated_rules = {} |
@@ -698,6 +697,34 @@ def test_rule_backports_for_restricted_fields(self): |
698 | 697 | self.fail(err_msg) |
699 | 698 |
|
700 | 699 |
|
| 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 | + |
701 | 728 | class TestRiskScoreMismatch(BaseRuleTest): |
702 | 729 | """Test that severity and risk_score fields contain corresponding values""" |
703 | 730 |
|
|
0 commit comments