Audit log on Azure AKS
On Azure AKS, the Kubernetes API server can send its audit log to a Log Analytics workspace. This page shows how to turn the audit log on, where to find it, and how to count the users of your platform, in Log Analytics or in a Grafana dashboard.
Each create, update and delete of a resource request carries the
audit annotation
attribution.platform.syntasso.io/last-changed-by, set to the user who made
the change.
Turning on the audit log
AKS does not send the audit log anywhere until you add a diagnostic setting
that sends the kube-audit-admin category to a Log Analytics workspace. See
AKS control plane resource logs
in the AKS documentation. For example, with the Azure CLI:
cluster_id=$(az aks show \
--resource-group <RESOURCE_GROUP> \
--name <CLUSTER_NAME> \
--query id --output tsv)
workspace_id=$(az monitor log-analytics workspace show \
--resource-group <RESOURCE_GROUP> \
--workspace-name <WORKSPACE_NAME> \
--query id --output tsv)
az monitor diagnostic-settings create \
--name ske-audit \
--resource "$cluster_id" \
--workspace "$workspace_id" \
--logs '[{"category":"kube-audit-admin","enabled":true}]' \
--export-to-resource-specific true
The kube-audit-admin category leaves out get and list events and keeps
every create, update and delete, so there is nothing else to set up. The
kube-audit category also records every read, so it sends far more logs.
In the Azure portal, the same setting is under the cluster's Diagnostic
settings. The category is called Kubernetes Audit Admin Logs, and
--export-to-resource-specific true is the Resource specific destination
table:


Finding the logs
With the
Resource specific
destination table, AKS writes the audit log to the AKSAuditAdmin table in
your workspace. Each row is one audit event, and the user is in
Annotations["attribution.platform.syntasso.io/last-changed-by"]. With the
Azure diagnostics destination table, the logs go to the shared
AzureDiagnostics table instead, and the queries on this page do not work.


Setting log retention
You can only see activity and count your platform users if their events are still in the workspace. A workspace keeps logs for 30 days by default. To count the users who used your platform over the last year, you need to keep a year of logs.
For that, check the retention of the AKSAuditAdmin table, and change it if
you need to:
az monitor log-analytics workspace table update \
--resource-group <RESOURCE_GROUP> \
--workspace-name <WORKSPACE_NAME> \
--name AKSAuditAdmin \
--retention-time 365 \
--total-retention-time 365
Log Analytics charges for the logs it takes in, and for keeping them longer than 31 days, so a longer retention will cost more. Check the Azure Monitor pricing page to find out the relevant price for your scenario.
Counting platform users
With the audit log turned on, you can count platform users in Log Analytics or in Grafana.
Using Log Analytics
- In the Azure portal, open the Log Analytics workspace and select Logs.
- Choose the time range to count over.
- Run this query:
AKSAuditAdmin
| extend user = tostring(Annotations["attribution.platform.syntasso.io/last-changed-by"])
| where isnotempty(user)
and Stage == "ResponseComplete"
and toint(ResponseStatus.code) < 400
and RequestUri !contains "dryRun"
| summarize users = count_distinct(user)
This where keeps the changes to resource requests that succeeded. It drops
the changes Kubernetes rejected and any dry runs, such as the ones
kubectl diff sends, which also carry the audit annotation.


Other useful Log Analytics queries
How many changes each user made:
AKSAuditAdmin
| extend user = tostring(Annotations["attribution.platform.syntasso.io/last-changed-by"])
| where isnotempty(user)
and Stage == "ResponseComplete"
and toint(ResponseStatus.code) < 400
and RequestUri !contains "dryRun"
| summarize changes = count() by user
| sort by changes desc


Who created, updated and deleted a request:
AKSAuditAdmin
| extend user = tostring(Annotations["attribution.platform.syntasso.io/last-changed-by"])
| where isnotempty(user)
and Stage == "ResponseComplete"
and toint(ResponseStatus.code) < 400
and RequestUri !contains "dryRun"
and ObjectRef.namespace == "<NAMESPACE>"
and ObjectRef.name == "<REQUEST_NAME>"
| project TimeGenerated, user, Verb, type = tostring(ObjectRef.resource)
| sort by TimeGenerated desc
Verb is the Kubernetes API verb, which depends on how the tool sends the
change. Most tools, such as kubectl apply, kubectl edit, Flux and Argo CD,
send changes to an existing request as patch. Tools that send the whole
request, such as kubectl replace, show as update.


Grafana
Grafana can run the same queries through its Azure Monitor data source, which is built into Grafana. The dashboard below shows the number of unique users, the number of changes, the changes each user made, and the most recent changes. To set it up:
- Give Grafana access to the workspace. See
Configure the Azure Monitor data source
in the Grafana documentation. Grafana needs to run Log Analytics queries on
<WORKSPACE_NAME>, so the identity it uses needs the Log Analytics Reader role on that workspace. - Add the Azure Monitor data source in Grafana.
- Copy the dashboard below, replace
<SUBSCRIPTION_ID>,<RESOURCE_GROUP>and<WORKSPACE_NAME>, then import it and choose your Azure Monitor data source.
Each query in the dashboard filters with $__timeFilter(TimeGenerated), so it
counts over the dashboard's time range.


