- first-seen egress is gated by the cumulative seen-set only (deleting a fingerprint re-arms the rule); the alert states the observed window (~20m / 3.2h / 2.1d) instead of implying the collector holds weeks. - a DROPPED attempt on a new to-world tuple is keyed separately from forwarded traffic to the same destination and graded HIGH. - daily report counts every external tuple seen that day, not just novel ones. - drop-prune of the dead state['egress'] map; tests cover the single-fire rearm, day counters and the egress union. - README: measured collector volume/tombstones/partial-tail behaviour, scheduler shim path.
92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
"""State-file semantics: union, atomic write, rollover, bounds, corruption."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from netsec_scan import config, state as state_mod
|
|
from netsec_scan.prom import SensorError
|
|
|
|
|
|
class TestStateFile(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.path = Path(self.tmp.name) / "state.json"
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def test_missing_file_is_a_fresh_state(self):
|
|
st = state_mod.load(self.path)
|
|
self.assertFalse(st["seeded"])
|
|
self.assertEqual(st["seen"], {})
|
|
self.assertEqual(st["files"], {})
|
|
|
|
def test_roundtrip_and_mode(self):
|
|
st = state_mod.load(self.path)
|
|
st["seen"]["abc"] = {"rule": "x", "kind": "state", "entity": "e",
|
|
"severity": "LOW", "first": "2026-11-27T00:00:00+00:00",
|
|
"last_alert": "2026-11-27T00:00:00+00:00"}
|
|
state_mod.save(self.path, st)
|
|
again = state_mod.load(self.path)
|
|
self.assertIn("abc", again["seen"])
|
|
self.assertEqual(os.stat(self.path).st_mode & 0o777, 0o600)
|
|
|
|
def test_corrupt_file_raises_instead_of_resetting(self):
|
|
self.path.write_text("{not json")
|
|
with self.assertRaises(SensorError) as ctx:
|
|
state_mod.load(self.path)
|
|
self.assertEqual(ctx.exception.cls, "state-corrupt")
|
|
|
|
def test_missing_key_is_added_not_replaced(self):
|
|
st = state_mod.load(self.path)
|
|
st["seen"]["aaaa"] = {"rule": "x", "first": "2026-11-27T00:00:00+00:00"}
|
|
state_mod.save(self.path, st)
|
|
again = state_mod.load(self.path)
|
|
again["seen"]["bbbb"] = {"rule": "x", "first": "2026-11-27T00:01:00+00:00"}
|
|
state_mod.save(self.path, again)
|
|
final = json.loads(self.path.read_text())
|
|
self.assertEqual(sorted(final["seen"]), ["aaaa", "bbbb"])
|
|
|
|
def test_prune_bounds_the_seen_set(self):
|
|
st = state_mod.load(self.path)
|
|
for i in range(config.MAX_SEEN + 500):
|
|
st["seen"][f"fp{i}"] = {"first": "2026-11-27T00:00:%02d+00:00" % (i % 60)}
|
|
state_mod.prune(st)
|
|
self.assertLessEqual(len(st["seen"]), config.MAX_SEEN)
|
|
|
|
def test_day_rolls_over(self):
|
|
st = state_mod.load(self.path)
|
|
st["day"] = {"date": "1999-01-01", "flows": 42, "rules": {"x": 1},
|
|
"egress": [], "new_dsts": []}
|
|
state_mod.roll_day(st)
|
|
self.assertNotEqual(st["day"]["date"], "1999-01-01")
|
|
self.assertEqual(st["day"]["flows"], 0)
|
|
|
|
def test_day_kept_within_same_day(self):
|
|
st = state_mod.load(self.path)
|
|
today = st["day"]["date"]
|
|
st["day"]["flows"] = 7
|
|
state_mod.roll_day(st)
|
|
self.assertEqual(st["day"]["date"], today)
|
|
self.assertEqual(st["day"]["flows"], 7)
|
|
|
|
|
|
class TestDegradeMemo(unittest.TestCase):
|
|
def test_memo_window(self):
|
|
memo = {}
|
|
state_mod.note_degraded({"degraded": memo}, "timeout")
|
|
now = time.time()
|
|
self.assertTrue(state_mod.degraded_memo({"degraded": memo}, "timeout", now))
|
|
self.assertFalse(state_mod.degraded_memo({"degraded": memo}, "other", now))
|
|
later = now + config.DEGRADED_MEMO + 1
|
|
self.assertFalse(state_mod.degraded_memo({"degraded": memo}, "timeout", later))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|