-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_azurerm_msi_oidc_service_connection.ps1
316 lines (284 loc) · 16 KB
/
create_azurerm_msi_oidc_service_connection.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Create a Service Connection in Azure DevOps that uses a Managed Identity and Workload Identity federation to authenticate to Azure.
.DESCRIPTION
Creates a Managed Identiy, sets up a federation subject on the Managed Identity for a Service Connection, creates the Service Connection, and grants the Managed Identity the Contributor role on the subscription.
.LINK
https://aka.ms/azdo-rm-workload-identity
.EXAMPLE
./create_azurerm_msi_oidc_service_connection.ps1 -Project MyProject -OrganizationUrl https://dev.azure.com/MyOrg -SubscriptionId 00000000-0000-0000-0000-000000000000
#>
#Requires -Version 7.2
param (
[parameter(Mandatory=$false,HelpMessage="Name of the Managed Identity")]
[string]
$IdentityName,
[parameter(Mandatory=$false,HelpMessage="Name of the Azure Resource Group where the Managed Identity will be created")]
[string]
$IdentityResourceGroupName,
[parameter(Mandatory=$false,HelpMessage="Id of the Azure Subscription where the Managed Identity will be created")]
[guid]
$IdentitySubscriptionId=($env:AZURE_SUBSCRIPTION_ID || $env:ARM_SUBSCRIPTION_ID),
[parameter(Mandatory=$false,HelpMessage="Azure region of the Managed Identity")]
[string]
$IdentityLocation,
[parameter(Mandatory=$false,HelpMessage="Name of the Service Connection")]
[string]
$ServiceConnectionName,
[parameter(Mandatory=$false,HelpMessage="Role to grant the Service Connection on the selected scope")]
[string]
[ValidateNotNullOrEmpty()]
$ServiceConnectionRole="Contributor",
[parameter(Mandatory=$false,HelpMessage="Scope of the Service Connection (e.g. /subscriptions/00000000-0000-0000-0000-000000000000)")]
[string]
[ValidatePattern("^$|(?i)/subscriptions/[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}(/resourcegroups/(.+?))?")]
$ServiceConnectionScope,
[parameter(Mandatory=$false,HelpMessage="Name of the Azure DevOps Project")]
[string]
[ValidateNotNullOrEmpty()]
$Project=$env:SYSTEM_TEAMPROJECT,
[parameter(Mandatory=$false,HelpMessage="Url of the Azure DevOps Organization")]
[uri]
[ValidateNotNullOrEmpty()]
$OrganizationUrl=($env:AZDO_ORG_SERVICE_URL ?? $env:SYSTEM_COLLECTIONURI)
)
Write-Verbose $MyInvocation.line
. (Join-Path $PSScriptRoot .. functions.ps1)
$apiVersion = "7.1-preview.4"
#-----------------------------------------------------------
# Log in to Azure
if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
Write-Error "Azure CLI is not installed. You can get it here: https://docs.microsoft.com/cli/azure/install-azure-cli"
exit 1
}
az account show -o json 2>$null | ConvertFrom-Json | Set-Variable subscription
if (!$subscription) {
az login -o json | ConvertFrom-Json | Set-Variable subscription
}
$subscription | Format-List | Out-String | Write-Debug
if ($IdentitySubscriptionId) {
az account set --subscription $IdentitySubscriptionId -o none
az account show -o json 2>$null | ConvertFrom-Json | Set-Variable subscription
} else {
# Prompt for subscription
az account list --query "sort_by([].{id:id, name:name},&name)" `
-o json `
| ConvertFrom-Json `
| Set-Variable subscriptions
if ($subscriptions.Length -eq 1) {
$occurrence = 0
} else {
# Active subscription may not be the desired one, prompt the user to select one
$index = 0
$subscriptions | Format-Table -Property @{name="index";expression={$script:index;$script:index+=1}}, id, name
Write-Host "Set `$env:ARM_SUBSCRIPTION_ID to the id of the subscription you want to use to prevent this prompt" -NoNewline
do {
Write-Host "`nEnter the index # of the subscription you want to use: " -ForegroundColor Cyan -NoNewline
[int]$occurrence = Read-Host
Write-Debug "User entered index '$occurrence'"
} while (($occurrence -notmatch "^\d+$") -or ($occurrence -lt 1) -or ($occurrence -gt $subscriptions.Length))
}
$subscription = $subscriptions[$occurrence-1]
$IdentitySubscriptionId = $subscription.id
Write-Host "Using subscription '$($subscription.name)'" -ForegroundColor Yellow
Start-Sleep -Milliseconds 250
}
# Log in to Azure & Azure DevOps
$OrganizationUrl = $OrganizationUrl.ToString().Trim('/')
az account get-access-token --resource 499b84ac-1321-427f-aa17-267ca6975798 `
--query "accessToken" `
--output tsv `
| Set-Variable accessToken
if (!$accessToken) {
Write-Error "$(subscription.user.name) failed to get access token for Azure DevOps"
exit 1
}
if (!(az extension list --query "[?name=='azure-devops'].version" -o tsv)) {
Write-Host "Adding Azure CLI extension 'azure-devops'..."
az extension add -n azure-devops -y -o none
}
$accessToken | az devops login --organization $OrganizationUrl
if ($lastexitcode -ne 0) {
Write-Error "$($subscription.user.name) failed to log in to Azure DevOps organization '${OrganizationUrl}'"
exit $lastexitcode
}
#-----------------------------------------------------------
# Process parameters, making sure they're not empty
$organizationName = $OrganizationUrl.ToString().Split('/')[3]
if (!$IdentityResourceGroupName) {
$IdentityResourceGroupName = (az config get defaults.group --query value -o tsv)
}
if (!$IdentityResourceGroupName) {
$IdentityResourceGroupName = "VS-${organizationName}-Group"
}
az group show -g $IdentityResourceGroupName -o json 2>$null | ConvertFrom-Json | Set-Variable resourceGroup
if ($resourceGroup) {
if (!$IdentityLocation) {
$IdentityLocation = $resourceGroup.location
}
} else {
if (!$IdentityLocation) {
$IdentityLocation = (az config get defaults.location --query value -o tsv)
}
if (!$IdentityLocation) {
# Azure location doesn't really matter for MI; the object is in AAD which is a global service
$IdentityLocation = "southcentralus"
}
az group create -g $IdentityResourceGroupName -l $IdentityLocation -o json | ConvertFrom-Json | Set-Variable resourceGroup
}
if (!$ServiceConnectionScope) {
$ServiceConnectionScope = "/subscriptions/${IdentitySubscriptionId}"
Write-Verbose "Parameter ServiceConnectionScope not provided, using '${ServiceConnectionScope}'."
}
$serviceConnectionSubscriptionId = $ServiceConnectionScope.Split('/')[2]
# Check whether project exists
az devops project show --project $Project --organization $OrganizationUrl --query id -o tsv | Set-Variable projectId
if (!$projectId) {
Write-Error "Project '${Project}' not found in organization '${OrganizationUrl}"
exit 1
}
# Test whether Service Connection already exists
$serviceConnectionSubscriptionName = $(az account show --subscription $serviceConnectionSubscriptionId --query name -o tsv)
if (!$ServiceConnectionName) {
$ServiceConnectionName = $serviceConnectionSubscriptionName
$serviceConnectionResourceGroupName = $ServiceConnectionScope.Split('/')[4]
if ($serviceConnectionResourceGroupName) {
$ServiceConnectionName += "-${serviceConnectionResourceGroupName}"
}
$ServiceConnectionName += "-oidc-msi"
}
do {
az devops service-endpoint list -p $Project `
--organization $OrganizationUrl `
--query "[?name=='${ServiceConnectionName}'].id" `
-o tsv `
| Set-Variable serviceEndpointId
$ServiceConnectionNameBefore = $ServiceConnectionName
if ($serviceEndpointId) {
Write-Warning "Service connection '${ServiceConnectionName}' already exists. Provide a different name to create a new service connection or press enter to overwrite '${ServiceConnectionName}'."
$ServiceConnectionName = Read-Host -Prompt "Provide the name of the service connection ('${ServiceConnectionName}')"
if (!$ServiceConnectionName) {
$ServiceConnectionName = $ServiceConnectionNameBefore
}
if ($ServiceConnectionName -ieq $ServiceConnectionNameBefore) {
Write-Verbose "Service connection '${ServiceConnectionName}' (${serviceEndpointId}) wil be updated"
break
}
} else {
Write-Verbose "Service connection '${ServiceConnectionName}' (${serviceEndpointId}) wil be created"
}
} while ($serviceEndpointId)
#-----------------------------------------------------------
# Create Managed Identity
if (!$IdentityName) {
$IdentityName = "${organizationName}-${Project}-${ServiceConnectionName}"
}
Write-Verbose "Creating Managed Identity '${IdentityName}' in resource group '${IdentityResourceGroupName}'..."
Write-Debug "az identity create -n $IdentityName -g $IdentityResourceGroupName -l $IdentityLocation --subscription $IdentitySubscriptionId"
az identity create -n $IdentityName `
-g $IdentityResourceGroupName `
-l $IdentityLocation `
--subscription $IdentitySubscriptionId `
-o json `
| ConvertFrom-Json `
| Set-Variable identity
Write-Verbose "Created Managed Identity $($identity.id)"
$identity | Format-List | Out-String | Write-Debug
Write-Verbose "Creating role assignment for Managed Identity '${IdentityName}' on subscription '$($subscription.name)'..."
az role assignment create --assignee-object-id $identity.principalId `
--assignee-principal-type ServicePrincipal `
--role $ServiceConnectionRole `
--scope $ServiceConnectionScope `
--subscription $serviceConnectionSubscriptionId `
-o json `
| ConvertFrom-Json `
| Set-Variable roleAssignment
Write-Verbose "Created role assignment $($roleAssignment.id)"
Write-Host "`nManaged Identity '$($identity.name)':"
$identity | Format-List -Property id, clientId, federatedSubject, role, scope, subscriptionId, tenantId
#-----------------------------------------------------------
# TODO: Create the service connection (Azure CLI)
# az devops service-endpoint azurerm create --azure-rm-service-principal-id $identity.clientId `
# --azure-rm-subscription-id $serviceConnectionSubscriptionId `
# --azure-rm-subscription-name $ServiceConnectionName `
# --azure-rm-tenant-id $identity.tenantId `
# --name $IdentityName `
# --organization $OrganizationUrl `
# --project $Project `
# Prepare service connection REST API request body
Write-Verbose "Creating / updating service connection '${ServiceConnectionName}'..."
Get-Content -Path (Join-Path $PSScriptRoot manualServiceEndpointRequest.json) `
| ConvertFrom-Json `
| Set-Variable serviceEndpointRequest
$serviceEndpointDescription = "Created by $($MyInvocation.MyCommand.Name). Configured Managed Identity ${IdentityName} (clientId $($identity.clientId)) federated on ${federatedSubject} as ${ServiceConnectionRole} on scope ${ServiceConnectionScope}."
$serviceEndpointRequest.authorization.parameters.servicePrincipalId = $identity.clientId
$serviceEndpointRequest.authorization.parameters.tenantId = $identity.tenantId
$serviceEndpointRequest.data.subscriptionId = $serviceConnectionSubscriptionId
$serviceEndpointRequest.data.subscriptionName = $serviceConnectionSubscriptionName
$serviceEndpointRequest.description = $serviceEndpointDescription
$serviceEndpointRequest.name = $ServiceConnectionName
$serviceEndpointRequest.serviceEndpointProjectReferences[0].description = $serviceEndpointDescription
$serviceEndpointRequest.serviceEndpointProjectReferences[0].name = $ServiceConnectionName
$serviceEndpointRequest.serviceEndpointProjectReferences[0].projectReference.id = $projectId
$serviceEndpointRequest.serviceEndpointProjectReferences[0].projectReference.name = $Project
$serviceEndpointRequest | ConvertTo-Json -Depth 4 | Set-Variable serviceEndpointRequestBody
Write-Debug "Service connection request body: `n${serviceEndpointRequestBody}"
$apiUri = "${OrganizationUrl}/_apis/serviceendpoint/endpoints"
if ($serviceEndpointId) {
$apiUri += "/${serviceEndpointId}"
}
$apiUri += "?api-version=${apiVersion}"
Invoke-RestMethod -Uri $apiUri `
-Method ($serviceEndpointId ? 'PUT' : 'POST') `
-Body $serviceEndpointRequestBody `
-ContentType 'application/json' `
-Authentication Bearer `
-Token (ConvertTo-SecureString $accessToken -AsPlainText) `
| Set-Variable serviceEndpoint
$serviceEndpoint | ConvertTo-Json -Depth 4 | Write-Debug
if (!$serviceEndpoint) {
Write-Error "Failed to create / update service connection '${ServiceConnectionName}'"
exit 1
}
if ($serviceEndpointId) {
Write-Host "Service connection '${ServiceConnectionName}' updated:"
} else {
Write-Host "Service connection '${ServiceConnectionName}' created:"
}
Write-Debug "Service connection data:"
$serviceEndpoint.data | Format-List | Out-String | Write-Debug
Write-Debug "Service connection authorization parameters:"
$serviceEndpoint.authorization.parameters | Format-List | Out-String | Write-Debug
# Create Federated Credential
Write-Verbose "Configuring Managed Identity '${IdentityName}' with federated subject '$($serviceEndpoint.authorization.parameters.workloadIdentityFederationSubject)'..."
az identity federated-credential create --name $IdentityName `
--identity-name $IdentityName `
--resource-group $IdentityResourceGroupName `
--issuer $serviceEndpoint.authorization.parameters.workloadIdentityFederationIssuer `
--subject $serviceEndpoint.authorization.parameters.workloadIdentityFederationSubject `
--subscription $IdentitySubscriptionId `
-o json `
| ConvertFrom-Json `
| Set-Variable federatedCredential
Write-Verbose "Created federated credential $($federatedCredential.id)"
$identity | Add-Member -NotePropertyName federatedSubject -NotePropertyValue $serviceEndpoint.authorization.parameters.workloadIdentityFederationSubject
$identity | Add-Member -NotePropertyName role -NotePropertyValue $ServiceConnectionRole
$identity | Add-Member -NotePropertyName scope -NotePropertyValue $ServiceConnectionScope
$identity | Add-Member -NotePropertyName subscriptionId -NotePropertyValue $IdentitySubscriptionId
$identity | Format-List | Out-String | Write-Debug
$serviceEndpoint | Select-Object -Property authorization, data, id, name, description, type, createdBy `
| ForEach-Object {
$_.createdBy = $_.createdBy.uniqueName
$_ | Add-Member -NotePropertyName clientId -NotePropertyValue $_.authorization.parameters.serviceprincipalid
$_ | Add-Member -NotePropertyName creationMode -NotePropertyValue $_.data.creationMode
$_ | Add-Member -NotePropertyName scheme -NotePropertyValue $_.authorization.scheme
$_ | Add-Member -NotePropertyName scopeLevel -NotePropertyValue $_.data.scopeLevel
$_ | Add-Member -NotePropertyName subscriptionName -NotePropertyValue $_.data.subscriptionName
$_ | Add-Member -NotePropertyName subscriptionId -NotePropertyValue $_.data.subscriptionId
$_ | Add-Member -NotePropertyName tenantid -NotePropertyValue $_.authorization.parameters.tenantid
$_ | Add-Member -NotePropertyName workloadIdentityFederationSubject -NotePropertyValue $_.authorization.parameters.workloadIdentityFederationSubject
$_
} `
| Select-Object -ExcludeProperty authorization, data
| Format-List