From 8c8be8ec6325f3caa9c41bbbd0eef7f723f62e00 Mon Sep 17 00:00:00 2001 From: patrickbeane Date: Wed, 21 Oct 2026 16:47:48 -0400 Subject: [PATCH] fix(auditor): don't heal when the LAPI is unreachable get_remote_lapi_truth() returned {} when 'cscli decisions list' failed, which is indistinguishable from "no active bans". reconcile() then saw every kernel ban as extra, reported membership_mismatch, and the heal restarted crowdsec-firewall-bouncer. A LAPI outage would do that on every node at once, every heal interval. Raise LapiUnavailableError instead. reconcile() propagates it, so the main loop records it as last_error and retries in 60s without healing; post-heal verification records it as verify_error. A genuinely empty decision list still returns {} as before. Co-Authored-By: Claude Opus 5.5 --- auditor.py | 11 +++++++++-- tests/test_auditor.py | 24 +++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/auditor.py b/auditor.py index 5d5c147..5526a6d 100644 --- a/auditor.py +++ b/auditor.py @@ -11,6 +11,10 @@ except ImportError: def load_dotenv(): return None +class LapiUnavailableError(RuntimeError): + """The LAPI could not be queried, so there is no decision truth to compare against.""" + + class FirewallAuditor: def __init__(self, targets=None, managed_sets=None): """Ensures the NFT kernel state matches the LAPI's desired state.""" @@ -98,8 +102,11 @@ class FirewallAuditor: logging.info(f"LAPI Local Parser: Found {len(truth_map)} active bans.") return truth_map except Exception as e: - logging.error(f"Failed to get LAPI truth: {e}") - return {} + # Raise rather than return {}: an empty truth map would make every kernel ban + # look like drift and trigger a bouncer restart whenever the LAPI is down. + detail = (getattr(e, "stderr", None) or "").strip() or str(e) + logging.error(f"Failed to get LAPI truth: {detail}") + raise LapiUnavailableError(f"LAPI unreachable: {detail}") from e def _normalize_truth_decision(self, decision): return { diff --git a/tests/test_auditor.py b/tests/test_auditor.py index caceb99..24b46c6 100644 --- a/tests/test_auditor.py +++ b/tests/test_auditor.py @@ -4,7 +4,7 @@ import json import subprocess from unittest.mock import patch, call -from auditor import FirewallAuditor +from auditor import FirewallAuditor, LapiUnavailableError class AuditorDecisionExtractionTests(unittest.TestCase): @@ -99,6 +99,28 @@ class AuditorDecisionExtractionTests(unittest.TestCase): self.assertEqual(1, count) self.assertEqual("in_sync", drift["reason"]) + @patch("auditor.subprocess.run") + def test_lapi_query_failure_raises_instead_of_returning_empty_truth(self, run_mock): + auditor = FirewallAuditor() + run_mock.side_effect = subprocess.CalledProcessError( + 1, ["/usr/bin/cscli"], stderr="dial tcp 100.64.0.1:8080: connection refused\n" + ) + + with self.assertRaisesRegex(LapiUnavailableError, "connection refused"): + auditor.get_remote_lapi_truth() + + def test_reconcile_does_not_report_drift_when_lapi_unreachable(self): + auditor = FirewallAuditor() + + def unreachable(): + raise LapiUnavailableError("LAPI unreachable: connection refused") + + auditor.get_remote_lapi_truth = unreachable + auditor.get_kernel_state = lambda: {"1.1.1.1": 3600} + + with self.assertRaises(LapiUnavailableError): + auditor.reconcile() + @patch("auditor.subprocess.run") def test_heal_with_metadata_drift_flushes_sets_before_restart(self, run_mock): auditor = FirewallAuditor()