206 lines
7.3 KiB
Python
206 lines
7.3 KiB
Python
import unittest
|
|
import datetime
|
|
import json
|
|
import subprocess
|
|
from unittest.mock import patch, call
|
|
|
|
from auditor import FirewallAuditor
|
|
|
|
|
|
class AuditorDecisionExtractionTests(unittest.TestCase):
|
|
def test_extract_decisions_handles_flat_and_nested_payloads(self):
|
|
auditor = FirewallAuditor()
|
|
payload = [
|
|
{"value": "1.1.1.1", "duration": "4h", "origin": "lists"},
|
|
{
|
|
"decisions": [
|
|
{"value": "2.2.2.2", "duration": "8h", "origin": "crowdsec"},
|
|
{"value": "2.2.2.2", "duration": "8h", "origin": "crowdsec"},
|
|
]
|
|
},
|
|
{
|
|
"source": {
|
|
"decisions": [
|
|
{"value": "3.3.3.3", "duration": "12h", "origin": "manual"}
|
|
]
|
|
}
|
|
},
|
|
]
|
|
|
|
decisions = list(auditor._extract_decisions(payload))
|
|
decision_keys = {
|
|
(d.get("value"), d.get("duration"), d.get("origin")) for d in decisions
|
|
}
|
|
|
|
self.assertIn(("1.1.1.1", "4h", "lists"), decision_keys)
|
|
self.assertIn(("2.2.2.2", "8h", "crowdsec"), decision_keys)
|
|
self.assertIn(("3.3.3.3", "12h", "manual"), decision_keys)
|
|
self.assertEqual(3, len(decision_keys))
|
|
|
|
def test_reconcile_detects_metadata_drift_for_extended_duration(self):
|
|
auditor = FirewallAuditor()
|
|
auditor.get_remote_lapi_truth = lambda: {
|
|
"1.1.1.1": {"duration": "24h", "until_epoch": None}
|
|
}
|
|
auditor.get_kernel_state = lambda: {"1.1.1.1": 3 * 3600 * 1000}
|
|
|
|
is_synced, count, drift = auditor.reconcile()
|
|
|
|
self.assertFalse(is_synced)
|
|
self.assertEqual(1, count)
|
|
self.assertEqual("metadata_drift", drift["reason"])
|
|
self.assertEqual(1, drift["metadata_drift_count"])
|
|
|
|
def test_reconcile_uses_until_for_expected_remaining(self):
|
|
now_epoch = datetime.datetime.now(datetime.timezone.utc).timestamp()
|
|
auditor = FirewallAuditor()
|
|
auditor.get_remote_lapi_truth = lambda: {
|
|
"2.2.2.2": {
|
|
"duration": "4h",
|
|
"until_epoch": now_epoch + (24 * 3600),
|
|
}
|
|
}
|
|
auditor.get_kernel_state = lambda: {"2.2.2.2": 2 * 3600 * 1000}
|
|
|
|
is_synced, _, drift = auditor.reconcile()
|
|
|
|
self.assertFalse(is_synced)
|
|
self.assertEqual("metadata_drift", drift["reason"])
|
|
|
|
def test_parse_duration_supports_compound_values(self):
|
|
auditor = FirewallAuditor()
|
|
self.assertEqual(5400, auditor.parse_duration("1h30m"))
|
|
self.assertEqual(1209600, auditor.parse_duration("14d"))
|
|
|
|
def test_reconcile_skips_metadata_check_when_kernel_expiry_unavailable(self):
|
|
auditor = FirewallAuditor()
|
|
auditor.get_remote_lapi_truth = lambda: {
|
|
"4.4.4.4": {"duration": "14d", "until_epoch": None}
|
|
}
|
|
auditor.get_kernel_state = lambda: {"4.4.4.4": None}
|
|
|
|
is_synced, count, drift = auditor.reconcile()
|
|
|
|
self.assertTrue(is_synced)
|
|
self.assertEqual(1, count)
|
|
self.assertEqual("in_sync", drift["reason"])
|
|
|
|
def test_reconcile_supports_kernel_ttl_reported_in_seconds(self):
|
|
auditor = FirewallAuditor()
|
|
auditor.get_remote_lapi_truth = lambda: {
|
|
"5.5.5.5": {"duration": "14d", "until_epoch": None}
|
|
}
|
|
# Some nft stacks report expires directly in seconds.
|
|
auditor.get_kernel_state = lambda: {"5.5.5.5": 13 * 24 * 3600}
|
|
|
|
is_synced, count, drift = auditor.reconcile()
|
|
|
|
self.assertTrue(is_synced)
|
|
self.assertEqual(1, count)
|
|
self.assertEqual("in_sync", drift["reason"])
|
|
|
|
@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
|
|
subprocess.CalledProcessError(1, ["/usr/sbin/nft"]), # inet crowdsec absent
|
|
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(
|
|
["/usr/sbin/nft", "-j", "list", "table", "inet", "crowdsec"],
|
|
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,
|
|
)
|
|
|
|
@patch("auditor.subprocess.run")
|
|
def test_heal_metadata_drift_continues_when_one_flush_fails(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"}}]}),
|
|
),
|
|
subprocess.CalledProcessError(
|
|
1,
|
|
["/usr/sbin/nft", "flush", "set", "ip", "crowdsec", "0.0.0.0/0-cscli"],
|
|
stderr="No such file or directory",
|
|
),
|
|
subprocess.CalledProcessError(1, ["/usr/sbin/nft"]), # ip6 crowdsec6 absent
|
|
subprocess.CalledProcessError(1, ["/usr/sbin/nft"]), # inet crowdsec absent
|
|
None, # restart still executes
|
|
]
|
|
|
|
ok = auditor.heal_with_drift({"reason": "metadata_drift"})
|
|
|
|
self.assertTrue(ok)
|
|
self.assertEqual(
|
|
call(["systemctl", "restart", "crowdsec-firewall-bouncer"], check=True),
|
|
run_mock.call_args_list[-1],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|