2021-03-05 11:16:02 -09:00
|
|
|
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
|
# or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
|
# 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
|
# 2.0.
|
|
|
|
|
|
|
|
|
|
"""Shared resources for tests."""
|
2025-07-01 15:20:55 +02:00
|
|
|
|
2024-08-06 18:07:12 -04:00
|
|
|
import os
|
2021-03-05 11:16:02 -09:00
|
|
|
import unittest
|
2023-03-07 14:40:41 -09:00
|
|
|
from functools import lru_cache
|
2025-07-01 15:20:55 +02:00
|
|
|
from pathlib import Path
|
2021-03-05 11:16:02 -09:00
|
|
|
|
2024-08-06 18:07:12 -04:00
|
|
|
from detection_rules.config import parse_rules_config
|
2021-03-24 10:24:32 -06:00
|
|
|
from detection_rules.rule import TOMLRule
|
2021-09-01 15:29:53 -08:00
|
|
|
from detection_rules.rule_loader import DeprecatedCollection, DeprecatedRule, RuleCollection, production_filter
|
2021-03-05 11:16:02 -09:00
|
|
|
|
2023-03-07 19:19:59 -09:00
|
|
|
RULE_LOADER_FAIL = False
|
|
|
|
|
RULE_LOADER_FAIL_MSG = None
|
|
|
|
|
RULE_LOADER_FAIL_RAISED = False
|
|
|
|
|
|
2025-07-01 15:20:55 +02:00
|
|
|
CUSTOM_RULES_DIR = os.getenv("CUSTOM_RULES_DIR", None)
|
2024-08-06 18:07:12 -04:00
|
|
|
RULES_CONFIG = parse_rules_config()
|
|
|
|
|
|
2023-03-07 19:19:59 -09:00
|
|
|
|
2023-03-07 14:40:41 -09:00
|
|
|
@lru_cache
|
2024-08-06 18:07:12 -04:00
|
|
|
def load_rules() -> RuleCollection:
|
|
|
|
|
if CUSTOM_RULES_DIR:
|
|
|
|
|
rc = RuleCollection()
|
2025-12-03 12:19:29 -05:00
|
|
|
path = Path(CUSTOM_RULES_DIR).expanduser()
|
2025-07-01 15:20:55 +02:00
|
|
|
assert path.exists(), f"Custom rules directory {path} does not exist"
|
2024-08-06 18:07:12 -04:00
|
|
|
rc.load_directories(directories=RULES_CONFIG.rule_dirs)
|
|
|
|
|
rc.freeze()
|
|
|
|
|
return rc
|
2023-03-07 14:40:41 -09:00
|
|
|
return RuleCollection.default()
|
|
|
|
|
|
|
|
|
|
|
2024-08-06 18:07:12 -04:00
|
|
|
def default_bbr(rc: RuleCollection) -> RuleCollection:
|
2025-07-01 15:20:55 +02:00
|
|
|
rules = [r for r in rc.rules if "rules_building_block" in r.path.parent.parts]
|
2024-08-06 18:07:12 -04:00
|
|
|
return RuleCollection(rules=rules)
|
2023-06-20 13:00:30 +00:00
|
|
|
|
|
|
|
|
|
2021-03-05 11:16:02 -09:00
|
|
|
class BaseRuleTest(unittest.TestCase):
|
|
|
|
|
"""Base class for shared test cases which need to load rules"""
|
|
|
|
|
|
2023-03-07 14:40:41 -09:00
|
|
|
RULE_LOADER_FAIL = False
|
|
|
|
|
RULE_LOADER_FAIL_MSG = None
|
|
|
|
|
RULE_LOADER_FAIL_RAISED = False
|
|
|
|
|
|
2021-03-05 11:16:02 -09:00
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2025-07-01 15:20:55 +02:00
|
|
|
global RULE_LOADER_FAIL, RULE_LOADER_FAIL_MSG # noqa: PLW0603
|
2023-03-07 14:40:41 -09:00
|
|
|
|
2023-03-07 19:19:59 -09:00
|
|
|
if not RULE_LOADER_FAIL:
|
2023-03-07 14:40:41 -09:00
|
|
|
try:
|
2024-08-06 18:07:12 -04:00
|
|
|
rc = load_rules()
|
|
|
|
|
rc_bbr = default_bbr(rc)
|
|
|
|
|
cls.rc = rc
|
|
|
|
|
cls.all_rules = rc.filter(production_filter)
|
2023-06-20 13:00:30 +00:00
|
|
|
cls.bbr = rc_bbr.rules
|
2023-03-07 14:40:41 -09:00
|
|
|
cls.deprecated_rules: DeprecatedCollection = rc.deprecated
|
2025-07-01 15:20:55 +02:00
|
|
|
except Exception as e: # noqa: BLE001
|
2023-03-07 19:19:59 -09:00
|
|
|
RULE_LOADER_FAIL = True
|
|
|
|
|
RULE_LOADER_FAIL_MSG = str(e)
|
2021-03-05 11:16:02 -09:00
|
|
|
|
2025-12-03 12:19:29 -05:00
|
|
|
cls.custom_dir = Path(CUSTOM_RULES_DIR).expanduser().resolve() if CUSTOM_RULES_DIR else None
|
2024-08-06 18:07:12 -04:00
|
|
|
cls.rules_config = RULES_CONFIG
|
|
|
|
|
|
2021-03-05 11:16:02 -09:00
|
|
|
@staticmethod
|
2025-07-01 15:20:55 +02:00
|
|
|
def rule_str(rule: DeprecatedRule | TOMLRule, trailer=" ->") -> str:
|
|
|
|
|
return f"{rule.id} - {rule.name}{trailer or ''}"
|
2023-03-07 14:40:41 -09:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2025-07-01 15:20:55 +02:00
|
|
|
global RULE_LOADER_FAIL_RAISED # noqa: PLW0603
|
2023-03-07 19:19:59 -09:00
|
|
|
|
|
|
|
|
if RULE_LOADER_FAIL:
|
2023-03-07 14:40:41 -09:00
|
|
|
# limit the loader failure to just one run
|
|
|
|
|
# raise a dedicated test failure for the loader
|
2023-03-07 19:19:59 -09:00
|
|
|
if not RULE_LOADER_FAIL_RAISED:
|
|
|
|
|
RULE_LOADER_FAIL_RAISED = True
|
2025-07-01 15:20:55 +02:00
|
|
|
with self.subTest("Test that the rule loader loaded with no validation or other failures."):
|
|
|
|
|
self.fail(f"Rule loader failure: {RULE_LOADER_FAIL_MSG}")
|
2023-03-07 14:40:41 -09:00
|
|
|
|
2025-07-01 15:20:55 +02:00
|
|
|
self.skipTest("Rule loader failure")
|
2023-03-07 14:40:41 -09:00
|
|
|
else:
|
|
|
|
|
super().setUp()
|