Azure 密钥保管库 是一项云服务,它为机密(例如密钥、密码和证书)提供安全存储。 本快速入门重点介绍部署Bicep文件以创建密钥保管库和自签名证书的过程。
Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 为 Azure 中的基础结构即代码解决方案提供最佳的编写体验。
Prerequisites
- 如果没有 Azure 订阅,请在开始之前先创建一个免费帐户。
查阅 Bicep 文件
本快速入门中使用的模板来自 Azure 快速入门模板。
@description('The name of the key vault to be created.')
param vaultName string
@description('The name of the certificate to be created.')
param certificateName string
@description('The location of the resources.')
param location string = resourceGroup().location
@description('The SKU of the vault to be created.')
@allowed([
'standard'
'premium'
])
param skuName string = 'standard'
@description('The common name (subject) for the self-signed certificate. Defaults to the certificate name.')
param certificateCommonName string = certificateName
@description('The validity of the certificate in months.')
@minValue(1)
@maxValue(1200)
param validityInMonths int = 12
resource vault 'Microsoft.KeyVault/vaults@2023-07-01' = {
name: vaultName
location: location
properties: {
enableRbacAuthorization: true
enableSoftDelete: true
softDeleteRetentionInDays: 90
enablePurgeProtection: true
enabledForDeployment: false
enabledForDiskEncryption: false
enabledForTemplateDeployment: false
tenantId: subscription().tenantId
sku: {
name: skuName
family: 'A'
}
networkAcls: {
defaultAction: 'Allow'
bypass: 'AzureServices'
}
}
}
// Key Vault certificates are a data-plane concept and cannot be created
// directly through ARM. Use the public Bicep registry module which wraps
// `az keyvault certificate create` in a deployment script (it provisions a
// user-assigned managed identity with the Key Vault Certificate Officer
// role on the vault for the duration of the deployment).
module certificate 'br/public:deployment-scripts/create-kv-certificate:3.4.2' = {
name: 'create-${certificateName}'
params: {
akvName: vault.name
location: location
certificateNames: [certificateName]
certificateCommonNames: [certificateCommonName]
validity: validityInMonths
}
}
output location string = location
output name string = vault.name
output resourceGroupName string = resourceGroup().name
output resourceId string = vault.id
output certificateSecretId string = certificate.outputs.certificateSecretIds[0][0]
output certificateThumbprint string = certificate.outputs.certificateThumbprintHexs[0][0]
该 Bicep 文件中定义了两个 Azure 资源:
-
Microsoft。KeyVault/vaults:创建启用了 Azure RBAC 授权的Azure密钥保管库(
enableRbacAuthorization: true)。 - Microsoft.Resources/deployments:用于运行 create-kv-certificate 模块的嵌套部署,以在保管库中创建自签名证书。 证书是数据平面资源,不能直接使用 ARM 资源类型创建。
部署 Bicep 文件
将 Bicep 文件另存为 main.bicep 到本地计算机。
使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。
az group create --name myResourceGroup --location chinaeast az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters vaultName=<vault-name> certificateName=myCert注释
将
<vault-name>替换为密钥保管库的名称,该名称在vault.azure.cn命名空间中必须是全局唯一的。部署完成后,应会看到一条指出部署成功的消息。
分配 密钥保管库 RBAC 角色
此Bicep文件创建的密钥保管库使用 Azure RBAC 进行授权。 若要通过数据平面访问证书(例如,使用Azure CLI或Azure PowerShell),需要为自己分配适当的角色。
echo "Enter your key vault name:" &&
read keyVaultName &&
az role assignment create --role "Key Vault Certificates 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 certificate list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."
清理资源
如果不再需要,可以使用 Azure 门户、Azure CLI 或 Azure PowerShell 删除资源组以及其资源。
az group delete --name myResourceGroup
注释
删除资源组也会删除密钥保管库,但保管库随后进入软删除状态,并在保留期(默认情况下为 90 天)保持可恢复状态。 保管库名称在该时间段内保持全局保留状态,并且由于启用了清除保护,因此无法提前清除保管库。 对于标准密钥保管库,已软删除的保管库不会产生费用。 有关详细信息,请参阅 密钥保管库 软删除概述。
后续步骤
在本快速入门中,你已使用Bicep创建了密钥保管库和证书,然后验证了部署。 若要详细了解密钥保管库和Bicep,请继续阅读以下文章。
- 阅读 Azure 密钥保管库 概述
- 详细了解 Bicep
- 请参阅 密钥保管库 安全性概述