# Audit Logs

## Audit Logs

Kubernetes audit logs are a critical feature for tracking the activities that occur within a Kubernetes cluster. These logs provide detailed records of interactions with the Kubernetes API server, allowing administrators to monitor, track, and review actions taken by users, service accounts, and other components within the cluster. Here's an in-depth look at Kubernetes audit logs:

### 1. **What Are Kubernetes Audit Logs?**

* **Definition:** Kubernetes audit logs are records of all requests made to the Kubernetes API server, capturing details about what happened, when it happened, who initiated it, and where it originated. These logs are essential for security auditing, compliance, and understanding the operational history of a cluster.
* **Purpose:** The primary purpose of audit logs is to provide visibility into the actions taken within a Kubernetes cluster. This visibility is crucial for security monitoring, detecting unauthorized activities, ensuring compliance with regulatory requirements, and investigating incidents.

### 2. **Components of Kubernetes Audit Logs**

* **Audit Events:** Each audit log entry is called an "audit event" and typically contains the following information:
  * **Timestamp:** The exact time the event occurred.
  * **User:** The identity of the user or service account that initiated the request.
  * **Source IP:** The IP address from which the request originated.
  * **Verb:** The HTTP method used in the request (e.g., GET, POST, PUT, DELETE).
  * **Resource:** The Kubernetes resource that was accessed or modified (e.g., pods, deployments, secrets).
  * **Namespace:** The namespace in which the action took place, if applicable.
  * **Object:** The specific object within the resource that was targeted (e.g., a specific pod or deployment).
  * **Response:** The result of the request, including the HTTP status code and any error messages.
  * **Audit Level:** The level of detail captured by the audit log (e.g., Metadata, Request, RequestResponse).

### 3. **Audit Policy**

* **Audit Policy Definition:** The Kubernetes audit policy is a configuration that determines what gets logged and how detailed the logs should be. The policy is defined in a YAML file and is passed to the API server via the `--audit-policy-file` flag.
* **Audit Policy Stages:**
  * **RequestReceived:** Logs the request when it is received by the API server.
  * **ResponseStarted:** Logs the request when the API server starts sending a response.
  * **ResponseComplete:** Logs the request after the response has been completed.
  * **Panic:** Logs the request if the API server encounters an internal error during the processing of the request.
* **Audit Levels:**
  * **None:** No events are logged.
  * **Metadata:** Only the metadata of the request is logged, such as the timestamp, user, and resource.
  * **Request:** Logs metadata and the full request body (excluding the response).
  * **RequestResponse:** Logs metadata, the full request, and the full response body.
  * **Minimal:** Logs the minimal necessary information, typically including the verb and object reference.
*   **Audit Policy Example:**

    ```yaml
    apiVersion: audit.k8s.io/v1
    kind: Policy
    rules:
    - level: RequestResponse
      verbs: ["create", "update", "patch", "delete"]
      resources:
      - group: ""
        resources: ["pods", "services", "deployments"]
    - level: Metadata
      verbs: ["get", "list"]
      resources:
      - group: ""
        resources: ["pods", "services", "deployments"]
    ```

### 4. **Configuring and Enabling Audit Logging**

* **Audit Policy File:** Create an audit policy file that defines the rules for what should be logged.
* **API Server Configuration:** Configure the API server to use the audit policy by specifying the `--audit-policy-file` flag.
* **Log Backend:** Choose where the audit logs will be stored. The API server can write logs to a file (`--audit-log-path`) or send them to a webhook (`--audit-webhook-config-file`).

### 5. **Audit Log Storage and Management**

* **Log File:** Audit logs can be stored in a log file on the API server. This method is straightforward but can lead to large log files that need to be managed and rotated.
* **Webhook Backend:** Audit logs can be sent to an external service via a webhook. This allows for more flexible log management, such as sending logs to a centralized logging system (e.g., Elasticsearch, Splunk).
* **Log Rotation and Retention:** Implement log rotation and retention policies to manage the size and lifespan of audit logs. This can be done using tools like `logrotate` or by configuring the API server’s log rotation options.

### 6. **Use Cases for Kubernetes Audit Logs**

* **Security Auditing:** Track who did what and when within the cluster, which is essential for detecting unauthorized access or potential security breaches.
* **Compliance:** Ensure that actions within the cluster comply with internal policies and external regulations (e.g., GDPR, HIPAA). Audit logs provide an auditable trail of activities.
* **Incident Investigation:** In the event of a security incident, audit logs can be used to trace the actions that led to the incident and understand its impact.
* **Operational Monitoring:** Monitor the behavior of users and applications, detect anomalies, and optimize resource usage by analyzing audit logs.

### 7. **Challenges with Kubernetes Audit Logs**

* **Volume of Logs:** In large clusters with high activity, audit logs can become voluminous, making storage and analysis challenging.
* **Performance Impact:** Extensive audit logging, especially at higher audit levels (like RequestResponse), can impact the performance of the API server.
* **Sensitive Information:** Care must be taken to avoid logging sensitive information, such as secrets or passwords, especially when logging request and response bodies.

### 8. **Best Practices for Kubernetes Audit Logging**

* **Tailor the Audit Policy:** Customize the audit policy to balance the need for information with storage and performance considerations. Log only what is necessary for your specific use case.
* **Use Webhooks for Flexibility:** Consider using a webhook backend for audit logs, which allows you to send logs to a centralized logging system, where they can be more easily managed and analyzed.
* **Monitor Audit Logs:** Regularly monitor audit logs for suspicious activity, unusual patterns, or errors that may indicate a security incident or misconfiguration.
* **Secure Audit Logs:** Protect audit logs from unauthorized access by encrypting them at rest and in transit. Ensure that only authorized personnel can view or manage the logs.

### 9. **Analyzing Kubernetes Audit Logs**

* **Centralized Logging Solutions:** Use centralized logging solutions like Elasticsearch, Splunk, or cloud-based logging services (AWS CloudWatch, Google Cloud Logging) to store and analyze audit logs.
* **Log Querying and Visualization:** Tools like Kibana or Grafana can be used to visualize and query audit logs, making it easier to detect anomalies or specific events.
* **Alerting:** Set up alerts based on specific patterns in audit logs, such as repeated failed authentication attempts, changes to critical resources, or unauthorized access.

## Kubernetes Audit Log Schema

{% embed url="https://kubernetes.io/docs/reference/config-api/apiserver-audit.v1/#audit-k8s-io-v1-Policy" %}
