# ai-model-logging / gcp / t0 "Quick start"
#
# A record of Vertex AI model calls across the organisation, kept for 400 days
# in one log bucket. It matches the 15 inference methods in Google's API
# definitions. Four alerts go with it: logging turned down, guardrails
# changed, the Vertex AI API turned on, and someone listing models.
#
# Model calls are Data Access audit logs, which are off by default. This turns
# them on for Vertex AI across the organisation. Each entry names the caller
# and the model. Prompts and responses are not in it.
#
# Change the three values below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#
# The audit setting goes on last, once the alerts exist, so the apply itself
# should email "AI models: logging-weakened" within minutes. If it does not,
# the sinks' write grants were still taking effect; use the drill below.
#
# Verify: in a test project with Vertex AI on, run
#   gcloud ai models list --region=us-central1
# The model-recon alert email arrives within minutes.

locals {
  org_id           = "123456789012"                # change me: gcloud organizations list
  security_project = "your-security-project"       # change me: where the record and alerts live
  alert_email      = "security-alerts@example.com" # change me

  # Every inference method in Google's v1 and v1beta1 PredictionService
  # definitions. The names come from those definitions, and a published log
  # sample confirms Predict, GenerateContent, StreamGenerateContent, and
  # StreamRawPredict. CountTokens is left out: it counts tokens and runs no
  # model.
  model_rpcs = [
    "Predict", "RawPredict", "StreamRawPredict", "DirectPredict", "DirectRawPredict",
    "StreamDirectPredict", "StreamDirectRawPredict", "StreamingPredict", "ServerStreamingPredict",
    "StreamingRawPredict", "Explain", "GenerateContent", "StreamGenerateContent", "ChatCompletions",
    "EmbedContent",
  ]
  # The method name carries the API version the caller used, v1 or v1beta1,
  # so this matches on the end of the name.
  model_calls = "protoPayload.serviceName=\"aiplatform.googleapis.com\" AND protoPayload.methodName=~\"[.]PredictionService[.](${join("|", local.model_rpcs)})$\""

  detections = {
    # Any change to a Data Access audit setting. A removal turns logging off,
    # and an addition that exempts a member narrows it, so every change
    # alerts. So does taking a sink's write grant away. Sinks and buckets
    # route and keep logs, an exclusion drops them before they are stored,
    # and UpdateSettings can start new projects with the _Default sink off.
    logging-weakened = {
      filter = join(" OR ", [
        "(protoPayload.methodName=\"SetIamPolicy\" AND protoPayload.serviceData.policyDelta.auditConfigDeltas:*)",
        "(protoPayload.methodName=\"SetIamPolicy\" AND protoPayload.serviceData.policyDelta.bindingDeltas.action=\"REMOVE\" AND protoPayload.serviceData.policyDelta.bindingDeltas.role=\"roles/logging.bucketWriter\")",
        "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\" OR \"google.logging.v2.ConfigServiceV2.UpdateBucketAsync\")",
        "protoPayload.methodName=(\"google.logging.v2.ConfigServiceV2.CreateExclusion\" OR \"google.logging.v2.ConfigServiceV2.UpdateExclusion\" OR \"google.logging.v2.ConfigServiceV2.UpdateSettings\")",
      ])
      doc = "A Data Access audit setting, log sink, log bucket, exclusion, sink write grant, or logging setting changed."
    }
    # Model Armor logs these as google.cloud.modelarmor.v1.ModelArmor.UpdateFloorSetting
    # and so on, and audits its v1alpha and v1beta forms too. The second part
    # watches the allowed-models organisation policy. Unverified: no published
    # sample shows how Organization Policy writes are logged, so it assumes
    # the service orgpolicy.googleapis.com and the constraint in the resource
    # name.
    guardrails-weakened = {
      filter = "(protoPayload.serviceName=\"modelarmor.googleapis.com\" AND protoPayload.methodName=~\"[.]ModelArmor[.](UpdateFloorSetting|UpdateTemplate|DeleteTemplate)$\") OR (protoPayload.serviceName=\"orgpolicy.googleapis.com\" AND protoPayload.resourceName:\"vertexai.allowedModels\")"
      doc    = "A Model Armor floor setting or template, or the allowed-models policy, was changed or deleted."
    }
    # Google Threat Intelligence lists EnableService under enablement in its
    # LLMjacking detection guidance for Google Cloud (November 2026). The
    # service name can sit in the resource name or the request, so this looks
    # anywhere in the entry.
    ai-api-enabled = {
      filter = "protoPayload.methodName=~\"[.]ServiceUsage[.](EnableService|BatchEnableServices)$\" AND \"aiplatform.googleapis.com\""
      doc    = "The Vertex AI API was turned on in a project."
    }
    # Listing and looking up models and endpoints. These read metadata, so
    # they are ADMIN_READ Data Access entries, which this file turns on.
    # Google Threat Intelligence names PublisherModelService.ListPublisherModels,
    # and Google's API definitions put the publisher model calls in
    # ModelGardenService, so those two match on the method alone. Noisy in
    # projects that use Vertex AI; it is here for the projects that never
    # should.
    model-recon = {
      filter = "protoPayload.serviceName=\"aiplatform.googleapis.com\" AND protoPayload.methodName=~\"[.](ModelService[.]ListModels|EndpointService[.]ListEndpoints|ListPublisherModels|GetPublisherModel)$\""
      doc    = "Someone listed or looked up Vertex AI models or endpoints."
    }
  }
}

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

provider "google" {
  project = local.security_project
}

# Data Access audit logs for Vertex AI in every project in the organisation.
# They are off by default. Model calls are DATA_READ entries that name the
# caller and the model, with no prompt text. They land in each project's
# _Default bucket at standard rates, and the sink below copies them here.
#
# A published test confirms StreamRawPredict as DATA_READ. Google's table of
# each method's type could not be read, so all three types are on, and every
# call is recorded whichever type it is.
#
# Authoritative for Vertex AI at the organisation: if something else manages
# this service's audit setting there, the two will undo each other.
resource "google_organization_iam_audit_config" "vertex_ai" {
  org_id  = local.org_id
  service = "aiplatform.googleapis.com"
  audit_log_config {
    log_type = "ADMIN_READ"
  }
  audit_log_config {
    log_type = "DATA_READ"
  }
  audit_log_config {
    log_type = "DATA_WRITE"
  }

  # After the alerts, so the change this makes is the first one they report.
  depends_on = [google_monitoring_alert_policy.detection, google_project_iam_member.sink_writes, google_project_iam_member.watch_writes]
}

# Log-based metrics only see their own project's logs, so an organisation sink
# copies the model calls and the matching changes from every project into one
# bucket first.
resource "google_logging_project_bucket_config" "ai" {
  project        = local.security_project
  location       = "global"
  bucket_id      = "ai-model-logging"
  retention_days = 400
}

resource "google_logging_organization_sink" "ai" {
  org_id           = local.org_id
  name             = "ai-model-logging"
  destination      = "logging.googleapis.com/${google_logging_project_bucket_config.ai.id}"
  filter           = join(" OR ", concat(["(${local.model_calls})"], [for d in local.detections : "(${d.filter})"]))
  include_children = true
}

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

# A second sink and bucket that carry logging-weakened's entries and nothing
# else. Deleting or disabling either sink, deleting either bucket, or taking
# either write grant away is a logging-weakened entry, and the other feed
# carries it to the alert.
resource "google_logging_project_bucket_config" "watch" {
  project        = local.security_project
  location       = "global"
  bucket_id      = "ai-model-logging-watch"
  retention_days = 400
}

resource "google_logging_organization_sink" "watch" {
  org_id           = local.org_id
  name             = "ai-model-logging-watch"
  destination      = "logging.googleapis.com/${google_logging_project_bucket_config.watch.id}"
  filter           = local.detections["logging-weakened"].filter
  include_children = true
}

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

resource "google_logging_metric" "detection" {
  for_each    = local.detections
  project     = local.security_project
  name        = "ai-model-logging-${each.key}"
  bucket_name = google_logging_project_bucket_config.ai.id
  filter      = each.value.filter
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}

resource "google_logging_metric" "watch" {
  project     = local.security_project
  name        = "ai-model-logging-logging-weakened-watch"
  bucket_name = google_logging_project_bucket_config.watch.id
  filter      = local.detections["logging-weakened"].filter
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}

resource "google_monitoring_notification_channel" "email" {
  project      = local.security_project
  display_name = "AI model logging alerts"
  type         = "email"
  labels = {
    email_address = local.alert_email
  }
}

# One condition per bucket the alert counts in. logging-weakened counts in
# both, so either feed can report the other being cut.
resource "google_monitoring_alert_policy" "detection" {
  for_each              = local.detections
  project               = local.security_project
  display_name          = "AI models: ${each.key}"
  combiner              = "OR"
  notification_channels = [google_monitoring_notification_channel.email.id]
  dynamic "conditions" {
    for_each = merge(
      { "ai-model-logging" = google_logging_metric.detection[each.key].name },
      each.key == "logging-weakened" ? { "ai-model-logging-watch" = google_logging_metric.watch.name } : {},
    )
    content {
      display_name = "${each.value.doc} Counted in ${conditions.key}."
      condition_threshold {
        filter          = "metric.type=\"logging.googleapis.com/user/${conditions.value}\""
        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} Open the ${each.key == "logging-weakened" ? "ai-model-logging and ai-model-logging-watch log buckets" : "ai-model-logging log bucket"} in ${local.security_project} to see who, what, and where."
  }
}
