# ai-model-logging / oci / t0 "Quick start"
#
# Four alarms on the moves around Oracle's Generative AI models. The first
# is a model endpoint, Agents endpoint, or routing profile created, changed,
# moved, or deleted. Guardrails and content moderation are endpoint
# settings, and a routing profile decides which models and regions on-demand
# calls may use. The second is a Generative AI API key made, renewed,
# switched on or off, changed, moved, or deleted. The third is new paid
# capacity: a dedicated AI cluster, or a custom or imported model. The
# fourth is a log, connector, alarm, or notification changed, moved, or
# deleted.
#
# Generative AI keeps no record of prompts or responses unless a project is
# set to store them. It is on neither Oracle's list of services with logs
# nor its list of services that send events. What is left is the Audit
# service, which records API calls in every compartment by the operation's
# name and keeps them 365 days. Oracle does not say whether Audit records
# model calls, so no alarm here depends on them.
#
# Alarms read metrics only, so each detection is a connector that counts
# matching audit events into a metric, with the caller as a dimension, and an
# alarm on that metric.
#
# IAM resources can only be made in the home region, so this is applied
# there, and the connectors read that region's audit log. Oracle's log
# aggregation guide repeats collection in every subscribed region, so treat
# models, endpoints, and keys in other regions as unwatched.
#
# Change the three values below. Credentials come from ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Verify: confirm the subscription email first. Then, in a test compartment,
# create a Generative AI API key in the console, and delete it. The
# ai-api-key-changed alarm email arrives within about 15 minutes, the time
# Audit usually takes to record a call.

locals {
  tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaa" # change me
  region       = "us-ashburn-1"                # change me: the home region
  alert_email  = "security-alerts@example.com" # change me

  namespace = "ai_model_logging"

  # The filter language has search, where, equality, and a wildcard inside a
  # value; parentheses are not documented. Each detection here is one list of event
  # names joined by "or", so each is one connector. Audit event names are the
  # API operations' own names. Connector Hub allows 20 connectors per region
  # by default and five on a trial account; this uses four.
  detections = {
    # Endpoints on dedicated AI clusters and Agents endpoints carry the
    # guardrails. A routing profile decides which models and regions
    # on-demand calls may use. AI Language endpoints use the same four
    # endpoint operation names, so their changes count here too.
    endpoint-changed = {
      metric = "endpoint_changed"
      events = ["CreateEndpoint", "UpdateEndpoint", "DeleteEndpoint", "ChangeEndpointCompartment", "CreateAgentEndpoint", "UpdateAgentEndpoint", "DeleteAgentEndpoint", "ChangeAgentEndpointCompartment", "CreateRoutingProfile", "UpdateRoutingProfile", "DeleteRoutingProfile", "ChangeRoutingProfileCompartment"]
      body   = "A Generative AI endpoint, Agents endpoint, or routing profile was created, changed, moved, or deleted. Endpoints carry the content moderation, prompt injection, and PII guardrails. A routing profile decides which models and regions on-demand calls may use. AI Language endpoints share four of these event names."
    }
    # API keys are bearer secrets that call models. Identity's API signing
    # keys share the names CreateApiKey and DeleteApiKey, so those count too.
    api-key-changed = {
      metric = "api_key_changed"
      events = ["CreateApiKey", "RenewApiKey", "SetApiKeyState", "UpdateApiKey", "DeleteApiKey", "ChangeApiKeyCompartment"]
      body   = "A Generative AI API key was created, renewed, switched on or off, changed, moved, or deleted. Anyone with the key can call models. Identity's API signing keys share two of these event names."
    }
    # Data Science, AI Language, AI Vision, and Document Understanding also
    # name an operation CreateModel, so their new models count too.
    hosting-created = {
      metric = "hosting_created"
      events = ["CreateDedicatedAiCluster", "CreateModel", "CreateImportedModel"]
      body   = "A dedicated AI cluster, custom model, or imported model was created. A hosting cluster for a pretrained or fine-tuned model commits to 744 unit-hours at least. Other AI services share CreateModel."
    }
    # A connector moved out of the root loses its grant to write metrics,
    # so moves count here too.
    logging-weakened = {
      metric = "logging_weakened"
      events = ["DeleteLog", "UpdateLog", "DeleteLogGroup", "UpdateLogGroup", "DeleteServiceConnector", "DeactivateServiceConnector", "UpdateServiceConnector", "ChangeServiceConnectorCompartment", "DeleteAlarm", "UpdateAlarm", "CreateAlarmSuppression", "ChangeAlarmCompartment", "DeleteTopic", "UpdateTopic", "ChangeTopicCompartment", "DeleteSubscription", "UpdateSubscription"]
      body   = "A log, log group, connector, alarm, alarm suppression, notification topic, or subscription was changed, moved, or deleted."
    }
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 9.0"
    }
  }
}

provider "oci" {
  region = local.region
}

resource "oci_ons_notification_topic" "ai" {
  compartment_id = local.tenancy_ocid
  name           = "ai-model-logging"
  description    = "Alarms on Generative AI endpoints, API keys, new hosting, and logging changes"
}

resource "oci_ons_subscription" "email" {
  compartment_id = local.tenancy_ocid
  topic_id       = oci_ons_notification_topic.ai.id
  protocol       = "EMAIL"
  endpoint       = local.alert_email
}

# The connectors write the metrics as themselves, in this namespace only.
resource "oci_identity_policy" "connector" {
  compartment_id = local.tenancy_ocid
  name           = "ai-model-logging-connectors"
  description    = "Lets the AI model logging connectors count audit events into the ${local.namespace} metrics"
  statements = [
    "Allow any-user to use metrics in tenancy where all {request.principal.type='serviceconnector', target.metrics.namespace='${local.namespace}', request.principal.compartment.id='${local.tenancy_ocid}'}",
  ]
}

resource "oci_sch_service_connector" "detection" {
  for_each       = local.detections
  compartment_id = local.tenancy_ocid
  display_name   = "ai-${each.key}"
  description    = "Counts ${each.key} audit events, from every compartment, into a metric"
  # Set, so a connector switched off by hand shows in the daily drift plan.
  state = "ACTIVE"

  source {
    kind = "logging"
    log_sources {
      compartment_id = local.tenancy_ocid
      log_group_id   = "_Audit_Include_Subcompartment"
    }
  }

  tasks {
    kind      = "logRule"
    condition = join(" or ", [for e in each.value.events : "data.eventName='${e}'"])
  }

  target {
    kind             = "monitoring"
    compartment_id   = local.tenancy_ocid
    metric_namespace = local.namespace
    metric           = each.value.metric
    dimensions {
      name = "principal"
      dimension_value {
        kind = "jmesPath"
        path = "logContent.data.identity.principalName"
      }
    }
  }

  depends_on = [oci_identity_policy.connector]
}

resource "oci_monitoring_alarm" "detection" {
  for_each              = local.detections
  compartment_id        = local.tenancy_ocid
  metric_compartment_id = local.tenancy_ocid
  display_name          = "ai-${each.key}"
  namespace             = local.namespace
  query                 = "${each.value.metric}[1m].count() > 0"
  severity              = "CRITICAL"
  is_enabled            = true
  destinations          = [oci_ons_notification_topic.ai.id]
  message_format        = "ONS_OPTIMIZED"
  body                  = "${each.value.body} The caller is in the alarm's dimensions. In Logging Search, search the tenancy's _Audit_Include_Subcompartment for the event names in this alarm's connector."
}
