# ai-model-logging / gcp / t1 "Standard" # # The quick start's record of Vertex AI model calls, its watch sink, and its # four alerts, with named exceptions and your own retention. Three lists switch on more. One # alerts on model calls by anyone outside a named set. One sets an # organisation policy naming the models that may be used. One turns on Model # Armor screening in chosen projects (guardrails.tf). Saved queries for # working the case are in queries.tf. # # The log bucket uses Google-managed encryption. Customer-managed keys on log # buckets need a regional bucket location, and this one is global so one # bucket serves every region. locals { # 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}\""])})" # 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. Each entry names the caller and # the model. Prompts and responses are not in it. model_calls = "protoPayload.serviceName=\"aiplatform.googleapis.com\" AND protoPayload.methodName=~\"[.]PredictionService[.](${join("|", local.model_rpcs)})$\"" # What each detection matches. The sink copies every match, and the # configuration change alerts skip the expected principals. matches = { # 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 = { match = 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." change = true } # 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 = { match = "(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." change = true } # 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 = { match = "protoPayload.methodName=~\"[.]ServiceUsage[.](EnableService|BatchEnableServices)$\" AND \"aiplatform.googleapis.com\"" doc = "The Vertex AI API was turned on in a project." change = true } # Listing and looking up models and endpoints. These read metadata, so # they are ADMIN_READ Data Access entries, which this folder 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. Listing is no change, so it alerts for everyone. model-recon = { match = "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." change = false } } any_match = join(" OR ", [for d in local.matches : "(${d.match})"]) detections = { for k, d in local.matches : k => { filter = d.change ? "(${d.match})${local.not_expected}" : d.match, doc = d.doc } } # Model calls by anyone outside model_callers. callers_on = length(var.model_callers) > 0 unexpected_caller = local.callers_on ? "(${local.model_calls}) AND NOT protoPayload.authenticationInfo.principalEmail=(${join(" OR ", [for p in var.model_callers : "\"${p}\""])})" : "" } # 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 = var.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, # unless the identity applying it is in expected_principals. depends_on = [google_monitoring_alert_policy.detection, google_project_iam_member.sink_writes, google_project_iam_member.watch_writes] } # Log Analytics is on so the seven-day count in queries.tf can run as SQL. # Google adds no charge for it, and it cannot be turned off once on. resource "google_logging_project_bucket_config" "ai" { project = var.security_project location = "global" bucket_id = "ai-model-logging" retention_days = var.retention_days locked = false enable_analytics = true description = "Organisation-wide Vertex AI model calls, and changes to logging, Model Armor, and enabled APIs." } # 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" "ai" { org_id = var.org_id name = "ai-model-logging" destination = "logging.googleapis.com/${google_logging_project_bucket_config.ai.id}" filter = "(${local.model_calls}) OR ${local.any_match}" include_children = true } resource "google_project_iam_member" "sink_writes" { project = var.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. One change that cuts both feeds at once is the gap # left. resource "google_logging_project_bucket_config" "watch" { project = var.security_project location = "global" bucket_id = "ai-model-logging-watch" retention_days = var.retention_days locked = false description = "Organisation-wide changes to logging, kept apart so cutting the main feed still alerts." } resource "google_logging_organization_sink" "watch" { org_id = var.org_id name = "ai-model-logging-watch" destination = "logging.googleapis.com/${google_logging_project_bucket_config.watch.id}" filter = local.matches["logging-weakened"].match include_children = true } resource "google_project_iam_member" "watch_writes" { project = var.security_project role = "roles/logging.bucketWriter" member = google_logging_organization_sink.watch.writer_identity } resource "google_logging_metric" "detection" { for_each = local.detections project = var.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 = var.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" { for_each = toset(var.alert_emails) project = var.security_project display_name = "AI model logging: ${each.value}" type = "email" labels = { email_address = each.value } } # 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 = var.security_project display_name = "AI models: ${each.key}" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.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} Run the saved query \"AI models: ${each.key}\" in ${var.security_project} to see who, what, and where." } } # One time series per principal, so the alert names who made the call, on # their first call. A stolen key for an account on the list calls as that # account and does not alert here. The saved query for key calls covers it. resource "google_logging_metric" "unexpected_caller" { count = local.callers_on ? 1 : 0 project = var.security_project name = "ai-model-logging-unexpected-caller" bucket_name = google_logging_project_bucket_config.ai.id filter = local.unexpected_caller label_extractors = { principal = "EXTRACT(protoPayload.authenticationInfo.principalEmail)" } metric_descriptor { metric_kind = "DELTA" value_type = "INT64" labels { key = "principal" value_type = "STRING" description = "Who called the model." } } } resource "google_monitoring_alert_policy" "unexpected_caller" { count = local.callers_on ? 1 : 0 project = var.security_project display_name = "AI models: unexpected-caller" combiner = "OR" notification_channels = [for c in google_monitoring_notification_channel.email : c.id] conditions { display_name = "A principal outside model_callers called a Vertex AI model" condition_threshold { filter = "metric.type=\"logging.googleapis.com/user/${google_logging_metric.unexpected_caller[0].name}\"" comparison = "COMPARISON_GT" threshold_value = 0 duration = "0s" aggregations { alignment_period = "60s" 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 \"AI models: one principal's model calls\" with that address to see which models they called. \"AI models: calls made with a service account key\" shows whether a key was used." } }