Azure Key Vault 是一項雲端服務,提供一個安全的秘密儲存,例如金鑰、密碼、憑證及其他秘密。 本快速入門專注於部署 Bicep 檔案,以建立 Key Vault 和秘密。
Bicep 是一種領域專用語言(DSL),使用宣告式語法來部署Azure資源。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 為你的 Azure 基礎設施即程式碼解決方案提供最佳的撰寫體驗。
先決條件
- 如果你沒有Azure訂閱,請在開始前建立一個free帳號。
檢閱 Bicep 檔案
這個快速入門中使用的範本來自 Azure Quickstart Templates。
@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 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 資源:
-
Microsoft。KeyVault/vaults:建立啟用 Azure RBAC 授權的 Azure 金鑰庫(
enableRbacAuthorization: true)。 -
Microsoft.KeyVault/vaults/secrets:建立一或多個金鑰保存庫機密,並迭代
secretsObject.secrets陣列。
部署 Bicep 檔案
將Bicep檔案儲存為 main.bicep 到你本地的電腦。
建立一個參數檔(例如, 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>" } ] } } } }使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。
az group create --name myResourceGroup --location eastus az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters @main.parameters.json備註
將
<vault-name>替換為金鑰保存庫的名稱,該名稱在vault.azure.net命名空間內必須是全域唯一的。 將<your-secret-value>取代為要儲存的秘密值。 因為secretsObject被宣告為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 的資訊,請繼續閱讀以下文章。
- 閱讀 Azure Key Vault 概述
- 了解更多關於Bicep
- 請檢視金鑰保存庫安全概述