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

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

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

Prerequisites

  • 如果没有 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 Azure Active Directory 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 the name of the secret that you want to create.')
param secretName string

@description('Specifies the value of the secret that you want to create.')
@secure()
param secretValue string

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

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

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

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

  • Microsoft.KeyVault/vaults:创建 Azure 密钥保管库。 该模板启用 Azure RBAC 授权(enableRbacAuthorization: true)、软删除和清除保护。

部署 Bicep 文件

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

  2. 使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。

    az group create --name myResourceGroup --location chinaeast
    az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters keyVaultName=<vault-name>
    

    注释

    <vault-name> 替换为密钥保管库的名称,该名称在 vault.azure.cn 命名空间中必须是全局唯一的。

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

分配 密钥保管库 RBAC 角色

此Bicep文件创建的密钥保管库使用 Azure RBAC 进行授权。 若要通过数据平面创建或读取密钥、机密或证书,需要为自己分配适当的角色。 例如,若要管理机密,请自行分配 密钥保管库 Secrets Officer 角色:

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门户检查密钥保管库,也可以使用以下Azure CLI或Azure PowerShell脚本:

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault show --name $keyVaultName

清理资源

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

az group delete --name myResourceGroup

注释

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

后续步骤

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