Skip to content

Commit c4f970d

Browse files
azure-sdkbenbp
andauthored
Source user ID from graph api in TME (#42791)
Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent aeca807 commit c4f970d

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,15 @@ try {
358358
# Make sure the provisioner OID is set so we can pass it through to the deployment.
359359
if (!$ProvisionerApplicationId -and !$ProvisionerApplicationOid) {
360360
if ($context.Account.Type -eq 'User') {
361-
# HomeAccountId format is '<object id>.<tenant id>'
362-
$userAccountId = (Get-AzContext).Account.ExtendedProperties.HomeAccountId.Split('.')[0]
361+
# Calls to graph API in corp tenant get blocked by conditional access policy now
362+
# but not in TME. For corp tenant we get the user's id from the login context
363+
# but for TME it is different so we have to source it from graph
364+
$userAccountId = if ($wellKnownTMETenants.Contains($TenantId)) {
365+
(Get-AzADUser -SignedIn).Id
366+
} else {
367+
# HomeAccountId format is '<object id>.<tenant id>'
368+
(Get-AzContext).Account.ExtendedProperties.HomeAccountId.Split('.')[0]
369+
}
363370
if ($null -eq $userAccountId) {
364371
throw "Failed to find entra object ID for the current user"
365372
}
@@ -430,12 +437,20 @@ try {
430437

431438
if (!$CI -and !$ServicePrincipalAuth) {
432439
if ($TestApplicationId) {
433-
Write-Warning "The specified TestApplicationId '$TestApplicationId' will be ignored when -ServicePrincipalAutth is not set."
440+
Write-Warning "The specified TestApplicationId '$TestApplicationId' will be ignored when -ServicePrincipalAuth is not set."
434441
}
435442

436443
$userAccountName = (Get-AzContext).Account.Id
437444
# HomeAccountId format is '<object id>.<tenant id>'
438-
$userAccountId = (Get-AzContext).Account.ExtendedProperties.HomeAccountId.Split('.')[0]
445+
# Calls to graph API in corp tenant get blocked by conditional access policy now
446+
# but not in TME. For corp tenant we get the user's id from the login context
447+
# but for TME it is different so we have to source it from graph
448+
$userAccountId = if ($wellKnownTMETenants.Contains($TenantId)) {
449+
(Get-AzADUser -SignedIn).Id
450+
} else {
451+
# HomeAccountId format is '<object id>.<tenant id>'
452+
(Get-AzContext).Account.ExtendedProperties.HomeAccountId.Split('.')[0]
453+
}
439454
if ($null -eq $userAccountId) {
440455
throw "Failed to find entra object ID for the current user"
441456
}

0 commit comments

Comments
 (0)