管理範本中的祕密

已完成

在部署期間傳遞安全值 (例如密碼) 作為參數時,您可以從 Azure Key Vault 擷取值。

可在參數檔案中參考金鑰保存庫和祕密。

您只參考其 Key Vault 識別碼,因此該值絕不會公開。

金鑰保存庫可以存在於與您部署的資源群組的不同訂用帳戶中。

部署金鑰保存庫和密碼

若要建立金鑰保存庫與密碼,請使用 Azure CLI 或 PowerShell。

若要從 Resource Manager 部署存取此金鑰保存庫內的秘密,enabledForTemplateDeployment 的金鑰保存庫屬性必須為 true

使用 Azure CLI

下列程式碼片段展示如何使用 Azure CLI 部署金鑰保存庫和秘密的範例:

keyVaultName='{your-unique-vault-name}'
resourceGroupName='{your-resource-group-name}'
location='centralus'
userPrincipalName='{your-email-address-associated-with-your-subscription}'

# Create a resource group
az group create --name $resourceGroupName --location $location

# Create a Key Vault
az keyvault create \
  --name $keyVaultName \
  --resource-group $resourceGroupName \
  --location $location \
  --enabled-for-template-deployment true
az keyvault set-policy --upn $userPrincipalName --name $keyVaultName --secret-permissions set delete get list

# Create a secret with the name, vmAdminPassword
password=$(openssl rand -base64 32)
echo $password
az keyvault secret set --vault-name $keyVaultName --name 'vmAdminPassword' --value $password

啟用祕密的存取

除了將金鑰保存庫屬性 enabledForTemplateDeployment 設定為 true 以外,部署範本的使用者還必須具備金鑰保存庫範圍的 Microsoft.KeyVault/vaults/deploy/action 權限。

此外,也包括資源群組和金鑰保存庫。 擁有者和參與者角色皆可授與此權限。

如果您建立了金鑰保存庫,您就是擁有者,因此原本就會有此權限。

然而,如果金鑰保存庫屬於不同的訂用帳戶,則必須由金鑰保存庫的擁有者授與存取權。

使用靜態識別碼參考密碼

金鑰保存庫是在參數檔案中參考,而不是範本。

下圖顯示參數檔案如何參考祕密,並將該值傳遞至範本。

Diagram showing the illustration of the flow of a secret during template deployment. The parameter file references the secret from the template and passes that value to the template.

下列範本部署了包含系統管理員密碼的 SQL 資料庫。

密碼參數會設定為安全字串。 然而,範本並未指定該值來自何處:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "adminLogin": {
      "type": "string"
    },
    "adminPassword": {
      "type": "securestring"
    },
    "sqlServerName": {
      "type": "string"
    }
  },
  "resources": [
    {
      "name": "[parameters('sqlServerName')]",
      "type": "Microsoft.Sql/servers",
      "apiVersion": "2015-05-01-preview",
      "location": "[resourceGroup().location]",
      "tags": {},
      "properties": {
        "administratorLogin": "[parameters('adminLogin')]",
        "administratorLoginPassword": "[parameters('adminPassword')]",
        "version": "12.0"
      }
    }
  ],
  "outputs": {
  }
}

現在,您便可為前述範本建立參數檔案。 在參數檔案中,指定符合範本中參數名稱的參數。

針對參數值,參考來自金鑰保存庫的密碼。 您可以藉由傳遞金鑰保存庫的資源識別碼和密碼的名稱來參考密碼。

金鑰保存庫祕密必須已經存在於下列參數檔案中,而且您要針對其資源識別碼提供靜態值。

在本機複製這個檔案,並設定訂用帳戶識別碼、保存庫名稱及 SQL 伺服器名稱:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "adminLogin": {
            "value": "exampleadmin"
        },
        "adminPassword": {
            "reference": {
              "keyVault": {
                "id": "/subscriptions/<subscription-id>/resourceGroups/examplegroup/providers/Microsoft.KeyVault/vaults/<vault-name>"
              },
              "secretName": "examplesecret"
            }
        },
        "sqlServerName": {
            "value": "<your-server-name>"
        }
    }
}

您必須部署範本,並將參數檔案傳遞至範本。

如需詳細資訊,請參閱在部署期間使用 Azure Key Vault 以傳遞安全的參數值

此網頁也有提供詳細資料,以參考使用動態識別碼的祕密。