netsec-scan: read flows-*.jsonl.gz so a cold start baselines the retained window, not the live file

The collector gzips on rotation, so FLOW_PATTERN 'flows-*.jsonl' saw 1 of 5
files (measured: 1/72 of the retained window). Archives are immutable, so each
is read once, whole, decrypted lazily and recorded in state['archives'].

- archives are bounded (ARCHIVE_MAX_SECONDS/BYTES/FILES, newest first) and
  flows are consumed in batches; a partial pass leaves state seeded=false and
  reports the unread files as pending, so the next tick continues instead of
  alerting from a window it has not examined
- the alert window comes from the coverage start in the consumed filenames
  (monotone), so 'no prior in ~Xh of observed flows' is now verifiable
- the seed absorbs every egress tuple in the window, not just findings (a
  web-port tuple never becomes one on its first tick); a warm state meeting
  archives for the first time absorbs them the same way
- a torn .gz keeps its readable prefix, is noted in state/seed.failed, and does
  not abort the tick; in-flight *.gz.tmp.* markers are never read
- report.json carries window_h/covered_since/seed; 12 new tests
This commit is contained in:
Sirius DevOps 2026-11-27 02:09:51 -05:00
commit ba1bef3814
8 changed files with 690 additions and 98 deletions

View file

@ -32,9 +32,31 @@ SSH_HOST = os.environ.get("NETSEC_SCAN_SSH_HOST", "[email protected]")
SSH_KEY = os.environ.get("NETSEC_SCAN_SSH_KEY", str(HOME / ".ssh" / "lancelot_priv"))
SSH_TIMEOUT = int(os.environ.get("NETSEC_SCAN_SSH_TIMEOUT", "60"))
FLOW_DIR = os.environ.get("NETSEC_SCAN_FLOW_DIR", "/var/log/netsec")
FLOW_PATTERN = os.environ.get("NETSEC_SCAN_FLOW_PATTERN", "flows-*.jsonl")
# The collector gzips a file the moment it rotates (t_cfea18ac), so the store is
# `flows-<stamp>-<n>.jsonl` (the live file) plus `flows-<stamp>-<n>.jsonl.gz`
# (closed, immutable). Without the trailing `*` the sensor can only see the live
# file — i.e. minutes of the ~74 h the 960 MiB cap actually retains.
FLOW_PATTERN = os.environ.get("NETSEC_SCAN_FLOW_PATTERN", "flows-*.jsonl*")
FLOW_GLOB = FLOW_DIR + "/" + FLOW_PATTERN
# ------------------------------------------------- archival seed budget ----
# Archives are read once, whole, and only at cold start / right after a
# rotation. Unbounded is not an option: 960 MiB of gzip is ~16 GB of JSON at the
# measured 17.5x ratio, and holding that as Flow objects would exhaust memory
# long before it finished. Measured on the admin host 2026-11-27: json.loads +
# parse_flow + egress-key extraction runs at ~140 MB/s of decompressed JSON, so
# these defaults are sized for a one-time seed of ~12 GB (~55 h of current
# volume) and a warm tick, which reads zero or one archive, finishes in ~1 s.
# Whichever budget binds first wins; the window actually covered is printed and
# persisted, never implied.
ARCHIVE_MAX_SECONDS = float(os.environ.get("NETSEC_SCAN_ARCHIVE_SECONDS", "90"))
ARCHIVE_MAX_BYTES = int(os.environ.get("NETSEC_SCAN_ARCHIVE_BYTES", str(8 * 1024 ** 3)))
ARCHIVE_MAX_FILES = int(os.environ.get("NETSEC_SCAN_ARCHIVE_FILES", "40"))
# Flows are consumed in batches so a seed never holds the whole window in RAM as
# Flow objects (16 GB of JSON is millions of them).
FLOW_BATCH = int(os.environ.get("NETSEC_SCAN_FLOW_BATCH", "20000"))
# ----------------------------------------------------- prometheus ----
PROM_PROXY = (
"/api/v1/namespaces/monitoring/services/"