# data-perimeter / oci / t1 "Standard" # # The quick start's perimeter around several compartments, with the VCNs # your workloads read from counted as inside. The network source itself is # locked, an email goes out when the perimeter is touched, and a security # zone can refuse the bucket settings that would carry data around it. # # Deny policies must be switched on for the tenancy first, once, in the # console, and that cannot be undone. The Administrators group of the # default domain is exempt from every deny policy. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) compartments = toset(var.compartment_ocids) zones = var.security_zone_recipe_ocid != "" ? local.compartments : toset([]) source_name = oci_identity_network_source.inside.name } # The networks that count as inside: public ranges, and subnets of your own # VCNs reaching Object Storage through a service gateway. Names are unique in # the tenancy. resource "oci_identity_network_source" "inside" { compartment_id = var.tenancy_ocid name = "data-perimeter" description = "Networks from which this tenancy's data may be read" public_source_list = var.office_cidrs freeform_tags = local.tags dynamic "virtual_source_list" { for_each = var.vcns content { vcn_id = virtual_source_list.value.vcn_ocid ip_ranges = virtual_source_list.value.ip_ranges } } lifecycle { precondition { condition = length(var.office_cidrs) + length(var.vcns) > 0 error_message = "Name at least one office range or VCN, or nothing at all can read the data." } } } # One deny policy per compartment. From outside the network source, reads of # objects and changes to buckets are refused for every principal. # Pre-authenticated requests cannot be made at all. Policy names are unique # in the tenancy, so each carries its compartment. resource "oci_identity_policy" "perimeter" { for_each = local.compartments compartment_id = each.value name = "data-perimeter-${substr(sha1(each.value), 0, 8)}" description = "Objects here are read and buckets changed from the named networks only. No pre-authenticated requests." freeform_tags = local.tags statements = [ "Deny any-user to { OBJECT_READ } in compartment id ${each.value} where request.networkSource.name != '${local.source_name}'", "Deny any-user to { BUCKET_UPDATE } in compartment id ${each.value} where request.networkSource.name != '${local.source_name}'", "Deny any-user to { PAR_MANAGE } in compartment id ${each.value}", ] } # Whoever can manage network sources could add their own address to this # one. Only the default domain's Administrators, who are exempt, may now. resource "oci_identity_policy" "lock_network_sources" { compartment_id = var.tenancy_ocid name = "data-perimeter-network-sources" description = "Only the default domain's Administrators may change network sources" freeform_tags = local.tags statements = [ "Deny any-user to manage network-sources in tenancy", ] } # A security zone refuses new violations: a public bucket, a bucket without # your own key, a resource moved out of the zone. Existing ones are reported # through Cloud Guard. A compartment can be in one zone only. resource "oci_cloud_guard_security_zone" "perimeter" { for_each = local.zones compartment_id = each.value display_name = "data-perimeter-${substr(sha1(each.value), 0, 8)}" description = "Bucket settings that would carry data around the perimeter are refused" security_zone_recipe_id = var.security_zone_recipe_ocid freeform_tags = local.tags } # An email when the perimeter is touched: the policies, the network source, # or a zone, changed or deleted, by anyone. The topic, its subscriptions, and # the connector's policy are watched too. resource "oci_ons_notification_topic" "perimeter" { compartment_id = var.tenancy_ocid name = "data-perimeter" description = "Changes to the data perimeter" 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.perimeter.id protocol = "EMAIL" endpoint = each.value freeform_tags = local.tags } # Deleting this policy or the topic stops the watch, and so does moving the # connector out of the root, which drops its grant. None of these can be # reported through the watch itself. The daily drift plan in Hardened is what # notices. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "data-perimeter-connector" description = "Lets the perimeter's audit connector publish to its topic" freeform_tags = local.tags statements = [ "Allow any-user to use ons-topics in tenancy where all {request.principal.type='serviceconnector', target.topic.id='${oci_ons_notification_topic.perimeter.id}', request.principal.compartment.id='${var.tenancy_ocid}'}", ] } # The watch also covers its own path: the topic, its subscriptions, and the # connector's policy. A change there is mailed when the watch still works # after it, and a subscription deleted is mailed to the others. locals { watched = concat( [oci_identity_network_source.inside.id, oci_identity_policy.lock_network_sources.id], [for p in oci_identity_policy.perimeter : p.id], [for z in oci_cloud_guard_security_zone.perimeter : z.id], [oci_ons_notification_topic.perimeter.id, oci_identity_policy.connector.id], [for s in oci_ons_subscription.email : s.id], ) } # A connector sending to a topic reads _Audit only, compartment by # compartment: the tenancy root for the network source and its lock, and # each perimeter compartment for its policy and zone. resource "oci_sch_service_connector" "changes" { compartment_id = var.tenancy_ocid display_name = "data-perimeter-changes" description = "Audit events that touch the perimeter's policies, network source, or zones" freeform_tags = local.tags source { kind = "logging" dynamic "log_sources" { for_each = setunion([var.tenancy_ocid], local.compartments) content { compartment_id = log_sources.value log_group_id = "_Audit" } } } tasks { kind = "logRule" condition = join(" or ", [for id in local.watched : "data.resourceId='${id}'"]) } target { kind = "notifications" topic_id = oci_ons_notification_topic.perimeter.id enable_formatted_messaging = true } depends_on = [oci_identity_policy.connector] }