|
| 1 | +--- |
| 2 | +description: > |
| 3 | + Understanding kcp audit event annotations and how to use them for monitoring and compliance. |
| 4 | +--- |
| 5 | + |
| 6 | +# Audit Logging |
| 7 | + |
| 8 | +kcp extends Kubernetes audit events with workspace-specific annotations that help identify which workspace (logical cluster) each request belongs to. This is essential for multi-tenant environments where you need to track and audit access across different workspaces. |
| 9 | + |
| 10 | +## Audit Event Annotations |
| 11 | + |
| 12 | +kcp automatically adds the following annotations to all audit events: |
| 13 | + |
| 14 | +### `kcp.io/path` |
| 15 | + |
| 16 | +The canonical workspace path (e.g., `root:consumer:production`). This is the human-readable hierarchical path that uniquely identifies the workspace. |
| 17 | + |
| 18 | +### `tenancy.kcp.io/workspace` |
| 19 | + |
| 20 | +The workspace cluster name (logical cluster identifier). This is the internal cluster name used by KCP. |
| 21 | + |
| 22 | +### `kcp.io/cluster` |
| 23 | + |
| 24 | +Alias for the workspace cluster name. Contains the same value as `tenancy.kcp.io/workspace`. |
| 25 | + |
| 26 | +## Example Audit Event |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "kind": "Event", |
| 31 | + "apiVersion": "audit.k8s.io/v1", |
| 32 | + "level": "Request", |
| 33 | + "auditID": "5684337a-48d6-4491-aed2-a0bca6fcda2b", |
| 34 | + "stage": "RequestReceived", |
| 35 | + "requestURI": "/api/v1/namespaces/default/configmaps?limit=500", |
| 36 | + "verb": "list", |
| 37 | + "user": { |
| 38 | + "username": "kcp-admin", |
| 39 | + "uid": "e6741a4d-fc7c-44c5-b5ec-9357b44b7e0b", |
| 40 | + "groups": [ |
| 41 | + "system:kcp:admin", |
| 42 | + "system:authenticated" |
| 43 | + ] |
| 44 | + }, |
| 45 | + "sourceIPs": [ |
| 46 | + "127.0.0.1" |
| 47 | + ], |
| 48 | + "userAgent": "kubectl/v1.30.1 (darwin/arm64) kubernetes/6911225", |
| 49 | + "objectRef": { |
| 50 | + "resource": "configmaps", |
| 51 | + "namespace": "default", |
| 52 | + "apiVersion": "v1" |
| 53 | + }, |
| 54 | + "requestReceivedTimestamp": "2025-12-09T16:03:44.214758Z", |
| 55 | + "stageTimestamp": "2025-12-09T16:03:44.214758Z", |
| 56 | + "annotations": { |
| 57 | + "kcp.io/cluster": "16iai06e7ob717ht", |
| 58 | + "kcp.io/path": "root:consumer:production", |
| 59 | + "tenancy.kcp.io/workspace": "16iai06e7ob717ht" |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +The example audit event above would be generated by running the following command: |
| 65 | + |
| 66 | +```bash |
| 67 | +kubectl --server=https://127.0.0.1:6443/clusters/root:consumer:production get configmap -n default |
| 68 | +``` |
| 69 | + |
| 70 | +This command lists configmaps in the `default` namespace within the `root:consumer:production` workspace, which triggers the audit event with the workspace annotations shown in the example. |
| 71 | + |
| 72 | + |
| 73 | +## Enabling Audit Logging |
| 74 | + |
| 75 | +To enable audit logging in kcp, you need to provide an audit policy file and configure where audit logs should be written. Audit logging is disabled by default. |
| 76 | + |
| 77 | +### Basic Setup |
| 78 | + |
| 79 | +Enable audit logging by starting kcp with the following flags: |
| 80 | + |
| 81 | +```bash |
| 82 | +kcp start \ |
| 83 | + --audit-policy-file=/path/to/audit-policy.yaml \ |
| 84 | + --audit-log-path=/path/to/kcp.audit \ |
| 85 | + --audit-log-format=json |
| 86 | +``` |
| 87 | + |
| 88 | +### Required Flags |
| 89 | + |
| 90 | +- **`--audit-policy-file`**: Path to the audit policy YAML file that defines which events should be logged |
| 91 | +- **`--audit-log-path`**: Path where audit logs will be written |
| 92 | +- **`--audit-log-format`**: Output format (`json` or `legacy`). JSON format is recommended for easier parsing |
| 93 | + |
| 94 | +### Example Audit Policy |
| 95 | + |
| 96 | +Create an audit policy file (e.g., `audit-policy.yaml`) to define what events to log: |
| 97 | + |
| 98 | +```yaml |
| 99 | +apiVersion: audit.k8s.io/v1 |
| 100 | +kind: Policy |
| 101 | +rules: |
| 102 | + - level: Request |
| 103 | + verbs: ["*"] |
| 104 | +``` |
| 105 | +
|
| 106 | +This example policy logs all requests at the `Request` level. For production environments, you may want to be more selective about what gets logged to manage log volume. |
| 107 | + |
| 108 | +### Complete Example |
| 109 | + |
| 110 | +Here's a complete example of starting kcp with audit logging enabled: |
| 111 | + |
| 112 | +```bash |
| 113 | +# Create audit policy file |
| 114 | +cat > audit-policy.yaml <<EOF |
| 115 | +apiVersion: audit.k8s.io/v1 |
| 116 | +kind: Policy |
| 117 | +rules: |
| 118 | + - level: Request |
| 119 | + verbs: ["*"] |
| 120 | + resources: |
| 121 | + - group: "*" |
| 122 | + resources: ["*"] |
| 123 | +EOF |
| 124 | +
|
| 125 | +# Start kcp with audit logging |
| 126 | +kcp start \ |
| 127 | + --audit-policy-file=./audit-policy.yaml \ |
| 128 | + --audit-log-path=./kcp.audit \ |
| 129 | + --audit-log-format=json |
| 130 | +``` |
| 131 | + |
| 132 | +## Configuration |
| 133 | + |
| 134 | +Audit logging in kcp uses the standard Kubernetes audit policy configuration. For detailed information on configuring audit policies, including different log levels and filtering options, see the [Kubernetes audit documentation](https://kubernetes.io/docs/tasks/debug/debug-cluster/audit/). |
| 135 | + |
| 136 | +## Notes |
| 137 | + |
| 138 | +- The `kcp.io/path` annotation is only populated when the LogicalCluster informer is available. In cache-server deployments, this annotation may be empty. |
| 139 | +- All annotations are added to both `RequestReceived` and `ResponseComplete` audit event stages. |
| 140 | +- The workspace annotations are automatically added to all audit events, regardless of the audit policy configuration. |
0 commit comments