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:
parent
f4001d589e
commit
ba1bef3814
8 changed files with 690 additions and 98 deletions
96
README.md
96
README.md
|
|
@ -70,7 +70,21 @@ count: the same finding keeps the same id across ticks, restarts and days.
|
|||
and broke again" is not silently forgotten.
|
||||
* First run is a **baseline run**: every novelty finding in the retained window
|
||||
is recorded into `seen` silently instead of dumping weeks of history into
|
||||
chat. PromQL findings still speak on the first tick — they are current state.
|
||||
chat — and so is **every distinct egress tuple** in that window, not only the
|
||||
ones that became findings (a web-port tuple never becomes one on its first
|
||||
tick). PromQL findings still speak on the first tick — they are current state.
|
||||
* A **warm** state that has never read the gzip archives (i.e. one upgraded to a
|
||||
gzipped store) absorbs its first archive pass the same way: announcing it would
|
||||
print `first-seen … (no prior in ~Xh of observed flows)` for a tuple that is
|
||||
demonstrably inside those X hours. `archive_baseline: true` in state means
|
||||
that pass has happened.
|
||||
* Baseline reads are **bounded** (`ARCHIVE_MAX_SECONDS` / `ARCHIVE_MAX_BYTES` /
|
||||
`ARCHIVE_MAX_FILES`). If the budget stops the pass, the state stays
|
||||
`seeded: false` and the unread archives are listed as `pending` in state and
|
||||
in the verbose line, so the next tick continues the baseline instead of
|
||||
alerting from a window that is still being filled. The window quoted in the
|
||||
alert (`window_h`, derived from the coverage start in the filenames actually
|
||||
consumed) grows tick by tick until the pass completes.
|
||||
|
||||
## The 8 rules
|
||||
|
||||
|
|
@ -143,8 +157,9 @@ promq 'count by (port) (hubble_port_distribution_total)'
|
|||
promq 'sum(rate(hubble_http_requests_total[15m]))' # rule 6
|
||||
promq 'sum(rate(hubble_http_requests_total{status=~"4..|5.."}[15m]))' # rule 6
|
||||
|
||||
# rules 7-8: what the flow store says this window
|
||||
ssh -i ~/.ssh/lancelot_priv Lancelot@10.10.0.2 'sudo -n cat /var/log/netsec/flows-*.jsonl' \
|
||||
# rules 7-8: what the flow store says this window (archives are gzip+immutable)
|
||||
ssh -i ~/.ssh/lancelot_priv Lancelot@10.10.0.2 \
|
||||
'sudo -n sh -c "zcat -f /var/log/netsec/flows-*.jsonl /var/log/netsec/flows-*.jsonl.gz"' \
|
||||
| python3 -c 'import sys,json,collections
|
||||
c=collections.Counter()
|
||||
for l in sys.stdin:
|
||||
|
|
@ -165,18 +180,42 @@ UTC day rollover. No alert is ever raised from it.
|
|||
* **Flows**: the `netsec/hubble-flow-collector` DaemonSet on siriussec writes
|
||||
filtered JSONL to the host's `/var/log/netsec` (drops, policy verdicts, all L7,
|
||||
and `trace:to-network` whose peer is outside `10.244.0.0/16` + `10.10.0.0/24`).
|
||||
This sensor reads it over SSH as `[email protected]` with `sudo -n`, using a
|
||||
**per-file byte offset** kept in state — a tick pulls only new bytes. It never
|
||||
This sensor reads it over SSH as `[email protected]` with `sudo -n`. It never
|
||||
polls `hubble observe`: the agent ring buffer is ~28 s at 148 flows/s, so a
|
||||
poll is lossy by design.
|
||||
* **The store is two shapes and both are read.** The collector gzips a file the
|
||||
moment it rotates, so `flows-<UTCstamp>-<n>.jsonl` is the single live file and
|
||||
every closed file is `….jsonl.gz`. `FLOW_PATTERN` is `flows-*.jsonl*`:
|
||||
* plain files are appended to → read from a **per-file byte offset**, so a
|
||||
tick pulls only new bytes (a trailing partial line is carried in state);
|
||||
* archives are **immutable** → read once, whole, `gzip.open(…, "rt")`, and
|
||||
recorded in `state["archives"]` so they are never re-read. Reading only
|
||||
`*.jsonl` would leave every rotated file invisible: measured 2026-11-27,
|
||||
`find -name 'flows-*.jsonl'` = **1** file, `find -name 'flows-*.jsonl*'` =
|
||||
**5**, i.e. 1/72 of the retained window.
|
||||
* `*.jsonl*` also matches the collector's in-flight `…jsonl.gz.tmp.<pid>`
|
||||
marker; those are filtered out by name, never read.
|
||||
* **Archive reads are bounded** (`config.ARCHIVE_MAX_*`): newest first, stop when
|
||||
the time/byte/file budget is spent, leave the rest unread and reported as
|
||||
pending. Unbounded is not an option — the 960 MiB cap is ~16 GB of JSON at the
|
||||
measured 17.5× ratio, and holding it as `Flow` objects would exhaust memory
|
||||
long before it finished, so flows are handed to the rules in batches
|
||||
(`FLOW_BATCH`). Measured throughput on the admin host: **~140 MB/s** of
|
||||
decompressed JSON through `json.loads` + `parse_flow` + egress-key extraction.
|
||||
* **A torn archive costs a note, not the tick.** A `.gz` that cannot be
|
||||
decompressed to the end (`EOFError: Compressed file ended before the
|
||||
end-of-stream marker was reached`) yields the readable prefix, records
|
||||
`archives[name].error`, is listed in `seed.failed`, and is not retried until
|
||||
its size changes.
|
||||
* **Measured volume**: 41 MB in ~20 min at collector start (the first file
|
||||
carries a 37.5 MB startup burst), lower in steady state, against a 1 GiB cap —
|
||||
so the file window is **days, not the month the design note assumed**. A
|
||||
"not seen before" judgement therefore comes from this sensor's own cumulative
|
||||
seen-set (which outlives the files), and the alert says how long that window
|
||||
actually is rather than implying a longer baseline.
|
||||
* Only `*.jsonl` counts toward the volume; rotation tombstones in the directory
|
||||
are ignored. A partial final line in a file that is still being appended is
|
||||
carries a 37.5 MB startup burst), lower in steady state; ~226 MB/h raw →
|
||||
~310 MB/day compressed → 960 MiB ≈ **74 h**. A "not seen before" judgement
|
||||
therefore comes from this sensor's own cumulative seen-set (which outlives the
|
||||
files) *plus* the retained window it has actually consumed, and the alert says
|
||||
which (`window_h`).
|
||||
* Only collector files (`flows-*.jsonl`, `flows-*.jsonl.gz`) are read; a
|
||||
rotation tombstone or an in-flight `…gz.tmp.<pid>` in the directory is
|
||||
ignored. A partial final line in a file that is still being appended is
|
||||
**normal**: the parser keeps the incomplete tail and re-reads it next tick
|
||||
from the last complete newline (a `JSONDecodeError` on that tail is expected,
|
||||
not corruption).
|
||||
|
|
@ -214,12 +253,33 @@ credentials, no LLM call, no secret reads.
|
|||
|
||||
## Verified behaviour (2026-11-27)
|
||||
|
||||
* 43 unit tests pass against recorded fixtures.
|
||||
* Baseline tick seeded the retained window silently (25 fingerprints, 21 beacon
|
||||
buckets), wrote the state file and `reports/2026-11-27.json`; the immediately
|
||||
following tick was empty with exit 0 **and a changed state mtime**.
|
||||
* Single-fire: deleting one fingerprint from `seen` re-emitted exactly one line
|
||||
naming that tuple, and the next run was silent; the state was then restored.
|
||||
* 56 unit tests pass against recorded fixtures (`tests/test_archives.py` covers
|
||||
the gz path: read-once, torn archive, budget/pending, cold seed, warm-state
|
||||
archive absorption, single-fire).
|
||||
* **Cold start on the live store** (state moved aside, `--no-prom --verbose`):
|
||||
`5 file(s) (4 archive(s), 1 plain), 150225 flows, 116 tuple(s), 116
|
||||
fingerprint(s), window 2.06h, 7.34s`, **stdout 0 bytes**, exit 0. The window is
|
||||
the retained one (the collector was restarted onto the gzip layout at 05:04Z,
|
||||
so 2.06 h is all that exists today; the same pass scales to the full 74 h cap).
|
||||
* **Warm tick**: 0.34 s / 0.50 s over two runs (before the change: 0.28 s /
|
||||
0.32 s on identical state), stdout empty, exit 0 — archives are not re-read
|
||||
(`state["archives"][name].read_at` unchanged).
|
||||
* **Single fire, live, over SSH** against an isolated copy of the store: a
|
||||
synthetic `203.0.113.77:5000/tcp` line fired exactly one line
|
||||
(`HIGH … egress-new-dst … (no prior in ~2.1h of observed flows)`) and the next
|
||||
two ticks were empty; deleting that `seen` entry re-armed exactly one more
|
||||
line, then silence.
|
||||
* **Torn archive**: a `.gz` truncated mid-file was noted
|
||||
(`EOFError: Compressed file ended before the end-of-stream marker was
|
||||
reached`, prefix of 2628 lines kept), the tick exited 0, and the next tick was
|
||||
silent.
|
||||
* **Budget**: `ARCHIVE_MAX_FILES=1` against 3 archives → `pending 2`, `seeded
|
||||
false`, window 1.14 h; the next tick `pending 1`, 1.98 h; the third completes
|
||||
at 2.14 h and seeds. The alert window grows with the evidence instead of
|
||||
claiming the full retention up front.
|
||||
* Baseline tick seeded the retained window silently and wrote the state file and
|
||||
`reports/2026-11-27.json` (which now carries `window_h`, `covered_since`,
|
||||
`seed`, and per-shape file counts).
|
||||
* Cron delivery proven through `~/.hermes/cron/executions.db`
|
||||
(`delivery_outcome=delivered`) — see the task handoff for the transcript.
|
||||
* Live tripwire values at commissioning: hubble-loss HIGH on siriussec (0.42-0.60
|
||||
|
|
|
|||
Loading…
Reference in a new issue