【應用服務 App Service】App Service證書導入,使用Key Vault中的證書
- 2020 年 10 月 28 日
- 筆記
- 【應用服務 App Service】, App Service, Key Vault, TLS/SSL
問題描述
正常情況下,如果需要為應用服務安裝SSL證書,可以在證書準備好的情況,通過門戶上傳即可,詳細步驟可以參考微軟官方文檔(在 Azure 應用服務中添加 TLS/SSL 證書://docs.azure.cn/zh-cn/app-service/configure-ssl-certificate), 但是由於Global Azure和中國區Azure的不同之處,在中國區微軟雲無法直接在門戶中導入Key Vault中的證書。 以下是兩個版本頁面之間的不同之處:
中國區 Azure | 國際版 Azure |
![]() |
![]() |
那要這樣才能在中國區中導入Key Vault中的SSL證書呢? 使用PowerShell腳本完成導入完成。(把KeyVaule中的證書轉變為Secrets後,配置到App Service)
執行步驟
1) 在Key Vault中把證書已PFX/PEM格式導出(其實導出證書後,可以直接在App Service中上傳證書,只是這樣就沒有Key Vault來保護證書)
(導航路徑: Key Vault –> Settings Certificates –> Certificates Name –> Current Version)
2) 把證書導入到Key Vault中的Secrets中。自定義一個Key Vault Secret Name並複製出key vault的資源ID,第三步會使用到。
- 選擇Upload Options為Certificate
- 上傳第一步中下載的證書
- 輸入證書名:mysitecertfile(第三步 Powershell命令中需要)
3)修改指定的參數,運行PowerShell命令,把證書導入到App Service中
Connect-AzureRmAccount -Environment AzureChinaCloud $location = "<resource location>" $ResourceGroupName = "<app service resource group name>" $AppServicePlanName = "/subscriptions/<subscription id>/resourceGroups/<app service resource group name>/providers/Microsoft.Web/serverfarms/<server farm id>" $KeyVault = "/subscriptions/<subscription id>/resourcegroups/<key vault resource group name>/providers/microsoft.keyvault/vaults/<key vault name>" $KeyVaultSecert = "<key vault secret name>" $newCert = "NewCertificate" $PropertiesObject = @{ keyVaultId= $KeyVault keyVaultSecretName= $KeyVaultSecert serverFarmId= $AppServicePlanName } New-AzureRmResource -Name $newCert -Location $location ` -PropertyObject $PropertiesObject ` -ResourceGroupName $ResourceGroupName ` -ResourceType Microsoft.Web/certificates ` -Force
以上值都可以在相對應的資源中查看:
- $AppServicePlanName:在應用服務的Overview頁面中,通過App Service Plan鏈接進入,在Porperties(左側目錄)頁面複製Resource ID值
- $KeyVault:在Key Vault的Overview頁面中,點擊Porperties(左側目錄)頁面複製Resource ID值
- 如果登錄後,有多個訂閱資訊,可以通過命令Select-AzureRmSubscription -Subscription ‘<your subscription name>’ 來指定訂閱
- 在執行綁定Key Vault證書時,如果遇見”The service does not have access to ‘/subscriptions/…………/microsoft.keyvault/vaults/…’ Key Vault.Please make sure that you have granted necessary permissions to the service to perform the request operation. 需要執行以下兩個步驟來為App Service複製訪問Key Vault的授權
- 在App Service中開啟Identity, System Assigned功能,並複製出Object ID用於在Key Value中賦予許可權
2. 在Key Vault中的Access Policies中為App Service賦予訪問許可權
4)訪問站點,驗證證書。
參考資料
在 Azure 應用服務中添加 TLS/SSL 證書://docs.azure.cn/zh-cn/app-service/configure-ssl-certificate
使用應用服務和 Azure Functions 的 Key Vault 引用: //docs.azure.cn/zh-cn/app-service/app-service-key-vault-references
關於 Azure Key Vault: //docs.azure.cn/zh-cn/key-vault/general/overview
Azure Key Vault 有助於解決以下問題:
- 機密管理 – Azure Key Vault 可以用來安全地存儲令牌、密碼、證書、API 密鑰和其他機密,並對其訪問進行嚴格控制
- 密鑰管理 – Azure Key Vault 也可用作密鑰管理解決方案。 可以通過 Azure Key Vault 輕鬆創建和控制用於加密數據的加密密鑰。
- 證書管理 – Azure Key Vault 也是一項服務,可用來輕鬆預配、管理和部署公用和專用傳輸層安全性/安全套接字層 (TLS/SSL) 證書,以用於 Azure 以及內部連接資源。
為何使用 Azure Key Vault?
集中管理應用程式機密
在 Azure Key Vault 中集中存儲應用程式機密就可以控制其分發。 Key Vault 可以大大減少機密意外泄露的可能性。 有了 Key Vault,應用程式開發人員就再也不需要將安全資訊存儲在應用程式中。 無需將安全資訊存儲在應用程式中,因此也無需將此資訊作為程式碼的一部分。 例如,如果某個應用程式需要連接到資料庫, 則可將連接字元串安全地存儲在 Key Vault 中,而不是存儲在應用程式碼中。
應用程式可以使用 URI 安全訪問其所需的資訊。 這些 URI 允許應用程式檢索特定版本的機密。 這樣就不需編寫自定義程式碼來保護存儲在 Key Vault 中的任何機密資訊。