# Customer-managed key for the evidence account. # # A vault with purge protection, a key, and an identity the storage account # uses to reach that key. Disabling the key cuts off every read of the # evidence at once, without touching the data. resource "azurerm_key_vault" "evidence" { name = "kvctrev${random_string.suffix.result}" location = azurerm_resource_group.evidence.location resource_group_name = azurerm_resource_group.evidence.name tenant_id = data.azurerm_client_config.current.tenant_id sku_name = "standard" # Storage will not accept a key from a vault without purge protection. purge_protection_enabled = true soft_delete_retention_days = 90 rbac_authorization_enabled = true # Storage reaches the key as a trusted Azure service, which the bypass # allows. Whoever runs Terraform must be inside allowed_ip_ranges when it # is set. network_acls { default_action = local.network_default bypass = "AzureServices" ip_rules = var.allowed_ip_ranges } tags = local.tags } # Whoever runs Terraform needs to create the key. On a first apply this role # can take a minute to take effect, and the key creation can fail with 403. # Running apply again is the fix. resource "azurerm_role_assignment" "runner_crypto_officer" { scope = azurerm_key_vault.evidence.id role_definition_name = "Key Vault Crypto Officer" principal_id = data.azurerm_client_config.current.object_id } resource "azurerm_key_vault_key" "evidence" { name = "container-evidence" key_vault_id = azurerm_key_vault.evidence.id key_type = "RSA" key_size = 3072 key_opts = ["wrapKey", "unwrapKey"] tags = local.tags rotation_policy { automatic { time_before_expiry = "P30D" } expire_after = "P1Y" notify_before_expiry = "P29D" } depends_on = [azurerm_role_assignment.runner_crypto_officer] } resource "azurerm_user_assigned_identity" "storage" { name = "id-container-evidence-${random_string.suffix.result}" location = azurerm_resource_group.evidence.location resource_group_name = azurerm_resource_group.evidence.name tags = local.tags } # The narrowest role that lets the storage account wrap and unwrap with the key. resource "azurerm_role_assignment" "storage_key_user" { scope = azurerm_key_vault.evidence.id role_definition_name = "Key Vault Crypto Service Encryption User" principal_id = azurerm_user_assigned_identity.storage.principal_id }