简体   繁体   English

Terraform azure - 密钥库密钥生成 - 访问被拒绝

[英]Terraform azure - keyvault key generation - access denied

I would like to generate a keyvault key with:我想生成一个密钥保管库密钥:

resource "azurerm_key_vault" "xxx-keyvault" {
  name                        = "xxx-keyvault"
  location             = var.location
  resource_group_name  = azurerm_resource_group.xxx-rg.name
  enabled_for_disk_encryption = true
  tenant_id                   = var.tenant_id
  sku_name = "standard"
  enabled_for_template_deployment = true
  enabled_for_deployment          = true

  access_policy {
    tenant_id = var.tenant_id
    object_id = var.service_principal_object_id

    key_permissions = [
      "backup","create","decrypt","delete","encrypt","get","import","list","purge","recover","restore","sign","unwrapKey","update","verify","wrapKey"
    ]

    secret_permissions = [
      "backup","get","list","purge","recover","restore","set"
    ]
  }

  network_acls {
    default_action = "Deny"
    bypass         = "AzureServices"
  }

}

resource "azurerm_key_vault_key" "xxx-keyvault-key" {
  name         = "xxx-keyvault-key"
  key_vault_id = azurerm_key_vault.xxx-keyvault.id
  key_type     = "RSA"
  key_size     = 2048

  key_opts = [
    "decrypt",
    "encrypt",
    "sign",
    "unwrapKey",
    "verify",
    "wrapKey",
  ]
}

but I get the following error:但我收到以下错误:

Error: Error Creating Key: keyvault.BaseClient#CreateKey: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error.错误:错误创建密钥:keyvault.BaseClient#CreateKey:响应请求失败:StatusCode=403 -- 原始错误:autorest/azure:服务返回错误。 Status=403 Code="Forbidden" Message="Access denied. Caller was not found on any access policy.\\r\\nCaller: appid=<...>;oid=<...>;numgroups=0;iss=<...>/\\r\\nVault: <...>;location=<...>" InnerError={"code":"AccessDenied"} Status=403 Code="Forbidden" Message="访问被拒绝。未在任何访问策略中找到调用者。\\r\\n调用者:appid=<...>;oid=<...>;numgroups=0;iss= <...>/\\r\\nVault: <...>;location=<...>" InnerError={"code":"AccessDenied"}

What is wrong?怎么了?

Thanks!谢谢!

For your issue, the reason is that you set the property network_acls for the Key vault.对于您的问题,原因是您为 Key Vault 设置了属性network_acls When the Key vault is created then the firewall is also enabled and you do not allow the public IP of the machine where you execute the Terraform code.创建 Key Vault 后,防火墙也会启用,并且不允许使用执行 Terraform 代码的计算机的公共 IP。 So the action that creates the key in the Key vault is Forbidden.因此,在 Key Vault 中创建密钥的操作是 Forbidden。

The simplest solution for you is that does not set the property network_acls for the Key vault.对您来说最简单的解决方案是不为 Key Vault 设置属性network_acls

Or add your public IP of the machine where you execute the Terraform code in the network_acls like this:或者在network_acls添加执行 Terraform 代码的机器的公共 IP,如下所示:

network_acls {
    default_action = "Deny"
    bypass         = "AzureServices"
    ip_rules       = ["your_machine_publicIp"]
  }

You can find the public IP in the error you got with the Client address .您可以在使用Client address得到的错误中找到公共 IP。

And you need also to make sure the object_id in the access policy of the Key vault is the object id of the service principal, not the application registry.并且您还需要确保 Key Vault 访问策略中的 object_id 是服务主体的对象 ID,而不是应用程序注册表。 This may be another reason that caused the issue.这可能是导致问题的另一个原因。

For this issue, could you please add the access policy(with permissions) manually via UI and then use Terraform to generate the key.对于这个问题,能否请您通过 UI 手动添加访问策略(带权限),然后使用 Terraform 生成密钥。 Here is a post which has similar issue with yours.这是一篇与您有类似问题的帖子 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM