pulumi:将 Web 应用程序添加到密钥保管库访问策略,读取机密并将其设置为应用程序设置

pulumi: add web app to key vault access policy, read secrets and set them as app setting

我正在使用 azure nextgen 提供程序。给定一个密钥库 vault,其中存储了一个秘密 secret1

我没有在 pulumi 文档中找到针对这些场景的任何函数。

包含缺失部分的脚本:

import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";
import * as resources from "@pulumi/azure-nextgen/resources/latest";
import * as web from "@pulumi/azure-nextgen/web/latest";

const config = new pulumi.Config();
const location = config.require("location");

const resourceGroup = new resources.ResourceGroup("rootResourceGroup", {
    resourceGroupName: "resources",
    location,
});

const suffix = new random.RandomString("suffix", {
    length: 6,
    special: false,
    upper: false,
});

const appServicePlan = new web.AppServicePlan("appserviceplan", {
    name: "my-appservice-plan",
    resourceGroupName: resourceGroup.name,
    location,
    kind: "Linux",
    reserved: true,
    sku: {
        name: "B1",
        tier: "Basic",
    },
});


const vault = ???; // Get the vault by name
const secret1Identifier = vault.???; // fetch the secret by name

const webApp = new web.WebApp("web-app", {
    name: pulumi.interpolate`webapp${suffix.result}`,
    resourceGroupName: resourceGroup.name,
    location,
    serverFarmId: appServicePlan.id,
    identity: {
        type: "SystemAssigned",
    },
    siteConfig: {
        appSettings: [
            {
                name: "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
                value: "false",
            },
            {
                name: "SECRET1",
                value: pulumi.interpolate`@Microsoft.KeyVault(SecretUri=${secret1Identifier})`
            }
        ],
        alwaysOn: true,
    },
});

const principalId = webApp.identity.apply(id => id?.principalId);

vault.??? // Set access policy for web apps principal id

正确的顺序是:

  1. 秘密
  2. 带有秘密的 Web 应用程序
  3. 访问政策

KeyVault 有一个奇怪的 API 模型,它仅通过 Azure 资源管理器部分公开。目前,Azure NextGen 不支持机密、密钥、证书或访问策略。这在 this issue.

中进行了跟踪

同时,您可以使用“旧”Azure 提供程序来添加缺失的对象。可以从同一个程序中使用这两个提供程序。 This example 接近您要按顺序实现的目标。