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.
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
import logging
|
|
import os
|
|
import time
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
def should_heal(history_file, drift_fingerprint, min_interval=900):
|
|
"""
|
|
Prevents heal flapping by suppressing repeated no-op heals.
|
|
"""
|
|
try:
|
|
if not os.path.exists(history_file):
|
|
return True
|
|
|
|
with open(history_file) as f:
|
|
lines = [l.strip() for l in f.readlines() if "HEAL:" in l]
|
|
|
|
if not lines:
|
|
return True
|
|
|
|
last = lines[-1]
|
|
ts_str, msg = last.split(" - ", 1)
|
|
last_time = datetime.fromisoformat(ts_str)
|
|
last_fingerprint = ""
|
|
marker = "drift_fingerprint="
|
|
if marker in msg:
|
|
last_fingerprint = msg.split(marker, 1)[1].split()[0].strip()
|
|
|
|
if not drift_fingerprint:
|
|
return True
|
|
|
|
# If healed recently AND nothing changed, suppress
|
|
if datetime.now() - last_time < timedelta(seconds=min_interval):
|
|
if last_fingerprint and last_fingerprint == drift_fingerprint:
|
|
logging.warning(
|
|
"Heal suppressed (recent identical drift fingerprint)."
|
|
)
|
|
return False
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
logging.error(f"Heal gate failed open: {e}")
|
|
return True
|
|
|
|
|
|
def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
|
|
"""
|
|
Executes heal + post-heal verification and persists reporter state.
|
|
"""
|
|
if hasattr(auditor, "heal_with_drift"):
|
|
heal_ok = auditor.heal_with_drift(drift or {})
|
|
else:
|
|
heal_ok = auditor.heal()
|
|
heal_result = "restart_failed"
|
|
post_drift = {
|
|
"reason": "not_verified",
|
|
"missing_count": 0,
|
|
"extra_count": 0,
|
|
"metadata_drift_count": 0,
|
|
"fingerprint": "",
|
|
}
|
|
|
|
if heal_ok:
|
|
logging.info(
|
|
f"Post-heal verification in {heal_verify_delay}s "
|
|
f"(pre_fingerprint={drift.get('fingerprint', '')})."
|
|
)
|
|
time.sleep(heal_verify_delay)
|
|
post_is_synced, post_count, post_drift = auditor.reconcile()
|
|
heal_result = "success" if post_is_synced else "failed"
|
|
verify_error = None if post_is_synced else "Post-heal verification failed"
|
|
reporter.update_status(post_is_synced, post_count, last_error=verify_error)
|
|
|
|
logging.warning(
|
|
"Post-heal verification result "
|
|
f"result={heal_result} pre={drift.get('fingerprint', '')} "
|
|
f"post={post_drift.get('fingerprint', '')} "
|
|
f"missing={post_drift.get('missing_count', 0)} "
|
|
f"extra={post_drift.get('extra_count', 0)} "
|
|
f"metadata={post_drift.get('metadata_drift_count', 0)}"
|
|
)
|
|
else:
|
|
reporter.update_status(False, count, last_error="Bouncer restart failed")
|
|
logging.error(
|
|
f"Heal restart failed; skipping verification "
|
|
f"(pre_fingerprint={drift.get('fingerprint', '')})."
|
|
)
|
|
|
|
return heal_result, post_drift
|