Skip to content

Commit 9b5e073

Browse files
committed
Simplify role assignments
1 parent 9d595fd commit 9b5e073

File tree

4 files changed

+23
-47
lines changed

4 files changed

+23
-47
lines changed

terraform/azure-devops/create-service-connection/main.tf

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
data azurerm_client_config current {}
2+
data azurerm_subscription current {}
3+
data azurerm_subscription target {
4+
subscription_id = split("/",tolist(local.azure_role_assignments)[0].scope)[2]
5+
}
26

37
# Random resource suffix, this will prevent name collisions when creating resources in parallel
48
resource random_string suffix {
@@ -15,8 +19,15 @@ locals {
1519
azdo_organization_name = split("/",var.azdo_organization_url)[3]
1620
azdo_organization_url = replace(var.azdo_organization_url,"/\\/$/","")
1721
azdo_project_url = "${local.azdo_organization_url}/${urlencode(var.azdo_project_name)}"
18-
azdo_service_connection_name = "${replace(module.azure_access.subscription_name,"/ +/","-")}-${var.azdo_creates_identity ? "aut" : "man"}-${var.create_managed_identity ? "msi" : "sp"}-${var.create_federation ? "oidc" : "secret"}${terraform.workspace == "default" ? "" : format("-%s",terraform.workspace)}-${local.resource_suffix}"
19-
azure_scope = var.azure_scope != null && var.azure_scope != "" ? var.azure_scope : "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
22+
azdo_service_connection_name = "${replace(data.azurerm_subscription.target.display_name,"/ +/","-")}-${var.azdo_creates_identity ? "aut" : "man"}-${var.create_managed_identity ? "msi" : "sp"}-${var.create_federation ? "oidc" : "secret"}${terraform.workspace == "default" ? "" : format("-%s",terraform.workspace)}-${local.resource_suffix}"
23+
azure_role_assignments = length(var.azure_role_assignments) > 0 ? var.azure_role_assignments : [
24+
{
25+
# Default role assignment
26+
role = "Contributor"
27+
scope = data.azurerm_subscription.current.id
28+
}
29+
]
30+
managed_identity_subscription_id = var.create_managed_identity ? split("/", var.managed_identity_resource_group_id)[2] : null
2031
principal_id = var.azdo_creates_identity ? null : (var.create_managed_identity ? module.managed_identity.0.principal_id : module.entra_app.0.principal_id)
2132
principal_name = var.azdo_creates_identity ? null : (var.create_managed_identity ? module.managed_identity.0.principal_name : module.entra_app.0.principal_name)
2233
resource_suffix = var.resource_suffix != null && var.resource_suffix != "" ? lower(var.resource_suffix) : random_string.suffix.result
@@ -30,8 +41,6 @@ locals {
3041
runId = var.run_id
3142
workspace = terraform.workspace
3243
}
33-
managed_identity_subscription_id = var.create_managed_identity ? split("/", var.managed_identity_resource_group_id)[2] : null
34-
target_subscription_id = split("/", local.azure_scope)[2]
3544
}
3645

3746
resource terraform_data managed_identity_validator {
@@ -80,18 +89,6 @@ module entra_app {
8089
count = var.create_managed_identity || var.azdo_creates_identity ? 0 : 1
8190
}
8291

83-
module azure_access {
84-
providers = {
85-
azurerm = azurerm.target
86-
}
87-
source = "./modules/azure-access"
88-
# create_role_assignment = !var.azdo_creates_identity
89-
create_role_assignment = true
90-
identity_object_id = local.principal_id
91-
resource_id = local.azure_scope
92-
role = var.azure_role
93-
}
94-
9592
module azure_role_assignments {
9693
providers = {
9794
azurerm = azurerm.target
@@ -114,6 +111,6 @@ module service_connection {
114111
project_name = var.azdo_project_name
115112
tenant_id = data.azurerm_client_config.current.tenant_id
116113
service_connection_name = local.azdo_service_connection_name
117-
subscription_id = local.target_subscription_id
118-
subscription_name = module.azure_access.subscription_name
114+
subscription_id = data.azurerm_subscription.target.subscription_id
115+
subscription_name = data.azurerm_subscription.target.display_name
119116
}

terraform/azure-devops/create-service-connection/outputs.tf

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,17 @@ output azdo_service_connection_url {
1414
description = "The Azure DevOps service connection portal URL"
1515
value = module.service_connection.service_connection_url
1616
}
17-
output azure_resource_group_name {
18-
description = "The name of the resource group the service connection was granted access to"
19-
value = try(split("/", local.azure_scope)[4],null)
20-
}
21-
output azure_scope {
22-
description = "The Azure scope the service connection was granted access to"
23-
value = local.azure_scope
24-
}
25-
output azure_scope_url {
26-
description = "The Azure scope portal URL the service connection was granted access to"
27-
value = module.azure_access.resource_url
17+
output azure_role_assignments {
18+
description = "Role assignments created for the service connection's identity"
19+
value = local.azure_role_assignments
2820
}
2921
output azure_subscription_id {
3022
description = "The Azure subscription id the service connection was granted access to"
31-
value = local.target_subscription_id
23+
value = data.azurerm_subscription.target.subscription_id
3224
}
3325
output azure_subscription_name {
3426
description = "The Azure subscription name the service connection was granted access to"
35-
value = module.azure_access.subscription_name
27+
value = data.azurerm_subscription.target.display_name
3628
}
3729

3830
output identity_application_id {

terraform/azure-devops/create-service-connection/terraform.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ provider azurerm {
5353
prevent_deletion_if_contains_resources = false
5454
}
5555
}
56-
subscription_id = local.target_subscription_id
56+
subscription_id = data.azurerm_subscription.target.subscription_id
5757
}

terraform/azure-devops/create-service-connection/variables.tf

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@ variable azdo_project_name {
1515
type = string
1616
}
1717

18-
variable azure_scope {
19-
default = null
20-
description = "The Azure scope to assign access to"
21-
type = string
22-
}
23-
24-
variable azure_role {
25-
default = "Contributor"
26-
description = "The Azure RBAC role to assign to the service connection's identity"
27-
nullable = false
28-
type = string
29-
}
30-
3118
variable azure_role_assignments {
3219
default = []
33-
description = "Additional role assignments to create for the service connection's identity"
34-
nullable = true
20+
description = "Role assignments to create for the service connection's identity. If this is empty, the Contributor role will be assigned on the azurerm provider subscription."
21+
nullable = false
3522
type = set(object({scope=string, role=string}))
3623
}
3724

0 commit comments

Comments
 (0)