Skip to content

Commit d9066df

Browse files
NoriZCisra-felVeryEarly
authored
[Az.Resources] Support AtScope for Get-AzRoleAssignment. (#27113)
* support atscope for get-roleassignment * Update Changelog * Switch Parameter * description * description * rename test case * rename test case * minor fix * Update src/Resources/Resources/ChangeLog.md Co-authored-by: Yeming Liu <[email protected]> --------- Co-authored-by: Yeming Liu <[email protected]> Co-authored-by: Yabo Hu <[email protected]>
1 parent 411801d commit d9066df

File tree

8 files changed

+590
-28
lines changed

8 files changed

+590
-28
lines changed

src/Resources/Resources.Test/ScenarioTests/RoleAssignmentTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public void RaGetByScope()
140140
TestRunner.RunTestScript("Test-RaGetByScope");
141141
}
142142

143+
[Fact]
144+
[Trait(Category.AcceptanceType, Category.CheckIn)]
145+
public void RaGetWithAtScope()
146+
{
147+
TestRunner.RunTestScript("Test-RaGetWithAtScope");
148+
}
149+
143150
[Fact]
144151
[Trait(Category.AcceptanceType, Category.LiveOnly)]
145152
public void RaGetOnlyByRoleDefinitionName()

src/Resources/Resources.Test/ScenarioTests/RoleAssignmentTests.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,31 @@ function Test-RaGetByScope
551551
VerifyRoleAssignmentDeleted $newAssignment1
552552
}
553553

554+
<#
555+
.SYNOPSIS
556+
Tests verifies get of RoleAssignment With AtScope
557+
#>
558+
function Test-RaGetWithAtScope
559+
{
560+
# Setup
561+
$subscription = $(Get-AzContext).Subscription
562+
$resourceGroups = Get-AzResourceGroup | Select-Object -Last 9 -Wait
563+
$scope1 = '/subscriptions/'+ $subscription[0].Id
564+
$scope2 = '/subscriptions/'+ $subscription[0].Id +'/resourceGroups/' + $resourceGroups[0].ResourceGroupName
565+
566+
$ras_scope_list = @()
567+
$ras_atscope_list = @()
568+
569+
$ras_scope = Get-AzRoleAssignment -Scope $scope1
570+
$ras_scope | Select-Object -ExpandProperty Scope -Unique | ForEach-Object { $ras_scope_list += $_ }
571+
572+
$ras_atscope = Get-AzRoleAssignment -Scope $scope1 -AtScope
573+
$ras_atscope | Select-Object -ExpandProperty Scope -Unique | ForEach-Object { $ras_atscope_list += $_ }
574+
575+
Assert-True { $ras_scope_list -contains $scope2 }
576+
Assert-False { $ras_Atscope_list -contains $scope2 }
577+
}
578+
554579
<#
555580
.SYNOPSIS
556581
Tests verifies get of RoleAssignment using only the role definition name

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.RoleAssignmentTests/RaGetWithAtScope.json

Lines changed: 484 additions & 0 deletions
Large diffs are not rendered by default.

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Supported getting role assignments at the exact scope via `-AtScope` for `Get-AzRoleAssignment`.
2223

2324
## Version 7.8.1
2425
* Updated to use bicep parameter --documentation-uri instead of the deprecated --documentationUri

src/Resources/Resources/Models.Authorization/AuthorizationClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
192192
// https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest
193193
// https://learn.microsoft.com/en-us/azure/role-based-access-control/elevate-access-global-admin#elevate-access-for-a-global-administrator-1
194194
// scope is path variable in REST API. When scope is '/', query '$filter=atScope()' is required, or else it will throw BadRequest.
195-
Boolean isRootScope = "/".Equals(options.Scope);
195+
Boolean needsAtScope = "/".Equals(options.Scope) || options.AtScope;
196196
Boolean needsFilterPrincipalId = false;
197197
if (options.ADObjectFilter?.HasFilter ?? false)
198198
{
@@ -215,7 +215,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
215215
}
216216

