面临Terraform KeyVault证书创建问题

时间:2020-10-07 10:56:59

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

大家好,我遇到的问题是我有一个脚本,其中正在创建资源组和密钥库,当我单独创建RG和密钥库,然后将凭证副本部分添加到服务器中时,我将凭证从中央KV复制到了新凭证中。脚本将创建所有内容,但是当我结合其中发生1 RG和1 KV以及凭据副本的代码时,它表示RG中的错误密钥库不存在。我问我需要遵循terraform中的任何顺序吗? Resource.tf

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resourceGB"
  location = var.location
}

# --- Get reference to logged on Azure subscription ---
data "azurerm_client_config" "current" {}


resource "azurerm_key_vault" "New" {
  name                        = "KV1"
  location                    = azurerm_resource_group.main.location
  resource_group_name         = azurerm_resource_group.main.name
  enabled_for_disk_encryption = true
  tenant_id                   = data.azurerm_client_config.current.tenant_id
  soft_delete_enabled         = true
  purge_protection_enabled    = false


  sku_name = "standard"


  access_policy {
    tenant_id = data.azurerm_client_config.current.tenant_id
    object_id = data.azurerm_client_config.current.object_id

    certificate_permissions = [
      "create",
      "delete",
      "deleteissuers",
      "get",
      "getissuers",
      "import",
      "purge",
      "list",
      "listissuers",
      "managecontacts",
      "manageissuers",
      "setissuers",
      "update",
    ]

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

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


  }
}
data "azurerm_key_vault" "existing" {
  name                = "Old-KV"
  resource_group_name = "Old-RG"

}

data "azurerm_key_vault" "New" {
  name                = "KV1"
  resource_group_name = "${var.prefix}-resourceGB"
}

module "Cert1" {
  source = "../module/copy_cert"
  source_key_vault_id = data.azurerm_key_vault.existing.id
  source_key_vault_cert_name = "Cert1"
  destination_key_vault_id = data.azurerm_key_vault.New.id
  destination_key_vault_cert_name = "Cert1"

}
module "Cert2" {
  source = "../module/copy_cert"
  source_key_vault_id = data.azurerm_key_vault.existing.id
  source_key_vault_cert_name = "Cert2"
  destination_key_vault_id = data.azurerm_key_vault.New.id
  destination_key_vault_cert_name = "Cert2"

}

复制证书main.tf

data "azurerm_key_vault_secret" "source_KV_cert" {
  name         = var.source_key_vault_cert_name
  key_vault_id = var.source_key_vault_id
}


data "azurerm_key_vault_certificate" "source_cert" {
  name         = var.source_key_vault_cert_name
  key_vault_id = var.source_key_vault_id
}


resource "azurerm_key_vault_certificate" "dest_cert" {
  name         = var.destination_key_vault_cert_name
  key_vault_id = var.destination_key_vault_id


certificate {
    contents = data.azurerm_key_vault_secret.source_KV_cert.value
  }


  certificate_policy {
    issuer_parameters {
      name = "self"
    }


    key_properties {
      exportable = true
      key_size   = 2048
      key_type   = "RSA"
      reuse_key  = true
    }


    secret_properties {
      content_type = "application/x-pkcs12"
    }


  }


}

复制证书变量.tf

variable "source_key_vault_id" {
  type    = string
}
variable "source_key_vault_cert_name" {
  type    = string
}

variable "destination_key_vault_id" {
  type    = string
}

variable "destination_key_vault_cert_name" {
  type    = string
}

1 个答案:

答案 0 :(得分:0)

由于您正在使用resource "azurerm_key_vault"创建新的密钥仓库,因此无法使用数据源查询模块module "Cert1"和{{1 }}在同一module "Cert2"文件中。 data "azurerm_key_vault"用于访问有关现有Key Vault的信息。

这样更改文件.tf中的相关代码:

Resource.tf