Skip to content

Commit 77c9d6b

Browse files
Felix Ziegerfelixzieger
authored andcommitted
feat: finer control over scope and service principal names
1 parent d65d6e7 commit 77c9d6b

File tree

6 files changed

+60
-34
lines changed

6 files changed

+60
-34
lines changed

main.tf

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ terraform {
1212
}
1313
}
1414

15-
data "azurerm_management_group" "root" {
16-
name = var.mgmt_group_name
15+
data "azurerm_management_group" "replicator_custom_role_scope" {
16+
name = var.replicator_custom_role_scope
1717
}
1818

19+
data "azurerm_management_group" "replicator_assignment_scopes" {
20+
for_each = var.replicator_assignment_scopes
21+
name = each.key
22+
}
23+
24+
locals {
25+
replicator_assignment_scopes = [
26+
for management_group in data.azurerm_management_group.replicator_assignment_scopes : management_group.id
27+
]
28+
}
29+
30+
data "azuread_client_config" "current" {}
31+
1932
module "replicator_service_principal" {
2033
count = var.replicator_enabled || var.replicator_rg_enabled ? 1 : 0
2134
source = "./modules/meshcloud-replicator-service-principal/"
2235

23-
service_principal_name_suffix = var.service_principal_name_suffix
24-
scope = data.azurerm_management_group.root.id
36+
service_principal_name = var.replicator_service_principal_name
37+
custom_role_scope = data.azurerm_management_group.replicator_custom_role_scope.id
38+
assignment_scopes = local.replicator_assignment_scopes
2539

2640
additional_required_resource_accesses = var.additional_required_resource_accesses
2741
additional_permissions = var.additional_permissions
@@ -31,15 +45,8 @@ module "metering_service_principal" {
3145
count = var.metering_enabled ? 1 : 0
3246
source = "./modules/meshcloud-metering-service-principal/"
3347

34-
service_principal_name_suffix = var.service_principal_name_suffix
35-
scope = data.azurerm_management_group.root.id
36-
}
37-
38-
module "idp_lookup_service_principal" {
39-
count = var.idplookup_enabled ? 1 : 0
40-
source = "./modules/meshcloud-idp-lookup-service-principal/"
41-
42-
service_principal_name_suffix = var.service_principal_name_suffix
48+
service_principal_name = var.metering_service_principal_name
49+
assignment_scope = data.azuread_client_config.current.tenant_id
4350
}
4451

4552
# facilitate migration from v0.1.0 of the module

modules/meshcloud-metering-service-principal/module.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ terraform {
2020
//---------------------------------------------------------------------------
2121
# For now we are using the following built-in role
2222
resource "azurerm_role_assignment" "meshcloud_metering" {
23-
scope = var.scope
23+
scope = var.assignment_scope
2424
role_definition_name = "Cost Management Reader"
2525
principal_id = azuread_service_principal.meshcloud_metering.id
2626
depends_on = [azuread_service_principal.meshcloud_metering]
@@ -31,7 +31,7 @@ resource "azurerm_role_assignment" "meshcloud_metering" {
3131
// Create New application in Microsoft Entra ID
3232
//---------------------------------------------------------------------------
3333
resource "azuread_application" "meshcloud_metering" {
34-
display_name = "metering.${var.service_principal_name_suffix}"
34+
display_name = var.service_principal_name
3535

3636
feature_tags {
3737
enterprise = true
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
variable "service_principal_name_suffix" {
1+
variable "service_principal_name" {
22
type = string
3-
description = "Service principal name suffix."
3+
description = "Service principal name. Must be unique per Entra ID."
44
}
55

6-
variable "scope" {
6+
variable "assignment_scope" {
77
type = string
88
description = "The scope to which Service Principal permissions should be assigned to. Usually this is the management group id of form `/providers/Microsoft.Management/managementGroups/<tenantId>` that sits atop the subscriptions."
99
}

modules/meshcloud-replicator-service-principal/module.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ terraform {
1919
// Role Definition for the Replicator on the specified Scope
2020
//---------------------------------------------------------------------------
2121
resource "azurerm_role_definition" "meshcloud_replicator" {
22-
name = "replicator.${var.service_principal_name_suffix}"
23-
scope = var.scope
22+
name = var.service_principal_name
23+
scope = var.custom_role_scope
2424
description = "Permissions required by meshcloud in order to configure subscriptions and manage users"
2525

2626
permissions {
@@ -59,7 +59,7 @@ resource "azurerm_role_definition" "meshcloud_replicator" {
5959
}
6060

6161
assignable_scopes = [
62-
var.scope
62+
var.custom_role_scope
6363
]
6464
}
6565

@@ -83,7 +83,7 @@ data "azuread_application_template" "enterprise_app" {
8383
template_id = "8adf8e6e-67b2-4cf2-a259-e3dc5476c621"
8484
}
8585
resource "azuread_application" "meshcloud_replicator" {
86-
display_name = "replicator.${var.service_principal_name_suffix}"
86+
display_name = var.service_principal_name
8787
template_id = data.azuread_application_template.enterprise_app.template_id
8888
feature_tags {
8989
enterprise = true
@@ -173,7 +173,8 @@ resource "azuread_service_principal" "meshcloud_replicator" {
173173
// Assign the created ARM role to the Enterprise application
174174
//---------------------------------------------------------------------------
175175
resource "azurerm_role_assignment" "meshcloud_replicator" {
176-
scope = var.scope
176+
for_each = var.assignment_scopes
177+
scope = each.key
177178
role_definition_id = azurerm_role_definition.meshcloud_replicator.role_definition_resource_id
178179
principal_id = azuread_service_principal.meshcloud_replicator.id
179180
depends_on = [azuread_service_principal.meshcloud_replicator]
@@ -213,7 +214,7 @@ resource "azurerm_policy_definition" "privilege_escalation_prevention" {
213214
policy_type = "Custom"
214215
mode = "All"
215216
display_name = "meshcloud Privilege Escalation Prevention"
216-
management_group_id = var.scope
217+
management_group_id = var.custom_role_scope
217218

218219
policy_rule = <<RULE
219220
{
@@ -240,5 +241,5 @@ RULE
240241
resource "azurerm_management_group_policy_assignment" "privilege-escalation-prevention" {
241242
name = "mesh-priv-escal-prev"
242243
policy_definition_id = azurerm_policy_definition.privilege_escalation_prevention.id
243-
management_group_id = var.scope
244+
management_group_id = var.custom_role_scope
244245
}

modules/meshcloud-replicator-service-principal/variables.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
variable "service_principal_name_suffix" {
1+
variable "service_principal_name" {
22
type = string
3-
description = "Service principal name suffix."
3+
description = "Display name of the replicator service principal."
44
}
55

6-
variable "scope" {
6+
variable "custom_role_scope" {
77
type = string
8-
description = "The scope to which Service Principal permissions should be assigned to. Usually this is the management group id of form `/providers/Microsoft.Management/managementGroups/<tenantId>` that sits atop the subscriptions."
8+
description = "The scope to which Service Principal permissions can be assigned to. Usually this is the management group id of form `/providers/Microsoft.Management/managementGroups/<tenantId>` that sits atop the subscriptions."
9+
}
10+
11+
variable "assignment_scopes" {
12+
type = list(string)
13+
description = "The scopes to which Service Principal permissions is assigned to. List of management group id of form `/providers/Microsoft.Management/managementGroups/<mgmtGroupId>/`."
914
}
1015

1116
variable "additional_required_resource_accesses" {
@@ -24,4 +29,4 @@ variable "replicator_rg_enabled" {
2429
type = bool
2530
default = false
2631
description = "Whether the created replicator Service Principal should be usable for Azure Resource Group based replication."
27-
}
32+
}

variables.tf

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
variable "service_principal_name_suffix" {
1+
variable "replicator_service_principal_name" {
22
type = string
3-
description = "Service principal name suffix. Make sure this is unique."
3+
default = "replicator"
4+
description = "Service principal for managing subscriptions. Replicator is the name of the meshStack component. Name must be unique per Entra ID."
45
}
56

6-
variable "mgmt_group_name" {
7+
variable "metering_service_principal_name" {
78
type = string
8-
description = "The name or UUID of the Management Group."
9+
default = "kraken"
10+
description = "Service principal for collecting cost data. Kraken ist the name of the meshStack component. Name must be unique per Entra ID."
11+
}
12+
13+
variable "replicator_assignment_scopes" {
14+
type = list(string)
15+
description = "Names or UUIDs of the Management Groups which replicator should manage."
916
}
1017

1118
# ---------------------------------------------------------------------------------------------------------------------
@@ -19,6 +26,12 @@ variable "replicator_enabled" {
1926
description = "Whether to create replicator Service Principal or not."
2027
}
2128

29+
variable "replicator_custom_role_scope" {
30+
type = string
31+
default = "Tenant Root Group"
32+
description = "Name or UUID of the Management Group of the replicator custom role definition. The custom role definition must be available for all assignment scopes."
33+
}
34+
2235
variable "replicator_rg_enabled" {
2336
type = bool
2437
default = false

0 commit comments

Comments
 (0)