# The alerts. Log-based metrics on the bucket count each match, and an alert # fires on the first one. resource "google_logging_metric" "detection" { for_each = local.detections project = var.security_project name = "workspace-${each.key}" bucket_name = google_logging_project_bucket_config.workspace.id filter = each.value.filter metric_descriptor { metric_kind = "DELTA" value_type = "INT64" } } resource "google_monitoring_alert_policy" "detection" { for_each = local.detections project = var.security_project display_name = "Workspace: ${each.key}" user_labels = local.labels 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 \"Workspace: ${each.key}\" in ${var.security_project} to see who, from where, and on whom." } } # Workspace records every sign-in, so a quiet stretch means sharing was # turned off or the sink stopped. resource "google_logging_metric" "sign_ins" { project = var.security_project name = "workspace-sign-in-events" bucket_name = google_logging_project_bucket_config.workspace.id filter = "protoPayload.serviceName=\"login.googleapis.com\"" metric_descriptor { metric_kind = "DELTA" value_type = "INT64" } } resource "google_monitoring_alert_policy" "silence" { project = var.security_project display_name = "Workspace: signals silent" user_labels = local.labels combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = "No Workspace sign-in events for ${var.silence_minutes} minutes" condition_absent { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.sign_ins.name}\"" duration = "${var.silence_minutes * 60}s" aggregations { alignment_period = "300s" per_series_aligner = "ALIGN_SUM" cross_series_reducer = "REDUCE_SUM" } } } documentation { content = "No Workspace sign-in events reached the workspace-signals bucket for ${var.silence_minutes} minutes. Check that sharing with Google Cloud is still on in the Admin console, and that the workspace-signals organisation sink exists." } }