fix(auditor): report range decisions the kernel can't enforce instead of healing
CI / Unit tests (push) Successful in 3s
CI / Unit tests (pull_request) Successful in 7s

A LAPI range decision (e.g. 195.178.110.0/24) inserted into an nft set
without the interval flag is stored as its bare network address. The
string diff saw that as one missing and one extra entry, and every heal
re-added the same address, so nodes stuck in DESYNC_DETECTED with the
flap guard suppressing repeat heals.

Pair such ranges with their network address, report them as
unenforceable_ranges with a dedicated warning, and keep them out of the
drift counts and fingerprint. The reporter records
unenforceable_range_count and shows DEGRADED for a node that is
otherwise in sync. Desync log lines now name the missing and extra
values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-23 09:43:46 -04:00
1 parent 57ed6cfc1c
commit e76e957282
6 files changed
+175 -4

No files matched your search

+87
View File
@@ -67,6 +67,93 @@ class AuditorDecisionExtractionTests(unittest.TestCase):
self.assertFalse(is_synced)
self.assertEqual("metadata_drift", drift["reason"])
def test_reconcile_reports_range_collapsed_to_network_address_without_drift(self):
auditor = FirewallAuditor()
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},
}
auditor.get_kernel_state = lambda: {"195.178.110.0": 40000, "1.1.1.1": 14000}
is_synced, count, drift = auditor.reconcile()
self.assertTrue(is_synced)
self.assertEqual(2, count)
self.assertEqual("in_sync", drift["reason"])
self.assertEqual(["195.178.110.0/24"], drift["unenforceable_ranges"])
def test_reconcile_keeps_real_drift_alongside_collapsed_range(self):
auditor = FirewallAuditor()
auditor.get_remote_lapi_truth = lambda: {
"195.178.110.0/24": {"duration": "11h", "until_epoch": None},
"2.2.2.2": {"duration": "4h", "until_epoch": None},
}
auditor.get_kernel_state = lambda: {"195.178.110.0": 40000, "9.9.9.9": 14000}
is_synced, _, drift = auditor.reconcile()
self.assertFalse(is_synced)
self.assertEqual("membership_mismatch", drift["reason"])
self.assertEqual(1, drift["missing_count"])
self.assertEqual(1, drift["extra_count"])
self.assertEqual(
auditor._fingerprint_drift(["2.2.2.2"], ["9.9.9.9"], []),
drift["fingerprint"],
)
self.assertEqual(["195.178.110.0/24"], drift["unenforceable_ranges"])
def test_reconcile_matches_range_whose_network_address_is_also_banned(self):
auditor = FirewallAuditor()
auditor.get_remote_lapi_truth = lambda: {
"10.0.0.0/24": {"duration": "4h", "until_epoch": None},
"10.0.0.0": {"duration": "4h", "until_epoch": None},
}
auditor.get_kernel_state = lambda: {"10.0.0.0": 14000}
is_synced, _, drift = auditor.reconcile()
self.assertTrue(is_synced)
self.assertEqual(["10.0.0.0/24"], drift["unenforceable_ranges"])
def test_reconcile_treats_full_length_prefix_stored_as_address_as_in_sync(self):
auditor = FirewallAuditor()
auditor.get_remote_lapi_truth = lambda: {
"1.2.3.4/32": {"duration": "4h", "until_epoch": None},
"2001:db8::1/128": {"duration": "4h", "until_epoch": None},
}
auditor.get_kernel_state = lambda: {"1.2.3.4": 14000, "2001:db8::1": 14000}
is_synced, _, drift = auditor.reconcile()
self.assertTrue(is_synced)
self.assertEqual([], drift["unenforceable_ranges"])
def test_reconcile_logs_missing_and_extra_values(self):
auditor = FirewallAuditor()
auditor.get_remote_lapi_truth = lambda: {
"2.2.2.2": {"duration": "4h", "until_epoch": None}
}
auditor.get_kernel_state = lambda: {"9.9.9.9": 14000}
with self.assertLogs(level="WARNING") as logs:
auditor.reconcile()
self.assertTrue(
any(
"missing_values=[2.2.2.2]" in line and "extra_values=[9.9.9.9]" in line
for line in logs.output
)
)
def test_preview_values_truncates_long_lists(self):
auditor = FirewallAuditor()
values = [f"10.0.0.{i}" for i in range(12)]
self.assertEqual(
"[" + ",".join(values[:10]) + ",+2 more]",
auditor._preview_values(values),
)
def test_parse_duration_supports_compound_values(self):
auditor = FirewallAuditor()
self.assertEqual(5400, auditor.parse_duration("1h30m"))
+18
View File
@@ -37,6 +37,24 @@ class ReporterStateTests(unittest.TestCase):
self.assertEqual("abc123", payload["last_drift_fingerprint"])
self.assertEqual("failed", payload["last_heal_result"])
def test_update_status_reports_degraded_when_synced_with_unenforceable_ranges(self):
with tempfile.TemporaryDirectory() as tmp_dir:
state_file = os.path.join(tmp_dir, "state.json")
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
reporter.update_status(True, 10, unenforceable_range_count=1)
with open(state_file, "r") as f:
degraded = json.load(f)
reporter.update_status(True, 10)
with open(state_file, "r") as f:
healthy = json.load(f)
self.assertEqual("DEGRADED", degraded["status"])
self.assertTrue(degraded["is_synced"])
self.assertEqual(1, degraded["unenforceable_range_count"])
self.assertEqual("HEALTHY", healthy["status"])
self.assertEqual(0, healthy["unenforceable_range_count"])
def test_update_status_preserves_last_heal_result_when_omitted(self):
with tempfile.TemporaryDirectory() as tmp_dir:
state_file = os.path.join(tmp_dir, "state.json")