Skip to content

Commit 74667cb

Browse files
azure-sdkjsquire
andauthored
Ensure ownership grant (Azure#25847)
The focus of these changes is to ensure that the service principal is explicitly granted the "Owner" role on the active resource group, whether the principal was newly created or a cached instance was used. Co-authored-by: Jesse Squire <jesse.squire@gmail.com>
1 parent 2ec2d29 commit 74667cb

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,18 @@ try {
580580
$PSBoundParameters['TestApplicationOid'] = $TestApplicationOid
581581
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret
582582

583-
# Grant the test service principal ownership over the resource group. This may fail if the provisioner is a
584-
# service principal without permissions to grant RBAC roles to other service principals. That should not be
585-
# considered a critical failure, as the test application may have subscription-level permissions and not require
586-
# the explicit grant.
587-
#
588-
# Ignore this check if $AzureTestPrincipal is specified as role assignment will already have been attempted on a
589-
# previous run, and these error messages can be misleading for local runs.
590-
if (!$resourceGroupRoleAssigned -and !$AzureTestPrincipal) {
583+
# If the role hasn't been explicitly assigned to the resource group and a cached service principal is in use,
584+
# query to see if the grant is needed.
585+
if (!$resourceGroupRoleAssigned -and $AzureTestPrincipal) {
586+
$roleAssignment = Get-AzRoleAssignment -ObjectId $AzureTestPrincipal.Id -RoleDefinitionName 'Owner' -ResourceGroupName "$ResourceGroupName" -ErrorAction SilentlyContinue
587+
$resourceGroupRoleAssigned = ($roleAssignment.RoleDefinitionName -eq 'Owner')
588+
}
589+
590+
# If needed, grant the test service principal ownership over the resource group. This may fail if the provisioner
591+
# is a service principal without permissions to grant RBAC roles to other service principals. That should not be
592+
# considered a critical failure, as the test application may have subscription-level permissions and not require
593+
# the explicit grant.
594+
if (!$resourceGroupRoleAssigned) {
591595
Log "Attempting to assigning the 'Owner' role for '$ResourceGroupName' to the Test Application '$TestApplicationId'"
592596
$principalOwnerAssignment = New-AzRoleAssignment -RoleDefinitionName "Owner" -ApplicationId "$TestApplicationId" -ResourceGroupName "$ResourceGroupName" -ErrorAction SilentlyContinue
593597

0 commit comments

Comments
 (0)