Azure Managed Applications のデプロイ時に Key Vault シークレットにアクセスする

デプロイ時に、セキュリティで保護された値 (パスワードなど) をパラメーターとして渡す必要がある場合は、Azure Key Vault からその値を取得できます。 Managed Applications のデプロイ時に Key Vault にアクセスするには、アプライアンス リソース プロバイダー サービス プリンシパルにアクセス許可を付与する必要があります。 マネージド アプリケーション サービスでは、この ID を使用して操作が実行されます。 デプロイ時にキー コンテナーから正常に値を取得するには、サービス プリンシパルでキー コンテナーにアクセスできる必要があります。

この記事では、Managed Applications を使用するように Key Vault を構成する方法について説明します。

テンプレートのデプロイを有効にする

  1. Azure portal にサインインします。

  2. キー コンテナーを開きます。 検索ボックスに「key vaults」と入力するか、[キー コンテナー] を選択します。

    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 Resource Manager (テンプレートの展開用)] を選択します。 次に、[適用] を選択します。

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

サービスを共同作成者として追加する

共同作成者ロールを、キー コンテナー スコープでアプライアンス リソース プロバイダー ユーザーに割り当てます。 共同作成者ロールは、ロール割り当ての "特権管理者ロール" です。 詳細な手順については、「Azure portal を使用して Azure ロールを割り当てる」にアクセスしてください。

アプライアンス リソース プロバイダーは、Microsoft Entra のテナントのサービス プリンシパルです。 Azure portal から、[Microsoft Entra ID][Enterprise アプリケーション] の順に移動し、検索フィルターを 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 を構成しました。