fix(reconcile): gate heal flapping by drift fingerprint instead of kernel count
This commit is contained in:
1 parent
3d7c1b3f2a
commit
e342d83a70
3 files changed
+100
-17
No files matched your search
@@ -18,7 +18,7 @@ logging.basicConfig(
|
||||
#### Helpers ###
|
||||
################
|
||||
|
||||
def should_heal(history_file, current_count, min_interval=900):
|
||||
def should_heal(history_file, drift_fingerprint, min_interval=900):
|
||||
"""
|
||||
Prevents heal flapping by suppressing repeated no-op heals.
|
||||
"""
|
||||
@@ -35,13 +35,19 @@ def should_heal(history_file, current_count, min_interval=900):
|
||||
last = lines[-1]
|
||||
ts_str, msg = last.split(" - ", 1)
|
||||
last_time = datetime.fromisoformat(ts_str)
|
||||
last_count = int(msg.split("desync of")[1].split("IPs")[0])
|
||||
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_count == current_count:
|
||||
if last_fingerprint and last_fingerprint == drift_fingerprint:
|
||||
logging.warning(
|
||||
f"Heal suppressed (recent identical heal: {last_count} IPs)"
|
||||
"Heal suppressed (recent identical drift fingerprint)."
|
||||
)
|
||||
return False
|
||||
|
||||
@@ -73,21 +79,39 @@ def main():
|
||||
|
||||
while True:
|
||||
try:
|
||||
is_synced, count = auditor.reconcile()
|
||||
is_synced, count, drift = auditor.reconcile()
|
||||
reporter.update_status(is_synced, count)
|
||||
|
||||
status_msg = f"Audit Success: {count} IPs in sync." if is_synced else f"DESYNC: {count} in kernel. Healing..."
|
||||
if is_synced:
|
||||
status_msg = f"Audit Success: {count} IPs in sync."
|
||||
else:
|
||||
status_msg = (
|
||||
f"DESYNC: kernel={count}, missing={drift['missing_count']}, "
|
||||
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']}. "
|
||||
"Healing..."
|
||||
)
|
||||
print(f"[+] {status_msg}") # This will show up in your terminal!
|
||||
|
||||
if not is_synced:
|
||||
logging.warning(f"Desync detected ({count} IPs in kernel).")
|
||||
logging.warning(
|
||||
"Desync detected "
|
||||
f"(kernel={count}, missing={drift['missing_count']}, "
|
||||
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})."
|
||||
)
|
||||
|
||||
history_file = reporter.state_file.replace(".json", ".history")
|
||||
|
||||
if should_heal(history_file, count):
|
||||
if should_heal(history_file, drift.get("fingerprint", "")):
|
||||
logging.warning("Healing approved.")
|
||||
auditor.heal()
|
||||
reporter.log_heal_event(count)
|
||||
reporter.log_heal_event(
|
||||
count,
|
||||
drift.get("missing_count", 0),
|
||||
drift.get("extra_count", 0),
|
||||
drift.get("metadata_drift_count", 0),
|
||||
drift.get("fingerprint", ""),
|
||||
drift.get("reason", "unknown"),
|
||||
)
|
||||
else:
|
||||
logging.warning("Healing skipped to prevent flapping.")
|
||||
else:
|
||||
|
||||
+52
-6
@@ -3,6 +3,7 @@
|
||||
import subprocess
|
||||
import json
|
||||
import logging
|
||||
import hashlib
|
||||
from dotenv import load_dotenv
|
||||
|
||||
class FirewallAuditor:
|
||||
@@ -129,10 +130,24 @@ class FirewallAuditor:
|
||||
|
||||
truth_set = set(truth_map.keys())
|
||||
reality_set = set(reality_map.keys())
|
||||
missing = sorted(truth_set - reality_set)
|
||||
extra = sorted(reality_set - truth_set)
|
||||
|
||||
if truth_set != reality_set:
|
||||
logging.warning("Desync: IP Membership mismatch.")
|
||||
return False, len(reality_set)
|
||||
if missing or extra:
|
||||
drift = {
|
||||
"reason": "membership_mismatch",
|
||||
"missing_count": len(missing),
|
||||
"extra_count": len(extra),
|
||||
"metadata_drift_count": 0,
|
||||
"fingerprint": self._fingerprint_drift(missing, extra, []),
|
||||
}
|
||||
logging.warning(
|
||||
"Desync: IP membership mismatch "
|
||||
f"(missing={drift['missing_count']}, extra={drift['extra_count']})."
|
||||
)
|
||||
return False, len(reality_set), drift
|
||||
|
||||
metadata_drift = []
|
||||
|
||||
for ip, duration_str in truth_map.items():
|
||||
siem_seconds = self.parse_duration(duration_str)
|
||||
@@ -140,7 +155,38 @@ class FirewallAuditor:
|
||||
|
||||
if siem_seconds > 86400: # If SIEM target is > 24h
|
||||
if kernel_seconds < 18000: # And kernel has < 5h left
|
||||
logging.warning(f"Metadata Drift: {ip} needs {duration_str}, but kernel only has {int(kernel_seconds/3600)}h left.")
|
||||
return False, len(reality_map)
|
||||
metadata_drift.append(ip)
|
||||
|
||||
return True, len(reality_map)
|
||||
if metadata_drift:
|
||||
drift = {
|
||||
"reason": "metadata_drift",
|
||||
"missing_count": 0,
|
||||
"extra_count": 0,
|
||||
"metadata_drift_count": len(metadata_drift),
|
||||
"fingerprint": self._fingerprint_drift([], [], metadata_drift),
|
||||
}
|
||||
logging.warning(
|
||||
"Metadata Drift detected "
|
||||
f"(count={drift['metadata_drift_count']})."
|
||||
)
|
||||
return False, len(reality_map), drift
|
||||
|
||||
drift = {
|
||||
"reason": "in_sync",
|
||||
"missing_count": 0,
|
||||
"extra_count": 0,
|
||||
"metadata_drift_count": 0,
|
||||
"fingerprint": "",
|
||||
}
|
||||
return True, len(reality_map), drift
|
||||
|
||||
def _fingerprint_drift(self, missing, extra, metadata_drift):
|
||||
payload = {
|
||||
"missing": sorted(missing),
|
||||
"extra": sorted(extra),
|
||||
"metadata_drift": sorted(metadata_drift),
|
||||
}
|
||||
digest = hashlib.sha256(
|
||||
json.dumps(payload, sort_keys=True).encode("utf-8")
|
||||
).hexdigest()
|
||||
return digest[:16]
|
||||
+15
-2
@@ -37,7 +37,15 @@ class LocalStateReporter:
|
||||
except Exception as e:
|
||||
logging.error(f"Reporter: Failed to write state file: {e}")
|
||||
|
||||
def log_heal_event(self, gap_count):
|
||||
def log_heal_event(
|
||||
self,
|
||||
kernel_count,
|
||||
missing_count,
|
||||
extra_count,
|
||||
metadata_drift_count,
|
||||
drift_fingerprint,
|
||||
reason,
|
||||
):
|
||||
"""
|
||||
Records a specific healing event in a persistent history log.
|
||||
"""
|
||||
@@ -46,6 +54,11 @@ class LocalStateReporter:
|
||||
|
||||
try:
|
||||
with open(history_file, 'a') as f:
|
||||
f.write(f"{timestamp} - HEAL: Fixed desync of {gap_count} IPs\n")
|
||||
f.write(
|
||||
f"{timestamp} - HEAL: reason={reason} "
|
||||
f"kernel={kernel_count} missing={missing_count} "
|
||||
f"extra={extra_count} metadata={metadata_drift_count} "
|
||||
f"drift_fingerprint={drift_fingerprint}\n"
|
||||
)
|
||||
except Exception as e:
|
||||
logging.error(f"Reporter: Failed to write to history log: {e}")
|
||||
Reference in new issue
Block a user