14 Commits
Author SHA1 Message Date
patrickandClaude Opus 5.5 16338a2507 fix(healing): judge post-heal verification against the pre-heal decisions
CI / Unit tests (push) Successful in 3s
CI / Unit tests (pull_request) Successful in 3s
A decision issued in the seconds between a heal and its verification is
missing from the kernel until the bouncer's next poll, so verification
reported the heal as failed even though it had fixed the drift it was
started for. Seen on a node: a metadata heal fixed a stale TTL, a new
ban arrived inside the 5 s window, and the heal was logged as failed;
the next audit found the node in sync.

reconcile() now records the decision values it saw (last_truth_values)
and takes an optional baseline_truth. Post-heal verification passes the
values the drift was found against; values whose LAPI membership changed
since then are left out of the drift, logged with a count, returned as
lapi_changed_values, and checked by the next regular audit. Drift on
decisions that existed when the heal started still fails verification.
The regular audit and the membership re-check are unchanged.

Tests: baseline handling in reconcile, the verification call, and a
replay of the logged sequence (failed before, success now). 78 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:32:41 -05:00
patrickandClaude Opus 5.5 7156cfd4fe 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>
2026-10-23 09:54:26 -04:00
patrickandClaude Opus 5.5 e76e957282 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>
2026-10-23 09:43:46 -04:00
patrickandClaude Opus 5.5 8c8be8ec63 fix(auditor): don't heal when the LAPI is unreachable
CI / Unit tests (push) Successful in 4s
CI / Unit tests (pull_request) Successful in 3s
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 <noreply@anthropic.com>
2026-10-21 16:47:48 -04:00
patrickandClaude Opus 5.5 79f72512e4 fix(auditor): use longest kernel expiry for IPs in multiple sets
The bouncer keeps one nft set per decision origin, so an IP with both a
crowdsec ban and a later, longer cscli ban sits in two sets. The kernel
scan kept whichever set it read last, so the short TTL could be compared
against the longest LAPI decision and reported as metadata drift.

Keep the longest expiry per IP instead, and log per-IP evidence (expected
vs kernel seconds, source sets, LAPI decisions) whenever drift is flagged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 19:27:59 -04:00
patrick c698d01c40 feat(auditor): add startup enforcement sanity check and dedupe set-mismatch warnings 2026-03-05 20:32:29 -05:00
patrick 172f5d7ced fix(auditor): fail open when configured enforcement sets do not match nft state 2026-03-05 20:23:00 -05:00
patrick bffe28485d feat(config): enforce runtime nft targets and managed sets from config.yaml 2026-03-05 20:19:31 -05:00
patrick 55cc29071e Add nft handle fallback when flushing CrowdSec sets with slash-based names 2026-02-26 14:14:20 -05:00
patrick 716468901c Harden metadata heal across nft table variants and non-fatal flush errors 2026-02-26 13:37:48 -05:00
patrick aae920fb73 fix: metadata drift false positives - corrected by normalizing nft TTL and skipping unverifiable expiry 2026-02-25 15:02:24 -05:00
patrick 0f7f03f005 Fix metadata-drift healing by flushing CrowdSec nft sets before restart
Add drift-aware heal path in FirewallAuditor to remediate metadata drift by discovering and flushing
  *-cscli sets in crowdsec/crowdsec6 tables, then restarting the bouncer.

Update execute_heal_and_verify to pass drift context to heal_with_drift when supported, keeping
  backward compatibility via heal().

Add unit tests covering metadata-drift flush+restart behavior, non-metadata restart-only behavior,
  and drift context propagation.
2026-02-25 14:59:44 -05:00
patrick 28267a0cf4 fix(auditor): heal on decision-duration drift, not just IP membership/count 2026-02-23 22:20:16 -05:00
patrick 5fc5b8c114 test(agent): add coverage for drift gating and post-heal verification; remove bare except 2026-02-10 22:04:57 -05:00