Share via


透過 Batch 使用憑證安全地存取 Azure Key Vault

警告

此文章中詳述的 Batch 帳戶憑證已被取代。 若要安全地存取 Azure Key Vault,只需使用集區受控識別搭配針對使用者指派的受控識別設定的適當存取權限來存取您的 Key Vault。 如果您需要在 Batch 節點上佈建憑證,請將可用的 Azure Key Vault VM 延伸模組與集區受控識別搭配使用,以便在 Batch 集區上安裝和管理憑證。 如需在 Batch 集區上使用受控識別從 Azure Key Vault 部署憑證的詳細資訊,請參閱在 Batch 集區中啟用自動憑證輪替

CloudServiceConfiguration 集區不提供指定受控識別或 Azure Key Vault VM 延伸模組的能力,而這些集區已被取代。 您應該移轉至 VirtualMachineConfiguration 集區,其中提供上述替代方案。

在此文章中,您將了解如何使用憑證設定 Batch 節點,安全地存取儲存於 Azure Key Vault 中的認證。

若要從 Batch 節點向 Azure Key Vault 進行驗證,您需要:

  • Microsoft Entra 認證
  • 憑證
  • Batch 帳戶
  • 至少具有一個節點的 Batch 集區

取得憑證

如果您還沒有憑證,請使用 PowerShell Cmdlet New-SelfSignedCertificate 來建立新的自我簽署憑證。

建立服務主體

Key Vault 的存取權會授與使用者服務主體。 若要以程式設計方式存取 Key Vault,請搭配您在上一個步驟中建立的憑證來使用服務主體。 服務主體必須與 Key Vault 位於相同的 Microsoft Entra 租用戶中。

$now = [System.DateTime]::Parse("2020-02-10")
# Set this to the expiration date of the certificate
$expirationDate = [System.DateTime]::Parse("2021-02-10")
# Point the script at the cer file you created $cerCertificateFilePath = 'c:\temp\batchcertificate.cer'
$cer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cer.Import($cerCertificateFilePath)
# Load the certificate into memory
$credValue = [System.Convert]::ToBase64String($cer.GetRawCertData())
# Create a new AAD application that uses this certificate
$newADApplication = New-AzureRmADApplication -DisplayName "Batch Key Vault Access" -HomePage "https://batch.mydomain.com" -IdentifierUris "https://batch.mydomain.com" -certValue $credValue -StartDate $now -EndDate $expirationDate
# Create new AAD service principal that uses this application
$newAzureAdPrincipal = New-AzureRmADServicePrincipal -ApplicationId $newADApplication.ApplicationId

應用程式的 URL 只是用來存取 Key Vault,並不重要。

將權限授與 Key Vault

在上一個步驟中建立的服務主體需要從 Key Vault 擷取密碼的權限。 您可以透過 Azure 入口網站或使用下列 PowerShell 命令來授與權限。

Set-AzureRmKeyVaultAccessPolicy -VaultName 'BatchVault' -ServicePrincipalName '"https://batch.mydomain.com' -PermissionsToSecrets 'Get'

將憑證指派給 Batch 帳戶

建立 Batch 集區,然後移至集區中的 [憑證] 索引標籤,並指派您所建立的憑證。 憑證現在已在所有 Batch 節點上。

接下來,將憑證指派給 Batch 帳戶。 將憑證指派給帳戶可讓 Batch 將憑證指派給集區,再指派給節點。 若要這麼做,最簡單的方式是在入口網站中移至您的 Batch 帳戶、瀏覽至 [憑證],然後選取 [新增]。 上傳您稍早產生的 .pfx 檔案,並提供密碼。 完成後,憑證就會新增至清單,而且您可以驗證指紋。

現在,建立 Batch 集區時,您可以瀏覽至集區內的 [憑證],並將您建立的憑證指派給該集區。 當您這麼做時,請確定您選取的是存放區位置的 LocalMachine。 憑證會載入到集區中的所有 Batch 節點上。

安裝 Azure PowerShell

如果您打算使用節點上的 PowerShell 指令碼存取 Key Vault,則需要安裝 Azure PowerShell 程式庫。 如果您的節點已安裝 Windows Management Framework (WMF) 5,您可以使用 install-module 命令來下載此元件。 如果您使用的節點沒有 WMF 5,最簡單的安裝方式就是將 Azure PowerShell .msi 檔案與您的 Batch 檔案組合在一起,然後在 Batch 啟動指令碼的開頭部分呼叫安裝程式。 如需詳細資訊,請參閱此範例:

$psModuleCheck=Get-Module -ListAvailable -Name Azure -Refresh
if($psModuleCheck.count -eq 0) {
    $psInstallerPath = Join-Path $downloadPath "azure-powershell.3.4.0.msi" Start-Process msiexec.exe -ArgumentList /i, $psInstallerPath, /quiet -wait
}

存取 Key Vault

現在,您可以在 Batch 節點上執行的指令碼中存取 Key Vault。 若要從指令碼存取 Key Vault,您只需要針對使用憑證的 Microsoft Entra ID 驗證指令碼。 若要在 PowerShell 中執行此操作,請使用下列範例命令。 為 [指紋]、[應用程式識別碼] (服務主體的識別碼),以及 [租用戶識別碼] (您服務主體所在的租用戶) 指定適當的 GUID。

Add-AzureRmAccount -ServicePrincipal -CertificateThumbprint -ApplicationId

經過驗證之後,您就可以像平常一樣存取 KeyVault。

$adminPassword=Get-AzureKeyVaultSecret -VaultName BatchVault -Name batchAdminPass

這些是要在您指令碼中使用的認證。

下一步