OCI Key Management: BYOK and Customer-Managed Encryption with Terraform

By default OCI encrypts data using Oracle-managed keys. With customer-managed keys, you control the master encryption keys: you create them, set rotation policy, and can disable or delete them to revoke access. OCI Key Management uses hardware security modules so master keys never exist outside the HSM in plaintext. This post covers setting up a production key management structure with Terraform and applying customer-managed keys across ADB, Object Storage, and Block Volumes.

Step 1: Vault and Master Keys

resource "oci_kms_vault" "production_vault" {
  compartment_id = var.compartment_id
  display_name   = "production-key-vault"
  vault_type     = "DEFAULT"  # Shared HSM
  defined_tags   = { "Operations.Environment" = "production", "Operations.ManagedBy" = "terraform" }
}

resource "oci_kms_key" "database_key" {
  compartment_id      = var.compartment_id
  management_endpoint = oci_kms_vault.production_vault.management_endpoint
  display_name        = "database-master-encryption-key"
  protection_mode     = "HSM"
  key_shape { algorithm = "AES"; length = 32 }
}

resource "oci_kms_key" "object_storage_key" {
  compartment_id      = var.compartment_id
  management_endpoint = oci_kms_vault.production_vault.management_endpoint
  display_name        = "object-storage-encryption-key"
  protection_mode     = "HSM"
  key_shape { algorithm = "AES"; length = 32 }
}

resource "oci_kms_key" "block_volume_key" {
  compartment_id      = var.compartment_id
  management_endpoint = oci_kms_vault.production_vault.management_endpoint
  display_name        = "block-volume-encryption-key"
  protection_mode     = "HSM"
  key_shape { algorithm = "AES"; length = 32 }
}

output "vault_endpoint"  { value = oci_kms_vault.production_vault.management_endpoint }
output "db_key_id"       { value = oci_kms_key.database_key.id }
output "os_key_id"       { value = oci_kms_key.object_storage_key.id }
output "bv_key_id"       { value = oci_kms_key.block_volume_key.id }

Step 2: IAM Policy for Key Access

resource "oci_identity_policy" "kms_policy" {
  compartment_id = var.compartment_id
  name           = "kms-service-access-policy"
  statements = [
    "Allow service objectstorage-me-jeddah-1 to use keys in compartment id COMPARTMENT_OCID",
    "Allow service database to use keys in compartment id COMPARTMENT_OCID",
    "Allow service blockstorage to use keys in compartment id COMPARTMENT_OCID",
    "Allow group PlatformEngineers to manage keys in compartment id COMPARTMENT_OCID",
    "Allow group PlatformEngineers to manage vaults in compartment id COMPARTMENT_OCID",
    "Allow group SecurityAuditors to read key-family in compartment id COMPARTMENT_OCID"
  ]
}

Step 3: Apply Keys to Services

resource "oci_database_autonomous_database" "encrypted_adb" {
  compartment_id           = var.compartment_id
  db_name                  = "PRODDB"
  display_name             = "production-adb-encrypted"
  db_workload              = "OLTP"
  compute_model            = "ECPU"
  compute_count            = 4
  data_storage_size_in_tbs = 1
  admin_password           = var.adb_admin_password
  kms_key_id               = oci_kms_key.database_key.id
  vault_id                 = oci_kms_vault.production_vault.id
  is_access_control_enabled   = true
  subnet_id                   = var.private_subnet_id
  is_mtls_connection_required = true
  defined_tags               = { "Operations.Environment" = "production" }
}

resource "oci_objectstorage_bucket" "encrypted_bucket" {
  compartment_id = var.compartment_id
  namespace      = var.tenancy_namespace
  name           = "sensitive-data-encrypted"
  access_type    = "NoPublicAccess"
  kms_key_id     = oci_kms_key.object_storage_key.id
}

resource "oci_core_volume" "encrypted_volume" {
  compartment_id      = var.compartment_id
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  display_name        = "encrypted-data-volume"
  size_in_gbs         = 500
  vpus_per_gb         = 20
  kms_key_id          = oci_kms_key.block_volume_key.id
}

Step 4: Rotation Policy Alarm

resource "oci_monitoring_alarm" "key_rotation_due" {
  compartment_id        = var.compartment_id
  display_name          = "kms-key-rotation-overdue"
  is_enabled            = true
  metric_compartment_id = var.compartment_id
  namespace             = "oci_vault"
  query                 = "KeyVersionAge[1d]{keyId = 'KEY_OCID'}.max() > 320"
  severity              = "INFORMATIONAL"
  pending_duration      = "PT24H"
  destinations          = [var.security_notification_topic_id]
  body                  = "Encryption key version is more than 320 days old. Rotate within 45 days to meet annual rotation policy."
}

Operational Notes

HSM-backed keys never exist outside the hardware security module in plaintext. If you delete a master key, the data encryption keys encrypted by it cannot be decrypted and your data becomes permanently inaccessible. Test your key deletion and recovery procedures in a non-production environment. Establish a key lifecycle policy that prevents accidental deletion through the IAM policy by requiring MANAGE permission for key deletion rather than USE.

Key rotation creates a new key version. New writes use the new version. Existing data stays encrypted with the previous version and is decrypted on read using the old version, which OCI retains automatically. Rotation does not re-encrypt existing data in place. The security benefit is that future writes are protected by the new version if a previous version was compromised, while reads of existing data continue to work normally.

Regards,
Osama

#OCI #OracleCloud #KMS #BYOK #Encryption #Terraform #IaC #TechBlog #Oracle #CloudSecurity #Compliance #PlatformEngineering #OracleCloudInfrastructure #HSM #CustomerManagedKeys #KeyRotation #DataProtection #ZeroTrust #AuditCompliance #EncryptionAtRest

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.