【问题标题】:How to activate Managed HSM and configure encryption with customer-managed keys stored in Azure Key Vault Managed HSM using Terraform如何使用 Terraform 激活托管 HSM 并使用存储在 Azure Key Vault 托管 HSM 中的客户托管密钥配置加密
【发布时间】:2022-01-26 19:16:27
【问题描述】:

我正在努力使用 terraform 创建 Azure Key Vault 托管 HSM。为此,我遵循了this 文档。

上述文档包含用于创建 HSM 的代码,但不包含用于激活托管 HSM 的代码。

我想使用 Terraform 配置和激活托管 HSM。是否可以通过 terraform 实现?

激活托管 HSM 后,我想使用存储在 Azure Key Vault 托管 HSM 中的客户托管密钥配置加密。为此,我遵循了 this 文档,但它包含 Azure CLI 代码。

【问题讨论】:

    标签: azure azure-keyvault terraform-provider-azure hsm


    【解决方案1】:

    不幸的是,无法直接从 Terraform 激活托管 HSM。目前,您只能从 terraform 或 ARM 模板配置它,但只能通过 PowerShell 和 Azure CLI 来激活它 。使用客户管理的密钥更新存储帐户并分配密钥保管库角色分配时也是如此。

    如果你使用azurerm_storage_account_customer_managed_key,那么你会得到以下错误:

    总体而言,所有 HSM 密钥保管库操作都需要在 CLI 或 Powershell 上执行。

    因此,对于解决方法,您可以在 terraform 中使用 local-exec 直接运行它,而无需执行单独的操作。

    代码:

    provider "azurerm" {
      features {}
    }
    data "azurerm_client_config" "current" {
    }
    
    resource "azurerm_resource_group" "example" {
      name     = "keyvaulthsm-resources"
      location = "West Europe"
    }
    
    resource "azurerm_key_vault_managed_hardware_security_module" "example" {
      name                       = "testKVHsm"
      resource_group_name        = azurerm_resource_group.example.name
      location                   = azurerm_resource_group.example.location
      sku_name                   = "Standard_B1"
      purge_protection_enabled   = true
      soft_delete_retention_days = 90
      tenant_id                  = data.azurerm_client_config.current.tenant_id
      admin_object_ids           = [data.azurerm_client_config.current.object_id]
    
      tags = {
        Env = "Test"
      }
    }
    
    variable "KeyName" {
      default=["C:/<Path>/cert_0.key","C:/<Path>/cert_1.key","C:/<Path>/cert_2.key"]
    }
    
    variable "CertName" {
      default=["C:/<Path>/cert_0.cer","C:/<Path>/cert_1.cer","C:/<Path>/cert_2.cer"]
    }
    
    resource "null_resource" "OPENSSLCERT" {
        count = 3
      provisioner "local-exec" {
        command = <<EOT
         cd  "C:\Program Files\OpenSSL-Win64\bin"
        ./openssl.exe req -newkey rsa:2048 -nodes -keyout ${var.KeyName[count.index]}  -x509 -days 365 -out ${var.CertName[count.index]} -subj "/C=IN/ST=Telangana/L=Hyderabad/O=exy ltd/OU=Stack/CN=domain.onmicrosoft.com"
        EOT
        interpreter = [
          "PowerShell","-Command"
        ]
      }
    }
    
    resource "null_resource" "securityDomain" {
      provisioner "local-exec" {
        command = <<EOT
        az keyvault security-domain download --hsm-name ${azurerm_key_vault_managed_hardware_security_module.example.name} --sd-wrapping-keys ./cert_0.cer ./cert_1.cer ./cert_2.cer --sd-quorum 2 --security-domain-file ${azurerm_key_vault_managed_hardware_security_module.example.name}-SD.json
        EOT
        interpreter = [
          "PowerShell","-Command"
        ]
      }
      depends_on = [
        null_resource.OPENSSLCERT
      ]
    }
    
    resource "azurerm_storage_account" "example" {
      name                     = "ansumanhsmstor1"
      resource_group_name      = azurerm_resource_group.example.name
      location                 = azurerm_resource_group.example.location
      account_tier             = "Standard"
      account_replication_type = "GRS"
    
      identity {
        type = "SystemAssigned"
      }
    }
    resource "null_resource" "roleassignkv" {
      provisioner "local-exec" {
        command = <<EOT
        az keyvault role assignment create --hsm-name ${azurerm_key_vault_managed_hardware_security_module.example.name} --role "Managed HSM Crypto Service Encryption User" --assignee ${azurerm_storage_account.example.identity[0].principal_id} --scope /keys
        az keyvault role assignment create --hsm-name ${azurerm_key_vault_managed_hardware_security_module.example.name} --role "Managed HSM Crypto User" --assignee ${data.azurerm_client_config.current.object_id} --scope /
        az keyvault key create --hsm-name ${azurerm_key_vault_managed_hardware_security_module.example.name} --name storageencryptionkey --ops wrapKey unwrapKey --kty RSA-HSM --size 3072
        EOT
        interpreter = [
          "PowerShell","-Command"
        ]
      }
      depends_on = [
        null_resource.securityDomain,
        azurerm_storage_account.example
      ]
    }
    
    resource "null_resource" "storageupdate" {
      provisioner "local-exec" {
        command = <<EOT
        az storage account update --name ${azurerm_storage_account.example.name} --resource-group ${azurerm_resource_group.example.name} --encryption-key-name storageencryptionkey --encryption-key-source Microsoft.Keyvault --encryption-key-vault ${azurerm_key_vault_managed_hardware_security_module.example.hsm_uri}
        EOT
        interpreter = [
          "PowerShell","-Command"
        ]
      }
      depends_on = [
        null_resource.securityDomain,
        azurerm_storage_account.example,
        null_resource.roleassignkv
      ]
    }
    

    输出:

    注意: 请确保在 HSM Keyvault 上启用 Purge Protection 并在 Management Plane 上拥有所有必需的权限(未添加在代码中)和Control Plane(我在代码中添加了)。要安装 OpenSSL,您可以在 SO thread 上通过 mtotowamkwe 参考这个答案。

    【讨论】:

    • 感谢@AnsumanBal,我在本地系统中找不到这个C:\Program Files\OpenSSL-Win64\bin" ./openssl.exe 路径。是否可以将证书存储在当前的 terraform 工作文件夹中?
    • 我正在尝试使用 openssl 从这个 C:\Program Files\Git\usr\bin 位置生成证书。但我收到了这个req: can't open "cert_0_key" for writing, permission denied error in req 错误
    • @Pradeep,对于第二个权限被拒绝错误,您必须使用提升的终端来创建证书..所以如果您使用的是 VSCode,请在管理员模式下打开它。
    • 是否可以使用在 Azure Key Vault 中创建的证书来激活 HSM,而不是在本地计算机中生成证书?
    • @Pradeep,您必须创建一个启用清除的 HSM 密钥库。所以保持 purge_protection_enabled = true 正如我在注释中提到的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2019-12-24
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多