Skip to content

Commit eb5a325

Browse files
feat(DTOSS-13037): move app role assignment out of Terraform into user script
The pipeline MI lacks AppRoleAssignment.ReadWrite.All and ownership is insufficient in application permission context. Removes azuread_app_role_assignment from Terraform and adds assign_arc_app_roles.sh, which runs under user credentials (where ownership of the SP is sufficient) to assign the Gateway.Access role to all Arc machine managed identities in an environment. Usage: ./scripts/bash/assign_arc_app_roles.sh <env>
1 parent a768d43 commit eb5a325

5 files changed

Lines changed: 52 additions & 27 deletions

File tree

infrastructure/modules/arc-infra/app_roles.tf

Lines changed: 0 additions & 15 deletions
This file was deleted.

infrastructure/modules/arc-infra/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,3 @@ variable "static_arc_machine_names" {
2828
type = list(string)
2929
default = []
3030
}
31-
32-
variable "enterprise_app_role_value" {
33-
description = "App role name to assign to each Arc machine's managed identity on the web API enterprise app (spn-manbrs-web-api-<env>). Leave empty to skip assignment."
34-
type = string
35-
default = ""
36-
}

infrastructure/terraform/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module "arc_infra" {
1111
env_config = var.env_config
1212
resource_group_name = local.resource_group_name
1313
enable_arc_servers = var.enable_arc_servers
14-
enterprise_app_role_value = var.enterprise_app_role_value
1514

1615
# Create the HC for the test VM in the same run as VM creation.
1716
# The Arc data source won't see a machine registered in the same apply.

infrastructure/terraform/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ variable "gateway_test_vm_size" {
5050
default = "Standard_B2s"
5151
}
5252

53-
variable "enterprise_app_role_value" {
54-
description = "App role name to assign to each Arc machine's managed identity on the web API enterprise app (spn-manbrs-web-api-<env>). Leave empty to skip assignment."
55-
type = string
56-
default = "Gateway.Access"
57-
}
5853

5954
locals {
6055
region = "uksouth"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
ENV_CONFIG="$1"
5+
6+
enterpriseAppName="spn-manbrs-web-api-${ENV_CONFIG}"
7+
rgName="rg-mbsgw-${ENV_CONFIG}-uks-arc-enabled-servers"
8+
appRoleValue="Gateway.Access"
9+
10+
echo "Fetching enterprise app details for: $enterpriseAppName"
11+
spObjectId=$(az ad sp list --filter "displayName eq '${enterpriseAppName}'" --query "[0].id" -o tsv)
12+
appRoleId=$(az ad sp list --filter "displayName eq '${enterpriseAppName}'" --query "[0].appRoles[?value=='${appRoleValue}'].id | [0]" -o tsv)
13+
14+
if [ -z "$spObjectId" ]; then
15+
echo "Error: Enterprise app '$enterpriseAppName' not found"
16+
exit 1
17+
fi
18+
19+
echo "SP object ID: $spObjectId"
20+
echo "App role ($appRoleValue): $appRoleId"
21+
22+
echo "Listing Arc machines in: $rgName"
23+
arcMachines=$(az connectedmachine list --resource-group "$rgName" --query "[].name" -o tsv)
24+
25+
if [ -z "$arcMachines" ]; then
26+
echo "No Arc machines found in $rgName"
27+
exit 0
28+
fi
29+
30+
while IFS= read -r machine; do
31+
[ -z "$machine" ] && continue
32+
33+
miPrincipalId=$(az connectedmachine show \
34+
--resource-group "$rgName" \
35+
--name "$machine" \
36+
--query "identity.principalId" -o tsv)
37+
38+
echo "Assigning $appRoleValue to $machine (MI: $miPrincipalId)..."
39+
if ! output=$(az rest --method POST \
40+
--uri "https://graph.microsoft.com/v1.0/servicePrincipals/${spObjectId}/appRoleAssignedTo" \
41+
--headers "Content-Type=application/json" \
42+
--body "{\"principalId\": \"${miPrincipalId}\", \"resourceId\": \"${spObjectId}\", \"appRoleId\": \"${appRoleId}\"}" 2>&1); then
43+
if echo "$output" | grep -q "Permission being assigned already exists"; then
44+
echo " Already assigned, skipping."
45+
else
46+
echo "Error: $output"
47+
exit 1
48+
fi
49+
fi
50+
done <<< "$arcMachines"
51+
52+
echo "Done."

0 commit comments

Comments
 (0)