feat(ranges): enforce LAPI range decisions in an agent-owned nft table
CI / Unit tests (push) Successful in 9s
CI / Unit tests (pull_request) Successful in 6s

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>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-23 09:54:26 -04:00
1 parent e76e957282
commit 7156cfd4fe
10 files changed
+671 -8

No files matched your search

+80
View File
@@ -128,6 +128,54 @@ class AuditorDecisionExtractionTests(unittest.TestCase):
self.assertTrue(is_synced)
self.assertEqual([], drift["unenforceable_ranges"])
def test_reconcile_uses_range_enforcer_for_range_decisions(self):
class StubEnforcer:
table = "crowdsec-ranges"
def __init__(self):
self.synced = None
def sync(self, range_truth):
self.synced = range_truth
return []
enforcer = StubEnforcer()
auditor = FirewallAuditor(range_enforcer=enforcer)
auditor.get_remote_lapi_truth = lambda: {
"195.178.110.0/24": {"duration": "11h", "until_epoch": None},
"1.1.1.1": {"duration": "4h", "until_epoch": None},
"1.2.3.4/32": {"duration": "4h", "until_epoch": None},
}
auditor.get_kernel_state = lambda: {
"195.178.110.0": 40000,
"1.1.1.1": 14000,
"1.2.3.4": 14000,
}
is_synced, _, drift = auditor.reconcile()
self.assertTrue(is_synced)
self.assertEqual(["195.178.110.0/24"], list(enforcer.synced))
self.assertEqual([], drift["unenforceable_ranges"])
def test_reconcile_reports_ranges_the_range_enforcer_could_not_apply(self):
class FailingEnforcer:
table = "crowdsec-ranges"
def sync(self, range_truth):
return sorted(range_truth)
auditor = FirewallAuditor(range_enforcer=FailingEnforcer())
auditor.get_remote_lapi_truth = lambda: {
"195.178.110.0/24": {"duration": "11h", "until_epoch": None}
}
auditor.get_kernel_state = lambda: {"195.178.110.0": 40000}
is_synced, _, drift = auditor.reconcile()
self.assertTrue(is_synced)
self.assertEqual(["195.178.110.0/24"], drift["unenforceable_ranges"])
def test_reconcile_logs_missing_and_extra_values(self):
auditor = FirewallAuditor()
auditor.get_remote_lapi_truth = lambda: {
@@ -396,6 +444,38 @@ class AuditorDecisionExtractionTests(unittest.TestCase):
self.assertEqual({"1.1.1.1": 600000.0}, kernel_state)
@patch("auditor.subprocess.run")
def test_get_kernel_state_normalizes_prefix_elements(self, run_mock):
auditor = FirewallAuditor(targets=[("ip", "crowdsec")])
run_mock.return_value = subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=json.dumps(
{
"nftables": [
{
"set": {
"name": "crowdsec-blacklists-cscli",
"elem": [
{
"elem": {
"val": {"prefix": {"addr": "10.0.0.0", "len": 24}},
"expires": 1000,
}
},
{"elem": {"val": "1.1.1.1", "expires": 2000}},
],
}
}
]
}
),
)
kernel_state = auditor.get_kernel_state()
self.assertEqual({"10.0.0.0/24": 1000.0, "1.1.1.1": 2000.0}, kernel_state)
@patch("auditor.subprocess.run")
def test_get_kernel_state_falls_back_when_configured_sets_do_not_match(self, run_mock):
auditor = FirewallAuditor(