Skip to content

Commit 2b26d6f

Browse files
committed
Add identity to security group
1 parent 877ef7d commit 2b26d6f

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module azure_role_assignments {
2+
providers = {
3+
azurerm = azurerm.target
4+
}
5+
source = "./modules/azure-access"
6+
create_role_assignment = !var.azdo_creates_identity
7+
identity_object_id = local.principal_id
8+
resource_id = each.value.scope
9+
role = each.value.role
10+
11+
for_each = { for ra in local.azure_role_assignments : format("%s-%s", ra.scope, ra.role) => ra }
12+
}
13+
14+
data azuread_group entra_security_group {
15+
display_name = each.value
16+
for_each = toset(var.entra_security_group_names)
17+
18+
lifecycle {
19+
postcondition {
20+
condition = self.security_enabled
21+
error_message = "${self.display_name} is not a security enabled"
22+
}
23+
postcondition {
24+
condition = !self.onpremises_sync_enabled || self.writeback_enabled
25+
error_message = "${self.display_name} is a synced group that is not writeback enabled"
26+
}
27+
}
28+
}
29+
30+
resource azuread_group_member entra_security_group {
31+
group_object_id = each.value.object_id
32+
member_object_id = local.principal_id
33+
34+
for_each = data.azuread_group.entra_security_group
35+
}

terraform/azure-devops/create-service-connection/doc-gen/header.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ Pre-requisites:
9494
- A resource group to hold the Managed Identity has been pre-created
9595
- The user is an owner of the Azure scopes to create role assignments on
9696

97+
#### Managed Identity with FIC assigned to Entra ID security group
98+
99+
This creates a Managed Identity with Federated Identity Credential and custom Azure RBAC (role-based access control) role assignments:
100+
101+
```hcl
102+
azdo_creates_identity = false
103+
azdo_organization_url = "https://dev.azure.com/my-organization"
104+
azdo_project_name = "my-project"
105+
azure_role_assignments = []
106+
create_federation = true
107+
create_managed_identity = true
108+
entra_security_group_names = ["my-security-group"]
109+
managed_identity_resource_group_id = "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/msi-rg"
110+
```
111+
112+
Pre-requisites:
113+
114+
- A resource group to hold the Managed Identity has been pre-created
115+
- The user is an owner of the security enabled Entra ID group to add the Managed Identity to
116+
97117
#### App registration with FIC and ITSM metadata
98118

99119
This creates an Entra ID app registration with IT service reference and notes fields populated as well as specifying co-owners:

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
data azurerm_client_config current {}
22
data azurerm_subscription current {}
33
data azurerm_subscription target {
4-
subscription_id = split("/",tolist(local.azure_role_assignments)[0].scope)[2]
4+
subscription_id = length(local.azure_role_assignments) > 0 ? split("/",tolist(local.azure_role_assignments)[0].scope)[2] : data.azurerm_subscription.current.subscription_id
55
}
66

77
# Random resource suffix, this will prevent name collisions when creating resources in parallel
@@ -21,7 +21,7 @@ locals {
2121
azdo_project_url = "${local.azdo_organization_url}/${urlencode(var.azdo_project_name)}"
2222
# 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}"
2323
azdo_service_connection_name = "${replace(data.azurerm_subscription.target.display_name,"/ +/","-")}${terraform.workspace == "default" ? "" : format("-%s",terraform.workspace)}-${local.resource_suffix}"
24-
azure_role_assignments = length(var.azure_role_assignments) > 0 ? var.azure_role_assignments : [
24+
azure_role_assignments = var.azure_role_assignments != null ? var.azure_role_assignments : [
2525
{
2626
# Default role assignment
2727
role = "Contributor"
@@ -91,19 +91,6 @@ module entra_app {
9191
count = var.create_managed_identity || var.azdo_creates_identity ? 0 : 1
9292
}
9393

94-
module azure_role_assignments {
95-
providers = {
96-
azurerm = azurerm.target
97-
}
98-
source = "./modules/azure-access"
99-
create_role_assignment = !var.azdo_creates_identity
100-
identity_object_id = local.principal_id
101-
resource_id = each.value.scope
102-
role = each.value.role
103-
104-
for_each = { for ra in local.azure_role_assignments : format("%s-%s", ra.scope, ra.role) => ra }
105-
}
106-
10794
module service_connection {
10895
source = "./modules/service-connection"
10996
application_id = local.application_id
@@ -115,4 +102,4 @@ module service_connection {
115102
service_connection_name = local.azdo_service_connection_name
116103
subscription_id = data.azurerm_subscription.target.subscription_id
117104
subscription_name = data.azurerm_subscription.target.display_name
118-
}
105+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ variable azdo_project_name {
1616
}
1717

1818
variable azure_role_assignments {
19-
default = []
19+
default = null
2020
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
21+
nullable = true
2222
type = set(object({scope=string, role=string}))
2323
}
2424

@@ -46,6 +46,12 @@ variable entra_app_owner_object_ids {
4646
type = list(string)
4747
}
4848

49+
variable entra_security_group_names {
50+
default = null
51+
description = "Names of the security groups to add the service connection identity to"
52+
type = list(string)
53+
}
54+
4955
variable entra_secret_expiration_days {
5056
description = "Secret expiration in days"
5157
default = 90

0 commit comments

Comments
 (0)