netsec-scan: a seed is silent for its whole span, --dry-run included
The seeding guard only ran in on_batch for the archive path, so a cold start still announced the entire retained window whenever the tick was a --dry-run (11-14 egress-new-dst lines: history reported as news, all of it inside the window the line itself quoted). The guard also lived *after* the flow rules had already produced findings, and it was skipped entirely for dry runs. - absorption now happens in on_batch and is keyed on 'seeded' alone, so it covers the cold start, every resume tick of a budget-limited seed, and --dry-run (history is not news in a dry run either); - each seeding tick explains its silence on stderr: 'seed: N tuple(s) absorbed, 0 reported; window Xh from <stamp>, N file(s)', including the still-pending count when the budget stopped the pass; - the warm-state archive pass gets the same one-line summary; - state.seed.absorbed records the count, and absorb() reports whether a fingerprint was new so the number is the baseline's growth, not a loop counter. tests/test_seed.py pins all four: cold start, dry-run cold start, resume tick while unseeded, and single-fire from a seeded state (the guard must not turn into a permanently silent sensor).
This commit is contained in:
parent
ba1bef3814
commit
bde343c75f
4 changed files with 244 additions and 48 deletions
124
tests/test_seed.py
Normal file
124
tests/test_seed.py
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
"""A seed is history, not news: it is absorbed, never announced.
|
||||
|
||||
Pins the acceptance of t_cf3e563a: a cold start and *every* resume tick of a
|
||||
budget-limited seed keep stdout empty (so the cron's delivery outcome is
|
||||
`suppressed`), `--dry-run` included — a dry run against an unseeded state must
|
||||
not look like the alert storm it used to print. Silence has to be explained,
|
||||
so each seeding tick writes one `seed: N tuple(s) absorbed, 0 reported` line
|
||||
with the window it actually covered.
|
||||
|
||||
The opposite failure is a permanently silent sensor, so the seeded-state
|
||||
single-fire test lives right next to these (test_archives.py pins it too).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import gzip
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from helpers import fixture_lines
|
||||
|
||||
from netsec_scan import __main__ as cli
|
||||
from netsec_scan import config, state as state_mod
|
||||
|
||||
LIVE = "flows-20261127T070000Z-1.jsonl"
|
||||
ARCHIVES = ("flows-20261127T060000Z-1.jsonl.gz",
|
||||
"flows-20261127T050000Z-1.jsonl.gz",
|
||||
"flows-20261127T040000Z-1.jsonl.gz")
|
||||
SEED_RE = r"seed: \d+ tuple\(s\) absorbed, 0 reported; window \S+ from 2026-11-27T04:00:00\+00:00"
|
||||
|
||||
|
||||
def _store(root: Path) -> None:
|
||||
lines = fixture_lines("egress-sample.jsonl")
|
||||
for name in ARCHIVES:
|
||||
with gzip.open(root / name, "wt", encoding="utf-8") as handle:
|
||||
handle.write("\n".join(lines) + "\n")
|
||||
(root / LIVE).write_text("\n".join(lines) + "\n")
|
||||
|
||||
|
||||
class TestSeedIsSilent(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.root = Path(self.tmp.name)
|
||||
self.flows = self.root / "flows"
|
||||
self.flows.mkdir()
|
||||
_store(self.flows)
|
||||
self.state = self.root / "state.json"
|
||||
self.reports = self.root / "reports"
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def tick(self, *extra: str):
|
||||
"""Returns (stdout, stderr) from one real main() run."""
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = cli.main(["--flows-dir", str(self.flows), "--no-prom",
|
||||
"--state", str(self.state), "--reports", str(self.reports),
|
||||
*extra])
|
||||
self.assertEqual(code, 0)
|
||||
return out.getvalue(), err.getvalue()
|
||||
|
||||
def test_cold_start_is_silent_and_names_the_absorbed_window(self):
|
||||
out, err = self.tick()
|
||||
self.assertEqual(out, "", "a cold start must not announce the retained window")
|
||||
self.assertRegex(err, SEED_RE)
|
||||
st = state_mod.load(self.state)
|
||||
self.assertTrue(st["seeded"])
|
||||
self.assertEqual(st["seed"]["absorbed"], len(st["seen"]))
|
||||
self.assertEqual(len(st["seen"]), len(st["egress_counts"]))
|
||||
self.assertGreater(len(st["seen"]), 0)
|
||||
self.assertEqual(st["observed_since"], "2026-11-27T04:00:00+00:00")
|
||||
|
||||
def test_dry_run_cold_start_is_silent_and_persists_nothing(self):
|
||||
out, err = self.tick("--dry-run")
|
||||
self.assertEqual(out, "", "a dry run over an unseeded state reads history, not news")
|
||||
self.assertIn("0 reported", err)
|
||||
self.assertIn("dry run: nothing persisted", err)
|
||||
self.assertFalse(self.state.exists())
|
||||
|
||||
def test_resume_tick_is_silent_while_still_unseeded(self):
|
||||
original = config.ARCHIVE_MAX_FILES
|
||||
config.ARCHIVE_MAX_FILES = 1 # 1 archive per tick, 3 archives
|
||||
try:
|
||||
out1, err1 = self.tick()
|
||||
st = state_mod.load(self.state)
|
||||
self.assertEqual(out1, "")
|
||||
self.assertFalse(st["seeded"], "a partial baseline is not a baseline")
|
||||
self.assertIn("still pending", err1)
|
||||
|
||||
out2, err2 = self.tick() # the tick t_cf3e563a was filed about
|
||||
st = state_mod.load(self.state)
|
||||
self.assertEqual(out2, "", "a resume tick must stay silent too")
|
||||
self.assertFalse(st["seeded"])
|
||||
self.assertIn("still pending", err2)
|
||||
|
||||
out3, _err3 = self.tick()
|
||||
self.assertEqual(out3, "")
|
||||
self.assertTrue(state_mod.load(self.state)["seeded"])
|
||||
finally:
|
||||
config.ARCHIVE_MAX_FILES = original
|
||||
|
||||
def test_seeded_state_still_fires_exactly_once(self):
|
||||
self.tick()
|
||||
obj = json.loads(fixture_lines("egress-sample.jsonl")[0])
|
||||
obj["flow"]["IP"]["destination"] = "203.0.113.77"
|
||||
proto = next(iter(obj["flow"]["l4"]))
|
||||
obj["flow"]["l4"][proto]["destination_port"] = 5000
|
||||
line = json.dumps(obj) + "\n"
|
||||
with open(self.flows / LIVE, "a", encoding="utf-8") as handle:
|
||||
handle.write(line)
|
||||
first, _err = self.tick()
|
||||
self.assertEqual(len(first.strip().splitlines()), 1, first)
|
||||
self.assertIn("203.0.113.77:5000/tcp", first)
|
||||
second, _err = self.tick()
|
||||
self.assertEqual(second, "", "a reported tuple does not repeat")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in a new issue