Files
sigma-rules/tests/test_packages.py
T

111 lines
4.3 KiB
Python
Raw Normal View History

2020-06-29 23:19:23 -06:00
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2021-03-03 22:12:11 -09:00
# 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.
2020-06-29 23:19:23 -06:00
"""Test that the packages are built correctly."""
2020-06-29 23:19:23 -06:00
import unittest
import uuid
from marshmallow import ValidationError
from semver import Version
2020-06-29 23:19:23 -06:00
from detection_rules import rule_loader
from detection_rules.packaging import PACKAGE_FILE, Package
from detection_rules.schemas.registry_package import RegistryPackageManifestV1, RegistryPackageManifestV3
from tests.base import BaseRuleTest
2020-06-29 23:19:23 -06:00
package_configs = Package.load_configs()
class TestPackages(BaseRuleTest):
2020-06-29 23:19:23 -06:00
"""Test package building and saving."""
@staticmethod
def get_test_rule(version=1, count=1):
def get_rule_contents():
return {
2020-06-29 23:19:23 -06:00
"author": ["Elastic"],
"description": "test description",
"language": "kuery",
2021-03-03 22:12:11 -09:00
"license": "Elastic License v2",
2020-06-29 23:19:23 -06:00
"name": "test rule",
"query": "process.name:test.query",
"risk_score": 21,
"rule_id": str(uuid.uuid4()),
"severity": "low",
"type": "query",
2020-06-29 23:19:23 -06:00
}
rules = [rule_loader.TOMLRule("test.toml", get_rule_contents()) for i in range(count)]
2020-06-29 23:19:23 -06:00
version_info = {
rule.id: {"rule_name": rule.name, "sha256": rule.contents.get_hash(), "version": version} for rule in rules
2020-06-29 23:19:23 -06:00
}
return rules, version_info
def test_package_loader_production_config(self):
"""Test that packages are loading correctly."""
@unittest.skipIf(rule_loader.RULES_CONFIG.bypass_version_lock, "Version lock bypassed")
2020-06-29 23:19:23 -06:00
def test_package_loader_default_configs(self):
2024-05-15 15:18:39 -05:00
"""Test configs in detection_rules/etc/packages.yaml."""
2024-08-06 18:07:12 -04:00
Package.from_config(rule_collection=self.rc, config=package_configs)
2020-06-29 23:19:23 -06:00
@unittest.skipIf(rule_loader.RULES_CONFIG.bypass_version_lock, "Version lock bypassed")
2020-06-29 23:19:23 -06:00
def test_package_summary(self):
"""Test the generation of the package summary."""
2024-08-06 18:07:12 -04:00
rules = self.rc
package = Package(rules, "test-package")
2021-08-24 16:56:11 -06:00
package.generate_summary_and_changelog(package.changed_ids, package.new_ids, package.removed_ids)
2020-06-29 23:19:23 -06:00
@unittest.skipIf(rule_loader.RULES_CONFIG.bypass_version_lock, "Version lock bypassed")
2020-06-29 23:19:23 -06:00
def test_rule_versioning(self):
"""Test that all rules are properly versioned and tracked"""
self.maxDiff = None
2024-08-06 18:07:12 -04:00
rules = self.rc
2020-06-29 23:19:23 -06:00
original_hashes = []
# test that no rules have versions defined
for rule in rules:
self.assertGreaterEqual(rule.contents.autobumped_version, 1, "{} - {}: version is not being set in package")
original_hashes.append(rule.contents.get_hash())
2020-06-29 23:19:23 -06:00
package = Package(rules, "test-package")
2020-06-29 23:19:23 -06:00
# test that all rules have versions defined
for rule in package.rules:
self.assertGreaterEqual(rule.contents.autobumped_version, 1, "{} - {}: version is not being set in package")
2020-06-29 23:19:23 -06:00
# test that rules validate with version
post_bump_hashes = [rule.contents.get_hash() for rule in package.rules]
2020-06-29 23:19:23 -06:00
# test that no hashes changed as a result of the version bumps
self.assertListEqual(original_hashes, post_bump_hashes, "Version bumping modified the hash of a rule")
2020-06-29 23:19:23 -06:00
class TestRegistryPackage(unittest.TestCase):
"""Test the OOB registry package."""
@classmethod
def setUpClass(cls) -> None:
assert "registry_data" in package_configs, f"Missing registry_data in {PACKAGE_FILE}"
cls.registry_config = package_configs["registry_data"]
stack_version = Version.parse(
cls.registry_config["conditions"]["kibana.version"].strip("^"), optional_minor_and_patch=True
)
if stack_version >= Version.parse("8.12.0"):
RegistryPackageManifestV3.from_dict(cls.registry_config)
else:
RegistryPackageManifestV1.from_dict(cls.registry_config)
def test_registry_package_config(self):
"""Test that the registry package is validating properly."""
registry_config = self.registry_config.copy()
registry_config["version"] += "7.1.1."
with self.assertRaises(ValidationError):
RegistryPackageManifestV1.from_dict(registry_config)