feat: implement self-healing auditor with remote LAPI sync

This commit is contained in:
patrick committed 2026-01-23 10:58:35 -05:00
1 parent 848a29543d
commit 672e821738
6 files changed
+56 -12

No files matched your search

+39 -1
View File
@@ -3,12 +3,18 @@
import subprocess
import json
import logging
from dotenv import load_dotenv
import os
import requests
class FirewallAuditor:
def __init__(self):
"""Ensures the NFT kernel state matches the LAPI's desired state."""
load_dotenv()
self.table = "crowdsec"
self.set_name = "0.0.0.0/0-cscli"
self.API_KEY = os.getenv("BOUNCER_KEY")
self.TRITON = "triton.beane.me"
def get_lapi_state(self):
"""Precision parser: Only harvests IPs from active remediation decisions."""
@@ -38,6 +44,36 @@ class FirewallAuditor:
logging.error(f"LAPI Query Failed: {e}")
return None
def get_remote_lapi_truth(self):
url = f"https://{self.TRITON}:8081/v1/decisions/stream?startup=true"
headers = {"X-Api-Key": self.API_KEY}
try:
# We add a shorter timeout and explicitly disable redirects
r = requests.get(
url,
headers=headers,
verify=True,
timeout=(3.05, 10), # Connect timeout of 3s, Read timeout of 10s
allow_redirects=False
)
data = r.json() or {}
total_items_received = len(new_decisions)
new_decisions = data.get('new') or []
unique_ips = {d['value'] for d in new_decisions if d.get('origin') != 'CAPI' and ':' not in d['value']}
return unique_ips
except requests.exceptions.ConnectTimeout:
logging.error("Connection to Triton timed out. Check if port 8081 is open on Triton's firewall.")
return set()
except Exception as e:
logging.error(f"LAPI Sync Error: {e}")
return set()
def get_kernel_state(self):
"""Scans ALL sets in the crowdsec table to find every blocked IP."""
try:
@@ -74,9 +110,11 @@ class FirewallAuditor:
logging.error(f"Heal Failed: Could not restart bouncer: {e}")
def reconcile(self):
truth = self.get_lapi_state()
truth = self.get_remote_lapi_truth()
reality = self.get_kernel_state()
logging.info(f"Received {len(truth)} IPs from CrowdSec, and {len(reality)} IPs from the firewall (nft)")
if truth is None or reality is None:
return False, 0