From fef71a280a7603e736597b32aa81f7bd0570b015 Mon Sep 17 00:00:00 2001 From: Lance Walters Date: Fri, 27 Nov 2026 00:49:07 -0500 Subject: [PATCH] infra(logging): ship kube-apiserver audit log with secret bodies stripped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit t_104f62a6 made the apiserver audit log live on siriussec: /var/log/kubernetes/audit/audit.log, mode 600 root, ~2.2 GB/day, rotated by the apiserver itself (100 MB x 48, compressed). A pod-log collector never sees a host file, so Alloy gets a read-only hostPath mount for that directory. The file MUST NOT be shipped verbatim: at level=RequestResponse every 'get secrets' record carries responseObject.data — the plaintext secret. The shipper therefore parses each record and re-emits a packed allowlist (level, stage, verb, user, objectRef.*, responseStatus.code, sourceIPs[0], userAgent, auditID, requestURI, requestReceivedTimestamp). requestObject and responseObject never reach Loki; unparseable lines are dropped by stage.json rather than passed through. Nothing in that directory is written or pruned by the collector: the apiserver's own 4.8 GB / 7-day rotation stays the only thing pruning it. --- infrastructure/logging/alloy.yaml | 10 +++ infrastructure/logging/alloy/config.alloy | 100 ++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/infrastructure/logging/alloy.yaml b/infrastructure/logging/alloy.yaml index c421ada..c1fa90a 100644 --- a/infrastructure/logging/alloy.yaml +++ b/infrastructure/logging/alloy.yaml @@ -83,6 +83,12 @@ spec: - name: dockerlogs mountPath: /var/lib/docker/containers readOnly: true + # kube-apiserver audit log (siriussec only). Read-only: the + # apiserver owns that directory and its rotation; the collector + # never writes to or prunes it. + - name: audit + mountPath: /var/log/kubernetes/audit + readOnly: true # NOT /var/lib/alloy/...: that path is 0770 alloy:alloy in the # image, and this pod drops ALL capabilities (so root has no # CAP_DAC_OVERRIDE) — traversal of the parent is denied. /var/lib @@ -114,6 +120,10 @@ spec: hostPath: path: /var/lib/docker/containers type: DirectoryOrCreate + - name: audit + hostPath: + path: /var/log/kubernetes/audit + type: DirectoryOrCreate - name: data hostPath: path: /var/lib/alloy-data diff --git a/infrastructure/logging/alloy/config.alloy b/infrastructure/logging/alloy/config.alloy index b4b4630..8cc99af 100644 --- a/infrastructure/logging/alloy/config.alloy +++ b/infrastructure/logging/alloy/config.alloy @@ -190,6 +190,106 @@ loki.source.file "docker_logs" { tail_from_end = false } +// ------------------------------------------- kube-apiserver audit log (host file) +// siriussec only. The apiserver writes JSON audit records to +// /var/log/kubernetes/audit/audit.log (mode 600 root; the apiserver rotates it +// itself: 100 MB x 48, compressed, ~2.2 GB/day). This is a host file, not pod +// output, hence the extra hostPath mount in alloy.yaml. +// +// SECURITY — this source does NOT forward records as-is. At +// `level: RequestResponse` the record for a `get secrets` carries +// `responseObject.data`: the plaintext secret value. Shipping verbatim would +// copy every secret that has been read into a second queryable system. Instead +// the record is parsed and re-emitted as a packed *allowlist* of fields, so +// `requestObject` / `responseObject` never reach Loki. `stage.json` drops any +// line it cannot parse, so a malformed record cannot fall through to raw bytes. +// (Filter is deliberately unconditional — stricter than "strip only for +// objectRef.resource == secrets". If audit bodies for *writes* are ever wanted, +// that is a separate, reviewed change.) +// +// Only the live file plus the rotated `.gz` archives are read; nothing in this +// directory is ever deleted by the collector, so the apiserver's own 4.8 GB / +// 7-day rotation stays the only thing pruning it. Loki's 192h retention is +// independent and applies to the *stored copies*. +local.file_match "audit_live" { + path_targets = [{ + "__path__" = "/var/log/kubernetes/audit/audit.log", + job = "kube/apiserver-audit", + node = sys.env("NODE_NAME"), + }] + sync_period = "10s" +} + +local.file_match "audit_rotated" { + path_targets = [{ + "__path__" = "/var/log/kubernetes/audit/*.log.gz", + job = "kube/apiserver-audit", + node = sys.env("NODE_NAME"), + }] + sync_period = "1m" +} + +loki.source.file "audit_live" { + targets = local.file_match.audit_live.targets + forward_to = [loki.process.audit.receiver] + tail_from_end = false +} + +loki.source.file "audit_rotated" { + targets = local.file_match.audit_rotated.targets + forward_to = [loki.process.audit.receiver] + tail_from_end = false + + decompression { + enabled = true + format = "gz" + } +} + +loki.process "audit" { + // Field allowlist. Everything not listed here — in particular + // requestObject/responseObject — is discarded with the parsed record. + stage.json { + expressions = { + level = "level", + stage = "stage", + verb = "verb", + user = "user.username", + resource = "objectRef.resource", + apiGroup = "objectRef.apiGroup", + ns = "objectRef.namespace", + name = "objectRef.name", + subresource = "objectRef.subresource", + responseCode = "responseStatus.code", + sourceIP = "sourceIPs.0", + userAgent = "userAgent", + auditID = "auditID", + requestURI = "requestURI", + time = "requestReceivedTimestamp", + } + } + + stage.timestamp { + source = "time" + format = "RFC3339Nano" + } + + // verb x resource is small and fixed; `user` and the object name stay in the + // packed body so they do not multiply the stream count. + stage.labels { + values = { + verb = "verb", + resource = "resource", + } + } + + stage.pack { + labels = [] + } + + forward_to = [loki.write.local.receiver] +} + loki.process "docker_logs" { stage.json { expressions = {