快速入门:使用 Bicep 创建Azure密钥保管库和机密

Azure 密钥保管库是一种云服务,它为机密(如密钥、密码、证书和其他机密)提供安全存储。 本快速入门重点介绍部署Bicep文件以创建密钥保管库和机密的过程。

Bicep是一种特定于域的语言(DSL),它使用声明性语法来部署Azure资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep为Azure中的基础结构即代码解决方案提供最佳创作体验。

先决条件

  • 如果没有 Azure 订阅,请在开始前创建 试用订阅

查看Bicep文件

本快速入门中使用的模板来自 Azure 快速入门模板

@description('Specifies the name of the key vault.')
param keyVaultName string

@description('Specifies the Azure location where the key vault should be created.')
param location string = resourceGroup().location

@description('Specifies whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault.')
param enabledForDeployment bool = false

@description('Specifies whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys.')
param enabledForDiskEncryption bool = false

@description('Specifies whether Azure Resource Manager is permitted to retrieve secrets from the key vault.')
param enabledForTemplateDeployment bool = false

@description('Specifies the Microsoft Entra ID tenant ID that should be used for authenticating requests to the key vault. Get it by using Get-AzSubscription cmdlet.')
param tenantId string = subscription().tenantId

@description('Specifies whether the key vault is a standard vault or a premium vault.')
@allowed([
  'standard'
  'premium'
])
param skuName string = 'standard'

@description('Specifies all secrets {"secretName":"","secretValue":""} wrapped in a secure object.')
@secure()
param secretsObject object

resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
  name: keyVaultName
  location: location
  properties: {
    enabledForDeployment: enabledForDeployment
    enabledForTemplateDeployment: enabledForTemplateDeployment
    enabledForDiskEncryption: enabledForDiskEncryption
    enableRbacAuthorization: true
    tenantId: tenantId
    enableSoftDelete: true
    softDeleteRetentionInDays: 90
    enablePurgeProtection: true
    sku: {
      name: skuName
      family: 'A'
    }
    networkAcls: {
      defaultAction: 'Allow'
      bypass: 'AzureServices'
    }
  }
}

resource secrets 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [for secret in secretsObject.secrets: {
  name: secret.secretName
  parent: kv
  properties: {
    value: secret.secretValue
  }
}]

output location string = location
output name string = kv.name
output resourceGroupName string = resourceGroup().name
output resourceId string = kv.id

Bicep 文件中定义了两个Azure资源:

部署 Bicep 文件

  1. 将Bicep文件保存为 main.bicep 到本地计算机。

  2. 创建一个参数文件(例如 main.parameters.json),为 secretsObject 提供值:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "keyVaultName": { "value": "<vault-name>" },
        "secretsObject": {
          "value": {
            "secrets": [
              { "secretName": "adminpassword", "secretValue": "<your-secret-value>" }
            ]
          }
        }
      }
    }
    
  3. 使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。

    az group create --name exampleRG --Location chinaeast2
    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters keyVaultName=<vault-name> objectId=<object-id>
    

    注意

    <vault-name> 替换为密钥保管库的名称,该名称在 vault.azure.cn 命名空间中必须是全局唯一的。 将 <your-secret-value> 替换为要存储的机密值。 由于 secretsObject 声明为 a secureObject,因此不会记录或回显其值。

    部署完成后,应会看到一条指出部署成功的消息。

分配 密钥保管库 RBAC 角色

此Bicep文件创建的密钥保管库使用 Azure RBAC 进行授权。 若要通过数据平面访问机密(例如,使用Azure CLI或Azure PowerShell),需要为自己分配适当的角色。

echo "Enter your key vault name:" &&
read keyVaultName &&
az role assignment create --role "Key Vault Secrets Officer" \
    --assignee-object-id $(az ad signed-in-user show --query id -o tsv) \
    --scope $(az keyvault show --name $keyVaultName --query id -o tsv)

注意

角色分配可能需要一两分钟才能生效。

查看已部署的资源

可以使用Azure门户检查密钥保管库和机密,也可以使用以下Azure CLI或Azure PowerShell脚本列出创建的机密。

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault secret list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."

清理资源

如果不再需要资源组及其资源,请使用 Azure 门户、Azure CLI 或 Azure PowerShell 删除它们。

az group delete --name myResourceGroup

注意

删除资源组也会删除密钥保管库,但保管库随后进入软删除状态,并在保留期(默认情况下为 90 天)保持可恢复状态。 保管库名称在该时间段内保持全局保留状态,并且由于启用了清除保护,因此无法提前清除保管库。 对于标准密钥保管库,已软删除的保管库不会产生费用。 有关详细信息,请参阅 密钥保管库 软删除概述

后续步骤

在本快速入门中,你已使用Bicep创建了密钥保管库和机密,然后验证了部署。 若要详细了解密钥保管库和Bicep,请继续阅读以下文章。