Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b36e129c7e |
6 changed files with 290 additions and 0 deletions
113
apps/base/netsec/collect.sh
Normal file
113
apps/base/netsec/collect.sh
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
#!/bin/sh
|
||||||
|
# hubble-flow-collector — long-lived, filtered, bounded capture of Hubble flows.
|
||||||
|
#
|
||||||
|
# Why a long-lived consumer: the Cilium agent's Hubble ring buffer holds ~4096
|
||||||
|
# events per type. At the measured ~150 flows/s that is ~28 s of history, so any
|
||||||
|
# polling design (cron + `hubble observe --last N`) loses almost everything
|
||||||
|
# between runs. This process stays attached to the flow API instead.
|
||||||
|
#
|
||||||
|
# What is written (see filter.awk): drop / policy-verdict / l7 events, plus
|
||||||
|
# trace events whose peer is outside the cluster CIDRs (to-world = the
|
||||||
|
# beacon/exfil signal). Intra-cluster trace ACK chatter is discarded.
|
||||||
|
#
|
||||||
|
# Bounds (hard, enforced here and not by a follow-up card):
|
||||||
|
# <= MAX_FILES files, <= MAX_TOTAL_BYTES total, oldest deleted first.
|
||||||
|
# MAX_FILES(6) * MAX_FILE_BYTES(150 MiB) = 900 MiB <= 1 GiB spec cap.
|
||||||
|
#
|
||||||
|
# Only /data (hostPath /var/log/netsec) is ever written. Logs go to stdout so
|
||||||
|
# they land in the container log, never on the node's disk.
|
||||||
|
set -u
|
||||||
|
|
||||||
|
DATA_DIR="${DATA_DIR:-/data}"
|
||||||
|
SOCK="${SOCK:-/var/run/cilium/hubble.sock}"
|
||||||
|
FILTER_AWK="${FILTER_AWK:-/scripts/filter.awk}"
|
||||||
|
MAX_FILE_BYTES="${MAX_FILE_BYTES:-157286400}" # 150 MiB
|
||||||
|
MAX_FILES="${MAX_FILES:-6}"
|
||||||
|
MAX_TOTAL_BYTES="${MAX_TOTAL_BYTES:-1006632960}" # 960 MiB (spec cap is 1 GiB)
|
||||||
|
RETRY_SECONDS="${RETRY_SECONDS:-5}"
|
||||||
|
CHECK_EVERY_LINES="${CHECK_EVERY_LINES:-200}"
|
||||||
|
|
||||||
|
log() { printf '%s collector: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"; }
|
||||||
|
|
||||||
|
mkdir -p "$DATA_DIR" || { log "cannot create $DATA_DIR"; exit 1; }
|
||||||
|
|
||||||
|
# Delete oldest files until the directory is inside both bounds.
|
||||||
|
# Newest first, keep while count <= MAX_FILES and running total <= MAX_TOTAL_BYTES.
|
||||||
|
prune() {
|
||||||
|
# Filenames are generated by this script and contain no spaces, so plain
|
||||||
|
# word splitting is safe. (Do not `set -f` here: GNU ls does not glob the
|
||||||
|
# pattern itself, so noglob would make this match nothing.)
|
||||||
|
files=$(ls -1t "$DATA_DIR"/flows-*.jsonl 2>/dev/null)
|
||||||
|
[ -n "$files" ] || return 0
|
||||||
|
n=0
|
||||||
|
total=0
|
||||||
|
over=0
|
||||||
|
for f in $files; do
|
||||||
|
n=$((n + 1))
|
||||||
|
if [ "$over" -eq 0 ]; then
|
||||||
|
sz=$(stat -c %s "$f" 2>/dev/null || echo 0)
|
||||||
|
total=$((total + sz))
|
||||||
|
if [ "$n" -gt "$MAX_FILES" ] || [ "$total" -gt "$MAX_TOTAL_BYTES" ]; then
|
||||||
|
over=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ "$over" -eq 1 ]; then
|
||||||
|
rm -f "$f" && log "pruned $f"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
prune
|
||||||
|
log "starting: dir=$DATA_DIR sock=$SOCK max_file=${MAX_FILE_BYTES}B max_files=$MAX_FILES max_total=${MAX_TOTAL_BYTES}B"
|
||||||
|
|
||||||
|
attempt=0
|
||||||
|
while :; do
|
||||||
|
attempt=$((attempt + 1))
|
||||||
|
stamp=$(date -u +%Y%m%dT%H%M%SZ)
|
||||||
|
seq=$attempt
|
||||||
|
cur="$DATA_DIR/flows-$stamp-$seq.jsonl"
|
||||||
|
log "attach attempt=$attempt file=$cur"
|
||||||
|
|
||||||
|
# --type is repeatable; filtering server-side keeps the 148/s trace bulk
|
||||||
|
# (all subtypes) off the wire. We only ask for trace:to-network, which is
|
||||||
|
# the packet-level "left the node toward a peer" half of the to-world test.
|
||||||
|
# stderr is left unredirected on purpose: it lands in the container log.
|
||||||
|
hubble observe \
|
||||||
|
--server "unix://$SOCK" \
|
||||||
|
--follow \
|
||||||
|
--output json \
|
||||||
|
--type drop \
|
||||||
|
--type policy-verdict \
|
||||||
|
--type l7 \
|
||||||
|
--type trace:to-network \
|
||||||
|
| awk -f "$FILTER_AWK" \
|
||||||
|
| {
|
||||||
|
lines=0
|
||||||
|
while IFS= read -r line; do
|
||||||
|
# Only JSON flows reach the file; anything else is printed to the
|
||||||
|
# container log instead of corrupting the JSONL.
|
||||||
|
case "$line" in
|
||||||
|
"{"*) ;;
|
||||||
|
*) printf '%s\n' "$line"; continue ;;
|
||||||
|
esac
|
||||||
|
printf '%s\n' "$line" >> "$cur"
|
||||||
|
lines=$((lines + 1))
|
||||||
|
if [ "$lines" -ge "$CHECK_EVERY_LINES" ]; then
|
||||||
|
lines=0
|
||||||
|
size=$(stat -c %s "$cur" 2>/dev/null || echo 0)
|
||||||
|
if [ "$size" -ge "$MAX_FILE_BYTES" ]; then
|
||||||
|
seq=$((seq + 1))
|
||||||
|
cur="$DATA_DIR/flows-$(date -u +%Y%m%dT%H%M%SZ)-$seq.jsonl"
|
||||||
|
log "rotated: $cur"
|
||||||
|
prune
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
log "stream ended ($cur)"
|
||||||
|
}
|
||||||
|
|
||||||
|
prune
|
||||||
|
log "reattaching in ${RETRY_SECONDS}s"
|
||||||
|
sleep "$RETRY_SECONDS"
|
||||||
|
done
|
||||||
100
apps/base/netsec/daemonset.yaml
Normal file
100
apps/base/netsec/daemonset.yaml
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: DaemonSet
|
||||||
|
metadata:
|
||||||
|
name: hubble-flow-collector
|
||||||
|
namespace: netsec
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: hubble-flow-collector
|
||||||
|
app.kubernetes.io/part-of: netsec
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/name: hubble-flow-collector
|
||||||
|
updateStrategy:
|
||||||
|
type: RollingUpdate
|
||||||
|
rollingUpdate:
|
||||||
|
maxUnavailable: 1
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/name: hubble-flow-collector
|
||||||
|
app.kubernetes.io/part-of: netsec
|
||||||
|
spec:
|
||||||
|
# Phase 1: siriussec only. It is the node that runs every public
|
||||||
|
# workload and the one whose egress/extrusion signal matters; rpi and
|
||||||
|
# the volatile linode node must not be load-bearing for this. Widening
|
||||||
|
# is a one-line change once the consumer (the netsec sensor) needs it.
|
||||||
|
nodeSelector:
|
||||||
|
kubernetes.io/hostname: siriussec
|
||||||
|
# Nothing here talks to the API server.
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
terminationGracePeriodSeconds: 10
|
||||||
|
containers:
|
||||||
|
- name: collector
|
||||||
|
# Exactly the image the Cilium agent on this node runs: same hubble
|
||||||
|
# binary, same protobuf, no version skew with the flow API. Never
|
||||||
|
# :latest, never a different minor.
|
||||||
|
image: quay.io/cilium/cilium:v1.20.1@sha256:ae9ea21f7427fe24bc6ea7247eb552157a1b0a431744045d3f641545ca71d11b
|
||||||
|
imagePullPolicy: IfNotPresent
|
||||||
|
command: ["/bin/sh", "/scripts/collect.sh"]
|
||||||
|
# hubble.sock on the host is `srw-rw---- root root` (0660, root-owned),
|
||||||
|
# so the container MUST run as uid 0 to connect to it. That is the only
|
||||||
|
# reason for root here: the mount set is one read-only unix socket plus
|
||||||
|
# one writable log directory, no capabilities, no API token, not
|
||||||
|
# privileged.
|
||||||
|
securityContext:
|
||||||
|
runAsUser: 0
|
||||||
|
runAsGroup: 0
|
||||||
|
allowPrivilegeEscalation: false
|
||||||
|
capabilities:
|
||||||
|
drop: ["ALL"]
|
||||||
|
seccompProfile:
|
||||||
|
type: RuntimeDefault
|
||||||
|
env:
|
||||||
|
- name: DATA_DIR
|
||||||
|
value: /data
|
||||||
|
- name: SOCK
|
||||||
|
value: /var/run/cilium/hubble.sock
|
||||||
|
# 6 files x 150 MiB = 900 MiB ceiling, inside the 1 GiB spec cap;
|
||||||
|
# MAX_TOTAL_BYTES is the second, independent bound.
|
||||||
|
- name: MAX_FILE_BYTES
|
||||||
|
value: "157286400"
|
||||||
|
- name: MAX_FILES
|
||||||
|
value: "6"
|
||||||
|
- name: MAX_TOTAL_BYTES
|
||||||
|
value: "1006632960"
|
||||||
|
resources:
|
||||||
|
# Honest and small: a `hubble observe --follow` client plus mawk at
|
||||||
|
# ~20 kept events/s. Requests keep it off the top of the eviction
|
||||||
|
# list on a node that has hit DiskPressure before; no CPU limit so
|
||||||
|
# a burst cannot throttle the writer, a memory limit so a leak
|
||||||
|
# cannot hurt etcd's neighbour.
|
||||||
|
requests:
|
||||||
|
cpu: 50m
|
||||||
|
memory: 64Mi
|
||||||
|
limits:
|
||||||
|
memory: 192Mi
|
||||||
|
volumeMounts:
|
||||||
|
# SINGLE FILE, never /var/run/cilium: that directory also holds
|
||||||
|
# cilium.sock, which is full datapath admin for the whole cluster.
|
||||||
|
- name: hubble-sock
|
||||||
|
mountPath: /var/run/cilium/hubble.sock
|
||||||
|
readOnly: true
|
||||||
|
- name: flow-log
|
||||||
|
mountPath: /data
|
||||||
|
- name: scripts
|
||||||
|
mountPath: /scripts
|
||||||
|
readOnly: true
|
||||||
|
volumes:
|
||||||
|
- name: hubble-sock
|
||||||
|
hostPath:
|
||||||
|
path: /var/run/cilium/hubble.sock
|
||||||
|
type: Socket
|
||||||
|
- name: flow-log
|
||||||
|
hostPath:
|
||||||
|
path: /var/log/netsec
|
||||||
|
type: DirectoryOrCreate
|
||||||
|
- name: scripts
|
||||||
|
configMap:
|
||||||
|
name: hubble-flow-collector-scripts
|
||||||
|
defaultMode: 0555
|
||||||
54
apps/base/netsec/filter.awk
Normal file
54
apps/base/netsec/filter.awk
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
# Post-filter for hubble-flow-collector.
|
||||||
|
#
|
||||||
|
# Input: one compact JSON flow per line from
|
||||||
|
# hubble observe --follow --output json --type drop --type policy-verdict
|
||||||
|
# --type l7 --type trace:to-network
|
||||||
|
# so every line is either a drop, a policy verdict, an L7 event, or a
|
||||||
|
# trace:to-network (L3_L4) event observed at the node's network egress point.
|
||||||
|
#
|
||||||
|
# Rule:
|
||||||
|
# drop / policy-verdict / l7 -> always keep (denial + protocol detail)
|
||||||
|
# trace:to-network, peer outside
|
||||||
|
# 10.244.0.0/16 and 10.10.0.0/24 -> keep (to-world: beacon / exfil signal)
|
||||||
|
# trace:to-network, both ends
|
||||||
|
# inside those CIDRs -> drop (intra-cluster ACK chatter)
|
||||||
|
#
|
||||||
|
# Every line is self-contained JSON. The top-level IP object is the only place
|
||||||
|
# with both endpoints: "IP":{"source":"a","destination":"b","ipVersion":"IPv4"}.
|
||||||
|
# Match that object FIRST and extract from the matched block — an unanchored
|
||||||
|
# '"source":"' search hits the ethernet MAC block that precedes IP on trace
|
||||||
|
# events ("ethernet":{"source":"98:fa:..."}), which silently defeated the
|
||||||
|
# intra-cluster test.
|
||||||
|
|
||||||
|
function getip(block, key, pat, blk) {
|
||||||
|
pat = "\"" key "\":\"[^\"]*\""
|
||||||
|
if (match(block, pat) == 0) return ""
|
||||||
|
blk = substr(block, RSTART, RLENGTH)
|
||||||
|
sub(/^[^:]*:"/, "", blk) # strip "key":"
|
||||||
|
sub(/"$/, "", blk) # strip trailing quote
|
||||||
|
return blk
|
||||||
|
}
|
||||||
|
|
||||||
|
function in_cluster(ip) {
|
||||||
|
return (ip ~ /^10\.244\./ || ip ~ /^10\.10\./)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
# Never pass through a non-JSON line (a stray stderr message must not end
|
||||||
|
# up in the JSONL); the writer prints it to the container log instead.
|
||||||
|
if ($0 !~ /^\{/) next
|
||||||
|
# Only trace events carry the "is this peer outside the cluster" question.
|
||||||
|
if ($0 !~ /"Type": ?"L3_L4"/) {
|
||||||
|
print
|
||||||
|
next
|
||||||
|
}
|
||||||
|
if (match($0, /"IP":\{[^}]*\}/) == 0) next
|
||||||
|
ipobj = substr($0, RSTART, RLENGTH)
|
||||||
|
s = getip(ipobj, "source")
|
||||||
|
d = getip(ipobj, "destination")
|
||||||
|
# Non-IP trace (ARP and friends) at to-network has no peer to judge; it is
|
||||||
|
# never the beacon signal, so it is not written.
|
||||||
|
if (s == "" || d == "") next
|
||||||
|
if (in_cluster(s) && in_cluster(d)) next
|
||||||
|
print
|
||||||
|
}
|
||||||
16
apps/base/netsec/kustomization.yaml
Normal file
16
apps/base/netsec/kustomization.yaml
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
resources:
|
||||||
|
- namespace.yaml
|
||||||
|
- daemonset.yaml
|
||||||
|
# The collector script and the post-filter ship as files (not inline YAML) so
|
||||||
|
# they stay readable and lintable; kustomize rewrites the DS volume to the
|
||||||
|
# hashed name, so a script change rolls the DaemonSet.
|
||||||
|
configMapGenerator:
|
||||||
|
- name: hubble-flow-collector-scripts
|
||||||
|
namespace: netsec
|
||||||
|
files:
|
||||||
|
- collect.sh
|
||||||
|
- filter.awk
|
||||||
|
generatorOptions:
|
||||||
|
disableNameSuffixHash: false
|
||||||
6
apps/base/netsec/namespace.yaml
Normal file
6
apps/base/netsec/namespace.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Namespace
|
||||||
|
metadata:
|
||||||
|
name: netsec
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/part-of: netsec
|
||||||
|
|
@ -8,3 +8,4 @@ resources:
|
||||||
- ../base/osint
|
- ../base/osint
|
||||||
- ../base/metabase
|
- ../base/metabase
|
||||||
- ../base/customer1
|
- ../base/customer1
|
||||||
|
- ../base/netsec
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue