# insider-data-theft / oci / t1 "Standard" # # The quick start's three alarms, with named principals left out and one # notification per caller. reads.tf records object reads on the buckets you # name and alarms on bulk downloads. queries.tf saves the searches behind # every alarm. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) namespace = "insider_data_theft" # The filter language has search, where, equality, and a wildcard inside a # value; parentheses are not documented. A detection that needs an "and" is its own # connector; several connectors may feed one metric. Audit event names are # the API operations' own names. rights_events = ["CreatePolicy", "UpdatePolicy", "ChangeKeyCompartment", "AddUserToGroup", "CreateDynamicGroup", "UpdateDynamicGroup", "CreateCustomerSecretKey", "CreateAuthToken", "UploadApiKey"] # 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", "DeleteRetentionRule"] connectors = { par-created = { metric = "shared_outside" condition = "data.eventName='CreatePreauthenticatedRequest'" } bucket-made-public = { metric = "shared_outside" condition = "data.eventName='UpdateBucket' and data.additionalDetails.publicAccessType='ObjectRead*'" } public-bucket-created = { metric = "shared_outside" condition = "data.eventName='CreateBucket' and data.additionalDetails.publicAccessType='ObjectRead*'" } # Replication copies a bucket to another region, where other policies # may apply; a copy request does the same for one object. data-copied-out = { metric = "shared_outside" condition = "data.eventName='CreateReplicationPolicy' or data.eventName='CopyObject'" } rights-changed = { metric = "rights_changed" condition = join(" or ", [for e in local.rights_events : "data.eventName='${e}'"]) } logging-weakened = { metric = "logging_weakened" condition = join(" or ", [for e in local.logging_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)}\"}" ) alarms = { shared-outside = { metric = "shared_outside" body = "A bucket was made public, a pre-authenticated request was made, or data was copied or replicated to another region." search = "data.eventName='CreatePreauthenticatedRequest' or data.eventName='UpdateBucket' or data.eventName='CreateBucket' or data.eventName='CreateReplicationPolicy' or data.eventName='CopyObject'" } rights-changed = { metric = "rights_changed" body = "A policy, group membership, or dynamic group changed, a key moved compartment, or someone made a credential that works outside the console." search = join(" or ", [for e in local.rights_events : "data.eventName='${e}'"]) } logging-weakened = { metric = "logging_weakened" body = "A log, log group, connector, alarm, alarm suppression, notification topic or subscription, or bucket retention rule was changed, moved, or deleted." search = join(" or ", [for e in local.logging_events : "data.eventName='${e}'"]) } } } resource "oci_ons_notification_topic" "insider" { compartment_id = var.tenancy_ocid name = "insider-data-theft" description = "Alarms on sharing, rights, logging changes, and bulk reads" 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.insider.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 every alarm, 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 = "insider-data-theft-connectors" description = "Lets the insider connectors count audit events and object reads 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.connectors compartment_id = var.tenancy_ocid display_name = "insider-${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 = each.value.condition } 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.alarms compartment_id = var.tenancy_ocid metric_compartment_id = var.tenancy_ocid display_name = "insider-${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.insider.id] message_format = "ONS_OPTIMIZED" body = "${each.value.body} The caller is in the alarm's dimensions. The saved search insider-${each.key} lists the events." freeform_tags = local.tags }