infra(logging): ship kube-apiserver audit log with secret bodies stripped

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.
This commit is contained in:
Lance Walters 2026-11-27 00:49:07 -05:00
commit fef71a280a
2 changed files with 110 additions and 0 deletions

View file

@ -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

View file

@ -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 = {