# Object reads in the projects you name, and an alert when one principal reads # more of them in five minutes than bulk_read_threshold. # # Cloud Storage reads are Data Access audit logs, which are off by default and # billed by volume once on. This turns them on only in the named projects. locals { reads_on = length(var.sensitive_projects) > 0 read_filter = "logName:\"cloudaudit.googleapis.com%2Fdata_access\" AND protoPayload.serviceName=\"storage.googleapis.com\" AND protoPayload.methodName=\"storage.objects.get\"" } # Authoritative for Cloud Storage in each named project: if something else # manages that service's audit setting there, the two will undo each other. resource "google_project_iam_audit_config" "storage_reads" { for_each = toset(var.sensitive_projects) project = each.value service = "storage.googleapis.com" audit_log_config { log_type = "DATA_READ" } } resource "google_logging_project_bucket_config" "reads" { count = local.reads_on ? 1 : 0 project = var.security_project location = "global" bucket_id = "insider-object-reads" retention_days = var.read_retention_days locked = false description = "Cloud Storage object reads from the sensitive projects." } resource "google_logging_project_sink" "reads" { for_each = toset(var.sensitive_projects) project = each.value name = "insider-object-reads" destination = "logging.googleapis.com/${google_logging_project_bucket_config.reads[0].id}" filter = local.read_filter unique_writer_identity = true } resource "google_project_iam_member" "reads_sink_writes" { for_each = google_logging_project_sink.reads project = var.security_project role = "roles/logging.bucketWriter" member = each.value.writer_identity } # One time series per principal, so the alert compares each person's reads # with the threshold on their own. resource "google_logging_metric" "reads" { count = local.reads_on ? 1 : 0 project = var.security_project name = "insider-object-reads" bucket_name = google_logging_project_bucket_config.reads[0].id filter = local.read_filter label_extractors = { principal = "EXTRACT(protoPayload.authenticationInfo.principalEmail)" } metric_descriptor { metric_kind = "DELTA" value_type = "INT64" labels { key = "principal" value_type = "STRING" description = "Who read the object." } } } resource "google_monitoring_alert_policy" "bulk_read" { count = local.reads_on ? 1 : 0 project = var.security_project display_name = "Insider: bulk-read" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = "One principal read more than ${var.bulk_read_threshold} objects in five minutes" condition_threshold { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.reads[0].name}\"" comparison = "COMPARISON_GT" threshold_value = var.bulk_read_threshold duration = "0s" aggregations { alignment_period = "300s" per_series_aligner = "ALIGN_SUM" cross_series_reducer = "REDUCE_SUM" group_by_fields = ["metric.label.principal"] } } } documentation { content = "The alert names the principal in metric.label.principal. Run the saved query \"Insider: one principal\" with that address to see what they read." } }