# k8s-audit-logging / azure / t0 "Quick start"
#
# AKS writes no audit log until a diagnostic setting asks for one. This sends
# the API server's audit events to a Log Analytics workspace kept for a year,
# and turns three of them into alerts: exec or attach into a pod, a new
# cluster role binding, and a request from someone who never signed in.
# Three more alerts read the subscription's activity log without the
# workspace: a diagnostic setting deleted or written, and a rule disabled.
#
# Change the four values below, then:
#   az login
#   terraform init && terraform apply
#
# Verify: a diagnostic setting can take up to 90 minutes to start sending,
# so wait that long after the first apply. Then:
#   kubectl exec -it <any pod> -- true
#   The pod-exec email follows the first five-minute run after the entry
#   reaches the workspace.
#
# The full audit category is used, kube-audit, because the admin-only
# category drops every get, and newer kubectl clients open exec as a get
# over a WebSocket. The full category is the costly one on a busy cluster:
# check the first week's ingestion.

locals {
  subscription_id        = "00000000-0000-0000-0000-000000000000" # change me
  cluster_name           = "aks-prod"                             # change me
  cluster_resource_group = "rg-aks-prod"                          # change me
  alert_email            = "you@example.com"                      # change me
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id = local.subscription_id
}

data "azurerm_kubernetes_cluster" "this" {
  name                = local.cluster_name
  resource_group_name = local.cluster_resource_group
}

# A workspace name allows letters, digits, and hyphens, up to 63 characters.
resource "azurerm_log_analytics_workspace" "audit" {
  name                = "law-${substr(replace(local.cluster_name, "/[^A-Za-z0-9-]/", "-"), 0, 53)}-audit"
  location            = data.azurerm_kubernetes_cluster.this.location
  resource_group_name = local.cluster_resource_group
  sku                 = "PerGB2018"
  retention_in_days   = 365
}

# Resource-specific mode puts the events in the AKSAudit table, with the
# request's verb, user, object, and status as columns of their own.
resource "azurerm_monitor_diagnostic_setting" "audit" {
  name                           = "k8s-audit"
  target_resource_id             = data.azurerm_kubernetes_cluster.this.id
  log_analytics_workspace_id     = azurerm_log_analytics_workspace.audit.id
  log_analytics_destination_type = "Dedicated"

  enabled_log {
    category = "kube-audit"
  }
}

resource "azurerm_monitor_action_group" "audit" {
  name                = "${local.cluster_name}-audit-alerts"
  resource_group_name = local.cluster_resource_group
  short_name          = "k8saudit"

  email_receiver {
    name          = "security"
    email_address = local.alert_email
  }
}

locals {
  # Each query keeps the rows that arrived in the last five minutes of a
  # one-hour window, so a late row is rarely missed or alerted twice.
  # Exec is matched whatever the verb, because newer clients open it as a
  # get over a WebSocket. Anonymous calls to the health and version paths
  # are ordinary on a public API server, so only calls that name an object
  # count.
  detections = {
    pod-exec             = "AKSAudit | where ingestion_time() > ago(5m) and Stage == \"ResponseComplete\" | where tostring(ObjectRef.resource) == \"pods\" and tostring(ObjectRef.subresource) in (\"exec\", \"attach\") | where toint(ResponseStatus.code) < 300"
    cluster-role-binding = "AKSAudit | where ingestion_time() > ago(5m) and Stage == \"ResponseComplete\" | where Verb == \"create\" and tostring(ObjectRef.resource) == \"clusterrolebindings\" | where toint(ResponseStatus.code) < 300"
    anonymous-request    = "AKSAudit | where ingestion_time() > ago(5m) and Stage == \"ResponseComplete\" | where tostring(User.username) == \"system:anonymous\" and isnotempty(tostring(ObjectRef.resource))"
  }
}

# Changes the workspace might never hear of. A deleted or rewritten setting
# stops the record of its own change, and a disabled rule does not run.
locals {
  native = {
    diagnostic-setting-deleted = {
      operation = "Microsoft.Insights/diagnosticSettings/delete"
      doc       = "A diagnostic setting was deleted in the subscription, possibly the one that sends the cluster's audit log."
    }
    # Policy remediation and other teams' monitoring write diagnostic
    # settings too, so expect some of these.
    diagnostic-setting-written = {
      operation = "Microsoft.Insights/diagnosticSettings/write"
      doc       = "A diagnostic setting was created or changed in the subscription. A rewrite can drop the audit category or send the log somewhere else."
    }
    # Azure writes this when a log search rule's query has failed for a week.
    rule-disabled = {
      operation = "Microsoft.Insights/ScheduledQueryRules/disable/action"
      doc       = "Azure disabled a log search alert rule in the subscription because its query kept failing."
    }
  }
}

# Azure evaluates these alerts on the activity log itself, with no workspace
# in between, and they cost nothing. They still notify through the one action
# group, so deleting or disabling it, or deleting the resource group,
# silences every alert here. Standard locks the alert path against deletes.
resource "azurerm_monitor_activity_log_alert" "native" {
  for_each            = local.native
  name                = "${local.cluster_name}-${each.key}"
  resource_group_name = local.cluster_resource_group
  location            = "global"
  scopes              = ["/subscriptions/${local.subscription_id}"]
  description         = each.value.doc

  criteria {
    category       = "Administrative"
    operation_name = each.value.operation
  }

  action {
    action_group_id = azurerm_monitor_action_group.audit.id
  }
}

# The table does not exist until the first event arrives, and a rule's query
# is checked against the workspace when the rule is made. Skipping that check
# lets the rules exist before the log does. They are made after the setting,
# though a setting can still take up to 90 minutes to start sending.
resource "azurerm_monitor_scheduled_query_rules_alert_v2" "detection" {
  for_each              = local.detections
  name                  = "${local.cluster_name}-${each.key}"
  resource_group_name   = local.cluster_resource_group
  location              = data.azurerm_kubernetes_cluster.this.location
  scopes                = [azurerm_log_analytics_workspace.audit.id]
  description           = "Seen in the AKS audit log. In the workspace, run: ${each.value}"
  severity              = 1
  evaluation_frequency  = "PT5M"
  window_duration       = "PT1H"
  skip_query_validation = true

  criteria {
    query                   = each.value
    time_aggregation_method = "Count"
    operator                = "GreaterThan"
    threshold               = 0
  }

  action {
    action_groups = [azurerm_monitor_action_group.audit.id]
  }

  depends_on = [azurerm_monitor_diagnostic_setting.audit]
}

output "detections" {
  description = "Each detection and the query behind it."
  value       = local.detections
}
