Skip to content

Commit 5e6d219

Browse files
author
Simon Schneider
committed
azuread_application reference for app service
To add Azure AD authentication to app service, we need to reference an existing azuread_application and service_principal_password
1 parent 012d6b0 commit 5e6d219

5 files changed

Lines changed: 119 additions & 26 deletions

File tree

.github/workflows/standalone-scenarios.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
"webapps/appservice/106-appservice-diagnostics",
247247
"webapps/appservice/107-appservice-private",
248248
"webapps/appservice/109-appservice-appgw",
249+
"webapps/appservice/110-appservice-auth",
249250
"webapps/function_app/101-function_app-private",
250251
"webapps/function_app/102-function_app-linux",
251252
"webapps/function_app/103-function_app-windows",

app_services.tf

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ module "app_services" {
66
depends_on = [module.networking]
77
for_each = local.webapp.app_services
88

9-
name = each.value.name
10-
client_config = local.client_config
11-
location = can(local.global_settings.regions[each.value.region]) ? local.global_settings.regions[each.value.region] : local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group.key, each.value.resource_group_key)].location
12-
resource_group_name = can(each.value.resource_group.name) || can(each.value.resource_group_name) ? try(each.value.resource_group.name, each.value.resource_group_name) : local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group_key, each.value.resource_group.key)].name
13-
app_service_plan_id = can(each.value.app_service_plan_id) ? each.value.app_service_plan_id : local.combined_objects_app_service_plans[try(each.value.lz_key, local.client_config.landingzone_key)][each.value.app_service_plan_key].id
14-
settings = each.value.settings
15-
identity = try(each.value.identity, null)
16-
connection_strings = try(each.value.connection_strings, {})
17-
app_settings = try(each.value.app_settings, null)
18-
slots = try(each.value.slots, {})
19-
global_settings = local.global_settings
20-
dynamic_app_settings = try(each.value.dynamic_app_settings, {})
21-
combined_objects = local.dynamic_app_settings_combined_objects
22-
base_tags = try(local.global_settings.inherit_tags, false) ? try(local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group.key, each.value.resource_group_key)].tags, {}) : {}
23-
application_insight = try(each.value.application_insight_key, null) == null ? null : module.azurerm_application_insights[each.value.application_insight_key]
24-
diagnostic_profiles = try(each.value.diagnostic_profiles, null)
25-
diagnostics = local.combined_diagnostics
26-
storage_accounts = local.combined_objects_storage_accounts
27-
tags = try(each.value.tags, null)
28-
private_endpoints = try(each.value.private_endpoints, {})
29-
vnets = local.combined_objects_networking
30-
subnet_id = can(each.value.subnet_id) || can(each.value.vnet_key) == false ? try(each.value.subnet_id, null) : local.combined_objects_networking[try(each.value.lz_key, local.client_config.landingzone_key)][each.value.vnet_key].subnets[each.value.subnet_key].id
31-
private_dns = local.combined_objects_private_dns
9+
name = each.value.name
10+
client_config = local.client_config
11+
location = can(local.global_settings.regions[each.value.region]) ? local.global_settings.regions[each.value.region] : local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group.key, each.value.resource_group_key)].location
12+
resource_group_name = can(each.value.resource_group.name) || can(each.value.resource_group_name) ? try(each.value.resource_group.name, each.value.resource_group_name) : local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group_key, each.value.resource_group.key)].name
13+
app_service_plan_id = can(each.value.app_service_plan_id) ? each.value.app_service_plan_id : local.combined_objects_app_service_plans[try(each.value.lz_key, local.client_config.landingzone_key)][each.value.app_service_plan_key].id
14+
settings = each.value.settings
15+
identity = try(each.value.identity, null)
16+
connection_strings = try(each.value.connection_strings, {})
17+
app_settings = try(each.value.app_settings, null)
18+
slots = try(each.value.slots, {})
19+
global_settings = local.global_settings
20+
dynamic_app_settings = try(each.value.dynamic_app_settings, {})
21+
combined_objects = local.dynamic_app_settings_combined_objects
22+
base_tags = try(local.global_settings.inherit_tags, false) ? try(local.combined_objects_resource_groups[try(each.value.resource_group.lz_key, local.client_config.landingzone_key)][try(each.value.resource_group.key, each.value.resource_group_key)].tags, {}) : {}
23+
application_insight = try(each.value.application_insight_key, null) == null ? null : module.azurerm_application_insights[each.value.application_insight_key]
24+
diagnostic_profiles = try(each.value.diagnostic_profiles, null)
25+
diagnostics = local.combined_diagnostics
26+
storage_accounts = local.combined_objects_storage_accounts
27+
tags = try(each.value.tags, null)
28+
private_endpoints = try(each.value.private_endpoints, {})
29+
vnets = local.combined_objects_networking
30+
subnet_id = can(each.value.subnet_id) || can(each.value.vnet_key) == false ? try(each.value.subnet_id, null) : local.combined_objects_networking[try(each.value.lz_key, local.client_config.landingzone_key)][each.value.vnet_key].subnets[each.value.subnet_key].id
31+
private_dns = local.combined_objects_private_dns
32+
azuread_applications = local.combined_objects_azuread_applications
33+
azuread_service_principal_passwords = local.combined_objects_azuread_service_principal_passwords
34+
3235
}
3336

