SecurityKubernetes
Kubernetes audit logs: what to record and what to look for
The API server can record who did what to which object. On your own control plane it logs no events until you pass it an audit policy. On EKS and AKS the audit log is not delivered until you enable it.

What an audit event holds
Kubernetes auditing provides a chronological set of records of the actions in a cluster. Each record is an event in the audit.k8s.io/v1 API group. Its fields include:
user: the authenticated user, withusernameandgroups.verb: the Kubernetes verb of the request.objectRef: the object the request targets, with fields such asresource,subresource,namespace,nameandapiGroup. The reference says it does not apply to list-type requests.requestURI: the request URI as the client sent it.sourceIPsanduserAgent: where the request came from. The user agent is provided by the client, and the reference says it must not be trusted.responseStatus: the response status.annotations: a key-value map that authentication, authorization and admission plugins can set.
On a cluster you run yourself, you pass the policy file to kube-apiserver with the --audit-policy-file flag. Omit the flag and the API server logs no events.
Levels and stages
A policy is a list of rules. When an event is processed, it is compared against the rules in order, and the first matching rule sets its audit level:
None: events that match the rule are not logged.Metadata: request metadata such as the user, timestamp, resource and verb, without the request or response body.Request: metadata and the request body.RequestResponse: metadata, the request body and the response body.
Each request can be recorded at a stage. The stages are RequestReceived, ResponseStarted (generated only for long-running requests such as a watch), ResponseComplete and Panic.
An example policy
apiVersion: audit.k8s.io/v1
kind: Policy
# No events at the RequestReceived stage
omitStages:
- "RequestReceived"
rules:
# Secrets, ConfigMaps and TokenReviews: metadata only
- level: Metadata
resources:
- group: ""
resources: ["secrets", "configmaps"]
- group: authentication.k8s.io
resources: ["tokenreviews"]
# Changes to RBAC objects, with the request body
- level: Request
verbs: ["create", "update", "patch", "delete"]
resources:
- group: "rbac.authorization.k8s.io"
# Watches by kube-proxy on endpoints and services
- level: None
users: ["system:kube-proxy"]
verbs: ["watch"]
resources:
- group: ""
resources: ["endpoints", "services"]
# A catch-all rule for all other requests
- level: MetadataThe omitStages setting, the kube-proxy rule and the catch-all rule come from the example policy in the Kubernetes documentation. The first rule is the one GKE documents for its own policy. The RBAC rule is built from documented fields: a rule with a group and no resources list applies to all resources in that API group.
The order decides the result, because the first matching rule sets the level. A policy with no rules is treated as illegal.
The log on your own control plane
The API server has two audit backends. The log backend writes events to a file in JSON lines format. The webhook backend sends events to an external HTTP API. The documentation enables the log backend with these two flags:
--audit-policy-file=/etc/kubernetes/audit-policy.yaml
--audit-log-path=/var/log/kubernetes/audit/audit.logThree more flags control rotation. --audit-log-maxage is the maximum number of days to retain old log files. --audit-log-maxbackup is the maximum number of log files to retain. --audit-log-maxsize is the maximum size in megabytes of a log file before it is rotated.
If the API server runs as a pod, mount the policy file and the log directory with hostPath volumes so that the audit records are persisted. Audit logging increases the memory consumption of the API server, and the amount depends on the audit configuration.
Logs on EKS, AKS and GKE
- Amazon EKS: by default, control plane logs are not sent to CloudWatch Logs. You enable each log type individually, and
auditis one of them. The log group name has the format/aws/eks/<cluster-name>/cluster. - Azure AKS: control plane logs are Azure Monitor resource logs. They are not collected and stored until you create a diagnostic setting. The audit categories are
kube-auditandkube-audit-admin, and the second excludes thegetandlistaudit events. - Google GKE: Kubernetes audit logs are part of Cloud Audit Logs, under the service name
k8s.io. Entries for create, delete and update requests go to the Admin Activity log, which is always enabled. Entries for get and list requests go to the Data Access log, which is disabled by default.
Events to look for
- Reads of Secrets. Filter on
verband onobjectRef.resource. The reference saysobjectRefdoes not apply to list-type requests, so searchrequestURIfor those. - Exec into pods.
execis a subresource of pods, andobjectRefhas asubresourcefield. On GKE, the documented Cloud Logging filter for these events isprotoPayload.methodName =~ "io.k8s.core.v1.pods.exec.*". - RBAC changes. Filter on
objectRef.apiGroupwith the valuerbac.authorization.k8s.io. The example policy logs write requests in that group with the request body. - Anonymous requests. A request that no authentication method rejects is treated as anonymous. It gets the username
system:anonymousand the groupsystem:unauthenticated. - Decisions by authorization and admission. The annotation
authorization.k8s.io/decisionisalloworforbid. Pod Security Admission addspod-security.kubernetes.io/audit-violationsin audit mode.
This jq query selects the events whose objectRef names the resource pods and the subresource exec:
jq 'select(.objectRef.resource == "pods" and .objectRef.subresource == "exec")
| {user: .user.username, namespace: .objectRef.namespace, pod: .objectRef.name}' \
/var/log/kubernetes/audit/audit.logMistakes to avoid
- Logging Secrets with their bodies.
Requestadds the request body andRequestResponseadds the response body as well. The example policy in the Kubernetes documentation logs Secrets and ConfigMaps atMetadata. - Putting a broad rule above a specific one. The first matching rule sets the level.
- Assuming a managed cluster records everything. EKS and AKS deliver audit logs only after you enable them, and GKE writes get and list requests only when Data Access logs are enabled.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Kubernetes RBAC: five rules for least privilege
Security · 2 min read
Groups over users, namespaces over clusters, and how to test what an account can do.

Kubernetes Secrets: encryption at rest and who can read them
Security · 4 min read
How Secrets are stored, how to encrypt them in etcd, and who can still read them.

Kyverno policies: block what should never deploy
Security · 4 min read
Five policy types, one documented example, and what happened to ClusterPolicy in 1.20.