# insider-data-theft / gcp / t1 "Standard" # # The three quick start alerts with named exceptions, an alert on any one # principal reading more objects than a set number from the projects you name, # and saved Logs Explorer queries for working the case. # # The log buckets use Google-managed encryption. Customer-managed keys on log # buckets need a regional bucket location, and these are global so one bucket # serves every region. locals { public = "(\"allUsers\" OR \"allAuthenticatedUsers\")" # Changes by these principals are expected and do not alert. not_expected = length(var.expected_principals) == 0 ? "" : " AND NOT protoPayload.authenticationInfo.principalEmail=(${join(" OR ", [for p in var.expected_principals : "\"${p}\""])})" # What each detection matches. The sink copies every match, and the alerts # skip the expected principals. matches = { shared-publicly = { match = "protoPayload.serviceData.policyDelta.bindingDeltas.member=${local.public} OR protoPayload.metadata.datasetChange.bindingDeltas.member=${local.public}" doc = "Something was shared with allUsers or allAuthenticatedUsers." } decrypt-rights = { match = "protoPayload.serviceName=\"cloudkms.googleapis.com\" AND protoPayload.methodName=\"SetIamPolicy\"" doc = "Someone changed who can use a Cloud KMS key or key ring." } logging-weakened = { match = join(" OR ", [ "protoPayload.methodName=(\"google.logging.v2.ConfigServiceV2.DeleteSink\" OR \"google.logging.v2.ConfigServiceV2.UpdateSink\")", "protoPayload.methodName=(\"google.logging.v2.ConfigServiceV2.DeleteBucket\" OR \"google.logging.v2.ConfigServiceV2.UpdateBucket\")", "protoPayload.methodName=(\"google.logging.v2.ConfigServiceV2.CreateExclusion\" OR \"google.logging.v2.ConfigServiceV2.UpdateExclusion\")", "protoPayload.serviceData.policyDelta.auditConfigDeltas.action=\"REMOVE\"", ]) doc = "A log sink, log bucket, or exclusion changed, or a Data Access audit setting was removed." } } any_match = join(" OR ", [for d in local.matches : "(${d.match})"]) # Kept in the bucket for investigations, without an alert: grants happen all # day, and they matter when they come just before one of the moves above. permission_adds = "protoPayload.methodName:\"SetIamPolicy\" AND protoPayload.serviceData.policyDelta.bindingDeltas.action=\"ADD\"" detections = { for k, d in local.matches : k => { filter = "(${d.match})${local.not_expected}", doc = d.doc } } } resource "google_logging_project_bucket_config" "insider" { project = var.security_project location = "global" bucket_id = "insider-data-theft" retention_days = var.retention_days locked = false description = "Organisation-wide entries for public sharing, key access changes, logging changes, and IAM grants." } # The sink copies every match, including the expected principals', so the # bucket is complete even where the alerts are quiet. resource "google_logging_organization_sink" "insider" { org_id = var.org_id name = "insider-data-theft" destination = "logging.googleapis.com/${google_logging_project_bucket_config.insider.id}" filter = "${local.any_match} OR (${local.permission_adds})" include_children = true } resource "google_project_iam_member" "sink_writes" { project = var.security_project role = "roles/logging.bucketWriter" member = google_logging_organization_sink.insider.writer_identity } resource "google_logging_metric" "detection" { for_each = local.detections project = var.security_project name = "insider-${each.key}" bucket_name = google_logging_project_bucket_config.insider.id filter = each.value.filter metric_descriptor { metric_kind = "DELTA" value_type = "INT64" } } resource "google_monitoring_notification_channel" "email" { for_each = toset(var.alert_emails) project = var.security_project display_name = "Insider data theft: ${each.value}" type = "email" labels = { email_address = each.value } } resource "google_monitoring_alert_policy" "detection" { for_each = local.detections project = var.security_project display_name = "Insider: ${each.key}" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = each.value.doc condition_threshold { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.detection[each.key].name}\"" comparison = "COMPARISON_GT" threshold_value = 0 duration = "0s" aggregations { alignment_period = "60s" per_series_aligner = "ALIGN_SUM" cross_series_reducer = "REDUCE_SUM" } } } documentation { content = "${each.value.doc} Run the saved query \"Insider: changes by person\" in ${var.security_project} to see who, what, and where." } }