3437
output "app_services" {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
global_settings = {
2+
default_region = "region1"
3+
regions = {
4+
region1 = "australiaeast"
5+
}
6+
}
7+
8+
resource_groups = {
9+
webapp_simple = {
10+
name = "webapp-simple"
11+
region = "region1"
12+
}
13+
}
14+
15+
azuread_applications = {
16+
test_client = {
17+
useprefix = true
18+
application_name = "test-client"
19+
reply_urls = ["https://example.azurewebsites.net/.auth/login/aad/callback"]
20+
}
21+
}
22+
23+
azuread_service_principals = {
24+
sp1 = {
25+
azuread_application = {
26+
key = "test_client"
27+
}
28+
app_role_assignment_required = true
29+
}
30+
}
31+
32+
azuread_service_principal_passwords = {
33+
sp1 = {
34+
azuread_service_principal = {
35+
key = "sp1"
36+
}
37+
password_policy = {
38+
length = 250
39+
special = false
40+
upper = true
41+
number = true
42+
expire_in_days = 10
43+
rotation = {
44+
mins = 3
45+
}
46+
}
47+
}
48+
}
49+
50+
# By default asp1 will inherit from the resource group location
51+
app_service_plans = {
52+
asp1 = {
53+
resource_group_key = "webapp_simple"
54+
name = "asp-simple"
55+
56+
sku = {
57+
tier = "Standard"
58+
size = "S1"
59+
}
60+
}
61+
}
62+
63+
app_services = {
64+
webapp1 = {
65+
resource_group_key = "webapp_simple"
66+
name = "webapp-simple"
67+
app_service_plan_key = "asp1"
68+
69+
app_settings = {
70+
"WEBSITE_NODE_DEFAULT_VERSION" = "6.9.1"
71+
}
72+
73+
settings = {
74+
enabled = true
75+
76+
auth_settings = {
77+
enabled = true
78+
unauthenticated_client_action = "RedirectToLoginPage"
79+
issuer = "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000"
80+
active_directory = {
81+
client_id_key = "test_client"
82+
client_secret_key = "sp1"
83+
}
84+
}
85+
}
86+
}
87+
}

modules/webapps/appservice/module.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ resource "azurerm_app_service" "app_service" {
126126
for_each = lookup(var.settings.auth_settings, "active_directory", {}) != {} ? [1] : []
127127

128128
content {
129-
client_id = var.settings.auth_settings.active_directory.client_id
130-
client_secret = lookup(var.settings.auth_settings.active_directory, "client_secret", null)
129+
client_id = can(var.settings.auth_settings.active_directory.client_id_key) ? var.azuread_applications[try(var.settings.auth_settings.active_directory.client_id_lz_key, var.client_config.landingzone_key)][var.settings.auth_settings.active_directory.client_id_key].application_id : var.settings.auth_settings.active_directory.client_id
130+
client_secret = can(var.settings.auth_settings.active_directory.client_secret_key) ? var.azuread_service_principal_passwords[try(var.settings.auth_settings.active_directory.client_secret_lz_key, var.client_config.landingzone_key)][var.settings.auth_settings.active_directory.client_secret_key].service_principal_password : try(var.settings.auth_settings.active_directory.client_secret, null)
131131
allowed_audiences = lookup(var.settings.auth_settings.active_directory, "allowed_audiences", null)
132132
}
133133
}

modules/webapps/appservice/variables.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@ variable "diagnostics" {
7373
variable "vnets" {}
7474
variable "subnet_id" {}
7575
variable "private_endpoints" {}
76-
variable "private_dns" {}
76+
variable "private_dns" {}
77+
variable "azuread_applications" {}
78+
variable "azuread_service_principal_passwords" {}

0 commit comments

Comments
 (0)