# guardrail-baseline / oci / t1 "Standard" # # A security zone recipe of your own, chosen from Oracle's security zone # policies, applied to several compartments, and an email when anyone changes # or removes a zone or the recipe. # # The zones sit in the compartments they protect, so the alert reads every # compartment's audit events. A connector that sends to Notifications may read # only _Audit, one compartment, so the events are counted into a metric from # _Audit_Include_Subcompartment and an alarm sends the email. # # Security zones need Cloud Guard. Turn it on first, such as with the # compliance evaluation template. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) namespace = "guardrail_baseline" # Oracle lists each policy under a friendly name, such as # "deny public_buckets". Both name fields are checked, so a change in which # one carries it does not break the lookup. all_policies = data.oci_cloud_guard_security_policies.all.security_policy_collection[0].items chosen = { for p in local.all_policies : (contains(var.policies, p.friendly_name) ? p.friendly_name : p.display_name) => p.id... if contains(var.policies, p.friendly_name) || contains(var.policies, p.display_name) } missing = [for name in var.policies : name if !contains(keys(local.chosen), name)] # Cloud Guard API operations that weaken a zone. watched_operations = [ "UpdateSecurityZone", "DeleteSecurityZone", "RemoveCompartment", "UpdateSecurityRecipe", "DeleteSecurityRecipe", ] } data "oci_cloud_guard_security_policies" "all" { compartment_id = var.tenancy_ocid } resource "oci_cloud_guard_security_recipe" "baseline" { compartment_id = var.tenancy_ocid display_name = "guardrail-baseline" description = "Security zone policies chosen in the guardrail baseline template." security_policies = distinct(flatten(values(local.chosen))) freeform_tags = local.tags lifecycle { precondition { condition = length(local.missing) == 0 error_message = "These policies were not found: ${join(", ", local.missing)}. Check the names against the Security Zones policy list." } } } resource "oci_cloud_guard_security_zone" "protected" { for_each = var.compartments compartment_id = each.value display_name = "guardrail-${each.key}" description = "Refuses changes that break the guardrail baseline recipe." security_zone_recipe_id = oci_cloud_guard_security_recipe.baseline.id freeform_tags = local.tags } # --------------------------------------------------------------------------- # An email when a zone or the recipe is weakened # --------------------------------------------------------------------------- resource "oci_ons_notification_topic" "guardrails" { compartment_id = var.tenancy_ocid name = "guardrail-baseline" description = "Changes to security zones and their recipe." freeform_tags = local.tags } resource "oci_ons_subscription" "email" { compartment_id = var.tenancy_ocid topic_id = oci_ons_notification_topic.guardrails.id protocol = "EMAIL" endpoint = var.alert_email freeform_tags = local.tags } # The connector writes the metric as itself, in this namespace only. Deleting # this policy or the topic stops the alarm, and so does moving the connector # out of the root, and nothing reports it. The daily drift plan in Hardened is # what notices. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "guardrail-baseline-connector" description = "Lets the guardrail connector write its metric." 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" "changes" { compartment_id = var.tenancy_ocid display_name = "guardrail-baseline-changes" description = "Counts audit events that change or remove a security zone or its recipe, in every compartment." # 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 = join(" or ", [for op in local.watched_operations : "data.eventName='${op}'"]) } target { kind = "monitoring" compartment_id = var.tenancy_ocid metric_namespace = local.namespace metric = "weakened" dimensions { name = "principal" dimension_value { kind = "jmesPath" path = "logContent.data.identity.principalName" } } dimensions { name = "event" dimension_value { kind = "jmesPath" path = "logContent.data.eventName" } } } depends_on = [oci_identity_policy.connector] } resource "oci_monitoring_alarm" "changes" { compartment_id = var.tenancy_ocid metric_compartment_id = var.tenancy_ocid display_name = "guardrail-baseline-changes" namespace = local.namespace query = "weakened[1m].count() > 0" severity = "CRITICAL" is_enabled = true destinations = [oci_ons_notification_topic.guardrails.id] message_format = "ONS_OPTIMIZED" body = "A security zone or the guardrail recipe was changed or removed. The caller and the operation are in the alarm's dimensions. In Logging Search, search the tenancy's _Audit_Include_Subcompartment for the operation to see the zone." freeform_tags = local.tags }