使用 Bicep 将证书从 Azure Keyvault 添加到 Azure 容器环境

Ama*_*may 3 azure azure-resource-manager azure-keyvault azure-bicep azure-container-apps

我需要一种机制来从 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>'
  }
}
Run Code Online (Sandbox Code Playgroud)

我想尝试实现的是从 Keyvault 下载 pfx 文件并在 Bicep 文件中将其转换为 base64(可能通过使用嵌入在 bicep 中的 powershell 命令),然后可以在上面的代码中使用。

如果有人以前这样做过,将非常感激看到其实施。

Tho*_*mas 6

如果您的证书作为证书存储在 Key Vault 中,则它已经经过 Base64 编码,并且可以作为 Key Vault 机密进行访问(请参阅证书的组成)。

您可以使用 bicepgetSecret函数将证书传递到容器应用程序环境:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

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)    
  }
}
Run Code Online (Sandbox Code Playgroud)