All guides

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.

Hands typing on a laptop on a dark table

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, with username and groups.
  • verb: the Kubernetes verb of the request.
  • objectRef: the object the request targets, with fields such as resource, subresource, namespace, name and apiGroup. The reference says it does not apply to list-type requests.
  • requestURI: the request URI as the client sent it.
  • sourceIPs and userAgent: 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

YAML

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: Metadata

The 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:

Text

--audit-policy-file=/etc/kubernetes/audit-policy.yaml
--audit-log-path=/var/log/kubernetes/audit/audit.log

Three 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 audit is 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-audit and kube-audit-admin, and the second excludes the get and list audit 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 verb and on objectRef.resource. The reference says objectRef does not apply to list-type requests, so search requestURI for those.
  • Exec into pods. exec is a subresource of pods, and objectRef has a subresource field. On GKE, the documented Cloud Logging filter for these events is protoPayload.methodName =~ "io.k8s.core.v1.pods.exec.*".
  • RBAC changes. Filter on objectRef.apiGroup with the value rbac.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:anonymous and the group system:unauthenticated.
  • Decisions by authorization and admission. The annotation authorization.k8s.io/decision is allow or forbid. Pod Security Admission adds pod-security.kubernetes.io/audit-violations in audit mode.

This jq query selects the events whose objectRef names the resource pods and the subresource exec:

Shell

jq 'select(.objectRef.resource == "pods" and .objectRef.subresource == "exec")
  | {user: .user.username, namespace: .objectRef.namespace, pod: .objectRef.name}' \
  /var/log/kubernetes/audit/audit.log

Mistakes to avoid

  1. Logging Secrets with their bodies. Request adds the request body and RequestResponse adds the response body as well. The example policy in the Kubernetes documentation logs Secrets and ConfigMaps at Metadata.
  2. Putting a broad rule above a specific one. The first matching rule sets the level.
  3. 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.

Book a call

More guides