Enable Kubernetes API Server Audit Logging and Read the Trail
Scenario
A security review found that the cluster keeps no record of API activity. Nobody can say who changed a Deployment last week or which workloads read which Secrets.
The invoice-sync Deployment in the paygate namespace talks to the API server with its own ServiceAccount. The security team wants the audit trail switched on first, and then a quick answer about what that workload actually reads.
The cluster was built with kubeadm, so the API server runs as a static Pod on cplane-01.
Task
- Write an audit policy at
/etc/kubernetes/audit/policy.yamlthat:- Does not record the
RequestReceivedstage. - Records requests on Secrets with metadata only, so that no Secret value is ever written to the log.
- Records requests on Deployments in the
paygatenamespace with their full request and response bodies. - Does not record requests on Leases (API group
coordination.k8s.io) at all. - Records every other request with metadata only.
- Does not record the
- Enable auditing in the API server with that policy. Write the log to
/var/log/kubernetes/audit/audit.log, keep rotated files for at most7days, keep at most3rotated files, and rotate the file when it reaches50MB. - Find out from the audit log which Secret the
invoice-syncServiceAccount reads successfully, and write only its name to/home/laborant/answers/secret-read.txt.
Work from cplane-01. The policy, the static Pod manifests and the audit log all belong to root, so use sudo to read or change them.
While the API server restarts, kubectl returns connection errors. That is expected. If it is still unreachable after a couple of minutes, the new configuration is broken: check the container logs on the node.
Hint 1: The shape of an audit policy
A policy is a list of rules. For each request the API server uses the first rule that matches, so order the rules from the most specific to the most general:
apiVersion: audit.k8s.io/v1
kind: Policy
omitStages:
- "<stage>"
rules:
- level: <None | Metadata | Request | RequestResponse>
namespaces: ["<namespace>"] # optional
resources:
- group: "<api-group>" # "" is the core group
resources: ["<resource>"]
- level: <level> # a rule with no filters matches everything
See: Auditing
Hint 2: Adding the policy to a static Pod
The kubelet watches /etc/kubernetes/manifests and recreates a static Pod whenever its file changes. Back up the manifest outside that directory before editing it.
The API server runs in a container, so it only sees host files that are mounted into it. Every host path a flag points to needs a matching volume and mount:
spec:
containers:
- command:
- kube-apiserver
- --<flag>=<container-path>
volumeMounts:
- name: <name>
mountPath: <container-path>
readOnly: <true | false>
volumes:
- name: <name>
hostPath:
path: <host-path>
type: <File | DirectoryOrCreate>
The audit flags all start with --audit-. List them with:
sudo crictl exec $(sudo crictl ps --name kube-apiserver -q) kube-apiserver --help | grep -- --audit-log
Hint 3: When the API server does not come back
kubectl cannot help while the API server is down. Ask the container runtime instead:
sudo crictl ps -a --name kube-apiserver
sudo crictl logs <container-id>
sudo journalctl -u kubelet --since "5 min ago" | tail -20
A typo in the policy, a flag pointing to an unmounted path, or invalid YAML in the manifest are the usual causes.
Hint 4: Reading the audit log
Each line is one JSON event. Pretty-print the newest one to see its fields:
sudo tail -n 1 /var/log/kubernetes/audit/audit.log | jq
Filter with jq on the fields that answer who, what and with which result:
sudo jq -c 'select(.user.username == "<username>") | [.verb, .objectRef.resource, .objectRef.name, .responseStatus.code]' /var/log/kubernetes/audit/audit.log