# secrets-backend / oci / t1 "Standard" # # An OCI Vault with a hardware-protected key for secrets, readers limited to # named secrets, workloads reading through dynamic groups, officers who manage # secrets but cannot delete the vault or key, and alerts on refused reads and # on every change to a secret, the key, or the vault. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) compartment = "compartment id ${var.compartment_ocid}" vault_only = "target.vault.id = '${oci_kms_vault.secrets.id}'" reader_statements = [ for r in values(var.readers) : ( length(r.secret_names) == 0 ? "Allow ${startswith(r.principal, "dynamic-group ") ? r.principal : "group ${r.principal}"} to read secret-bundles in ${local.compartment} where ${local.vault_only}" : "Allow ${startswith(r.principal, "dynamic-group ") ? r.principal : "group ${r.principal}"} to read secret-bundles in ${local.compartment} where all {${local.vault_only}, any {${join(", ", [for n in r.secret_names : "target.secret.name = '${n}'"])}}}" ) ] # Oracle documents "and" and "or" for a connector's filter, with no # parentheses and no order for mixing the two. So each filter here is one # "and" of two equalities, or event names joined by "or". A refused read is # either of two operations answered 401 or 404, which is four connectors. # Connector Hub allows 20 connectors per region by default and five on a # trial account; this uses five. alerts = { refused-get-401 = { description = "Secret reads by ID that were refused with 401." condition = "data.eventName='GetSecretBundle' and data.response.status='401'" } refused-get-404 = { description = "Secret reads by ID that were refused with 404." condition = "data.eventName='GetSecretBundle' and data.response.status='404'" } refused-by-name-401 = { description = "Secret reads by name that were refused with 401." condition = "data.eventName='GetSecretBundleByName' and data.response.status='401'" } refused-by-name-404 = { description = "Secret reads by name that were refused with 404." condition = "data.eventName='GetSecretBundleByName' and data.response.status='404'" } changed = { description = "Changes to a secret, the key, or the vault." condition = join(" or ", [for op in [ "CreateSecret", "UpdateSecret", "ScheduleSecretDeletion", "CancelSecretDeletion", "ChangeSecretCompartment", "UpdateKey", "DisableKey", "ScheduleKeyDeletion", "ChangeKeyCompartment", "UpdateVault", "ScheduleVaultDeletion", "ChangeVaultCompartment", ] : "data.eventName='${op}'"]) } } } resource "oci_kms_vault" "secrets" { compartment_id = var.compartment_ocid display_name = var.name vault_type = "DEFAULT" freeform_tags = local.tags } resource "oci_kms_key" "secrets" { compartment_id = var.compartment_ocid display_name = var.name management_endpoint = oci_kms_vault.secrets.management_endpoint protection_mode = "HSM" freeform_tags = local.tags key_shape { algorithm = "AES" length = 32 } } resource "oci_identity_policy" "secrets" { compartment_id = var.tenancy_ocid name = "${var.name}-access" description = "Who may read and manage secrets in the ${var.name} vault." freeform_tags = local.tags statements = concat(local.reader_statements, [ "Allow group ${var.officers_group} to manage secret-family in ${local.compartment} where ${local.vault_only}", "Allow group ${var.officers_group} to use keys in ${local.compartment} where target.key.id = '${oci_kms_key.secrets.id}'", "Allow group ${var.officers_group} to use vaults in ${local.compartment} where ${local.vault_only}", ]) } # --------------------------------------------------------------------------- # Alerts # --------------------------------------------------------------------------- resource "oci_ons_notification_topic" "secrets" { compartment_id = var.compartment_ocid name = "${var.name}-alerts" description = "Refused secret reads, and changes to secrets, the key, or the vault." freeform_tags = local.tags } resource "oci_ons_subscription" "email" { compartment_id = var.compartment_ocid topic_id = oci_ons_notification_topic.secrets.id protocol = "EMAIL" endpoint = var.alert_email freeform_tags = local.tags } # Deleting this policy or the topic stops every alert, and none can report # it. So does moving a connector out of this compartment, for its own alert. # The daily drift plan in Hardened is what notices. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "${var.name}-connector" description = "Lets the ${var.name} alert connectors publish to their topic." freeform_tags = local.tags statements = [ "Allow any-user to use ons-topics in ${local.compartment} where all {request.principal.type='serviceconnector', target.topic.id='${oci_ons_notification_topic.secrets.id}', request.principal.compartment.id='${var.compartment_ocid}'}", ] } # The refused-read alert was one connector before v0.42.0. It becomes the # first of the four, changed in place. moved { from = oci_sch_service_connector.alert["refused"] to = oci_sch_service_connector.alert["refused-get-401"] } resource "oci_sch_service_connector" "alert" { for_each = local.alerts compartment_id = var.compartment_ocid display_name = "${var.name}-${each.key}" description = each.value.description freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.compartment_ocid log_group_id = "_Audit" } } tasks { kind = "logRule" condition = each.value.condition } target { kind = "notifications" topic_id = oci_ons_notification_topic.secrets.id enable_formatted_messaging = true } depends_on = [oci_identity_policy.connector] }