3

我正在尝试创建一个主密钥库,其中将包含所有证书以作为某个用户进行身份验证。

我有 2 个服务主体 => 一个用于我的应用程序,一个用于部署。这个想法是部署服务主体可以访问 Key Vault 并将位于那里的证书添加到 Web 应用程序的存储区。

我创建了服务主体,并授予了他对密钥保管库的所有权限。我还在 ARM 模板中为该密钥库启用了访问机密。

使用 powershell,我能够以 Deploying 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."
    },

我的 ARM 模板如下所示:

    {
     "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')]"
     }
  },

这是一个错误吗?因为我能够使用 Deploy 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
        }
    }
]
}
4

3 回答 3

7

我相信您缺少资源提供者访问 Key Vault 的权限,因此 WebApp 正在使用自己的资源提供者来执行此操作,您需要授予该 RP 对密钥库的访问权限:

Set-AzureRmKeyVaultAccessPolicy -VaultName KEYVAULTNAME -PermissionsToSecrets get `
   -ServicePrincipalName abfa0a7c-a6b6-4736-8310-5855508787cd

参考:

https://azure.github.io/AppService/2016/05/24/Deploying-Azure-Web-App-Certificate-through-Key-Vault.html

于 2017-02-09T10:46:32.787 回答
0

我尝试了所有答案,但没有奏效。这对我有用:

设置密钥保管库上两个服务主体的访问权限:

在此处输入图像描述


在这里阅读更多:

https://devsdaily.com/key-vault-failed-to-sync-the-certificate-the-service-does-not-have-access-to-key-vault/

于 2021-07-29T09:52:45.350 回答
0

Set-AzureRmKeyVaultAccessPolicy 由于控制台中的错误,我无法通过命令添加策略。

但是,我能够通过打开 KeyVault 访问控制 (IAM) 并添加角色来通过 Azure Web 界面Key Vault Reader解决Key Vault Secrets User问题Microsoft.Azure.Websites

在此处输入图像描述

我也在这里写了同样的答案。

于 2022-02-04T18:29:30.007 回答