# k8s-audit-logging / oci / t0 "Quick start"
#
# OKE sends the API server's audit events to the Audit service, under the
# source "OKE API Server Admin Access", with nothing to switch on. They are
# kept for 365 days, tenancy-wide, and that period cannot be changed. This
# turns three of them into alerts: exec or attach into a pod, a new cluster
# role binding, and a burst of refused requests from one caller.
#
# Oracle describes the feed as the administrative changes, the writes.
# Whether a read appears is not stated, and nothing in OKE adds one. A test
# kubectl get secret, then the search in the Standard version's verify
# output, settles it for your cluster.
#
# Change the three values below. Credentials come from ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Verify: confirm the subscription email first. Then:
#   kubectl exec -it <any pod> -- true
#   The pod-exec alarm emails within a few minutes. If it stays quiet, search
#   the compartment's audit log for "OKE API Server Admin Access" and the
#   pod's name: the verify output in Standard shows the query.

locals {
  compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa" # change me: the cluster's compartment, where its audit events are
  region           = "us-ashburn-1"                    # change me
  alert_email      = "you@example.com"                 # change me
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 9.0"
    }
  }
}

provider "oci" {
  region = local.region
}

locals {
  source    = "source='OKE API Server Admin Access'"
  namespace = "oke_audit"

  # Each connector is a filter on the audit log and a metric the matching
  # events are counted into. Oracle documents no parentheses for the filter, so exec
  # and attach are two connectors feeding one metric.
  connectors = {
    pod-exec             = { metric = "pod_exec", condition = "${local.source} and logContent='*/exec*'" }
    pod-attach           = { metric = "pod_exec", condition = "${local.source} and logContent='*/attach*'" }
    cluster-role-binding = { metric = "cluster_role_binding", condition = "${local.source} and logContent='*clusterrolebindings*' and data.request.action='POST'" }
    forbidden-request    = { metric = "forbidden_request", condition = "${local.source} and data.response.status='403'" }
  }

  # Each alarm is a condition on one of those metrics. A single refused
  # request is ordinary. Ten from one caller in five minutes is not.
  alarms = {
    pod-exec             = { metric = "pod_exec", query = "pod_exec[1m].count() > 0" }
    cluster-role-binding = { metric = "cluster_role_binding", query = "cluster_role_binding[1m].count() > 0" }
    forbidden-request    = { metric = "forbidden_request", query = "forbidden_request[5m].groupBy(principal).count() > 9" }
  }
}

resource "oci_ons_notification_topic" "audit" {
  compartment_id = local.compartment_ocid
  name           = "oke-audit-alerts"
  description    = "Alerts from the OKE API server audit log"
}

resource "oci_ons_subscription" "email" {
  compartment_id = local.compartment_ocid
  topic_id       = oci_ons_notification_topic.audit.id
  protocol       = "EMAIL"
  endpoint       = local.alert_email
}

# The connectors write the metrics as themselves.
resource "oci_identity_policy" "connector" {
  compartment_id = local.compartment_ocid
  name           = "oke-audit-connectors"
  description    = "Lets the audit connectors count events into the ${local.namespace} metrics"
  statements = [
    "Allow any-user to use metrics in compartment id ${local.compartment_ocid} where all {request.principal.type='serviceconnector', target.metrics.namespace='${local.namespace}', request.principal.compartment.id='${local.compartment_ocid}'}",
  ]
}

# Alarms read metrics only, so each matching event is counted into a metric
# first, with the caller as a dimension.
resource "oci_sch_service_connector" "detection" {
  for_each       = local.connectors
  compartment_id = local.compartment_ocid
  display_name   = "oke-audit-${each.key}"
  description    = "Counts ${each.key} events from the OKE API server audit log into a metric"

  source {
    kind = "logging"
    log_sources {
      compartment_id = local.compartment_ocid
      log_group_id   = "_Audit"
    }
  }

  tasks {
    kind      = "logRule"
    condition = each.value.condition
  }

  target {
    kind             = "monitoring"
    compartment_id   = local.compartment_ocid
    metric_namespace = local.namespace
    metric           = each.value.metric
    dimensions {
      name = "principal"
      dimension_value {
        kind = "jmesPath"
        path = "logContent.data.identity.principalName"
      }
    }
  }

  depends_on = [oci_identity_policy.connector]
}

resource "oci_monitoring_alarm" "detection" {
  for_each              = local.alarms
  compartment_id        = local.compartment_ocid
  metric_compartment_id = local.compartment_ocid
  display_name          = "oke-audit-${each.key}"
  namespace             = local.namespace
  query                 = each.value.query
  severity              = "CRITICAL"
  is_enabled            = true
  destinations          = [oci_ons_notification_topic.audit.id]
  message_format        = "ONS_OPTIMIZED"
  body                  = "Seen in the OKE API server audit log. The caller's OCID is in the alarm's dimensions. In Logging Search, run: search \"${local.compartment_ocid}/_Audit\" | where ${[for c in local.connectors : c.condition if c.metric == each.value.metric][0]}"
}

output "detections" {
  description = "Each alarm, the audit log filters feeding its metric, and its condition."
  value = { for k, a in local.alarms : k => {
    conditions = [for c in local.connectors : c.condition if c.metric == a.metric]
    query      = a.query
  } }
}
