Skip to content

Commit 77f6bbd

Browse files
fix: temporarily disable the privilege escalation policy for deployments
this is the "stricter" version of the change introduced in the previous commit. We still have a strict enforcement that no additional assignments can be done to the replicator, but temporarily disable that policy while deploying the meshPlatform terraform module
1 parent 35e81ca commit 77f6bbd

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

modules/meshcloud-replicator-service-principal/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
|------|---------|
1414
| <a name="provider_azuread"></a> [azuread](#provider\_azuread) | 2.46.0 |
1515
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.81.0 |
16+
| <a name="provider_terraform"></a> [terraform](#provider\_terraform) | n/a |
1617
| <a name="provider_time"></a> [time](#provider\_time) | 0.11.1 |
1718

1819
## Modules
@@ -38,6 +39,7 @@ No modules.
3839
| [azurerm_role_definition.meshcloud_replicator](https://registry.terraform.io/providers/hashicorp/azurerm/3.81.0/docs/resources/role_definition) | resource |
3940
| [azurerm_role_definition.meshcloud_replicator_rg_deleter](https://registry.terraform.io/providers/hashicorp/azurerm/3.81.0/docs/resources/role_definition) | resource |
4041
| [azurerm_role_definition.meshcloud_replicator_subscription_canceler](https://registry.terraform.io/providers/hashicorp/azurerm/3.81.0/docs/resources/role_definition) | resource |
42+
| [terraform_data.allowed_assignments](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
4143
| [time_rotating.replicator_secret_rotation](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/rotating) | resource |
4244
| [azuread_application_published_app_ids.well_known](https://registry.terraform.io/providers/hashicorp/azuread/2.46.0/docs/data-sources/application_published_app_ids) | data source |
4345
| [azuread_application_template.enterprise_app](https://registry.terraform.io/providers/hashicorp/azuread/2.46.0/docs/data-sources/application_template) | data source |

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,20 @@ resource "azurerm_role_assignment" "meshcloud_replicator" {
186186
scope = each.key
187187
role_definition_id = azurerm_role_definition.meshcloud_replicator.role_definition_resource_id
188188
principal_id = azuread_service_principal.meshcloud_replicator.id
189-
depends_on = [azuread_service_principal.meshcloud_replicator]
190189
}
191190

192191
resource "azurerm_role_assignment" "meshcloud_replicator_subscription_canceler" {
193192
for_each = toset(var.can_cancel_subscriptions_in_scopes)
194193
scope = each.key
195194
role_definition_id = azurerm_role_definition.meshcloud_replicator_subscription_canceler.role_definition_resource_id
196195
principal_id = azuread_service_principal.meshcloud_replicator.id
197-
depends_on = [azuread_service_principal.meshcloud_replicator]
198196
}
199197

200198
resource "azurerm_role_assignment" "meshcloud_replicator_rg_deleter" {
201199
for_each = toset(var.can_delete_rgs_in_scopes)
202200
scope = each.key
203201
role_definition_id = azurerm_role_definition.meshcloud_replicator_rg_deleter.role_definition_resource_id
204202
principal_id = azuread_service_principal.meshcloud_replicator.id
205-
depends_on = [azuread_service_principal.meshcloud_replicator]
206203
}
207204

208205
//---------------------------------------------------------------------------
@@ -229,14 +226,6 @@ resource "azuread_app_role_assignment" "meshcloud_replicator-user" {
229226
depends_on = [azuread_application.meshcloud_replicator]
230227
}
231228

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-
240229
//---------------------------------------------------------------------------
241230
// Policy Definition for preventing the Application from assigning other privileges to itself
242231
// Assign it to the specified scope
@@ -260,10 +249,6 @@ resource "azurerm_policy_definition" "privilege_escalation_prevention" {
260249
{
261250
"field": "Microsoft.Authorization/roleAssignments/principalId",
262251
"equals": "${azuread_service_principal.meshcloud_replicator.object_id}"
263-
},
264-
{
265-
"field": "Microsoft.Authorization/roleAssignments/roleDefinitionId",
266-
"notIn": ${jsonencode(local.assignable_role_definition_ids)}
267252
}
268253
]
269254
},
@@ -274,9 +259,38 @@ resource "azurerm_policy_definition" "privilege_escalation_prevention" {
274259
RULE
275260
}
276261

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+
}
277270

278271
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
280274
policy_definition_id = azurerm_policy_definition.privilege_escalation_prevention.id
281275
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+
]
282295
}
296+

0 commit comments

Comments
 (0)