crowdsec-firewall-bouncer's nftables mode strips the prefix from Range decisions and bans only the network address, so a /24 decision blocked a single IP. With ranges.enabled, every audit now syncs range decisions into a dedicated `inet crowdsec-ranges` table (interval+timeout sets with input/forward drop chains) in one atomic `nft -f` transaction, then reads it back to confirm each range is enforced. - element timeouts track each decision's remaining lifetime; stale elements are removed and extended decisions refreshed - ranges nested in another active range are covered, not inserted - ranges broader than min_prefix_v4/v6 (default /16, /32) are refused - ranges still missing after the sync stay in unenforceable_ranges, so the node reports DEGRADED rather than HEALTHY - the bouncer-set scan normalizes nft prefix elements to addr/len instead of failing the table scan on them - doctor checks the range table; the audit log line says when a synced node is degraded Disabled by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
4.6 KiB
Python
136 lines
4.6 KiB
Python
import os
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from config import RuntimeSettings, load_runtime_settings, validate_runtime_settings
|
|
|
|
|
|
class RuntimeSettingsEnforcementTests(unittest.TestCase):
|
|
@patch("config.load_config")
|
|
def test_enforcement_uses_explicit_targets_when_present(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"agent": {"node_name": "n1"},
|
|
"enforcement": {
|
|
"targets": [
|
|
{"family": "ip", "table": "custom_v4"},
|
|
{"family": "inet", "table": "custom_inet"},
|
|
],
|
|
"sets": ["0.0.0.0/0-cscli", "crowdsec-blacklists"],
|
|
},
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(
|
|
(("ip", "custom_v4"), ("inet", "custom_inet")),
|
|
settings.enforcement_targets,
|
|
)
|
|
self.assertEqual(
|
|
("0.0.0.0/0-cscli", "crowdsec-blacklists"),
|
|
settings.enforcement_sets,
|
|
)
|
|
|
|
@patch("config.load_config")
|
|
def test_enforcement_falls_back_to_table_fields(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"enforcement": {
|
|
"table": "crowdsec_main",
|
|
"table6": "crowdsec6_main",
|
|
"families": ["ip", "ip6"],
|
|
}
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(
|
|
(("ip", "crowdsec_main"), ("ip6", "crowdsec6_main")),
|
|
settings.enforcement_targets,
|
|
)
|
|
self.assertEqual((), settings.enforcement_sets)
|
|
|
|
@patch("config.load_config")
|
|
def test_check_jitter_pct_loaded_and_clamped(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"agent": {
|
|
"check_jitter_pct": 1.5,
|
|
}
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(0.90, settings.check_jitter_pct)
|
|
|
|
@patch("config.load_config")
|
|
def test_ranges_settings_default_to_disabled(self, load_config_mock):
|
|
load_config_mock.return_value = {}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertFalse(settings.ranges_enabled)
|
|
self.assertEqual("crowdsec-ranges", settings.ranges_table)
|
|
self.assertEqual(16, settings.ranges_min_prefix_v4)
|
|
self.assertEqual(32, settings.ranges_min_prefix_v6)
|
|
|
|
@patch("config.load_config")
|
|
def test_ranges_settings_loaded_and_validated(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"ranges": {"enabled": True, "table": "r", "min_prefix_v4": 0, "min_prefix_v6": 48}
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
issues = validate_runtime_settings(settings)
|
|
|
|
self.assertTrue(settings.ranges_enabled)
|
|
self.assertEqual("r", settings.ranges_table)
|
|
self.assertEqual(48, settings.ranges_min_prefix_v6)
|
|
self.assertTrue(
|
|
any(i.level == "ERROR" and "min_prefix_v4" in i.message for i in issues)
|
|
)
|
|
|
|
def test_validate_runtime_settings_reports_errors(self):
|
|
settings = RuntimeSettings(
|
|
node_name="n1",
|
|
log_path="/tmp/agent.log",
|
|
state_file="/tmp/state.json",
|
|
check_interval=0,
|
|
check_jitter_pct=0.10,
|
|
heal_verify_delay=-1,
|
|
heal_min_interval=-2,
|
|
enforcement_targets=(),
|
|
enforcement_sets=(),
|
|
config_path="config.yaml",
|
|
)
|
|
|
|
issues = validate_runtime_settings(settings)
|
|
errors = [issue for issue in issues if issue.level == "ERROR"]
|
|
|
|
self.assertGreaterEqual(len(errors), 4)
|
|
|
|
def test_validate_runtime_settings_reports_warn_for_relative_state_file(self):
|
|
settings = RuntimeSettings(
|
|
node_name="n1",
|
|
log_path="/tmp/agent.log",
|
|
state_file="relative-state.json",
|
|
check_interval=600,
|
|
check_jitter_pct=0.10,
|
|
heal_verify_delay=5,
|
|
heal_min_interval=900,
|
|
enforcement_targets=(("ip", "crowdsec"),),
|
|
enforcement_sets=(),
|
|
config_path="config.yaml",
|
|
)
|
|
|
|
issues = validate_runtime_settings(settings)
|
|
warns = [issue.message for issue in issues if issue.level == "WARN"]
|
|
|
|
self.assertTrue(any("state_file is not absolute" in msg for msg in warns))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|