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

标签 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>'
  }
}

我想尝试实现的是从 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
  }
}

从您的 ma​​in.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/

相关文章:

Azure ARM 解析值时遇到意外字符[

将证书添加到 Key Vault 时出现 Azure 错误

typescript - 如何获取Powershell脚本输出fin Typescript文件?

azure - 使用托管标识从逻辑应用程序进行身份验证来调用 Azure 函数

azure - 如何在没有本地数据网关的情况下自动刷新 Power BI 仪表板

php - 使用 Azure 媒体服务 REST API 和 PHP 创建 Assets 时出错

azure - 微软登录失败:AADSTS70001: Application with identifier was not found in the directory

powershell - 使用 SAS token 上传 Blob 内容

azure - 使用 Azure 资源管理器模板创建容器

Azure Key Vault 证书 - 创建基本约束 CA :True