2017-02-09 45 views
0

我正在嘗試創建一個主密鑰保管庫,它將包含所有要作爲特定用戶進行身份驗證的證書。ARM模板中的KeyVault參考問題

我有2個服務主體=>一個用於我的應用程序,一個用於部署。 這個想法是,部署服務主體可以訪問密鑰保管庫,並將位於那裏的證書添加到Web應用程序的Store中。

我已經創建了服務主體,並且已授予他關鍵密鑰庫的所有權限。此外,我還在ARM模板中爲該密鑰保管庫啓用了訪問機密。

使用powershell我能夠以部署SP身份登錄並檢索祕密(證書)。

但是,在部署引用密鑰庫的ARM模板時,這不起作用。我得到了以下錯誤:

New-AzureRmResourceGroupDeployment : 11:16:44 - Resource Microsoft.Web/certificates 'test-certificate' failed with message '{ "Code": "BadRequest", "Message": "The service does not have access to '/subscriptions/98f06e7e-1016-4088-843f-62690f3bb306/resourcegroups/rg-temp/providers/microsoft.keyvault/vaults/master-key-vault' Key Vault. Please make sure that you have granted necessary permissions to the service to perform the request operation.", "Target": null, "Details": [ { "Message": "The service does not have access to '/subscriptions/xxxx/resourcegroups/xxx/providers/microsoft.keyvault/vaults/master-key-vault' Key Vault. Please make sure that you have granted necessary permissions to the service to perform the request operation." },

我的胳膊模板看起來是這樣的:

{ 
    "type":"Microsoft.Web/certificates", 
    "name":"test-certificate", 
    "apiVersion":"2016-03-01", 
    "location":"[resourceGroup().location]", 
    "properties":{ 
     "keyVaultId":"[resourceId('rg-temp', 'Microsoft.KeyVault/vaults', 'master-key-vault')]", 
     "keyVaultSecretName":"kv-certificate-test", 
     "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', 'asp-test')]" 
    } 
    }, 

這是一個錯誤?因爲我能夠檢索使用部署SP與證書:

$key = Get-AzureKeyVaultSecret -VaultName "master-key-vault" -Name "testenvironmentcertificate" 

這是我的ARM模板:(注意,密鑰庫的生活比在ARM模板的資源另一個資源組)

{ 
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json", 
"contentVersion": "1.0.0.0", 
"parameters": {}, 
"variables": {}, 
"resources": [ 
    { 
    "type":"Microsoft.Web/certificates", 
    "name":"test-certificate", 
    "apiVersion":"2016-03-01", 
    "location":"[resourceGroup().location]", 
    "properties":{ 
     "keyVaultId":"/subscriptions/xxx/resourceGroups/rg-temp/providers/Microsoft.KeyVault/vaults/xxx", 
     "keyVaultSecretName":"testcert", 
     "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', 'asp-test')]" 
    } 
    }, 
    { 
     "name": "wa-test1", 
     "type": "Microsoft.Web/sites", 
     "location": "[resourceGroup().location]", 
     "apiVersion": "2016-08-01", 
     "dependsOn": [ 
      "[concat('Microsoft.Web/serverfarms/', 'asp-test')]" 
     ], 
     "tags": { 
      "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/asp-test')]": "Resource", 
      "displayName": "wa-test1" 
     }, 
     "properties": { 
      "name": "wa-test1", 
      "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', 'asp-test')]" 
     } 
    }, 
    { 
     "name": "asp-test", 
     "type": "Microsoft.Web/serverfarms", 
     "location": "[resourceGroup().location]", 
     "apiVersion": "2014-06-01", 
     "dependsOn": [], 
     "tags": { 
      "displayName": "appServicePlan" 
     }, 
     "properties": { 
      "name": "asp-test", 
      "sku": "Free", 
      "workerSize": "Small", 
      "numberOfWorkers": 1 
     } 
    } 
], 
"outputs": {} 
} 
+0

可以分享你的模板?你可以刪除敏感信息 – 4c74356b41

+0

我已編輯我的問題:) – Identity

+0

好吧,乍一看這看起來很好,我不知道什麼可能是錯的,你可以創建另一個關鍵的保險櫃,只需使用您的訂閱管理部署模板(並驗證管理員擁有keyvault的所有權利,keyvault允許模板部署?)? – 4c74356b41

回答

2

我相信你缺少的資源提供訪問密鑰庫權限,所以Web應用程序是使用自己的資源提供者要做到這一點,你需要授予關鍵隔間內,RP訪問:

集,AzureRmKeyVaultAccessPolicy -VaultName KEYVAULTNAME -Servi cePrincipalName abfa0a7c-a6b6-4736-8310-5855508787cd -PermissionsToSecrets得到

參考:

https://blogs.msdn.microsoft.com/appserviceteam/2016/05/24/deploying-azure-web-app-certificate-through-key-vault/

+0

我已經檢查了第二個複選框=>我的密鑰庫=>高級訪問策略。即使在部署ARM模板時使用我的訂閱管理員,也給我提供了同樣的錯誤,任何想法? – Identity

+0

我執行了這個命令,但是我沒有使用'abfa0a7c-a6b6-4736-8310-5855508787cd',而是我部署了SP ID。感謝您的回覆,它的工作原理! – Identity