# key-baseline / azure / t1 "Standard" # # A Key Vault that cannot be purged early, closed to the internet, with key # managers and key users kept apart, and every operation on the vault written # to a Log Analytics workspace. data "azurerm_client_config" "current" {} resource "random_string" "suffix" { length = 5 special = false upper = false } locals { vault_name = "kv${substr(replace(lower(var.name), "/[^a-z0-9]/", ""), 0, 14)}${random_string.suffix.result}" admins = distinct(concat([data.azurerm_client_config.current.object_id], var.key_admin_ids)) tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) } resource "azurerm_resource_group" "keys" { name = "rg-${var.name}-keys" location = var.location tags = local.tags } resource "azurerm_key_vault" "this" { name = local.vault_name location = azurerm_resource_group.keys.location resource_group_name = azurerm_resource_group.keys.name tenant_id = data.azurerm_client_config.current.tenant_id # Premium is what allows hardware-protected keys, which the Google Cloud and # Oracle Cloud versions of this template also use. sku_name = "premium" rbac_authorization_enabled = true # With purge protection, a deleted vault and its keys stay recoverable for the # whole retention period and nobody can purge them sooner. That is what makes # an accidental or malicious delete survivable. purge_protection_enabled = true soft_delete_retention_days = 90 network_acls { default_action = "Deny" bypass = "AzureServices" ip_rules = var.allowed_ip_ranges } tags = local.tags } # Managers: rotate, disable, delete. Granted on the vault. resource "azurerm_role_assignment" "admins" { for_each = toset(local.admins) scope = azurerm_key_vault.this.id role_definition_name = "Key Vault Crypto Officer" principal_id = each.value } resource "azurerm_key_vault_key" "this" { name = var.name key_vault_id = azurerm_key_vault.this.id key_type = "RSA-HSM" key_size = 3072 key_opts = ["encrypt", "decrypt", "wrapKey", "unwrapKey"] tags = local.tags rotation_policy { automatic { time_before_expiry = "P30D" } expire_after = "P1Y" notify_before_expiry = "P29D" } depends_on = [azurerm_role_assignment.admins] } # Users: encrypt and decrypt with this one key, nothing else. Granted on the # key itself, so a new key in the same vault is not automatically theirs. resource "azurerm_role_assignment" "users" { for_each = toset(var.key_user_ids) scope = azurerm_key_vault_key.this.resource_versionless_id role_definition_name = "Key Vault Crypto User" principal_id = each.value } resource "azurerm_log_analytics_workspace" "keys" { name = "law-${var.name}-keys" location = azurerm_resource_group.keys.location resource_group_name = azurerm_resource_group.keys.name sku = "PerGB2018" retention_in_days = var.log_retention_days tags = local.tags } # Every operation on the vault, including each encrypt and decrypt, with who # asked. Without this the vault keeps no history you can search. resource "azurerm_monitor_diagnostic_setting" "vault" { name = "vault-audit" target_resource_id = azurerm_key_vault.this.id log_analytics_workspace_id = azurerm_log_analytics_workspace.keys.id enabled_log { category = "AuditEvent" } }