# ai-model-logging / oci / t1 "Standard" # # The quick start's four alarms, with named principals left out and one # notification per caller. queries.tf saves the search behind every alarm, # and one more for model calls, which Oracle does not say Audit records. # # Nothing here takes a key of your own. In provider 9.8.0 the topic, # subscriptions, policy, connectors, alarms, and saved searches have no key # argument. Nothing here stores data of its own either: the record is # Oracle's audit log, kept 365 days. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) namespace = "ai_model_logging" # 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_events = ["CreateEndpoint", "UpdateEndpoint", "DeleteEndpoint", "ChangeEndpointCompartment", "CreateAgentEndpoint", "UpdateAgentEndpoint", "DeleteAgentEndpoint", "ChangeAgentEndpointCompartment", "CreateRoutingProfile", "UpdateRoutingProfile", "DeleteRoutingProfile", "ChangeRoutingProfileCompartment"] # Identity's API signing keys share the names CreateApiKey and DeleteApiKey. api_key_events = ["CreateApiKey", "RenewApiKey", "SetApiKeyState", "UpdateApiKey", "DeleteApiKey", "ChangeApiKeyCompartment"] # Data Science, AI Language, AI Vision, and Document Understanding also # name an operation CreateModel. hosting_events = ["CreateDedicatedAiCluster", "CreateModel", "CreateImportedModel"] # A connector moved out of the root loses its grant to write metrics, so # moves count here too. logging_events = ["DeleteLog", "UpdateLog", "DeleteLogGroup", "UpdateLogGroup", "DeleteServiceConnector", "DeactivateServiceConnector", "UpdateServiceConnector", "ChangeServiceConnectorCompartment", "DeleteAlarm", "UpdateAlarm", "CreateAlarmSuppression", "ChangeAlarmCompartment", "DeleteTopic", "UpdateTopic", "ChangeTopicCompartment", "DeleteSubscription", "UpdateSubscription"] # 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 = { endpoint-changed = { metric = "endpoint_changed" events = local.endpoint_events 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-key-changed = { metric = "api_key_changed" events = local.api_key_events 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." } hosting-created = { metric = "hosting_created" events = local.hosting_events 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." } logging-weakened = { metric = "logging_weakened" events = local.logging_events body = "A log, log group, connector, alarm, alarm suppression, notification topic, or subscription was changed, moved, or deleted." } } conditions = { for k, d in local.detections : k => join(" or ", [for e in d.events : "data.eventName='${e}'"]) } # Expected principals are left out in the alarm's query: the connector's # filter has no inequality, and the metric keeps every caller. Monitoring # takes != for one value and !~ with | between values for several. exclude = ( length(var.expected_principals) == 0 ? "" : length(var.expected_principals) == 1 ? "{principal != \"${var.expected_principals[0]}\"}" : "{principal !~ \"${join("|", var.expected_principals)}\"}" ) } resource "oci_ons_notification_topic" "ai" { compartment_id = var.tenancy_ocid name = "ai-model-logging" description = "Alarms on Generative AI endpoints, API keys, new hosting, and logging changes" freeform_tags = local.tags } resource "oci_ons_subscription" "email" { for_each = toset(var.alert_emails) compartment_id = var.tenancy_ocid topic_id = oci_ons_notification_topic.ai.id protocol = "EMAIL" endpoint = each.value freeform_tags = local.tags } # The connectors write the metrics as themselves, in this namespace only. # Deleting this policy or the topic stops all four alarms, and none of them # can report it. The daily drift plan in Hardened is what notices. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "ai-model-logging-connectors" description = "Lets the AI model logging connectors count audit events into the ${local.namespace} metrics" freeform_tags = local.tags statements = [ "Allow any-user to use metrics in tenancy where all {request.principal.type='serviceconnector', target.metrics.namespace='${local.namespace}', request.principal.compartment.id='${var.tenancy_ocid}'}", ] } resource "oci_sch_service_connector" "detection" { for_each = local.detections compartment_id = var.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" freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.tenancy_ocid log_group_id = "_Audit_Include_Subcompartment" } } tasks { kind = "logRule" condition = local.conditions[each.key] } target { kind = "monitoring" compartment_id = var.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] } # One notification per caller, so two people at once are two alarms. resource "oci_monitoring_alarm" "detection" { for_each = local.detections compartment_id = var.tenancy_ocid metric_compartment_id = var.tenancy_ocid display_name = "ai-${each.key}" namespace = local.namespace query = "${each.value.metric}[1m]${local.exclude}.groupBy(principal).count() > 0" severity = "CRITICAL" is_enabled = true is_notifications_per_metric_dimension_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. The saved search ai-${each.key} lists the events." freeform_tags = local.tags }