【问题标题】:Terraform key vault access policyTerraform 密钥保管库访问策略
【发布时间】:2021-03-19 03:39:50
【问题描述】:

我正在尝试自动创建密钥库、存储帐户和用于加密存储的密钥。

block的代码如下:

resource "azurerm_key_vault_access_policy" "storage" {
  for_each = toset(var.storage-foreach)
  key_vault_id = azurerm_key_vault.tenantsnbshared.id
  tenant_id = <tenant-id>
  object_id    = azurerm_storage_account.storage-foreach[each.value]
  key_permissions    = ["get", "create", "list", "restore", "recover", "unwrapkey", "wrapkey", "purge", "encrypt", "decrypt", "sign", "verify"]
  secret_permissions = ["get"]
  depends_on = [azurerm_storage_account.storage-foreach]
}

esource "azurerm_key_vault" "tenantsnbshared" {
  location                        = var.location
  name                            = "tenantsnbshared"
  resource_group_name             = azurerm_resource_group.tenant-testing-test.name
  sku_name                        = "standard"
  tenant_id                       = "tenant-id"
  enabled_for_deployment          = false
  enabled_for_disk_encryption     = false
  enabled_for_template_deployment = false
  purge_protection_enabled        = true
  soft_delete_enabled             = true
  access_policy {
    object_id = "my-obj-id"
    tenant_id = "tenant-id"
    secret_permissions = [
      "Get",
      "List",
      "Set",
      "Delete",
      "Recover",
      "Backup",
      "Restore",
      "Purge"

    ]
    key_permissions = [
      "Get",
      "List",
      "Update",
      "Create",
      "Import",
      "Delete",
      "Recover",
      "Backup",
      "Restore",
      "Purge",
      "encrypt",
      "decrypt",
      "sign",
      "verify"
    ]
    certificate_permissions = [
      "Get",
      "List",
      "Update",
      "Create",
      "Import",
      "Delete",
      "Recover",
      "Backup",
      "Restore",
      "ManageContacts",
      "ManageIssuers",
      "GetIssuers",
      "ListIssuers",
      "SetIssuers",
      "DeleteIssuers",
    ]
  }
  tags = {
    "Owner" : "test"
  }
}

resource "azurerm_storage_account" "storage-foreach" {
  for_each                 = toset(var.storage-foreach)
  access_tier              = "Hot"
  account_kind             = "StorageV2"
  account_replication_type = "LRS"
  account_tier             = "Standard"
  location                 = var.location
  name                     = each.value
  resource_group_name      = azurerm_resource_group.tenant-testing-hamza.name
  identity {
    type = "SystemAssigned"
  }

  lifecycle {
    prevent_destroy = false
  }
}

如您所见,所有资源都使用 for each 来自动化流程。 但是当我尝试运行这段代码时,我得到了这个与 key_vault_access_policy 相关的错误

Error: Incorrect attribute value type

  on main.tf line 13, in resource "azurerm_key_vault_access_policy" "storage":
  13:   object_id    = azurerm_storage_account.storage-foreach[each.value]
    |----------------
    | azurerm_storage_account.storage-foreach is object with 2 attributes
    | each.value is "storage2"

Inappropriate value for attribute "object_id": string required.

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

    您应该将object ids of storage accounts 分配给KV 访问策略。

    请将访问策略资源更改为以下内容:

    resource "azurerm_key_vault_access_policy" "storage" {
      for_each = toset(var.storage-foreach)
    
      ...
    
      object_id    = azurerm_storage_account.storage-foreach[each.value].0.principal_id
    
      ...    
    }
    

    【讨论】:

    • 需要..它指向存储帐户的第一个标识,而不是存储本身..
    • 好的,知道了,在这种情况下根本不知道。
    • 没有问题 .. 以防万一我在这里留下一个 azurerm_key_vault_access_policy 示例:registry.terraform.io/providers/hashicorp/azurerm/latest/docs/…
    • 对不起,我要回家了。我试过你的代码,错误如下:``` n main.tf line 21, in resource "azurerm_key_vault_access_policy" "storage": 21: depends_on = [azurerm_storage_account.storage-foreach[each.value]] 单个静态变量需要参考:仅使用常量键进行属性访问和索引。这里不允许计算、函数调用、模板表达式等。 ```
    • hmm ok ..我认为您可以简单地删除depends_on块..访问策略和存储之间已经存在隐式依赖关系,因为您将存储的对象ID分配给访问策略..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多