我需要一种机制来从 Keyvault 下载 .pfx 证书,然后将其上传到 Azure 容器环境,这一切都通过 Bicep 进行。这将最大限度地减少更新证书时的任何手动干预。
我目前正在使用使用 powershell 手动转换的 base64 编码值将证书添加到我的 Azure 容器环境。如下:
resource certificate 'Microsoft.App/managedEnvironments/certificates@2022-06-01-preview' = {
parent: env
location: location
name: 'ta-cert'
properties: {
password: certificatePassword
value: '<base64>'
}
}
我想尝试实现的是从 Keyvault 下载 pfx 文件并转换为 base64(可能通过使用嵌入在 bicep 中的 powershell 命令),所有这些都在 Bicep 文件中,然后可以在上面的代码中使用。
如果有人以前这样做过,我将非常感激看到其实现。
最佳答案
如果您的证书作为证书存储在 Key Vault 中,则它已经经过 Base64 编码,并且可以作为 Key Vault secret 进行访问(请参阅 Composition of a Certificate )。
您可以使用 bicep getSecret
函数将证书传递到容器应用环境:
containerapp-env-certificate.bicep 模块:
param containerAppEnvName string
param location string = resourceGroup().location
param certificateName string
@secure()
param certificateValue string
resource containerAppEnv 'Microsoft.App/managedEnvironments@2022-03-01' existing = {
name: containerAppEnvName
}
resource certificate 'Microsoft.App/managedEnvironments/certificates@2022-06-01-preview' = {
parent: containerAppEnv
location: location
name: certificateName
properties: {
// Dont need password here
value: certificateValue
}
}
从您的 main.bicep 模板中,您可以像这样调用它:
param containerAppEnvName string
param location string = resourceGroup().location
param keyVaultName string
param keyVaultCertificateName string
// Get a reference to key vault
resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
name: keyVaultName
}
module certificate 'containerapp-env-certificate.bicep' = {
name: 'containerapp-env-certificate'
params: {
containerAppEnvName: containerAppEnvName
certificateName: 'ta-cert'
location: location
// Get the certificate as a base64 secret
certificateValue: keyVault.getSecret(keyVaultCertificateName)
}
}
关于azure - 使用 Bicep 将证书从 Azure Keyvault 添加到 Azure 容器环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74437135/