Query Details

Fn AWS EKS Audit Normalized

Query

// REVIEW ONLY - Reusable Workspace Function Spec: fn_AWS_EKSAuditNormalized
// Purpose: Normalizes Kubernetes audit.k8s.io/v1 JSON delivered via AWSCloudWatch (/aws/eks/<cluster>/cluster).
// Validate actual destination table first using discovery/D07-eks-audit-discovery.kql.
let Lookback = 24h;
AWSCloudWatch
| where TimeGenerated > ago(Lookback)
| where Message has "\"kind\":\"Event\"" and Message has "audit.k8s.io/"
| extend Audit = parse_json(Message)
| where tostring(Audit.stage) == "ResponseComplete"
| extend Username = tostring(Audit.user.username),
    UserGroups = tostring(Audit.user.groups),
    Verb = tostring(Audit.verb),
    Resource = tostring(Audit.objectRef.resource),
    Subresource = tostring(Audit.objectRef.subresource),
    Namespace = tostring(Audit.objectRef.namespace),
    ObjectName = tostring(Audit.objectRef.name),
    SourceIp = tostring(Audit.sourceIPs[0]),
    UserAgent = tostring(Audit.userAgent),
    ResponseCode = toint(Audit.responseStatus.code),
    ClusterHint = coalesce(tostring(Audit.annotations["arn.amazonaws.com/cluster-name"]), extract(@"/aws/eks/([^/]+)/cluster", 1, Message), "UnknownCluster"),
    ReqObj = Audit.requestObject
| extend IsControllerOrNode = (
    Username startswith "system:node:"
    or Username startswith "system:kube-"
    or Username in ("system:apiserver", "eks:addon-manager", "eks:node-manager", "eks:fargate-manager")
    or UserAgent has_any ("kube-controller-manager", "kube-scheduler", "kubelet", "cluster-autoscaler", "argocd", "flux"))
| project TimeGenerated, IngestTime=ingestion_time(), ExtractedTime, AuditId=tostring(Audit.auditID),
    ClusterHint, Stage=tostring(Audit.stage), Verb, Username, UserGroups, IsControllerOrNode,
    Namespace, Resource, Subresource, ObjectName, SourceIp, UserAgent, ResponseCode, ReqObj
| take 2000

Explanation

This query is designed to process and normalize Kubernetes audit logs from AWS CloudWatch for a specific EKS (Elastic Kubernetes Service) cluster. Here's a simplified breakdown of what it does:

  1. Time Frame: It looks at logs generated in the last 24 hours.

  2. Filter Logs: It filters the logs to only include those that are of the "Event" kind and are part of the Kubernetes audit API.

  3. Parse JSON: It parses the JSON message to extract relevant audit information.

  4. Filter by Stage: It only includes logs where the audit stage is "ResponseComplete", indicating the request has been fully processed.

  5. Extract Information: It extracts various pieces of information from the logs, such as:

    • Username and user groups involved in the event.
    • The action (verb) taken and the resource affected.
    • Additional details like namespace, object name, source IP, user agent, and response code.
    • The cluster name, inferred from annotations or the log path.
  6. Identify System Users: It checks if the user is a system component or node by looking at the username and user agent.

  7. Select and Limit Data: It selects specific fields to include in the output and limits the results to 2000 entries.

The query is essentially used to create a normalized view of Kubernetes audit logs for analysis, focusing on completed responses and identifying whether actions were performed by system components or regular users.