使用安全存放在 Azure Stack Hub 上的憑證部署 VM

本文將說明如何部署已安裝 Key Vault 憑證的 Azure Stack Hub 虛擬機器 (VM)。

概觀

在許多情況下都會用到憑證,例如,對 Active Directory 進行驗證時,或將網路流量加密時。 您可以安全地將憑證儲存為 Azure Stack Hub 金鑰保存庫中的密碼。 使用 Azure Stack Hub Key Vault 的優點如下:

  • 憑證不會在指令碼、命令列歷程記錄或範本中公開。
  • 憑證管理程序得以簡化。
  • 您可以控制存取憑證的金鑰。

程序說明

下列步驟說明將憑證推送至 VM 所需的程序:

  1. 建立金鑰保存庫祕密。
  2. 更新 azuredeploy.parameters.json 檔案。
  3. 部署範本。

注意

您可以從 Azure Stack 開發套件 (ASDK),或從外部用戶端 (如果是透過 VPN 連線) 來使用這些步驟。

必要條件

建立金鑰保存庫祕密

下列指令碼會建立 .pfx 格式的憑證、建立金鑰保存庫,並將憑證存放在金鑰保存庫中當做祕密。 祕密的 contentType 必須設定為 pfx

重要

建立金鑰保存庫時,您必須使用 -EnabledForDeployment 參數。 此參數可確保您能夠從 Azure Resource Manager 範本參考金鑰保存庫。

# Create a certificate in the .pfx format
New-SelfSignedCertificate `
  -certstorelocation cert:\LocalMachine\My `
  -dnsname contoso.microsoft.com

$pwd = ConvertTo-SecureString `
  -String "<Password used to export the certificate>" `
  -Force `
  -AsPlainText

Export-PfxCertificate `
  -cert "cert:\localMachine\my\<certificate thumbprint that was created in the previous step>" `
  -FilePath "<Fully qualified path to where the exported certificate can be stored>" `
  -Password $pwd

# Create a key vault and upload the certificate into the key vault as a secret
$vaultName = "contosovault"
$resourceGroup = "contosovaultrg"
$location = "local"
$secretName = "servicecert"
$fileName = "<Fully qualified path to where the exported certificate can be stored>"
$certPassword = "<Password used to export the certificate>"

$fileContentBytes = get-content $fileName `
  -Encoding Byte

$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$jsonObject = @"
{
"data": "$filecontentencoded",
"dataType" :"pfx",
"password": "$certPassword"
}
"@
$jsonObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$jsonEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)

New-AzResourceGroup `
  -Name $resourceGroup `
  -Location $location

New-AzKeyVault `
  -VaultName $vaultName `
  -ResourceGroupName $resourceGroup `
  -Location $location `
  -sku standard `
  -EnabledForDeployment

$secret = ConvertTo-SecureString `
  -String $jsonEncoded `
  -AsPlainText -Force

Set-AzureKeyVaultSecret `
  -VaultName $vaultName `
  -Name $secretName `
   -SecretValue $secret

當您執行此指令碼時,輸出會包含祕密 URI。 請記下此 URI,因為在將憑證推送至 Windows Resource Manager 範本中,您必須參考此 URI。 將 vm-push-certificate-windows 範本資料夾下載到您的開發電腦。 此資料夾包含 azuredeploy.jsonazuredeploy.parameters.json 檔案,您會在下列步驟中用到這些檔案。

根據您環境的值修改 azuredeploy.parameters.json 檔案。 重要參數是保存庫名稱、保存庫資源群組以及祕密 URI (產生自先前的指令碼)。 下一節顯示一個參數檔範例。

更新 azuredeploy.parameters.json 檔案

根據您的環境,以 vaultName、祕密 URI、VmName 和其他參數來更新 azuredeploy.parameters.json 檔案。 下列 JSON 檔案會顯示範本參數檔案的範例:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "newStorageAccountName": {
      "value": "kvstorage01"
    },
    "vmName": {
      "value": "VM1"
    },
    "vmSize": {
      "value": "Standard_D1_v2"
    },
    "adminUserName": {
      "value": "demouser"
    },
    "adminPassword": {
      "value": "demouser@123"
    },
    "vaultName": {
      "value": "contosovault"
    },
    "vaultResourceGroup": {
      "value": "contosovaultrg"
    },
    "secretUrlWithVersion": {
      "value": "https://testkv001.vault.local.azurestack.external/secrets/testcert002/82afeeb84f4442329ce06593502e7840"
    }
  }
}

部署範本

使用下列 PowerShell 指令碼部署範本:

# Deploy a Resource Manager template to create a VM and push the secret to it
New-AzResourceGroupDeployment `
  -Name KVDeployment `
  -ResourceGroupName $resourceGroup `
  -TemplateFile "<Fully qualified path to the azuredeploy.json file>" `
  -TemplateParameterFile "<Fully qualified path to the azuredeploy.parameters.json file>"

成功部署範本之後,會顯示下列輸出:

範本部署結果

Azure Stack Hub 會在部署期間將憑證推送至 VM。 憑證位置取決於 VM 的作業系統:

  • 在 Windows 中,憑證會新增至 LocalMachine 憑證位置,與使用者提供的憑證存放區在一起。
  • 在 Linux 中,憑證會置於 /var/lib/waagent 目錄底下,其中 X509 憑證檔案的檔案名稱為 UppercaseThumbprint.crt,且私密金鑰的檔案名稱為 UppercaseThumbprint.prv

淘汰憑證

淘汰憑證是憑證管理程序的一部分。 您無法刪除舊版的憑證,但可以使用 Set-AzureKeyVaultSecretAttribute Cmdlet 加以停用。

下列範例說明如何停用憑證。 使用您自己的值作為 VaultNameNameVersion 參數。

Set-AzureKeyVaultSecretAttribute -VaultName contosovault -Name servicecert -Version e3391a126b65414f93f6f9806743a1f7 -Enable 0

後續步驟