Skip to content

Commit 2276bce

Browse files
authored
fix(iac/azure): move reservation role definition to bootstrap; runtime keeps only the assignment (closes #1092) (#1095)
The runtime container-apps module instantiated the cudly-reservation-role module, which creates an azurerm_role_definition. Creating a role definition needs Microsoft.Authorization/roleDefinitions/write, a bootstrap-class permission the CI deploy service principal intentionally lacks (it has only roleDefinitions/read plus roleAssignments/{read,write,delete}). Every Azure deploy therefore 403'd at the role-definition write, exactly as the bootstrap-vs-runtime IAM split intends. Fix, honoring the split (no roleDefinitions/write granted to the deploy SP): - Bootstrap: create the host-side reservation role definition in the human-applied ci-cd-permissions module (new reservation_role.tf) and output its resource ID and display name. - Runtime: stop creating the definition in container-apps; resolve it via data.azurerm_role_definition by name + subscription scope and keep only the azurerm_role_assignment. A missing definition fails loud ("not found"), signalling the operator to re-run the bootstrap rather than widening the SP. - Shared module: factor the role display name into a local (single source of truth) and expose it via a role_definition_name output so the runtime lookup stays in lockstep. - Drop the tenant-root "/providers/Microsoft.Capacity" assignable scope (a subscription-scoped principal cannot register a scope above its subscription, a latent 403). It is now gated behind include_capacity_provider_scope (default false); the role is only ever assigned at subscription scope. REQUIRED MANUAL STEP: a privileged human must re-apply the ci-cd-permissions bootstrap once to create the role definition before the next runtime deploy. Verified offline: terraform fmt -check, terraform validate, and tflint are clean on the shared module, the bootstrap env, and the container-apps module (apply not run; staging-first).
1 parent ee34869 commit 2276bce

7 files changed

Lines changed: 109 additions & 13 deletions

File tree

‎terraform/environments/azure/ci-cd-permissions/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ secrets ever need to be stored.
1515
| `azuread_application_federated_identity_credential.github_pr` | Federated credential for pull-request plan checks |
1616
| `azurerm_role_definition.cudly_deploy` | Custom role with minimum required permissions |
1717
| `azurerm_role_assignment.cudly_deploy` | Assigns the custom role to the SP at subscription scope |
18+
| `module.cudly_reservation_role` (`azurerm_role_definition`) | Host-side custom "CUDly Reservation Purchaser" role definition consumed by the runtime container-apps deploy |
19+
20+
> **Bootstrap-only role definition.** The reservation-purchaser role *definition*
21+
> lives here, not in the runtime container-apps module, because creating an
22+
> `azurerm_role_definition` requires
23+
> `Microsoft.Authorization/roleDefinitions/write`, a bootstrap-class permission
24+
> the runtime CI deploy SP intentionally does not have (it only holds
25+
> `roleDefinitions/read` + `roleAssignments/{read,write,delete}`). The runtime
26+
> deploy looks the role up by name via `data.azurerm_role_definition` and creates
27+
> only the assignment.
28+
>
29+
> **Re-apply this module once after pulling this change** (and any time the
30+
> reservation role's name or actions change) so the role definition exists before
31+
> the next runtime Azure deploy. If it is missing, the runtime
32+
> `data.azurerm_role_definition` lookup fails loudly with a "not found" error:
33+
> the signal to re-run this bootstrap, not to widen the deploy SP's permissions.
1834
1935
### Permissions granted
2036

‎terraform/environments/azure/ci-cd-permissions/outputs.tf‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ output "tenant_id" {
2222
description = "Azure AD tenant ID — use as AZURE_TENANT_ID in GitHub Actions"
2323
value = azuread_service_principal.cudly_deploy.application_tenant_id
2424
}
25+
26+
output "reservation_role_definition_id" {
27+
description = "Full ARM resource ID of the host-side custom reservation-purchaser role definition created by this bootstrap. The runtime container-apps deploy looks the role up by name and assigns it."
28+
value = module.cudly_reservation_role.role_definition_resource_id
29+
}
30+
31+
output "reservation_role_definition_name" {
32+
description = "Display name of the host-side custom reservation-purchaser role definition. The runtime container-apps module resolves the role by this exact name via data.azurerm_role_definition."
33+
value = module.cudly_reservation_role.role_definition_name
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Host-side custom reservation-purchaser role definition (BOOTSTRAP).
2+
#
3+
# Creating an azurerm_role_definition requires
4+
# Microsoft.Authorization/roleDefinitions/write, a bootstrap-class permission
5+
# the runtime CI deploy service principal intentionally does NOT have (it only
6+
# has roleDefinitions/read; see locals_data.tf). This module is applied once by
7+
# a privileged human with role-admin authority, so it is the correct home for
8+
# the role *definition*.
9+
#
10+
# The runtime container-apps module
11+
# (terraform/modules/compute/azure/container-apps) no longer creates this role.
12+
# It looks it up via data.azurerm_role_definition and creates only the
13+
# *assignment* (which the deploy SP can do via roleAssignments/write). If this
14+
# bootstrap has not been (re-)applied, that runtime data lookup fails loudly,
15+
# signalling the operator to re-run this module.
16+
#
17+
# name_suffix matches the runtime side (the subscription ID) so both reference
18+
# the identical role display name.
19+
module "cudly_reservation_role" {
20+
source = "../../../modules/iam/azure/cudly-reservation-role"
21+
scope = "/subscriptions/${var.subscription_id}"
22+
name_suffix = var.subscription_id
23+
}

‎terraform/modules/compute/azure/container-apps/main.tf‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,39 @@ resource "azurerm_role_assignment" "subscription_reader" {
262262
# Custom reservation-purchaser role: same role as customer-side but scoped to
263263
# the host subscription. The built-in Reservation Purchaser lacks
264264
# reservationOrders/purchase/action (same gap fixed customer-side by PR #744).
265-
# The role definition is factored into a shared module so both sides stay in
266-
# lockstep with the ARM template.
267-
module "cudly_reservation_role" {
268-
source = "../../../iam/azure/cudly-reservation-role"
269-
scope = data.azurerm_subscription.current.id
270-
name_suffix = data.azurerm_subscription.current.subscription_id
265+
#
266+
# BOOTSTRAP-vs-RUNTIME SPLIT: the role *definition* is created by the
267+
# human-applied bootstrap module
268+
# (terraform/environments/azure/ci-cd-permissions/reservation_role.tf), NOT
269+
# here. Creating an azurerm_role_definition needs
270+
# Microsoft.Authorization/roleDefinitions/write, which the runtime CI deploy
271+
# service principal intentionally lacks. This runtime module only looks the
272+
# definition up and creates the *assignment* (roleAssignments/write, which the
273+
# deploy SP does have).
274+
#
275+
# The lookup name MUST stay in lockstep with
276+
# terraform/modules/iam/azure/cudly-reservation-role (its role_definition_name
277+
# output / local.role_definition_name). If the bootstrap has not been
278+
# (re-)applied, this data source fails loudly with "Role Definition ... was not
279+
# found" -- the signal to re-run the ci-cd-permissions bootstrap, NOT to grant
280+
# roleDefinitions/write to the deploy SP.
281+
data "azurerm_role_definition" "cudly_reservation_purchaser" {
282+
name = "CUDly Reservation Purchaser (custom) - ${data.azurerm_subscription.current.subscription_id}"
283+
scope = data.azurerm_subscription.current.id
271284
}
272285

273286
resource "azurerm_role_assignment" "reservations_purchaser" {
274-
scope = data.azurerm_subscription.current.id
275-
role_definition_id = module.cudly_reservation_role.role_definition_resource_id
287+
scope = data.azurerm_subscription.current.id
288+
# .id is the full scoped ARM resource ID of the role definition
289+
# (/subscriptions/<sub>/providers/Microsoft.Authorization/roleDefinitions/<guid>),
290+
# the same form the bootstrap exposes via role_definition_resource_id and what
291+
# azurerm_role_assignment.role_definition_id expects.
292+
role_definition_id = data.azurerm_role_definition.cudly_reservation_purchaser.id
276293
principal_id = azurerm_user_assigned_identity.container_app.principal_id
277294

278-
depends_on = [module.cudly_reservation_role]
295+
# RBAC propagation: the bootstrap-created role definition can take time to be
296+
# readable at assignment time; the data dependency below also gates this.
297+
depends_on = [data.azurerm_role_definition.cudly_reservation_purchaser]
279298
}
280299

281300
# Key Vault Crypto User: allows the container app's managed identity

‎terraform/modules/iam/azure/cudly-reservation-role/main.tf‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ terraform {
2121
# container-app user-assigned identity running the CUDly process itself
2222
#
2323
# Keep the actions list here in sync with arm/CUDly-CrossSubscription/template.json.
24+
locals {
25+
# Single source of truth for the role display name. Consumers that look the
26+
# role up via data.azurerm_role_definition (rather than creating it) must
27+
# reconstruct this exact string; see role_definition_name output below.
28+
role_definition_name = "CUDly Reservation Purchaser (custom) - ${var.name_suffix}"
29+
}
30+
2431
resource "azurerm_role_definition" "cudly_reservation_purchaser" {
25-
name = "CUDly Reservation Purchaser (custom) - ${var.name_suffix}"
32+
name = local.role_definition_name
2633
scope = var.scope
2734
description = "Custom role granting CUDly exactly the Microsoft.Capacity and Microsoft.BillingBenefits actions required by the calculatePrice -> purchase flow. Replaces the built-in Reservation Purchaser, which lacks reservationOrders/purchase/action."
2835

@@ -44,8 +51,18 @@ resource "azurerm_role_definition" "cudly_reservation_purchaser" {
4451
not_actions = []
4552
}
4653

47-
assignable_scopes = [
54+
# assignable_scopes is intentionally limited to the subscription scope.
55+
#
56+
# The tenant-root provider scope "/providers/Microsoft.Capacity" was removed:
57+
# a subscription-scoped principal cannot register an assignable scope above its
58+
# own subscription, so including it makes the role-definition write 403 at the
59+
# higher scope. The role is only ever assigned at subscription scope (see the
60+
# azurerm_role_assignment blocks in the consuming modules), so the subscription
61+
# scope is sufficient. Set include_capacity_provider_scope = true only when the
62+
# applying principal has tenant-root authority and a tenant-level assignment is
63+
# actually required.
64+
assignable_scopes = compact([
4865
var.scope,
49-
"/providers/Microsoft.Capacity",
50-
]
66+
var.include_capacity_provider_scope ? "/providers/Microsoft.Capacity" : "",
67+
])
5168
}

‎terraform/modules/iam/azure/cudly-reservation-role/outputs.tf‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ output "role_definition_resource_id" {
22
description = "Full ARM resource ID of the custom role definition. Use as role_definition_id in azurerm_role_assignment blocks."
33
value = azurerm_role_definition.cudly_reservation_purchaser.role_definition_resource_id
44
}
5+
6+
output "role_definition_name" {
7+
description = "Display name of the custom role definition. Runtime modules that look the role up via data.azurerm_role_definition must match this exact name."
8+
value = local.role_definition_name
9+
}

‎terraform/modules/iam/azure/cudly-reservation-role/variables.tf‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ variable "name_suffix" {
77
description = "Suffix appended to the role display name to keep customer and host role definitions distinct within the same Azure AD tenant (e.g. the subscription ID)."
88
type = string
99
}
10+
11+
variable "include_capacity_provider_scope" {
12+
description = "When true, adds the tenant-root \"/providers/Microsoft.Capacity\" scope to assignable_scopes. Leave false (the default) for subscription-scoped principals: registering an assignable scope above the subscription requires tenant-root authority and otherwise 403s."
13+
type = bool
14+
default = false
15+
}

0 commit comments

Comments
 (0)