# breakglass-identity / gcp / t0 "Quick start"
#
# Organisation rights for a break-glass account, and an alert on anything it
# does in any project of the organisation.
#
# First, in the Google Workspace or Cloud Identity admin console, create the
# account outside single sign-on, give it a hardware security key, and seal the
# password where your runbook says. Terraform cannot create it, and never sees
# its password.
#
# Change the four values below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#
# Verify: sign in as the account in a drill and change something harmless,
# such as a label on a test project. The alert email arrives within minutes.

locals {
  org_id           = "123456789012"            # change me: gcloud organizations list
  security_project = "your-security-project"   # change me: where the alert lives
  break_glass      = "break-glass@example.com" # change me
  alert_email      = "oncall@example.com"      # change me
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 8.0"
    }
  }
}

provider "google" {
  project = local.security_project
}

# Organization Administrator can grant any role anywhere in the organisation,
# which is what repairing a broken sign-in setup needs.
resource "google_organization_iam_member" "break_glass" {
  org_id = local.org_id
  role   = "roles/resourcemanager.organizationAdmin"
  member = "user:${local.break_glass}"
}

# Log-based metrics only see their own project's logs, so an organisation sink
# copies this account's entries from every project into one bucket first.
resource "google_logging_project_bucket_config" "break_glass" {
  project        = local.security_project
  location       = "global"
  bucket_id      = "break-glass-activity"
  retention_days = 400
}

resource "google_logging_organization_sink" "break_glass" {
  org_id           = local.org_id
  name             = "break-glass-activity"
  destination      = "logging.googleapis.com/${google_logging_project_bucket_config.break_glass.id}"
  filter           = "protoPayload.authenticationInfo.principalEmail=\"${local.break_glass}\""
  include_children = true
}

resource "google_project_iam_member" "sink_writes" {
  project = local.security_project
  role    = "roles/logging.bucketWriter"
  member  = google_logging_organization_sink.break_glass.writer_identity
}

resource "google_logging_metric" "used" {
  project     = local.security_project
  name        = "break-glass-used"
  bucket_name = google_logging_project_bucket_config.break_glass.id
  filter      = "protoPayload.authenticationInfo.principalEmail=\"${local.break_glass}\""
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}

resource "google_monitoring_notification_channel" "email" {
  project      = local.security_project
  display_name = "Break-glass alerts"
  type         = "email"
  labels = {
    email_address = local.alert_email
  }
}

resource "google_monitoring_alert_policy" "used" {
  project               = local.security_project
  display_name          = "Break-glass account used"
  combiner              = "OR"
  notification_channels = [google_monitoring_notification_channel.email.id]
  conditions {
    display_name = "any activity by ${local.break_glass}"
    condition_threshold {
      filter          = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.used.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 = "The break-glass account acted. In Logs Explorer, open the break-glass-activity bucket in ${local.security_project} to see what it did."
  }
}
