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
114
tests/test_flows.py
Normal file
114
tests/test_flows.py
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
"""Parser tests: workload attribution, egress classification, chunk handling."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from helpers import fixture_flows, fixture_lines
|
||||
|
||||
from netsec_scan.flows import (
|
||||
RemoteFlowStore,
|
||||
is_external,
|
||||
iter_lines,
|
||||
parse_flow,
|
||||
workload_name,
|
||||
)
|
||||
|
||||
|
||||
class TestIterLines(unittest.TestCase):
|
||||
def test_partial_tail_is_carried_not_consumed(self):
|
||||
lines, partial = iter_lines(b'{"a":1}\n{"b":2}\n{"c":')
|
||||
self.assertEqual(lines, ['{"a":1}', '{"b":2}'])
|
||||
self.assertEqual(partial, b'{"c":')
|
||||
|
||||
def test_no_newline_yet(self):
|
||||
lines, partial = iter_lines(b'{"a":1}')
|
||||
self.assertEqual(lines, [])
|
||||
self.assertEqual(partial, b'{"a":1}')
|
||||
|
||||
def test_empty(self):
|
||||
self.assertEqual(iter_lines(b""), ([], b""))
|
||||
|
||||
|
||||
class TestEgressClassification(unittest.TestCase):
|
||||
def test_external_only_global_unicast(self):
|
||||
for ip in ("203.0.113.9", "172.67.209.185", "2606:4700::1111"):
|
||||
self.assertTrue(is_external(ip), ip)
|
||||
for ip in ("10.244.0.5", "10.10.0.2", "192.168.1.179", "127.0.0.1",
|
||||
"fe80::1", "ff02::2", "100.100.100.100", "10.96.0.1"):
|
||||
self.assertFalse(is_external(ip), ip)
|
||||
|
||||
def test_no_ip_is_not_external(self):
|
||||
self.assertFalse(is_external(None))
|
||||
self.assertFalse(is_external(""))
|
||||
|
||||
|
||||
class TestWorkloadAttribution(unittest.TestCase):
|
||||
def test_reserved_identity_has_no_workload(self):
|
||||
self.assertIsNone(workload_name({"identity": 1, "labels": ["reserved:host"]}))
|
||||
self.assertIsNone(workload_name(None))
|
||||
|
||||
def test_pod_name_change_does_not_change_identity(self):
|
||||
a = {"namespace": "osint",
|
||||
"pod_name": "osint-app-7d9f8c6b5-abcde",
|
||||
"labels": ["k8s:app=osint-app"]}
|
||||
b = {"namespace": "osint",
|
||||
"pod_name": "osint-app-5bb4d7f9c-xyzwq",
|
||||
"labels": ["k8s:app=osint-app"]}
|
||||
self.assertEqual(workload_name(a), "osint/osint-app")
|
||||
self.assertEqual(workload_name(a), workload_name(b))
|
||||
|
||||
def test_deployment_workload_wins_over_label(self):
|
||||
peer = {"namespace": "flux-system", "pod_name": "source-controller-abc12-def34",
|
||||
"labels": ["k8s:app=source-controller"],
|
||||
"workloads": [{"name": "source-controller", "kind": "Deployment"}]}
|
||||
self.assertEqual(workload_name(peer), "flux-system/source-controller")
|
||||
|
||||
def test_pod_name_fallback_strips_hashes(self):
|
||||
peer = {"namespace": "x", "pod_name": "web-7d9f8c6b5-abcde"}
|
||||
self.assertEqual(workload_name(peer), "x/web")
|
||||
|
||||
|
||||
class TestRealFixtures(unittest.TestCase):
|
||||
def test_egress_fixture_parses_and_keeps_only_workload_egress(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
self.assertGreaterEqual(len(flows), 5)
|
||||
external = [f for f in flows if f.src_workload and is_external(f.dst_ip)]
|
||||
# 4 workload->public 443 lines + 1 :80 line; the reserved:host and LAN
|
||||
# lines must not qualify.
|
||||
self.assertGreaterEqual(len(external), 4)
|
||||
self.assertTrue(all(f.dst_port for f in external))
|
||||
self.assertTrue(all(f.src_workload for f in external))
|
||||
|
||||
def test_reserved_host_egress_is_not_attributed_to_a_workload(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
host_flows = [f for f in flows if not f.src_workload and is_external(f.dst_ip)]
|
||||
self.assertTrue(host_flows, "fixture should contain a reserved:host to-world flow")
|
||||
|
||||
def test_l7_and_drop_fixture_parses(self):
|
||||
flows = fixture_flows("l7-and-drop.jsonl")
|
||||
self.assertTrue(any(f.type == "L7" for f in flows))
|
||||
self.assertTrue(any(f.verdict == "DROPPED" for f in flows))
|
||||
|
||||
def test_garbage_line_is_skipped(self):
|
||||
self.assertIsNone(parse_flow({"flow": None}))
|
||||
self.assertIsNone(parse_flow({}))
|
||||
self.assertIsNone(parse_flow({"flow": {"Type": "L3_L4"}}))
|
||||
|
||||
|
||||
class TestRemoteStoreCommand(unittest.TestCase):
|
||||
def test_list_command_does_not_quote_the_glob(self):
|
||||
"""A quoted glob is literal, and stat then fails with 'No such file'."""
|
||||
cmd = "sudo -n find %s -maxdepth 1 -type f -name %s -printf '%%f %%s\\n'" % (
|
||||
"'/var/log/netsec'", "'flows-*.jsonl'")
|
||||
self.assertIn("flows-*.jsonl", cmd)
|
||||
self.assertNotIn("'/var/log/netsec/flows-*.jsonl'", cmd)
|
||||
|
||||
def test_remote_store_defaults(self):
|
||||
store = RemoteFlowStore()
|
||||
self.assertIn("@", store.host)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in a new issue