Trace Secret Reads in an Audit Log and Rotate Only the Exposed Credentials
Scenario
The credstore namespace holds five Opaque Secrets with production credentials: database, payment webhook, mail relay, archive storage and directory bind accounts.
A post-incident review found that the insight-bot ServiceAccount in the analytics namespace, which only exists to read report settings, could read Secrets in credstore for part of a day. It should never have had access to any Secret in that namespace. Nobody knows yet which credentials it actually saw.
The investigation team exported the API server audit log for that day to /home/laborant/incident/audit.log. The bindings that caused the exposure have not been touched yet.
Task
- Find out which Secrets in the
credstorenamespace theinsight-botServiceAccount actually read, using the audit log at/home/laborant/incident/audit.log. - Rotate the exposed credentials: change the
passwordkey of only those Secrets to any new value. Keep theirusernamekey as it is, and leave every other Secret incredstoreuntouched. - Revoke the access: make sure
insight-botcan no longerget,listorwatchSecrets incredstore. It must keep its read access to ConfigMaps inanalytics, and thebackup-agentServiceAccount must keep its access to Secrets incredstore.
Work from cplane-01. The audit log is a historical export: it shows what happened that day, not the current state of the cluster.
Do not delete or recreate any Secret or ServiceAccount. Rotating a credential that was never exposed is a production change with its own risk, so a Secret that does not need a new password must keep its current one.
Hint 1 | Reading an audit event
The audit log holds one JSON object per line. These fields describe a single request:
.user.username who sent it
.verb get, list, watch, create, ...
.objectRef.resource secrets, configmaps, ...
.objectRef.namespace namespace of the object
.objectRef.name object name (absent for list and watch)
.responseStatus.code what the API server answered
jq can filter on any of them:
jq -c 'select(.user.username == "<username>") | [.verb, .objectRef.resource, .objectRef.name]' <file>
Hint 2 | Recorded is not the same as read
The API server writes an audit event for every request it receives, including the ones it refuses. Before treating an event as a successful read, check who exactly sent it, which kind of object it targeted, in which namespace, and what the response code was.
A ServiceAccount authenticates with this username:
system:serviceaccount:<namespace>:<name>
Hint 3 | Changing one key of a Secret
Values under data are base64 encoded. A patch with stringData takes plain text and leaves the other keys as they are:
kubectl patch secret <name> -n <namespace> -p '{"stringData":{"<key>":"<value>"}}'
Decode a single key to check it:
kubectl get secret <name> -n <namespace> -o jsonpath='{.data.<key>}' | base64 -d
Hint 4 | Finding every path to a permission
Test a permission as the ServiceAccount itself. Impersonating a ServiceAccount also applies the groups it belongs to:
kubectl auth can-i <verb> <resource> -n <namespace> --as=system:serviceaccount:<namespace>:<name>
A binding can name a ServiceAccount directly, or a Group the ServiceAccount is a member of. Every ServiceAccount belongs to system:serviceaccounts and to system:serviceaccounts:<its-namespace>:
kubectl get rolebindings -n <namespace> -o wide
kubectl get clusterrolebindings -o wide
The authorization.k8s.io/reason annotation in an audit event names the binding that allowed a request.