You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: improve role definition clean up and add exclusion filters (#495)
# Pull Request
## Description
This PR updates the role assignment query to use resouce graph due to a
bug with the az cli functions.
It also adds two filter to specify which child management groups and
which role assignments to delete. These are to enable the use of this
function at scale for e2e testing.
## License
By submitting this pull request, I confirm that my contribution is made
under the terms of the projects associated license.
Write-ToConsoleLog"Including custom role definition for deletion due to pattern match '$pattern': $($roleDef.roleName) (ID: $($roleDef.name))"-NoNewLine
659
+
$shouldDelete=$true
660
+
break
661
+
}
662
+
}
663
+
if($shouldDelete) {
664
+
$filteredRoleDefinitions+=$roleDef
665
+
} else {
666
+
Write-ToConsoleLog"Skipping custom role definition (no pattern match): $($roleDef.roleName) (ID: $($roleDef.name))"-NoNewLine
667
+
}
668
+
}
669
+
$customRoleDefinitions=$filteredRoleDefinitions
670
+
}
671
+
599
672
if (-not$customRoleDefinitions-or$customRoleDefinitions.Count-eq0) {
600
673
Write-ToConsoleLog"No custom role definitions found on management group: $ManagementGroupId ($ManagementGroupDisplayName), skipping."-NoNewLine
601
674
return
602
675
}
603
676
604
677
Write-ToConsoleLog"Found $($customRoleDefinitions.Count) custom role definition(s) on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
605
678
606
-
# For each custom role definition, find and delete all assignments first
679
+
# For each custom role definition, find and delete all assignments using Resource Graph, then delete the definition
Write-ToConsoleLog"Processing custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))"-NoNewLine
609
682
610
-
#Find all role assignments for this custom role on the management group
611
-
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
612
-
$mgRoleAssignments= (az role assignment list --role $roleDefinition.roleName--scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"--query "[].{id:id,principalName:principalName,principalId:principalId}"-o json) |ConvertFrom-Json
683
+
#Use Resource Graph to find all role assignments for this custom role definition across all scopes
684
+
$resourceGraphQuery="authorizationresources | where type == 'microsoft.authorization/roleassignments' | where properties.roleDefinitionId == '/providers/Microsoft.Authorization/RoleDefinitions/$($roleDefinition.name)' | project id, name, properties"
685
+
$roleAssignments= (az graph query -q $resourceGraphQuery--query "data"-o json) |ConvertFrom-Json
613
686
614
-
if ($mgRoleAssignments-and$mgRoleAssignments.Count-gt0) {
615
-
Write-ToConsoleLog"Found $($mgRoleAssignments.Count) role assignment(s) of custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
687
+
if ($roleAssignments-and$roleAssignments.Count-gt0) {
688
+
Write-ToConsoleLog"Found $($roleAssignments.Count) role assignment(s) for custom role '$($roleDefinition.roleName)'"-NoNewLine
Write-ToConsoleLog"Deleting role assignment of custom role '$roleDefinitionName' for principal: $($assignment.principalName) ($($assignment.principalId)) from management group: $managementGroupId ($managementGroupDisplayName)"-NoNewLine
696
+
Write-ToConsoleLog"Deleting role assignment '$($assignment.name)' of custom role '$roleDefinitionName' for principal: $($assignment.properties.principalId)"-NoNewLine
626
697
627
698
if($using:PlanMode) {
628
699
Write-ToConsoleLog`
629
-
"Deleting role assignment of custom role '$roleDefinitionName' for principal: $($assignment.principalName) ($($assignment.principalId)) from management group: $managementGroupId ($managementGroupDisplayName)",`
700
+
"Deleting role assignment '$($assignment.name)' of custom role '$roleDefinitionName' for principal: $($assignment.properties.principalId)",`
630
701
"Would run: az role assignment delete --ids $($assignment.id)"`
631
702
-IsPlan -LogFilePath $using:TempLogFileForPlan
632
703
} else {
633
704
$result= az role assignment delete --ids $assignment.id2>&1
634
705
if (!$result) {
635
-
Write-ToConsoleLog"Deleted role assignment of custom role '$roleDefinitionName' from management group: $managementGroupId ($managementGroupDisplayName)"-NoNewLine
706
+
Write-ToConsoleLog"Deleted role assignment '$($assignment.name)' of custom role '$roleDefinitionName'"-NoNewLine
636
707
} else {
637
-
Write-ToConsoleLog"Failed to delete role assignment of custom role '$roleDefinitionName' from management group: $managementGroupId ($managementGroupDisplayName)"-IsWarning -NoNewLine
708
+
Write-ToConsoleLog"Failed to delete role assignment '$($assignment.name)' of custom role '$roleDefinitionName'"-IsWarning -NoNewLine
638
709
}
639
710
}
640
711
} -ThrottleLimit $using:ThrottleLimit
641
712
} else {
642
-
Write-ToConsoleLog"No role assignments found for custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
643
-
}
644
-
645
-
# Find all role assignments for this custom role on subscriptions under the management group
646
-
if ($Subscriptions-and$Subscriptions.Count-gt0) {
647
-
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on subscriptions under management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
658
-
659
-
$subRoleAssignments= (az role assignment list --role $roleDefinition.roleName--subscription $subscription.Id--query "[].{id:id,principalName:principalName,principalId:principalId}"-o json) |ConvertFrom-Json
660
-
661
-
if ($subRoleAssignments-and$subRoleAssignments.Count-gt0) {
662
-
Write-ToConsoleLog"Found $($subRoleAssignments.Count) role assignment(s) of custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
663
-
664
-
foreach ($assignmentin$subRoleAssignments) {
665
-
Write-ToConsoleLog"Deleting role assignment of custom role '$($roleDefinition.roleName)' for principal: $($assignment.principalName) ($($assignment.principalId)) from subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
666
-
667
-
if($using:PlanMode) {
668
-
Write-ToConsoleLog`
669
-
"Deleting role assignment of custom role '$($roleDefinition.roleName)' for principal: $($assignment.principalName) ($($assignment.principalId)) from subscription: $($subscription.Name) (ID: $($subscription.Id))",`
670
-
"Would run: az role assignment delete --ids $($assignment.id)"`
671
-
-IsPlan -LogFilePath $using:TempLogFileForPlan
672
-
} else {
673
-
$result= az role assignment delete --ids $assignment.id2>&1
674
-
if (!$result) {
675
-
Write-ToConsoleLog"Deleted role assignment of custom role '$($roleDefinition.roleName)' from subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
676
-
} else {
677
-
Write-ToConsoleLog"Failed to delete role assignment of custom role '$($roleDefinition.roleName)' from subscription: $($subscription.Name) (ID: $($subscription.Id))"-IsWarning -NoNewLine
678
-
}
679
-
}
680
-
}
681
-
} else {
682
-
Write-ToConsoleLog"No role assignments found for custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
683
-
}
684
-
} -ThrottleLimit $using:ThrottleLimit
713
+
Write-ToConsoleLog"No role assignments found for custom role '$($roleDefinition.roleName)'"-NoNewLine
685
714
}
686
715
687
716
# Now delete the custom role definition itself
@@ -691,7 +720,7 @@ function Remove-PlatformLandingZone {
691
720
Write-ToConsoleLog`
692
721
"Deleting custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))",`
693
722
"Would run: az role definition delete --name $($roleDefinition.name) --scope `"/providers/Microsoft.Management/managementGroups/$ManagementGroupId`""`
694
-
-IsPlan -LogFilePath $using:TempLogFileForPlan
723
+
-IsPlan -LogFilePath $TempLogFileForPlan
695
724
} else {
696
725
$result= az role definition delete --name $roleDefinition.name--scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"2>&1
697
726
if (!$result) {
@@ -841,7 +870,8 @@ function Remove-PlatformLandingZone {
0 commit comments