Silent ticks (empty stdout, exit 0), one line per stable fingerprint, cumulative union-only seen-set at ~/.hermes/cron/netsec-scan-state.json, memoized sensor- degradation line, daily baseline report. Stdlib only; reads the collector's JSONL over SSH with per-file byte offsets and Prometheus via the k8s API proxy. 40 unit tests against fixtures recorded from the live collector.
92 lines
3.3 KiB
Python
92 lines
3.3 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["egress"]["a->1.2.3.4:443/tcp"] = "2026-11-27T00:00:00+00:00"
|
|
state_mod.save(self.path, st)
|
|
again = state_mod.load(self.path)
|
|
again["egress"]["b->5.6.7.8:80/tcp"] = "2026-11-27T00:01:00+00:00"
|
|
state_mod.save(self.path, again)
|
|
final = json.loads(self.path.read_text())
|
|
self.assertEqual(len(final["egress"]), 2)
|
|
|
|
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()
|