-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvert-ServicePrincipals.ps1
724 lines (621 loc) · 31.4 KB
/
Convert-ServicePrincipals.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
param (
#[string] $serviceConnectionJsonPath = "../data/service_connections.json",
[int] $jsonDepth = 100,
[bool] $isProductionRun = $false,
[bool] $refreshServiceConnectionsIfTheyExist = $false,
[string] $apiVersion = "6.0-preview.4",
[bool] $skipPauseAfterError = $false,
[bool] $skipPauseAfterWarning = $false,
[bool] $revertAll = $false
)
$totalNumberOfArmServiceConnections = 0
$numberOfArmServiceConnectionsWithWorkloadIdentityFederationAutomatic = 0
$numberOfArmServiceConnectionsWithWorkloadIdentityFederationManual = 0
$numberOfArmServiceConnectionsWithServicePrincipalAutomatic = 0
$numberOfArmServiceConnectionsWithServicePrincipalManual = 0
$numberOfArmServiceConnectionsWithManagedIdentity = 0
$numberOfArmServiceConnectionsWithPublishProfile = 0
$numberOfFederatedCredentialsCreatedManually = 0
$numberOfSharedArmServiceConnections = 0
$totalNumberOfArmServiceConnectionWithServicePrincipalConvertedToWorkloadIdentityFederation = 0
$totalNumberOfArmServiceConnectionWithServicePrincipalThatDidNotConvertToWorkloadIdentityFederation = 0
$totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationRevertedBackToServicePrincipal = 0
$totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationThatDidNotRevertBackToServicePrincipal = 0
$hashTableAdoResources = @{}
function Get-AzureDevOpsOrganizationOverview {
[CmdletBinding()]
param (
[string] $tenantId
)
#Disconnect-AzAccount
Clear-AzContext -Force
$login = Connect-AzAccount -Tenant $tenantId
if (!$login) {
Write-Error 'Error logging in and validating your credentials.'
return;
}
$adoResourceId = "499b84ac-1321-427f-aa17-267ca6975798" # Azure DevOps app ID
$msalToken = (Get-AzAccessToken -ResourceUrl $adoResourceId).Token
if (!$tenantId) {
$tenantId = $msalToken.tenantId
Write-Verbose "Set TenantId to $tenantId (retrieved from MSAL token)"
}
# URL retrieved thanks to developer mod at page https://dev.azure.com/<organizationName>/_settings/organizationAad
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer ${msalToken}")
$response = Invoke-WebRequest -Uri "https://aexprodweu1.vsaex.visualstudio.com/_apis/EnterpriseCatalog/Organizations?tenantId=$tenantId" `
-Method get -ContentType "application/json" `
-Headers $headers | Select-Object -ExpandProperty content | ConvertFrom-Csv
$responseJson = $response | ConvertTo-Json -Depth $jsonDepth
$outputFile = "organizations_${tenantId}.json"
Set-Content -Value $responseJson -Path $outputFile
}
# Get-OrganizationId
# This function is used to get the organization id for an organization name
# Parameters
# organizationName: the name of the organization
# tenantId: the id of the tenant that this organization belongs to
# Returns
# the id of the organization
# "" if the organization was not found
function Get-OrganizationId {
param (
[string] $organizationName,
[string] $tenantId
)
$outputFile = "organizations_${tenantId}.json"
$exists = Test-Path -Path $outputFile -PathType Leaf
if (-not $exists) {
Write-Host "File $outputFile not found..."
Get-AzureDevOpsOrganizationOverview -tenantId $tenantId
}
$allOrganizationsJson = Get-Content -Path $outputFile
$allOrganizations = $allOrganizationsJson | ConvertFrom-Json
$organizationFound = $allOrganizations | Where-Object { $_."Organization Name" -eq $organizationName }
if ($organizationFound) {
Write-Host $organizationFound
$organizationId = $organizationFound[0]."Organization Id"
Write-Host "Organization $organizationName has id ${organizationId}"
return $organizationId
}
else {
Write-Warning "did not find org $organizationName in tenant $tenantId"
return ""
}
}
function Get-Projects {
param (
[string] $organizationUrl
)
# get projects - Implement continuation token
$token = $null
$allProjects = @()
do {
if ($null -eq $token) {
$projectsRawJson = az devops project list --organization $organizationUrl
}
else {
$projectsRawJson = az devops project list --organization $organizationUrl --continuation-token $Token
}
$projectsRaw = $projectsRawJson | ConvertFrom-Json -Depth $jsonDepth
$projects = $projectsRaw.value
$token = $projectsRaw.ContinuationToken
$allProjects += $projects
}
while ($null -ne $token)
return $projects
}
function Get-ServiceConnections {
param (
[Parameter(mandatory = $true)]
[string] $tenantId,
[Parameter(mandatory = $true)]
[string] $serviceConnectionJsonPath,
[string] $organizationsOutputFile = "organizations_${tenantId}.json",
[bool] $refreshServiceConnectionsIfTheyExist = $false,
[string] $filterType = "AzureRM"
)
$exported = $false
$organizationsOutputFileExists = Test-Path -Path $organizationsOutputFile -PathType Leaf
if (-not $organizationsOutputFileExists) {
Write-Host "File $organizationsOutputFile not found..."
Get-AzureDevOpsOrganizationOverview -tenantId $tenantId
}
$allOrganizationsJson = Get-Content -Path $organizationsOutputFile
$allOrganizations = $allOrganizationsJson | ConvertFrom-Json
$serviceConnectionsOutputFileExists = Test-Path -Path $serviceConnectionJsonPath -PathType Leaf
$skipFetchingServiceConnections = $serviceConnectionsOutputFileExists -and (-not $refreshServiceConnectionsIfTheyExist)
Write-Host "serviceConnectionsOutputFileExists: $serviceConnectionsOutputFileExists"
Write-Host "refreshServiceConnectionsIfTheyExist: $refreshServiceConnectionsIfTheyExist"
Write-Host "skipFetchingServiceConnections: $skipFetchingServiceConnections"
if ($skipFetchingServiceConnections) {
Write-Host "File $serviceConnectionJsonPath already exists and refreshServiceConnectionsIfTheyExist is set to false. Skipping..."
$exported = $true
return $exported
}
# get all service connections
$allServiceConnections = @()
foreach ($organization in $allOrganizations ) {
$organizationName = $organization."Organization Name"
$organizationId = $organization."Organization Id"
$organizationUrl = $organization."Url"
$projects = Get-Projects -organizationUrl $organizationUrl
foreach ($project in $projects) {
$projectName = $project.name
Write-Host "Org: ${organizationName} Proj: ${projectName}"
# get service connections
$serviceEndpointsJson = az devops service-endpoint list --organization $organizationUrl --project $projectName
$serviceEndpoints = $serviceEndpointsJson | ConvertFrom-Json -Depth 100
Write-Host "`tOrganization $organizationName has project $projectName with $($serviceEndpoints.Length) service endpoints."
$armServiceEndpoints = $serviceEndpoints | Where-Object { $_.type -eq $filterType } #should be case insensitive
Write-Host "`tOrganization $organizationName has project $projectName with $($armServiceEndpoints.Length) ARM service endpoints."
$allServiceConnections += $armServiceEndpoints
foreach ($armServiceEndpoint in $armServiceEndpoints) {
$serviceConnectionName = $armServiceEndpoint.name
$endpointId = $armServiceEndpoint.id
Write-Host "`t`tOrganization $organizationName has project $projectName with ARM service endpoint $serviceConnectionName with id $endpointId."
$projSvcEndpoint = @{
"organizationName" = $organizationName
"organizationId" = $organizationId
"projectName" = $projectName
"serviceEndpoint" = $armServiceEndpoint
}
if ($hashTableAdoResources.ContainsKey("$endpointId")) {
Write-Warning "endpointId $endpointId already exists in hash table"
$isShared = $($armServiceEndpoint.isShared)
if ($isShared) {
Write-Warning "connection is shared as expected"
}
else {
throw "endpointId $endpointId already exists in hash table but is not shared"
}
}
else {
Write-Host "adding endpointId $endpointId to hash table"
$hashTableAdoResources.Add("$endpointId", $projSvcEndpoint)
}
}
}
}
Write-Host "Writing all service connections to json"
$allServiceConnectionsJson = $allServiceConnections | ConvertTo-Json -Depth $jsonDepth
Set-Content -Value $allServiceConnectionsJson -Path $serviceConnectionJsonPath
$exported = $true
return $exported
}
function New-FederatedCredential {
param (
[Parameter(mandatory = $true)]
[string] $organizationName,
[Parameter(mandatory = $true)]
[string] $projectName,
[Parameter(mandatory = $true)]
[string] $serviceConnectionName,
[Parameter(mandatory = $true)]
[string] $appObjectId,
[Parameter(mandatory = $true)]
[string] $endpointId,
[Parameter(mandatory = $true)]
[string] $organizationId
)
$minifiedString = Get-Content .\credential.template.json | Out-String
$parametersJsonContent = (ConvertFrom-Json $minifiedString) | ConvertTo-Json -Depth 100 -Compress; #for PowerShell 7.3
#$issuer = "https://vstoken.dev.azure.com/${organizationId}"
$parametersJsonContent = $parametersJsonContent.Replace("__ENDPOINT_ID__", $endpointId)
$parametersJsonContent = $parametersJsonContent.Replace("__ORGANIZATION_NAME__", $organizationName)
$parametersJsonContent = $parametersJsonContent.Replace("__PROJECT_NAME__", $projectName)
$parametersJsonContent = $parametersJsonContent.Replace("__SERVICE_CONNECTION_NAME__", $serviceConnectionName)
$parametersJsonContent = $parametersJsonContent.Replace("__ORGANIZATION_ID__", $organizationId)
Set-Content -Value $parametersJsonContent -Path .\credential.json
$responseJson = az ad app federated-credential create --id $appObjectId --parameters credential.json
return $responseJson
}
# This function pauses the script until the user presses a key.
function PauseOn {
param (
[bool] $boolValue
)
if ($boolValue) {
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Write-Host
}
}
function ConvertTo-OrRevertFromWorkloadIdentityFederation {
param (
[string] $body,
[string] $patTokenBase64,
[string] $organizationName,
[string] $endpointId
)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Basic $patTokenBase64")
Try {
# undocumented REST API call that is in preview
$uri = "https://dev.azure.com/${organizationName}/_apis/serviceendpoint/endpoints/${endpointId}?operation=ConvertAuthenticationScheme&api-version=${apiVersion}"
Write-Host "Trying url:"
Write-Host $uri
Write-Host
Write-Host "Trying body:"
Write-Host $body
Write-Host
$response = Invoke-RestMethod $uri -Method 'PUT' -Headers $headers -Body $body
if ($response -is [string]) {
if ($response.Contains("Azure DevOps Services | Sign In")) {
Write-Warning "need to sign in - ensure it's the right tenant"
PauseOn -boolValue (-not $skipPauseAfterError)
return ""
}
}
$responseJson = $response | ConvertTo-Json -Depth $jsonDepth
}
Catch {
if ($_.ErrorDetails.Message) {
$errorMessage = $_.ErrorDetails.Message
Write-Error $errorMessage
#{"$id":"1","innerException":null,"message":"Converting endpoint type azurerm scheme from WorkloadIdentityFederation to WorkloadIdentityFederation is neither an upgrade or a downgrade and is not supported.","typeName":"System.ArgumentException, mscorlib","typeKey":"ArgumentException","errorCode":0,"eventId":0}
if ($errorMessage.Contains("is neither an upgrade or a downgrade and is not supported")) {
PauseOn -boolValue (-not $skipPauseAfterError)
return ""
}
elseif ($errorMessage.Contains("Azure Stack environment")) {
#{"$id":"1","innerException":null,"message":"Unable to connect to the Azure Stack environment. Ignore the failure if the source is Azure DevOps.","typeName":"Microsoft.VisualStudio.Services.ServiceEndpoints.WebApi.ServiceEndpointException, Microsoft.VisualStudio.Services.ServiceEndpoints.WebApi","typeKey":"ServiceEndpointException","errorCode":0,"eventId":3000}
PauseOn -boolValue (-not $skipPauseAfterError)
return ""
}
else {
throw "unhandled exception (unexpected exception)" # you may find more errors depending on your environment
}
}
else {
Write-Host $_
throw "should NOT happen" # if it does - ensure you handle it appropriately
}
}
return $responseJson
}
function Get-Body {
param (
[string] $id,
[string] $type,
[string] $authorizationScheme,
[object] $serviceEndpointProjectReferences
)
# Create a custom object to hold the body of the request.
$myBody = [PSCustomObject]@{
id = $id
type = $type
authorization = [PSCustomObject]@{
scheme = $authorizationScheme
}
serviceEndpointProjectReferences = @( $serviceEndpointProjectReferences ) # array
}
# Convert the custom object to JSON.
$myBodyJson = $myBody | ConvertTo-Json -Depth $jsonDepth
return $myBodyJson
}
function Get-Base64 {
# Convert a string to Base64.
param (
[string] $patToken
)
# Convert to Base64.
return [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("`:$patToken"))
}
function Get-PatTokenBase64 {
param (
[string] $tenantId
)
#Expecting an environment variable such as ADO-PAT-TOKEN-TENANT-a34*****-****-****-****-************
Write-Host "ADO PAT is not set. Trying to get it from environment variable `"ADO-PAT-TOKEN-TENANT-$tenantId`" ..."
$AdoPAT = [Environment]::GetEnvironmentVariable("ADO-PAT-TOKEN-TENANT-$tenantId", "User")
if (!$AdoPAT) {
Write-Error "Could not find ADO PAT in environment variable `"ADO-PAT-TOKEN-TENANT-$tenantId`". Please set it and try again."
return
}
else {
Write-Host "Found ADO PAT in environment variable `"ADO-PAT-TOKEN-TENANT-$tenantId`"."
}
$AdoPATBase64 = Get-Base64 -patToken $AdoPAT
return $AdoPATBase64
}
function Get-AuthenticodeMode {
param (
[object] $serviceConnection
)
$authorizationScheme = $($serviceConnection.authorization.scheme)
$creationMode = $($serviceConnection.data.creationMode)
if ($authorizationScheme -eq "WorkloadIdentityFederation") {
return "Workload Identity Federation ($creationMode)"
}
elseif ($authorizationScheme -eq "ServicePrincipal") {
return "Service Principal ($creationMode)"
}
elseif ($authorizationScheme -eq "ManagedServiceIdentity") {
return "Managed Identity"
}
elseif ($authorizationScheme -eq "PublishProfile") {
return "Publish Profile"
}
else {
throw "Unexpected authorization scheme $authorizationScheme"
return $authorizationScheme
}
}
try {
# STEP 1: Login to Azure and Get Service Connections
Write-Host 'Login to your Azure account using az login (use an account that has access to your Microsoft Entra ID) ...'
az account clear
$login = az login --only-show-errors
if (!$login) {
Write-Error 'Error logging in and validating your credentials.'
return;
}
$accountJson = az account show
$account = $accountJson | ConvertFrom-Json
$currentTenantId = $($account.tenantId)
Write-Host "Current Tenant ID: $currentTenantId"
Write-Host "Step 1: Get Service Connections using az devops CLI and export to JSON $serviceConnectionJsonPath ..."
$serviceConnectionJsonPath = "../data/service_connections_${currentTenantId}.json"
$exported = Get-ServiceConnections -serviceConnectionJsonPath $serviceConnectionJsonPath `
-refreshServiceConnectionsIfTheyExist $refreshServiceConnectionsIfTheyExist `
-tenantId $currentTenantId
$hashTableAdoResourcesJson = $hashTableAdoResources | ConvertTo-Json -Depth $jsonDepth
Set-Content -Value $hashTableAdoResourcesJson -Path "hashTableAdoResources.json"
}
catch {
throw
}
if ($exported) {
Write-Host 'Step 2: Loop through all service connections ...'
$serviceConnectionJson = Get-Content -Path $serviceConnectionJsonPath -Raw
$allServiceConnections = ConvertFrom-Json -InputObject $serviceConnectionJson -Depth $jsonDepth
foreach ($entry in $hashTableAdoResources.Values) {
$serviceConnection = $($entry.serviceEndpoint)
$organizationName = $($entry.organizationName)
$projectName = $($entry.projectName)
$organizationId = $($entry.organizationId)
$totalNumberOfArmServiceConnections++
Write-Host "-----------------------"
$applicationRegistrationClientId = $($serviceConnection.authorization.parameters.serviceprincipalid)
Write-Host "App Registration Client Id : $applicationRegistrationClientId"
$tenantId = $($serviceConnection.authorization.parameters.tenantid)
Write-Host "Tenant ID : $tenantId"
Write-Host "Current AAD Tenant is : $currentTenantId"
Write-Host "Service Connection Tenant : $tenantId"
$tenantsMatch = $tenantId -eq $currentTenantId
Write-Host "Tenants Match : $tenantsMatch"
$authorizationScheme = $($serviceConnection.authorization.scheme)
Write-Host "Authorization Scheme : $authorizationScheme"
Write-Host "Organization : $organizationName"
Write-Host "Organization ID : $organizationId"
Write-Host "Project : $projectName"
$serviceConnectionName = $($serviceConnection.name)
Write-Host "Service Connection Name : $serviceConnectionName"
$endpointId = $($serviceConnection.id)
Write-Host "Endpoint ID : $endpointId"
$revertSchemeDeadline = $($serviceConnection.data.revertSchemeDeadline)
Write-Host "Revert Scheme Deadline : $revertSchemeDeadline"
$TimeSpan = $revertSchemeDeadline - (Get-Date -asUTC)
if ($revertSchemeDeadline) {
$totalDays = $TimeSpan.TotalDays
}
else {
$totalDays = 0
}
$canRevert = $totalDays -gt 0
if ($canRevert) {
$foregroundColor = "Green"
}
else {
$foregroundColor = "Red"
}
Write-Host "Can Revert : " -NoNewline
Write-Host "$canRevert" -ForegroundColor $foregroundColor
Write-Host "Total Days left to Revert : $totalDays"
$pauseNeeded = $false
$creationMode = $($serviceConnection.data.creationMode)
if ($creationMode -eq "Automatic") {
$foregroundColor = "Green"
}
elseif ($creationMode -eq "Manual") {
$foregroundColor = "Yellow"
}
elseif ($creationMode -eq "") {
$foregroundColor = "Red"
$creationMode = "<EMPTY>"
$pauseNeeded = $true
}
else {
Write-Host "Unexpected creation mode [${creationMode}]"
throw "Unexpected creation mode [${creationMode}]"
}
Write-Host "Creation Mode : " -NoNewline
Write-Host "$creationMode" -ForegroundColor $foregroundColor
if ($pauseNeeded) {
PauseOn -boolValue (-not $skipPauseAfterError)
}
$AuthenticationMethod = Get-AuthenticodeMode -serviceConnection $serviceConnection
Write-Host "Authentication Method : $AuthenticationMethod"
if ($AuthenticationMethod -eq "Workload Identity Federation (Automatic)") {
$numberOfArmServiceConnectionsWithWorkloadIdentityFederationAutomatic++
}
elseif ($AuthenticationMethod -eq "Workload Identity Federation (Manual)") {
$numberOfArmServiceConnectionsWithWorkloadIdentityFederationManual++
}
elseif ($AuthenticationMethod -eq "Service Principal (Automatic)") {
$numberOfArmServiceConnectionsWithServicePrincipalAutomatic++
}
elseif ($AuthenticationMethod -eq "Service Principal (Manual)") {
$numberOfArmServiceConnectionsWithServicePrincipalManual++
}
elseif ($AuthenticationMethod -eq "Managed Identity") {
$numberOfArmServiceConnectionsWithManagedIdentity++
}
elseif ($AuthenticationMethod -eq "Publish Profile") {
$numberOfArmServiceConnectionsWithPublishProfile++
}
else {
throw "Unexpected authentication mode $AuthenticationMode"
}
$isShared = $($serviceConnection.isShared)
Write-Host "Is Shared : $isShared"
if ($isShared) {
Write-Warning "connection is shared!"
$numberOfSharedArmServiceConnections++
PauseOn -boolValue (-not $skipPauseAfterWarning)
}
Write-Host "Body :"
$serviceEndpointProjectReferences = $($serviceConnection.serviceEndpointProjectReferences)
Write-Host $serviceEndpointProjectReferences
$serviceEndpointProjectReferencesJson = $serviceEndpointProjectReferences | ConvertTo-Json
Write-Host $serviceEndpointProjectReferencesJson
$refCount = $serviceEndpointProjectReferences.Length
Write-Host "Number of Project Refs : $refCount"
if ($refCount -eq 1) {
# Write-Host "expected"
}
else {
Write-Warning "Shared Service Connections are discouraged. This one is shared with $refCount projects."
PauseOn -boolValue (-not $skipPauseAfterWarning)
}
$id = $($serviceConnection.id)
$type = $($serviceConnection.type)
$myBodyJson = Get-Body -id $id `
-type $type `
-authorizationScheme $authorizationScheme `
-serviceEndpointProjectReferences $serviceEndpointProjectReferences
Write-Host $myBodyJson
#common
$patTokenBase64 = Get-PatTokenBase64 -tenantId $tenantId
if ($authorizationScheme -eq "ServicePrincipal") {
$destinationAuthorizationScheme = "WorkloadIdentityFederation"
}
elseif ($authorizationScheme -eq "WorkloadIdentityFederation") {
$destinationAuthorizationScheme = "ServicePrincipal"
}
else {
Write-Warning "Unexpected authorization scheme $authorizationScheme - will not convert (or revert)"
}
$myNewBodyJson = Get-Body -id $id `
-type $type `
-authorizationScheme $destinationAuthorizationScheme `
-serviceEndpointProjectReferences $serviceEndpointProjectReferences
if ($revertAll) {
if ($authorizationScheme -eq "WorkloadIdentityFederation") {
Write-Host "Found workload identity service connection - analyzing if it's a candidate to revert"
if ($isProductionRun -and $tenantsMatch) {
if ($canRevert) {
$responseJson = ConvertTo-OrRevertFromWorkloadIdentityFederation -body $myNewBodyJson `
-organizationName $organizationName `
-endpointId $endpointId `
-patTokenBase64 $patTokenBase64
if ($responseJson) {
Write-Host "Call was successful and returned JSON response:"
Write-Host $responseJson
Write-Host "Reverted service connection!"
$totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationRevertedBackToServicePrincipal++
PauseOn -boolValue (-not $skipPauseAfterError)
}
else {
Write-Warning "Got empty response (check above for message) so moving on..."
$totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationThatDidNotRevertBackToServicePrincipal++
}
}
else {
Write-Warning "Cannot revert since deadline has passed"
}
}
else {
if (-not $isProductionRun) {
Write-Host "Skipping reverting since not a production run"
}
else {
Write-Host "tenants do NOT match so skipping reverting"
}
}
}
}
else {
if ($authorizationScheme -eq "ServicePrincipal") {
Write-Host "Found Service Principal - analyzing if it's a candidate to convert"
if ($isProductionRun -and $tenantsMatch) {
if ($creationMode -eq "Manual") {
Write-Host "Need to create fed cred for Manual Svc Conn"
# $organizationId = Get-OrganizationId -tenantId $tenantId `
# -organizationName $organizationName
if ($organizationId) {
$existingFedCredsJson = az ad app federated-credential list --id $applicationRegistrationClientId
$existingFedCreds = $existingFedCredsJson | ConvertFrom-Json
$subject = "sc://$organizationName/$projectName/$serviceConnectionName"
$matchingCred = $existingFedCreds | Where-Object { $_.Subject -eq $subject }
if ($matchingCred) {
Write-Host "cred with subject $subject ALREADY exists!"
Write-Host $matchingCred
}
else {
Write-Warning "cred with subject $subject does not exist! Creating it now..."
$responseJson = New-FederatedCredential -organizationName $organizationName `
-projectName $projectName `
-organizationId $organizationId `
-serviceConnectionName $serviceConnectionName `
-endpointId $endpointId `
-appObjectId $applicationRegistrationClientId
if ($responseJson) {
$numberOfFederatedCredentialsCreatedManually++
}
PauseOn -boolValue (-not $skipPauseAfterError)
}
}
else {
Write-Warning "Skipping creation of fed cred since we did not find org id in tenant"
}
}
$responseJson = ConvertTo-OrRevertFromWorkloadIdentityFederation -body $myNewBodyJson `
-organizationName $organizationName `
-endpointId $endpointId `
-patTokenBase64 $patTokenBase64
if ($responseJson) {
Write-Host "Call was successful and returned JSON response:"
Write-Host $responseJson
Write-Host "Converted service connection!"
$totalNumberOfArmServiceConnectionWithServicePrincipalConvertedToWorkloadIdentityFederation++
PauseOn -boolValue (-not $skipPauseAfterError)
}
else {
Write-Warning "Got empty response (check above for message) so moving on..."
$totalNumberOfArmServiceConnectionWithServicePrincipalThatDidNotConvertToWorkloadIdentityFederation++
}
}
else {
if (-not $isProductionRun) {
Write-Host "Skipping conversion since not a production run"
}
else {
Write-Host "tenants do NOT match so skipping conversion"
}
}
}
}
Write-Host "-----------------------"
Write-Host
Write-Host "ARM SC with Workload Identity Federation (Automatic) : $numberOfArmServiceConnectionsWithWorkloadIdentityFederationAutomatic"
Write-Host "ARM SC with Workload Identity Federation (Manual) : $numberOfArmServiceConnectionsWithWorkloadIdentityFederationManual"
Write-Host "ARM SC with Service Principal (Automatic) : $numberOfArmServiceConnectionsWithServicePrincipalAutomatic"
Write-Host "ARM SC with Service Principal (Manual) : $numberOfArmServiceConnectionsWithServicePrincipalManual"
Write-Host "ARM SC with Managed Identity : $numberOfArmServiceConnectionsWithManagedIdentity"
Write-Host "ARM SC with Publish Profile : $numberOfArmServiceConnectionsWithPublishProfile"
Write-Host
Write-Host "Total Number of Arm Service Connections : $totalNumberOfArmServiceConnections"
Write-Host
Write-Host "Total Number of Arm Service Connections Converted : $totalNumberOfArmServiceConnectionWithServicePrincipalConvertedToWorkloadIdentityFederation"
Write-Host "Total Number of Arm Service Connections That did NOT Convert : $totalNumberOfArmServiceConnectionWithServicePrincipalThatDidNotConvertToWorkloadIdentityFederation"
Write-Host
Write-Host "Total Number of Arm Service Connections Reverted : $totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationRevertedBackToServicePrincipal"
Write-Host "Total Number of Arm Service Connections That did NOT revert : $totalNumberOfArmServiceConnectionWithWorkloadIdentityFederationThatDidNotRevertBackToServicePrincipal"
Write-Host
Write-Host "Number Of Federated Credentials Created Manually : $numberOfFederatedCredentialsCreatedManually"
Write-Host "Number Of Shared Arm Service Connections : $numberOfSharedArmServiceConnections"
}
}