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.
This commit is contained in:
1 parent
28267a0cf4
commit
0f7f03f005
4 files changed
+122
-2
No files matched your search
@@ -1,5 +1,8 @@
|
||||
import unittest
|
||||
import datetime
|
||||
import json
|
||||
import subprocess
|
||||
from unittest.mock import patch, call
|
||||
|
||||
from auditor import FirewallAuditor
|
||||
|
||||
@@ -69,6 +72,73 @@ class AuditorDecisionExtractionTests(unittest.TestCase):
|
||||
self.assertEqual(5400, auditor.parse_duration("1h30m"))
|
||||
self.assertEqual(1209600, auditor.parse_duration("14d"))
|
||||
|
||||
@patch("auditor.subprocess.run")
|
||||
def test_heal_with_metadata_drift_flushes_sets_before_restart(self, run_mock):
|
||||
auditor = FirewallAuditor()
|
||||
|
||||
run_mock.side_effect = [
|
||||
subprocess.CompletedProcess(
|
||||
args=[],
|
||||
returncode=0,
|
||||
stdout=json.dumps({"nftables": [{"set": {"name": "0.0.0.0/0-cscli"}}]}),
|
||||
),
|
||||
None, # flush ip set
|
||||
subprocess.CompletedProcess(
|
||||
args=[],
|
||||
returncode=0,
|
||||
stdout=json.dumps({"nftables": [{"set": {"name": "::/0-cscli"}}]}),
|
||||
),
|
||||
None, # flush ip6 set
|
||||
None, # systemctl restart
|
||||
]
|
||||
|
||||
ok = auditor.heal_with_drift({"reason": "metadata_drift"})
|
||||
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(
|
||||
[
|
||||
call(
|
||||
["/usr/sbin/nft", "-j", "list", "table", "ip", "crowdsec"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
),
|
||||
call(
|
||||
["/usr/sbin/nft", "flush", "set", "ip", "crowdsec", "0.0.0.0/0-cscli"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
),
|
||||
call(
|
||||
["/usr/sbin/nft", "-j", "list", "table", "ip6", "crowdsec6"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
),
|
||||
call(
|
||||
["/usr/sbin/nft", "flush", "set", "ip6", "crowdsec6", "::/0-cscli"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
),
|
||||
call(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True),
|
||||
],
|
||||
run_mock.call_args_list,
|
||||
)
|
||||
|
||||
@patch("auditor.subprocess.run")
|
||||
def test_heal_without_metadata_drift_only_restarts_bouncer(self, run_mock):
|
||||
auditor = FirewallAuditor()
|
||||
run_mock.return_value = None
|
||||
|
||||
ok = auditor.heal_with_drift({"reason": "membership_mismatch"})
|
||||
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(
|
||||
[call(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True)],
|
||||
run_mock.call_args_list,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user