部署 Azure 托管应用程序时访问 Key Vault 机密

在部署过程中,需要将安全值(例如密码)作为参数传递时,可从 Azure 密钥保管库检索值。 若要在部署托管应用程序时访问 Key Vault,必须授予对设备资源提供程序服务主体的访问权限。 托管应用程序服务使用此标识来运行操作。 若要在部署过程中从密钥保管库成功检索某个值,服务主体必须能够访问密钥保管库。

本文介绍如何配置 Key Vault 以与托管应用程序一起使用。

启用模板部署

  1. 登录到 Azure 门户

  2. 打开密钥保管库。 在搜索框中输入“密钥保管库”,或者选择“密钥保管库”。

    Screenshot of the Azure home page to open a key vault using search or by selecting key vault.

  3. 选择访问配置

    Screenshot of the key vault setting to select access configuration.

  4. 选择“用于模板部署的 Azure 资源管理器”。 然后选择“应用”。

    Screenshot of the key vault's access configuration that enables Azure Resource Manager for template deployment.

将服务添加为参与者

将“参与者”角色分配给密钥保管库范围内的“设备资源提供程序”用户 。 参与者角色是角色分配的一种特权管理员角色。 有关详细步骤,请转到使用 Azure 门户分配 Azure 角色

设备资源提供程序 是 Microsoft Entra 租户中的服务主体。 从 Azure 门户中,可以验证是否已将其注册,方法是转到“Microsoft Entra ID”>“企业应用程序”并将搜索筛选器更改为“Microsoft 应用程序”。 搜索设备资源提供程序。 如果未找到,请注册Microsoft.Solutions 资源提供程序。

引用 Key Vault 机密

若要将 Key Vault 中的机密传递给托管应用程序中的模板,必须使用链接或嵌套模板并在链接或嵌套模板的参数中引用 Key Vault。 提供 Key Vault 的资源 ID 和机密名称。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The location where the resources will be deployed."
      }
    },
    "vaultName": {
      "type": "string",
      "metadata": {
        "description": "The name of the key vault that contains the secret."
      }
    },
    "secretName": {
      "type": "string",
      "metadata": {
        "description": "The name of the secret."
      }
    },
    "vaultResourceGroupName": {
      "type": "string",
      "metadata": {
        "description": "The name of the resource group that contains the key vault."
      }
    },
    "vaultSubscription": {
      "type": "string",
      "defaultValue": "[subscription().subscriptionId]",
      "metadata": {
        "description": "The name of the subscription that contains the key vault."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "dynamicSecret",
      "properties": {
        "mode": "Incremental",
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "adminLogin": {
              "type": "string"
            },
            "adminPassword": {
              "type": "securestring"
            },
            "location": {
              "type": "string"
            }
          },
          "variables": {
            "sqlServerName": "[concat('sql-', uniqueString(resourceGroup().id, 'sql'))]"
          },
          "resources": [
            {
              "type": "Microsoft.Sql/servers",
              "apiVersion": "2022-05-01-preview",
              "name": "[variables('sqlServerName')]",
              "location": "[parameters('location')]",
              "properties": {
                "administratorLogin": "[parameters('adminLogin')]",
                "administratorLoginPassword": "[parameters('adminPassword')]"
              }
            }
          ],
          "outputs": {
            "sqlFQDN": {
              "type": "string",
              "value": "[reference(variables('sqlServerName')).fullyQualifiedDomainName]"
            }
          }
        },
        "parameters": {
          "location": {
            "value": "[parameters('location')]"
          },
          "adminLogin": {
            "value": "ghuser"
          },
          "adminPassword": {
            "reference": {
              "keyVault": {
                "id": "[resourceId(parameters('vaultSubscription'), parameters('vaultResourceGroupName'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"
              },
              "secretName": "[parameters('secretName')]"
            }
          }
        }
      }
    }
  ],
  "outputs": {
  }
}

后续步骤

已将 Key Vault 配置为在部署托管应用程序期间可访问。