简体   繁体   English

使用 Azure Key Vault 进行 Terraform 以获取秘密值

[英]Terraform with Azure Key Vault to get secret value

Is there any way to get the value of a secret from Azure Key Vault?有什么方法可以从 Azure Key Vault 中获取机密的值?

Doesn't look like value gets exposed in the key vault secret object here . 此处的密钥保管库机密对象中似乎没有公开value

Now you can do it with azurerm_key_vault_secret data source.现在您可以使用azurerm_key_vault_secret数据源来完成。

I'm enjoying without any scripting.我很享受没有任何脚本。

data "azurerm_key_vault_secret" "test" {
  name      = "secret-sauce"
  vault_uri = "https://rickslab.vault.azure.net/"
}

output "secret_value" {
  value = "${data.azurerm_key_vault_secret.test.value}"
}

You first need to create a data resource to the azure key vault to get the key vault resource ID:首先需要为 azure Key Vault 创建数据资源以获取 Key Vault 资源 ID:

data "azurerm_key_vault" "keyvault" {
  name                = "${var.keyvault_name}"
  resource_group_name = "${var.resourcegroup_name}"
}

And then use azurerm_key_vault_secret to get the secret with the key vault resource Id:然后使用azurerm_key_vault_secret获取密钥保管库资源 Id 的机密:

data "azurerm_key_vault_secret" "win_admin_pass" {
  name         = "${var.secret_name}"
  key_vault_id = "${data.azurerm_key_vault.keyvault.id}"
}

Please note that the use of vault_uri in azurerm_key_vault_secret is deprecated and not recommended.请注意,使用vault_uriazurerm_key_vault_secret已被弃用,不建议使用。

Is there any way to get the value of a secret from Azure Key Vault?有什么方法可以从 Azure Key Vault 中获取机密的值?

As a workaround, we can use PowerShell to get this value, like this:作为一种解决方法,我们可以使用PowerShell来获取此值,如下所示:

$a = Get-AzureKeyVaultSecret -VaultName "jasonkey" -Name "jason"
$a.SecretValueText

在此处输入图片说明

Unfortunately, this is not currently possible in Terraform.不幸的是,目前这在 Terraform 中是不可能的。 Terraform will only output the secret ID and version. Terraform 只会输出秘密 ID 和版本。 If you need to retrieve azure keyvault secrets, the best method is to use the Azure-CLI, or Powershell if that's not available.如果需要检索 azure keyvault 机密,最好的方法是使用 Azure-CLI 或 Powershell(如果不可用)。

Using Azure-CLI (2.0)使用 Azure-CLI (2.0)

az keyvault secret show --vault-name <vault-name> --name <secret-name>

Syntax:语法:

az keyvault secret show --name
                        --vault-name
                        [--version]

For more, see: Managing Azure Keyvault Secrets with Azure-CLi有关更多信息,请参阅: 使用 Azure-CLi 管理 Azure Keyvault Secrets


Using Powershell : Get-AzureKeyVaultSecret使用 PowershellGet-AzureKeyVaultSecret

get-azurekeyvaultsecret -vaultName "<vault-name>" -name "<secret-name>"

I've been working on this to get password from key vault secret.我一直在努力从密钥库秘密中获取密码。 The code below worked for me , Give it a try.下面的代码对我有用,试一试。

data "azurerm_key_vault" "terrakv" {
  name                = "terrakv" // KeyVault name
  resource_group_name = "mykv" // resourceGroup
}

data "azurerm_key_vault_secret" "kvsecret" {
name = "secret" // Name of secret
key_vault_id = data.azurerm_key_vault.terrakv.id
}

os_profile {
computer_name  = "vm-01"
admin_username = "testadmin"
admin_password = data.azurerm_key_vault_secret.kvsecret.value // Toget actual value
}

I hope it will help you for sure.我希望它肯定会帮助你。

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

相关问题 从 azure Key Vault 按值获取秘密名称 - Get secret name by value from azure Key vault 从 YAML 脚本中的 Azure 密钥库获取秘密值 - Get secret value from Azure key vault in YAML script 无法在 Azure Key Vault 中设置机密值 - Cannot set secret value in Azure Key Vault 在使用 Azure 的 terraform 中,是否可以在同一运行中创建密钥保管库机密并在同一文件中引用该机密? - In terraform using Azure, is it possible to create a key vault secret and reference that secret in the same file on the same run? 如何在一次 rest api 调用中从 azure 密钥保管库中获取最新的秘密版本值 - how to get the latest secret version value from azure key vault in one rest api call WaitingForActivation 而不是密钥保管库机密值(在 Azure 中),为什么? - WaitingForActivation instead of key vault secret value (in Azure), why? 如何获取用于 CI/CD 部署的 Azure 密钥保管库机密 URI? - How to get the Azure key vault secret URI for CI/CD deployment? Azure Key Vault 从 VMSS 实例获取 Secret - Azure Key Vault get Secret from VMSS Instances 如何使用Powershell和证书身份验证获取Azure Key Vault机密? - How to get Azure Key Vault secret using powershell with certificate auth? Terraform Azure Key Vault Secret 不依赖于 access_policy - Terraform Azure Key Vault Secret does not depend_on access_policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM