|
| 1 | +--- |
| 2 | +title: "Auditing" |
| 3 | +menu: |
| 4 | + main: |
| 5 | + parent: "user" |
| 6 | + identifier: "user-auditing" |
| 7 | + weight: 4 |
| 8 | +description: |- |
| 9 | + This guide covers how to enable Kubernetes API [auditing] on a kind cluster. |
| 10 | +
|
| 11 | + [auditing]: https://kubernetes.io/docs/tasks/debug-application-cluster/audit/ |
| 12 | +--- |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +Kubernetes auditing provides a security-relevant, chronological set of records documenting the sequence of actions in a cluster. Auditing requires a file to define the [audit policy] and a backend configuration to store the logged events. Auditing supports two types of backends: log (file) & webhook. The following exercise uses the log backend. |
| 17 | + |
| 18 | +Steps: |
| 19 | + |
| 20 | +- Create the local audit-policy file |
| 21 | +- Mount the local audit-policy file into the kind control plane |
| 22 | +- Expose the control plane mounts to the API server |
| 23 | +- Enable the auditing API flags |
| 24 | +- Create a cluster |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +### Create an `audit-policy.yaml` file |
| 29 | + |
| 30 | +The [audit policy] defines the level of granularity outputted by the Kubernetes API server. The example below logs all requests at the "Metadata" level. See the [audit policy] docs for more examples. |
| 31 | + |
| 32 | +{{< codeFromInline lang="bash" >}} |
| 33 | +cat <<EOF > audit-policy.yaml |
| 34 | +apiVersion: audit.k8s.io/v1 |
| 35 | +kind: Policy |
| 36 | +rules: |
| 37 | +- level: Metadata |
| 38 | +EOF |
| 39 | +{{< /codeFromInline >}} |
| 40 | + |
| 41 | +### Create a `kind-config.yaml` file. |
| 42 | + |
| 43 | +To enable audit logging, use kind's [configuration file] to pass additional setup instructions. Kind uses `kubeadm` to provision the cluster and the configuration file has the ability to pass `kubeadmConfigPatches` for further customization. |
| 44 | + |
| 45 | +{{< codeFromInline lang="bash" >}} |
| 46 | +cat <<EOF > kind-config.yaml |
| 47 | +kind: Cluster |
| 48 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 49 | +nodes: |
| 50 | +- role: control-plane |
| 51 | + kubeadmConfigPatches: |
| 52 | + - | |
| 53 | + kind: ClusterConfiguration |
| 54 | + apiServer: |
| 55 | + # enable auditing flags on the API server |
| 56 | + extraArgs: |
| 57 | + audit-log-path: /var/log/kubernetes/kube-apiserver-audit.log |
| 58 | + audit-policy-file: /etc/kubernetes/policies/audit-policy.yaml |
| 59 | + # mount new files / directories on the control plane |
| 60 | + extraVolumes: |
| 61 | + - name: audit-policies |
| 62 | + hostPath: /etc/kubernetes/policies |
| 63 | + mountPath: /etc/kubernetes/policies |
| 64 | + readOnly: true |
| 65 | + pathType: "DirectoryOrCreate" |
| 66 | + - name: "audit-logs" |
| 67 | + hostPath: "/var/log/kubernetes" |
| 68 | + mountPath: "/var/log/kubernetes" |
| 69 | + readOnly: false |
| 70 | + pathType: DirectoryOrCreate |
| 71 | + # mount the local file on the control plane |
| 72 | + extraMounts: |
| 73 | + - hostPath: ./audit-policy.yaml |
| 74 | + containerPath: /etc/kubernetes/policies/audit-policy.yaml |
| 75 | + readOnly: true |
| 76 | +EOF |
| 77 | +{{< /codeFromInline >}} |
| 78 | + |
| 79 | +## Launch a new cluster |
| 80 | + |
| 81 | +{{< codeFromInline lang="bash" >}} |
| 82 | +kind create cluster --config kind-config.yaml |
| 83 | +{{< /codeFromInline >}} |
| 84 | + |
| 85 | +## View audit logs |
| 86 | + |
| 87 | +Once the cluster is running, view the log files on the control plane in `/var/log/kubernetes/kube-apiserver-audit.log`. |
| 88 | + |
| 89 | +{{< codeFromInline lang="bash" >}} |
| 90 | +docker exec kind-control-plane cat /var/log/kubernetes/kube-apiserver-audit.log |
| 91 | +{{< /codeFromInline >}} |
| 92 | + |
| 93 | +## Troubleshooting |
| 94 | + |
| 95 | +If logs are not present, let's ensure a few things are in place. |
| 96 | + |
| 97 | +### Is the local audit-policy file mounted in the control-plane? |
| 98 | + |
| 99 | +{{< codeFromInline lang="bash" >}} |
| 100 | +docker exec kind-control-plane ls /etc/kubernetes/policies |
| 101 | +{{< /codeFromInline >}} |
| 102 | + |
| 103 | +Expected output: |
| 104 | + |
| 105 | +```bash |
| 106 | +audit-policy.yaml |
| 107 | +``` |
| 108 | + |
| 109 | +### Does the API server contain the mounts and arguments? |
| 110 | + |
| 111 | +{{< codeFromInline lang="bash" >}} |
| 112 | +docker exec kind-control-plane cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep audit |
| 113 | +{{< /codeFromInline >}} |
| 114 | + |
| 115 | +Expected output: |
| 116 | + |
| 117 | +```bash |
| 118 | + - --audit-log-path=/var/log/kubernetes/kube-apiserver-audit.log |
| 119 | + - --audit-policy-file=/etc/kubernetes/policies/audit-policy.yaml |
| 120 | + name: audit-logs |
| 121 | + name: audit-policies |
| 122 | + name: audit-logs |
| 123 | + name: audit-policies |
| 124 | +``` |
| 125 | + |
| 126 | +If the control plane requires further debugging use `docker exec -it kind-control-plane bash` to start an interactive terminal session with the container. |
| 127 | + |
| 128 | +[audit policy]: https://kubernetes.io/docs/tasks/debug-application-cluster/audit/#audit-policy |
| 129 | +[configuration file]: /docs/user/configuration |
0 commit comments