azure - ARM 模板中的 KeyVault 引用存在问题

标签 azure azure-powershell azure-keyvault azure-rm-template

我正在尝试创建一个主 key 保管库,其中包含用于以特定用户身份进行身份验证的所有证书。

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

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

使用 powershell,我可以作为部署 SP 登录并检索 secret (证书)。

但是,当使用对 key 保管库的引用来部署 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 模板:(请注意,Key Vault 位于与 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
        }
    }
]
}

最佳答案

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

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

关于azure - ARM 模板中的 KeyVault 引用存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42134416/

相关文章:

Azure 应用程序网关 TLS 加密不适用于 Key Vault 中的证书

azure - 无法在 Azure 政府云中导入 Key Vault 证书

powershell - 如何隐藏 Azure PowerShell cmdlet 的警告

python - 我需要 'requirements.txt' 文件还是应该安装依赖项? Azure 开发运营

asp.net-mvc - Aspnet Core Azure 失败并出现 HTTP 错误 502.5 - 进程失败

javascript - Azure 文本转语音 : how can I change language and voice for output?

azure-devops - Azure Powershell - Az 模块无法在 Ubuntu 托管的构建代理上运行

azure - PowerShell 版本及其管理 Azure Active Directory 的能力有何区别?

azure - 如何使用蓝图将访问策略分配到 Azure Key Vault

unit-testing - Windows Azure 测试最佳实践(单元)