Dashboard JSON
{
"__inputs": [
{
"name": "DS_AZURE_MONITOR",
"label": "Azure Monitor",
"type": "datasource",
"pluginId": "grafana-azure-monitor-datasource",
"pluginName": "Azure Monitor"
}
],
"title": "SKE platform audit",
"uid": "ske-platform-audit",
"tags": [
"ske",
"audit"
],
"time": {
"from": "now-30d",
"to": "now"
},
"schemaVersion": 39,
"panels": [
{
"id": 1,
"title": "Unique users",
"type": "stat",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"gridPos": {
"x": 0,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"query": "AKSAuditAdmin\n| where $__timeFilter(TimeGenerated)\n| extend user = tostring(Annotations[\"attribution.platform.syntasso.io/last-changed-by\"])\n| where isnotempty(user)\n and Stage == \"ResponseComplete\"\n and toint(ResponseStatus.code) < 400\n and RequestUri !contains \"dryRun\"\n| summarize users = count_distinct(user)",
"resources": [
"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.OperationalInsights/workspaces/<WORKSPACE_NAME>"
],
"resultFormat": "table"
}
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 2,
"title": "Changes to resource requests",
"type": "stat",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"gridPos": {
"x": 8,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"query": "AKSAuditAdmin\n| where $__timeFilter(TimeGenerated)\n| extend user = tostring(Annotations[\"attribution.platform.syntasso.io/last-changed-by\"])\n| where isnotempty(user)\n and Stage == \"ResponseComplete\"\n and toint(ResponseStatus.code) < 400\n and RequestUri !contains \"dryRun\"\n| summarize changes = count()",
"resources": [
"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.OperationalInsights/workspaces/<WORKSPACE_NAME>"
],
"resultFormat": "table"
}
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 3,
"title": "Average changes per user",
"type": "stat",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"gridPos": {
"x": 16,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"query": "AKSAuditAdmin\n| where $__timeFilter(TimeGenerated)\n| extend user = tostring(Annotations[\"attribution.platform.syntasso.io/last-changed-by\"])\n| where isnotempty(user)\n and Stage == \"ResponseComplete\"\n and toint(ResponseStatus.code) < 400\n and RequestUri !contains \"dryRun\"\n| summarize changes = count() by user\n| summarize average = avg(changes)",
"resources": [
"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.OperationalInsights/workspaces/<WORKSPACE_NAME>"
],
"resultFormat": "table"
}
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 4,
"title": "Changes per user",
"type": "table",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"gridPos": {
"x": 0,
"y": 5,
"w": 12,
"h": 10
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"query": "AKSAuditAdmin\n| where $__timeFilter(TimeGenerated)\n| extend user = tostring(Annotations[\"attribution.platform.syntasso.io/last-changed-by\"])\n| where isnotempty(user)\n and Stage == \"ResponseComplete\"\n and toint(ResponseStatus.code) < 400\n and RequestUri !contains \"dryRun\"\n| summarize changes = count() by user\n| sort by changes desc",
"resources": [
"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.OperationalInsights/workspaces/<WORKSPACE_NAME>"
],
"resultFormat": "table"
}
}
]
},
{
"id": 5,
"title": "Recent changes",
"type": "table",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"gridPos": {
"x": 12,
"y": 5,
"w": 12,
"h": 10
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "grafana-azure-monitor-datasource",
"uid": "${DS_AZURE_MONITOR}"
},
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"query": "AKSAuditAdmin\n| where $__timeFilter(TimeGenerated)\n| extend user = tostring(Annotations[\"attribution.platform.syntasso.io/last-changed-by\"])\n| where isnotempty(user)\n and Stage == \"ResponseComplete\"\n and toint(ResponseStatus.code) < 400\n and RequestUri !contains \"dryRun\"\n| project TimeGenerated, user, Verb, type = tostring(ObjectRef.resource), namespace = tostring(ObjectRef.namespace), name = tostring(ObjectRef.name)\n| sort by TimeGenerated desc\n| take 100",
"resources": [
"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.OperationalInsights/workspaces/<WORKSPACE_NAME>"
],
"resultFormat": "table"
}
}
]
}
]
}
Known limitations
- Counts only go back as far as your retention.
- The recorded user is only as specific as the way users log in. If users
share the cluster's local account, the audit log records
masterclientfor all of them.az aks get-credentialsgives the local account on a cluster without Microsoft Entra ID, and--admingives it on any cluster. With Microsoft Entra ID, each user logs in as themselves, and you can disable local accounts. Runkubectl auth whoami -o yamlto see which fields your cluster gives you, and setuserInfoFieldto a field that is different for each user. - Some changes have no user. For example, deletes through GitOps. See What is not recorded.
