# k8s-audit-logging / gcp / t1 "Standard" # # Five detections on GKE's audit logs, reads recorded as well as changes, and # an optional route for every cluster audit entry into your SIEM. locals { k8s = "resource.type=\"k8s_cluster\"" detections = merge({ pod-exec = "${local.k8s} AND protoPayload.methodName=(\"io.k8s.core.v1.pods.exec.create\" OR \"io.k8s.core.v1.pods.exec.get\" OR \"io.k8s.core.v1.pods.attach.create\" OR \"io.k8s.core.v1.pods.attach.get\")" cluster-role-binding = "${local.k8s} AND protoPayload.methodName=\"io.k8s.authorization.rbac.v1.clusterrolebindings.create\"" anonymous-request = "${local.k8s} AND protoPayload.authenticationInfo.principalEmail=\"system:anonymous\"" # Refused by authorisation. A burst is someone probing what a stolen # credential can do. forbidden-request = "${local.k8s} AND protoPayload.status.code=7" }, var.log_reads ? { # People reading secrets. Controllers read them constantly under system: # names, so those are left out. secret-read-by-person = "${local.k8s} AND protoPayload.methodName=(\"io.k8s.core.v1.secrets.get\" OR \"io.k8s.core.v1.secrets.list\") AND NOT protoPayload.authenticationInfo.principalEmail=~\"^system:\"" } : {}) } # Reads of Kubernetes objects are Data Access logs under the Kubernetes Engine # API, and those are off by default. This resource owns that API's audit # settings for the whole project; merge first if another tool manages them. resource "google_project_iam_audit_config" "gke_reads" { count = var.log_reads ? 1 : 0 project = var.project_id service = "container.googleapis.com" audit_log_config { log_type = "DATA_READ" } } resource "google_logging_metric" "detection" { for_each = local.detections project = var.project_id name = "gke-${each.key}" filter = each.value metric_descriptor { metric_kind = "DELTA" value_type = "INT64" } } resource "google_monitoring_notification_channel" "email" { for_each = toset(var.alert_emails) project = var.project_id display_name = "GKE audit alerts: ${each.value}" type = "email" labels = { email_address = each.value } } resource "google_monitoring_alert_policy" "detection" { for_each = local.detections project = var.project_id display_name = "GKE: ${each.key}" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = each.key condition_threshold { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.detection[each.key].name}\" AND resource.type=\"k8s_cluster\"" # A single refused request is ordinary. Ten in five minutes is not. comparison = "COMPARISON_GT" threshold_value = each.key == "forbidden-request" ? 9 : 0 duration = "0s" aggregations { alignment_period = "300s" per_series_aligner = "ALIGN_SUM" } } } documentation { content = "Seen in a GKE audit log. In Logs Explorer, search: ${each.value}" } } # Optional: every cluster audit entry also goes to the SIEM's topic. The sink # writes as its own identity, which needs publish rights on that topic. resource "google_logging_project_sink" "to_siem" { count = var.siem_topic == "" ? 0 : 1 project = var.project_id name = "gke-audit-to-siem" destination = "pubsub.googleapis.com/${var.siem_topic}" filter = "${local.k8s} AND logName:\"cloudaudit.googleapis.com\"" unique_writer_identity = true } resource "google_pubsub_topic_iam_member" "sink_publishes" { count = var.siem_topic == "" ? 0 : 1 topic = var.siem_topic role = "roles/pubsub.publisher" member = google_logging_project_sink.to_siem[0].writer_identity }