# Saved queries for working a case, shared with everyone who can see the # security project. The entries they search were copied in from other # projects and live in the ai-model-logging bucket. Logs Explorer does not # search it by default: open a query, choose Refine scope, then Scope by # storage, and pick ai-model-logging, plus ai-model-logging-watch for # logging-weakened. The seven-day count runs in Log Analytics, which reads # the bucket directly. locals { saved = merge( { for k, d in local.matches : k => { title = "AI models: ${k}", filter = d.match } }, { one-principal = { title = "AI models: one principal's model calls" filter = "(${local.model_calls}) AND protoPayload.authenticationInfo.principalEmail=\"REPLACE-WITH-EMAIL\"" } # Google Threat Intelligence names a service account key as one LLMjacking # actor's way in. The entry names the key that signed the call. key-calls = { title = "AI models: calls made with a service account key" filter = "(${local.model_calls}) AND protoPayload.authenticationInfo.serviceAccountKeyName:*" } }, local.callers_on ? { unexpected-caller = { title = "AI models: unexpected-caller" filter = local.unexpected_caller } } : {} ) } resource "google_logging_saved_query" "ai" { for_each = local.saved parent = "projects/${var.security_project}" location = "global" name = "ai-model-logging-${each.key}" display_name = each.value.title visibility = "SHARED" logging_query { filter = each.value.filter } } # Who called which model, and how often, over the last seven days. The # resource name is the model, or the endpoint for a model you deployed. The # method test follows Google's Community Security Analytics query. resource "google_logging_saved_query" "calls_by_principal" { parent = "projects/${var.security_project}" location = "global" name = "ai-model-logging-calls-by-principal" display_name = "AI models: calls by principal and model, last seven days" visibility = "SHARED" ops_analytics_query { sql_query_text = <<-EOT SELECT proto_payload.audit_log.authentication_info.principal_email AS principal, proto_payload.audit_log.resource_name AS model, COUNT(*) AS calls FROM `${var.security_project}.global.${google_logging_project_bucket_config.ai.bucket_id}._AllLogs` WHERE timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY) AND proto_payload.audit_log.service_name = "aiplatform.googleapis.com" AND SPLIT(proto_payload.audit_log.method_name, '.')[SAFE_OFFSET(4)] = "PredictionService" AND SPLIT(proto_payload.audit_log.method_name, '.')[SAFE_OFFSET(5)] IN (${join(", ", [for r in local.model_rpcs : "\"${r}\""])}) GROUP BY principal, model ORDER BY calls DESC LIMIT 1000 EOT } }