Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Azure Key Vault是一种云服务,它为机密(如密钥、密码、证书和其他机密)提供安全存储。 本快速入门重点介绍部署Bicep文件以创建密钥保管库和机密的过程。
Bicep是一种特定于域的语言(DSL),它使用声明性语法来部署Azure资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep为Azure中的基础结构即代码解决方案提供最佳创作体验。
先决条件
如果没有 Azure 订阅,请在开始前创建 试用订阅。
模板需要 Microsoft Entra 用户对象 ID 才能配置权限。 以下过程获取对象的 ID (GUID)。
通过选择 Try it 来运行以下 Azure PowerShell 或 Azure CLI 命令,然后将脚本粘贴到 shell 窗格中。 若要粘贴脚本,请右键单击 shell,然后选择“粘贴”。
echo "Enter your email address that is used to sign in to Azure:" && read upn && az ad user show --id $upn --query "id" && echo "Press [ENTER] to continue ..."请记下对象 ID, 在本快速入门的下一部分,您需要这个。
查看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 the object ID of a user, service principal or security group in the Microsoft Entra ID tenant for the vault. The object ID must be unique for the list of access policies. Get it by using Get-AzADUser or Get-AzADServicePrincipal cmdlets.')
param objectId string
@description('Specifies the permissions to keys in the vault. Valid values are: all, encrypt, decrypt, wrapKey, unwrapKey, sign, verify, get, list, create, update, import, delete, backup, restore, recover, and purge.')
param keysPermissions array = [
'list'
]
@description('Specifies the permissions to secrets in the vault. Valid values are: all, get, list, set, delete, backup, restore, recover, and purge.')
param secretsPermissions array = [
'list'
]
@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
tenantId: tenantId
enableSoftDelete: true
softDeleteRetentionInDays: 90
accessPolicies: [
{
objectId: objectId
tenantId: tenantId
permissions: {
keys: keysPermissions
secrets: secretsPermissions
}
}
]
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密钥保管库。
- Microsoft.KeyVault/vaults/secrets:创建密钥保管库机密。
部署 Bicep 文件
将Bicep文件保存为 main.bicep 到本地计算机。
使用Azure CLI或Azure PowerShell部署Bicep文件。
az group create --name exampleRG --location chinaeast az deployment group create --resource-group exampleRG --template-file main.bicep --parameters keyVaultName=<vault-name> objectId=<object-id>注意
将
<vault-name>替换为密钥保管库的名称。 将<object-id>替换为保管库Microsoft Entra租户中用户、服务主体或安全组的对象 ID。 对于访问策略列表,对象 ID 必须是唯一的。 通过使用 Get-AzADUser 或 Get-AzADServicePrincipal cmdlet,可以获取它。部署完成后,应会看到一条指出部署成功的消息。
查看已部署的资源
可以使用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
后续步骤
在本快速入门中,你已使用Bicep创建了密钥保管库和机密,然后验证了部署。 若要详细了解Key Vault和Bicep,请继续阅读以下文章。
- 查看 Azure Key Vault 概述
- 详细了解 Bicep
- 查看 Key Vault 安全概述