Azure Azure AD 身份验证 - 错误 500 - 如何通过 Terraform (active_directory_v2) 设置允许的身份和租户

问题描述 投票:0回答:1

我已通过 Azure 门户为我的 Web 应用程序成功手动配置了 Azure AD 身份验证,但我正在努力使用 Terraform 将其自动化。

设置详情

  • Azure 门户中的手动配置:按预期工作。 我的工作设置图片
  • Terraform 配置:遇到未反映手动设置的身份和租户要求问题。具体来说:
  • a)身份要求默认为“特定身份”,但我需要它是“任何身份”。
  • b) 租户要求默认为“使用基于发行人的默认限制”。我需要指定两个租户 - 一个是应用程序所在的租户,另一个是 AAD 所在的租户。

当前行为

通过 Terraform 设置身份验证后,成功的 AAD 登录结果为:

错误消息:“HTTP 错误 500”

登录后的URL是:https://.azurewebsites.net/.auth/login/aad/callback

当我尝试使用 Terraform 配置它时,“身份要求”设置为“特定身份”列表为空。 此外,租户要求根据发行人设置为默认限制。为了使其正常工作,我必须添加两个租户 - 一个是应用程序所在的租户,第二个是 AAD 所在的租户。

我的地形粘贴在下面。

问题:

  • 有没有办法通过 Terraform 配置“允许的租户”?
  • 有没有办法使用 Terraform 将“身份要求”变为“任何身份”?我尝试了空列表并省略了参数,但最终总是得到特定的身份。
  • 通过 terraform 创建身份验证后,我尝试手动调整这两件事,但它仍然不起作用。我必须将其删除并从头开始手动创建才能使其正常工作。可能是什么原因造成的?
resource "azurerm_linux_web_app" "my_web_app" {
  service_plan_id     = azurerm_service_plan.my_app_serive_plan.id
  name                = local.my_web_app_name
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  site_config {
    application_stack {
      python_version = "3.11"
    }
  }

    app_settings = {
    "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET" = var.web_app_client_secret
    ...
  }

  sticky_settings {
    app_setting_names = ["MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"]
  }

  auth_settings_v2 {
    auth_enabled           = true
    default_provider       = "aad"
    require_authentication = true
    require_https          = true
    unauthenticated_action = "RedirectToLoginPage"


    login {
      token_store_enabled  = true
    }

    active_directory_v2 {
      client_id = var.web_app_client_id
      tenant_auth_endpoint = "https://login.microsoftonline.com/${var.auth_tenant_id}/v2.0/"

      client_secret_setting_name =  "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"
      allowed_applications = [
            "https://${local.my_web_app_name}.azurewebsites.net"
          ]
      # allowed_identities = [ ]

      # tenants = ["...", "..." ]

    }    
  }

  identity {
    type = "SystemAssigned"
  }
}

文档:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_web_app#active_directory_v2

azure terraform azure-active-directory active-directory azure-web-app-service
1个回答
0
投票

通过 Terraform (active_directory_v2) 设置允许的身份和租户

目前,通过 Terraform 为 Azure Web 应用程序配置“允许的租户”和“身份要求”,AzureRM 提供程序对于直接在 Terraform 中进行这些特定配置存在一些限制。这是满足您要求的方法:

  1. 允许的租户配置:虽然 AzureRM 提供程序本身不支持直接在

    auth_settings_v2
    块中指定多个租户,但您可以通过在 Terraform 中使用 Azure CLI 命令来设置允许的租户来解决此问题。

  2. 身份要求配置:当提供空列表时,Terraform 提供程序可能默认为特定身份。这可能需要在初始部署后手动管理。

我尝试了以下配置,并且能够满足您正在寻找的要求。

Terraform 配置:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "vkbtest-rg"
  location = "East US"
}

resource "azurerm_service_plan" "my_app_service_plan" {
  name                = "tesvk-service-plan"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  os_type             = "Linux"
  sku_name            = "P1v2"
}

resource "azurerm_linux_web_app" "my_web_app" {
  service_plan_id     = azurerm_service_plan.my_app_service_plan.id
  name                = "tevk-web-app"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  site_config {
    application_stack {
      python_version = "3.11"
    }
  }

  app_settings = {
    "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET" = var.web_app_client_secret
  }

  sticky_settings {
    app_setting_names = ["MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"]
  }

  auth_settings_v2 {
    auth_enabled           = true
    default_provider       = "aad"
    require_authentication = true
    require_https          = true
    unauthenticated_action = "RedirectToLoginPage"

    login {
      token_store_enabled  = true
    }

    active_directory_v2 {
      client_id = var.web_app_client_id
      tenant_auth_endpoint = "https://login.microsoftonline.com/${var.auth_tenant_id}/v2.0/"
      client_secret_setting_name = "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"
      # allowed_applications will be set in the null_resource
      # allowed_applications = [
      #   "https://${azurerm_linux_web_app.my_web_app.name}.azurewebsites.net"
      # ]
    }    
  }

  identity {
    type = "SystemAssigned"
  }
}

resource "null_resource" "configure_additional_settings" {
  depends_on = [azurerm_linux_web_app.my_web_app]

  provisioner "local-exec" {
     interpreter = ["pwsh", "-Command"]
  command = <<EOT
    $allowedTenants = "tenant1,tenant2"
    $allowNoIdentity = "true"
    az webapp auth update --name tevk-web-app --resource-group vkbtest-rg --enabled true
    az webapp config appsettings set --name tevk-web-app --resource-group vkbtest-rg --settings identityProviders.azureActiveDirectory.allowedTenants="$allowedTenants" identityProviders.azureActiveDirectory.allowedAudiences="https://tevk-web-app.azurewebsites.net" identityProviders.azureActiveDirectory.tokenIssuers="$allowedTenants"
    az webapp config appsettings set --name tevk-web-app --resource-group vkbtest-rg --settings identityProviders.azureActiveDirectory.login.allowNoIdentity=$allowNoIdentity
  EOT
}

}

variable "web_app_client_id" {
  description = "The Client ID for the web app."
}

variable "web_app_client_secret" {
  description = "The Client Secret for the web app."
}

variable "auth_tenant_id" {
  description = "The Tenant ID for the Azure AD."
}

variable "allowed_tenants" {
  description = "The list of allowed tenants."
  type        = list(string)
}

variable "allow_no_identity" {
  description = "Flag to allow requests from any identity."
  type        = bool
  default     = true
}

部署成功:

enter image description here

enter image description here

enter image description here

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.