【问题标题】:Azure terraform Use existing managed identity to authenticate web appAzure terraform 使用现有托管标识对 Web 应用进行身份验证
【发布时间】:2021-11-19 02:37:15
【问题描述】:

希望有人能帮我解决这个问题。

我使用 terraform 编写了一些资源组和 web 应用程序的脚本。这些网络应用程序有一些配置需要访问密钥库以检索一些秘密。 但要这样做,我需要在 Web 应用程序上激活 azure 身份。 到目前为止,一切正常,没有任何问题。但由于我仍在学习如何将 terraform 与 azure 结合使用,我需要继续销毁和启动 web 应用程序,这意味着每次我需要激活 identity 并在密钥库中添加 access policy

所以我所做的是在我拥有密钥保管库的同一资源组中创建一个azure managed identity 资源。现在我想在每次启动网络应用程序时使用这个managed identity 来验证我的网络应用程序。

我的网络应用代码如下所示:


resource "azurerm_app_service" "app-hri-stg-eur-configurations-api" {
  name                = "app-hri-${var.env}-${var.reg-name}-webapp-testing"
  app_service_plan_id = azurerm_app_service_plan.ASP-hri-stg-eur-webapp.id
  location            = var.location
  resource_group_name = azurerm_resource_group.rg-hri-stg-eur-webapp.name
  app_settings = {
    "secret"        = "@Microsoft.KeyVault(SecretUri=https://mykeyvault.vault.azure.net/secrets/test)"
    ...... <My configuration>
  }
  identity {
    type = "UserAssigned"
  }
}

这就是我感到困惑的地方,我如何引用我已经创建的 Azure 托管标识来授予对我的 Web 应用程序的访问权限以读取机密?

我希望我的问题足够清楚,如果不只是询问更多信息,请询问

非常感谢您提供的任何帮助

【问题讨论】:

    标签: azure-devops terraform-provider-azure


    【解决方案1】:

    A identity block supports the following:

    • type -(必需)指定应用服务的标识类型。可能的值是 SystemAssigned(Azure 将为您生成服务主体)、UserAssigned(您可以在 identity_ids 字段中指定服务主体 ID)和 SystemAssigned、UserAssigned(它分配系统托管标识以及指定的用户分配标识)。
    • identity_ids - (可选)指定要分配的用户管理的身份 ID 列表。如果类型是 UserAssigned,则为必需。

    所以你应该有像this这样的东西:

    data "azurerm_user_assigned_identity" "example" {
      name                = "name_of_user_assigned_identity"
      resource_group_name = "name_of_resource_group"
    }
    

    如果您的身份在另一个资源组中。它允许您引用已创建的 azure 资源。

      identity {
        type = "UserAssigned",
        identity_ids = [data.azurerm_user_assigned_identity.example.id]
      }
    

    【讨论】:

    • 非常感谢您的回复。这解决了我的疑问。但即使 Web 应用程序具有 UserAssigned 标识,并且密钥保管库在访问策略中具有托管标识。我的 Web 应用程序无法解析密钥保管库引用。你知道为什么会这样吗?托管身份有权获取、设置和列出机密。
    • 这可能取决于您尝试联系 KeyVault 的方式。请关注AzureDefaultCredentials
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2021-11-30
    • 2021-12-23
    • 2022-06-14
    • 2021-09-01
    • 1970-01-01
    • 2020-06-08
    • 2020-01-24
    相关资源
    最近更新 更多