@@ -186,23 +186,20 @@ resource "azurerm_role_assignment" "meshcloud_replicator" {
186
186
scope = each. key
187
187
role_definition_id = azurerm_role_definition. meshcloud_replicator . role_definition_resource_id
188
188
principal_id = azuread_service_principal. meshcloud_replicator . id
189
- depends_on = [azuread_service_principal . meshcloud_replicator ]
190
189
}
191
190
192
191
resource "azurerm_role_assignment" "meshcloud_replicator_subscription_canceler" {
193
192
for_each = toset (var. can_cancel_subscriptions_in_scopes )
194
193
scope = each. key
195
194
role_definition_id = azurerm_role_definition. meshcloud_replicator_subscription_canceler . role_definition_resource_id
196
195
principal_id = azuread_service_principal. meshcloud_replicator . id
197
- depends_on = [azuread_service_principal . meshcloud_replicator ]
198
196
}
199
197
200
198
resource "azurerm_role_assignment" "meshcloud_replicator_rg_deleter" {
201
199
for_each = toset (var. can_delete_rgs_in_scopes )
202
200
scope = each. key
203
201
role_definition_id = azurerm_role_definition. meshcloud_replicator_rg_deleter . role_definition_resource_id
204
202
principal_id = azuread_service_principal. meshcloud_replicator . id
205
- depends_on = [azuread_service_principal . meshcloud_replicator ]
206
203
}
207
204
208
205
// ---------------------------------------------------------------------------
@@ -229,14 +226,6 @@ resource "azuread_app_role_assignment" "meshcloud_replicator-user" {
229
226
depends_on = [azuread_application . meshcloud_replicator ]
230
227
}
231
228
232
- locals {
233
- assignable_role_definition_ids = compact ([
234
- azurerm_role_definition . meshcloud_replicator . role_definition_id ,
235
- azurerm_role_definition . meshcloud_replicator_subscription_canceler . role_definition_id ,
236
- azurerm_role_definition . meshcloud_replicator_rg_deleter . role_definition_id
237
- ])
238
- }
239
-
240
229
// ---------------------------------------------------------------------------
241
230
// Policy Definition for preventing the Application from assigning other privileges to itself
242
231
// Assign it to the specified scope
@@ -260,10 +249,6 @@ resource "azurerm_policy_definition" "privilege_escalation_prevention" {
260
249
{
261
250
"field": "Microsoft.Authorization/roleAssignments/principalId",
262
251
"equals": "${ azuread_service_principal . meshcloud_replicator . object_id } "
263
- },
264
- {
265
- "field": "Microsoft.Authorization/roleAssignments/roleDefinitionId",
266
- "notIn": ${ jsonencode (local. assignable_role_definition_ids )}
267
252
}
268
253
]
269
254
},
@@ -274,9 +259,38 @@ resource "azurerm_policy_definition" "privilege_escalation_prevention" {
274
259
RULE
275
260
}
276
261
262
+ resource "terraform_data" "allowed_assignments" {
263
+ input = compact (
264
+ concat (
265
+ var. assignment_scopes ,
266
+ var. can_cancel_subscriptions_in_scopes ,
267
+ var. can_delete_rgs_in_scopes
268
+ ))
269
+ }
277
270
278
271
resource "azurerm_management_group_policy_assignment" "privilege-escalation-prevention" {
279
- name = " msh-escal-prev-${ local . spp_hash } "
272
+ name = " meshStack-PEP-${ local . spp_hash } "
273
+ description = azurerm_policy_definition. privilege_escalation_prevention . description
280
274
policy_definition_id = azurerm_policy_definition. privilege_escalation_prevention . id
281
275
management_group_id = var. custom_role_scope
276
+
277
+ lifecycle {
278
+ # ensure we unassign the policy whenver we make intentional changes to the replicators role assignments and then reassign it after
279
+ # note that we can't directly depend on the azurerm_role_assignment resources here because terraform fails with
280
+ # > Error: no change found for azurerm_role_assignment.meshcloud_replicator_rg_deleter
281
+ # whenever no role_assignment exists because the for_each condition is empty (so no instances exist).
282
+ # We therefore trigger the replacement directly using the for_each keys
283
+ replace_triggered_by = [
284
+ terraform_data . allowed_assignments
285
+ ]
286
+ }
287
+
288
+ # only deploy this after the replicator roles have been assigned, here it's fine for terraform to directly reference
289
+ # resources that use for_each, even if there are no instances of that resources
290
+ depends_on = [
291
+ azurerm_role_assignment . meshcloud_replicator ,
292
+ azurerm_role_assignment . meshcloud_replicator_rg_deleter ,
293
+ azurerm_role_assignment . meshcloud_replicator_subscription_canceler
294
+ ]
282
295
}
296
+
0 commit comments