217217
principalId = adObject.Id.ToString();
218-
if (isRootScope)
218+
if (needsAtScope)
219219
{
220220
odataQuery = new ODataQuery<RoleAssignmentFilter>(f => (f.AtScope() && f.AssignedTo(principalId)));
221221
}
@@ -227,7 +227,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
227227
else
228228
{
229229
principalId = string.IsNullOrEmpty(options.ADObjectFilter.Id) ? adObject.Id.ToString() : options.ADObjectFilter.Id;
230-
if (isRootScope)
230+
if (needsAtScope)
231231
{
232232
/* $filter = principalId + eq + '{objectId}' Lists role assignments for a specified user, group, or service principal.
233233
* If you use atScope() and principalId+eq + '{objectId}' together, it will throw exception because the API doesn't allow it.
@@ -243,7 +243,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
243243
}
244244
}
245245
}
246-
else if (isRootScope)
246+
else if (needsAtScope)
247247
{
248248
odataQuery = new ODataQuery<RoleAssignmentFilter>(f => f.AtScope());
249249
}

src/Resources/Resources/Models.Authorization/FilterRoleAssignmentsOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public string Scope
5858

5959
public ADObjectFilterOptions ADObjectFilter { get; set; }
6060

61+
public bool AtScope { get; set; }
62+
6163
public bool ExpandPrincipalGroups { get; set; }
6264

6365
public bool IncludeClassicAdministrators { get; set; }

src/Resources/Resources/RoleAssignments/GetAzureRoleAssignmentCommand.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ public class GetAzureRoleAssignmentCommand : ResourcesBaseCmdlet
178178
[ScopeCompleter]
179179
public string Scope { get; set; }
180180

181+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.Scope,
182+
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
183+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithObjectId,
184+
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
185+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithSignInName,
186+
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
187+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithSPN,
188+
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
189+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
190+
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
191+
public SwitchParameter AtScope { get; set; }
192+
181193
[Parameter(Mandatory = false, ParameterSetName = ParameterSet.ObjectId,
182194
HelpMessage = "If specified, returns role assignments directly assigned to the principal as well as assignments to the principal's groups (transitive). Supported only for User Principals.")]
183195
[Parameter(Mandatory = false, ParameterSetName = ParameterSet.SignInName,
@@ -245,8 +257,9 @@ public override void ExecuteCmdlet()
245257
ResourceType = ResourceType,
246258
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString()
247259
},
248-
ExpandPrincipalGroups = ExpandPrincipalGroups.IsPresent,
249-
IncludeClassicAdministrators = IncludeClassicAdministrators.IsPresent,
260+
AtScope = AtScope,
261+
ExpandPrincipalGroups = ExpandPrincipalGroups,
262+
IncludeClassicAdministrators = IncludeClassicAdministrators,
250263
};
251264

252265
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)

src/Resources/Resources/help/Get-AzRoleAssignment.md

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,123 +29,123 @@ Please notice that this cmdlet will mark `ObjectType` as `Unknown` in output if
2929
```
3030
Get-AzRoleAssignment [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
3131
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
32-
[<CommonParameters>]
32+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
3333
```
3434

3535
### ObjectIdParameterSet
3636
```
3737
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] [-ExpandPrincipalGroups]
3838
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
39-
[<CommonParameters>]
39+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
4040
```
4141

4242
### ResourceGroupWithObjectIdParameterSet
4343
```
4444
Get-AzRoleAssignment -ObjectId <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
4545
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
46-
[<CommonParameters>]
46+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
4747
```
4848

4949
### ResourceWithObjectIdParameterSet
5050
```
5151
Get-AzRoleAssignment -ObjectId <String> -ResourceGroupName <String> -ResourceName <String>
5252
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
5353
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
54-
[<CommonParameters>]
54+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
5555
```
5656

5757
### ScopeWithObjectIdParameterSet
5858
```
59-
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] -Scope <String>
59+
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
6060
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
61-
[<CommonParameters>]
61+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
6262
```
6363

6464
### RoleIdWithScopeAndObjectIdParameterSet
6565
```
66-
Get-AzRoleAssignment [-ObjectId <String>] -RoleDefinitionId <Guid> [-Scope <String>]
66+
Get-AzRoleAssignment [-ObjectId <String>] -RoleDefinitionId <Guid> [-Scope <String>] [-AtScope]
6767
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
68-
[<CommonParameters>]
68+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
6969
```
7070

7171
### ResourceGroupWithSignInNameParameterSet
7272
```
7373
Get-AzRoleAssignment -SignInName <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
7474
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
75-
[<CommonParameters>]
75+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
7676
```
7777

7878
### ResourceWithSignInNameParameterSet
7979
```
8080
Get-AzRoleAssignment -SignInName <String> -ResourceGroupName <String> -ResourceName <String>
8181
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
8282
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
83-
[<CommonParameters>]
83+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
8484
```
8585

8686
### ScopeWithSignInNameParameterSet
8787
```
88-
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] -Scope <String>
88+
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
8989
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
90-
[<CommonParameters>]
90+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
9191
```
9292

9393
### SignInNameParameterSet
9494
```
9595
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] [-ExpandPrincipalGroups]
9696
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
97-
[<CommonParameters>]
97+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
9898
```
9999

100100
### ResourceGroupWithSPNParameterSet
101101
```
102102
Get-AzRoleAssignment -ServicePrincipalName <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
103103
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
104-
[<CommonParameters>]
104+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
105105
```
106106

107107
### ResourceWithSPNParameterSet
108108
```
109109
Get-AzRoleAssignment -ServicePrincipalName <String> -ResourceGroupName <String> -ResourceName <String>
110110
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
111111
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
112-
[<CommonParameters>]
112+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
113113
```
114114

115115
### ScopeWithSPNParameterSet
116116
```
117-
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>] -Scope <String>
117+
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
118118
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
119-
[<CommonParameters>]
119+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
120120
```
121121

122122
### SPNParameterSet
123123
```
124124
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>]
125125
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
126-
[<CommonParameters>]
126+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
127127
```
128128

129129
### ResourceGroupParameterSet
130130
```
131131
Get-AzRoleAssignment -ResourceGroupName <String> [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
132132
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
133-
[<CommonParameters>]
133+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
134134
```
135135

136136
### ResourceParameterSet
137137
```
138138
Get-AzRoleAssignment -ResourceGroupName <String> -ResourceName <String> -ResourceType <String>
139139
[-ParentResource <String>] [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
140140
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
141-
[<CommonParameters>]
141+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
142142
```
143143

144144
### ScopeParameterSet
145145
```
146-
Get-AzRoleAssignment [-RoleDefinitionName <String>] -Scope <String> [-IncludeClassicAdministrators]
146+
Get-AzRoleAssignment [-RoleDefinitionName <String>] -Scope <String> [-AtScope] [-IncludeClassicAdministrators]
147147
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
148-
[<CommonParameters>]
148+
[-ProgressAction <ActionPreference>] [<CommonParameters>]
149149
```
150150

151151
## DESCRIPTION
@@ -213,6 +213,21 @@ Gets role assignments for the specified Service Principal using Get-AzAdServiceP
213213

214214
## PARAMETERS
215215

216+
### -AtScope
217+
If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.
218+
219+
```yaml
220+
Type: System.Management.Automation.SwitchParameter
221+
Parameter Sets: ScopeWithObjectIdParameterSet, RoleIdWithScopeAndObjectIdParameterSet, ScopeWithSignInNameParameterSet, ScopeWithSPNParameterSet, ScopeParameterSet
222+
Aliases:
223+
224+
Required: False
225+
Position: Named
226+
Default value: None
227+
Accept pipeline input: True (ByPropertyName)
228+
Accept wildcard characters: False
229+
```
230+
216231
### -DefaultProfile
217232
The credentials, account, tenant, and subscription used for communication with azure
218233
@@ -303,6 +318,21 @@ Accept pipeline input: True (ByPropertyName)
303318
Accept wildcard characters: False
304319
```
305320
321+
### -ProgressAction
322+
{{ Fill ProgressAction Description }}
323+
324+
```yaml
325+
Type: System.Management.Automation.ActionPreference
326+
Parameter Sets: (All)
327+
Aliases: proga
328+
329+
Required: False
330+
Position: Named
331+
Default value: None
332+
Accept pipeline input: False
333+
Accept wildcard characters: False
334+
```
335+
306336
### -ResourceGroupName
307337
The resource group name.
308338
Lists role assignments that are effective at the specified resource group.

0 commit comments

Comments
 (0)