# k8s-audit-logging / oci / t1 "Standard" # # The quick start's three detections, with each alarm naming the caller. A # copy of every OKE API server audit event is kept past the Audit service's # year, in a bucket with a retention rule, and every event can also go to # your SIEM. # # What the platform gives. OKE sends the API server's administrative calls, # the writes, to the Audit service under the source "OKE API Server Admin # Access", kept 365 days with no setting to change it. Oracle does not say # whether reads appear; the verify output shows how to find out. Keep secrets # out of etcd (the secrets-backend outcome) and keep the right to read them # to a few names, so the question seldom needs the log. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) 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, per caller. 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].groupBy(principal).count() > 0" body = "Someone opened a shell or attached to a pod. The caller's OCID is in the alarm's dimensions; the audit event names the pod." } cluster-role-binding = { metric = "cluster_role_binding" query = "cluster_role_binding[1m].groupBy(principal).count() > 0" body = "A cluster role binding was created. Read it: it may hand cluster-admin to a new subject." } forbidden-request = { metric = "forbidden_request" query = "forbidden_request[5m].groupBy(principal).count() > 9" body = "One caller had ten or more requests refused in five minutes. That is someone probing what a credential can do." } } archive = var.archive_retention_days > 0 siem = var.siem_stream_ocid != "" siem_compartment = var.siem_stream_compartment_ocid != "" ? var.siem_stream_compartment_ocid : var.compartment_ocid } data "oci_objectstorage_namespace" "this" { compartment_id = var.compartment_ocid } resource "oci_ons_notification_topic" "audit" { compartment_id = var.compartment_ocid name = "oke-audit-alerts" description = "Alerts from the OKE API server audit log" freeform_tags = local.tags } resource "oci_ons_subscription" "email" { for_each = toset(var.alert_emails) compartment_id = var.compartment_ocid topic_id = oci_ons_notification_topic.audit.id protocol = "EMAIL" endpoint = each.value freeform_tags = local.tags } # The connectors act as themselves: metrics here, objects in the archive # bucket, and messages on the SIEM stream, each named. Deleting this policy # or the topic stops every alarm, and none of them can report it. So does # moving a connector out of this compartment, for its own alarm. The daily # drift plan in Hardened is what notices. resource "oci_identity_policy" "connector" { compartment_id = var.compartment_ocid name = "oke-audit-connectors" description = "Lets the audit connectors write the ${local.namespace} metrics and the archive" freeform_tags = local.tags statements = concat([ "Allow any-user to use metrics in compartment id ${var.compartment_ocid} where all {request.principal.type='serviceconnector', target.metrics.namespace='${local.namespace}', request.principal.compartment.id='${var.compartment_ocid}'}", ], local.archive ? [ "Allow any-user to manage objects in compartment id ${var.compartment_ocid} where all {request.principal.type='serviceconnector', target.bucket.name='${oci_objectstorage_bucket.archive[0].name}', request.principal.compartment.id='${var.compartment_ocid}'}", ] : []) } resource "oci_identity_policy" "connector_pushes_to_siem" { count = local.siem ? 1 : 0 compartment_id = local.siem_compartment name = "oke-audit-connector-siem" description = "Lets the OKE audit connector push to the SIEM stream" freeform_tags = local.tags statements = [ "Allow any-user to use stream-push in compartment id ${local.siem_compartment} where all {request.principal.type='serviceconnector', target.stream.id='${var.siem_stream_ocid}', request.principal.compartment.id='${var.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 = var.compartment_ocid display_name = "oke-audit-${each.key}" description = "Counts ${each.key} events from the OKE API server audit log into a metric" freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = "_Audit" } } tasks { kind = "logRule" condition = each.value.condition } target { kind = "monitoring" compartment_id = var.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] } # One notification per caller, so two people probing at once are two alarms. resource "oci_monitoring_alarm" "detection" { for_each = local.alarms compartment_id = var.compartment_ocid metric_compartment_id = var.compartment_ocid display_name = "oke-audit-${each.key}" namespace = local.namespace query = each.value.query severity = "CRITICAL" is_enabled = true is_notifications_per_metric_dimension_enabled = true destinations = [oci_ons_notification_topic.audit.id] message_format = "ONS_OPTIMIZED" body = "${each.value.body} In Logging Search, run: search \"${var.compartment_ocid}/_Audit\" | where ${[for c in local.connectors : c.condition if c.metric == each.value.metric][0]}" freeform_tags = local.tags } # The archive: every OKE API server audit event, in a bucket with a retention # rule that refuses deletion and overwriting while it stands. Until the rule # is locked, whoever can manage the bucket can remove the rule; set lock_at # to close that. Retention rules cannot be added to a bucket with versioning # on. resource "oci_objectstorage_bucket" "archive" { count = local.archive ? 1 : 0 compartment_id = var.compartment_ocid namespace = data.oci_objectstorage_namespace.this.namespace name = "oke-audit-archive-${substr(sha1(var.compartment_ocid), 0, 8)}" access_type = "NoPublicAccess" storage_tier = "Standard" versioning = "Disabled" kms_key_id = oci_kms_key.archive[0].id freeform_tags = local.tags retention_rules { display_name = "audit-retention" duration { time_amount = var.archive_retention_days time_unit = "DAYS" } time_rule_locked = var.lock_at == "" ? null : var.lock_at } lifecycle { prevent_destroy = true } depends_on = [oci_identity_policy.storage_uses_key] } resource "oci_sch_service_connector" "archive" { count = local.archive ? 1 : 0 compartment_id = var.compartment_ocid display_name = "oke-audit-archive" description = "Copies every OKE API server audit event to the archive bucket" freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = "_Audit" } } tasks { kind = "logRule" condition = local.source } target { kind = "objectStorage" namespace = data.oci_objectstorage_namespace.this.namespace bucket = oci_objectstorage_bucket.archive[0].name object_name_prefix = "oke-audit" } depends_on = [oci_identity_policy.connector] } # Optional: the same events onto the SIEM's stream. resource "oci_sch_service_connector" "to_siem" { count = local.siem ? 1 : 0 compartment_id = var.compartment_ocid display_name = "oke-audit-to-siem" description = "Sends every OKE API server audit event to the SIEM stream" freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = "_Audit" } } tasks { kind = "logRule" condition = local.source } target { kind = "streaming" stream_id = var.siem_stream_ocid } depends_on = [oci_identity_policy.connector_pushes_to_siem] }