netsec-scan: deterministic sensor (6 PromQL tripwires + 2 flow-store novelty rules)
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.
This commit is contained in:
commit
68b2616cc5
19 changed files with 1902 additions and 0 deletions
47
netsec_scan/report.py
Normal file
47
netsec_scan/report.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""Daily baseline report — no alert, just the numbers thresholds get tuned from.
|
||||
|
||||
Written to `reports/YYYY-MM-DD.json` on every tick, so the file for "today"
|
||||
always exists and carries real counts; it becomes final at the UTC day
|
||||
rollover when a new file starts.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
def build(state: Dict[str, Any]) -> Dict[str, Any]:
|
||||
day = state.get("day") or {}
|
||||
egress = day.get("egress", [])
|
||||
new_dsts = day.get("new_dsts", [])
|
||||
rules = day.get("rules", {})
|
||||
flows = int(day.get("flows", 0))
|
||||
return {
|
||||
"date": day.get("date"),
|
||||
"generated_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
||||
"flows": flows,
|
||||
"findings_emitted": {k: int(v) for k, v in sorted(rules.items())},
|
||||
"findings_total": sum(int(v) for v in rules.values() if str(v).isdigit()),
|
||||
"egress_tuples_seen": len(egress),
|
||||
"egress_tuples": sorted(egress),
|
||||
"top_new_destinations": new_dsts[-10:],
|
||||
"seen_fingerprints_cumulative": len(state.get("seen", {})),
|
||||
"state_totals": state.get("totals", {}),
|
||||
}
|
||||
|
||||
|
||||
def write(state: Dict[str, Any], directory: Path = None) -> Path:
|
||||
directory = Path(directory or config.REPORT_DIR)
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
day = (state.get("day") or {}).get("date") or datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
||||
path = directory / f"{day}.json"
|
||||
tmp = path.with_suffix(".json.tmp")
|
||||
tmp.write_text(json.dumps(build(state), indent=2, sort_keys=True) + "\n")
|
||||
os.replace(tmp, path)
|
||||
return path
|
||||
Loading…
Reference in a new issue