我已通过 Azure 门户为我的 Web 应用程序成功手动配置了 Azure AD 身份验证,但我正在努力使用 Terraform 将其自动化。
通过 Terraform 设置身份验证后,成功的 AAD 登录结果为:
错误消息:“HTTP 错误 500”
登录后的URL是:https://.azurewebsites.net/.auth/login/aad/callback
当我尝试使用 Terraform 配置它时,“身份要求”设置为“特定身份”列表为空。 此外,租户要求根据发行人设置为默认限制。为了使其正常工作,我必须添加两个租户 - 一个是应用程序所在的租户,第二个是 AAD 所在的租户。
我的地形粘贴在下面。
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"
}
}
通过 Terraform (active_directory_v2) 设置允许的身份和租户
目前,通过 Terraform 为 Azure Web 应用程序配置“允许的租户”和“身份要求”,AzureRM 提供程序对于直接在 Terraform 中进行这些特定配置存在一些限制。这是满足您要求的方法:
允许的租户配置:虽然 AzureRM 提供程序本身不支持直接在
auth_settings_v2
块中指定多个租户,但您可以通过在 Terraform 中使用 Azure CLI 命令来设置允许的租户来解决此问题。
身份要求配置:当提供空列表时,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
}
部署成功: