Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Az.Resources] Support AtScope for Get-AzRoleAssignment. #27113

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public void RaGetByScope()
TestRunner.RunTestScript("Test-RaGetByScope");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void RaGetWithAtScope()
{
TestRunner.RunTestScript("Test-RaGetWithAtScope");
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void RaGetOnlyByRoleDefinitionName()
Expand Down
25 changes: 25 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/RoleAssignmentTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,31 @@ function Test-RaGetByScope
VerifyRoleAssignmentDeleted $newAssignment1
}

<#
.SYNOPSIS
Tests verifies get of RoleAssignment With AtScope
#>
function Test-RaGetWithAtScope
{
# Setup
$subscription = $(Get-AzContext).Subscription
$resourceGroups = Get-AzResourceGroup | Select-Object -Last 9 -Wait
$scope1 = '/subscriptions/'+ $subscription[0].Id
$scope2 = '/subscriptions/'+ $subscription[0].Id +'/resourceGroups/' + $resourceGroups[0].ResourceGroupName

$ras_scope_list = @()
$ras_atscope_list = @()

$ras_scope = Get-AzRoleAssignment -Scope $scope1
$ras_scope | Select-Object -ExpandProperty Scope -Unique | ForEach-Object { $ras_scope_list += $_ }

$ras_atscope = Get-AzRoleAssignment -Scope $scope1 -AtScope
$ras_atscope | Select-Object -ExpandProperty Scope -Unique | ForEach-Object { $ras_atscope_list += $_ }

Assert-True { $ras_scope_list -contains $scope2 }
Assert-False { $ras_Atscope_list -contains $scope2 }
}

<#
.SYNOPSIS
Tests verifies get of RoleAssignment using only the role definition name
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Supported `-AtScope` for `Get-AzRoleAssignment`.
NoriZC marked this conversation as resolved.
Show resolved Hide resolved

## Version 7.8.1
* Updated to use bicep parameter --documentation-uri instead of the deprecated --documentationUri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
// https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest
// https://learn.microsoft.com/en-us/azure/role-based-access-control/elevate-access-global-admin#elevate-access-for-a-global-administrator-1
// scope is path variable in REST API. When scope is '/', query '$filter=atScope()' is required, or else it will throw BadRequest.
Boolean isRootScope = "/".Equals(options.Scope);
Boolean needsAtScope = "/".Equals(options.Scope) || options.AtScope;
Boolean needsFilterPrincipalId = false;
if (options.ADObjectFilter?.HasFilter ?? false)
{
Expand All @@ -215,7 +215,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
}

principalId = adObject.Id.ToString();
if (isRootScope)
if (needsAtScope)
{
odataQuery = new ODataQuery<RoleAssignmentFilter>(f => (f.AtScope() && f.AssignedTo(principalId)));
}
Expand All @@ -227,7 +227,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
else
{
principalId = string.IsNullOrEmpty(options.ADObjectFilter.Id) ? adObject.Id.ToString() : options.ADObjectFilter.Id;
if (isRootScope)
if (needsAtScope)
{
/* $filter = principalId + eq + '{objectId}' Lists role assignments for a specified user, group, or service principal.
* If you use atScope() and principalId+eq + '{objectId}' together, it will throw exception because the API doesn't allow it.
Expand All @@ -243,7 +243,7 @@ public List<PSRoleAssignment> FilterRoleAssignments(FilterRoleAssignmentsOptions
}
}
}
else if (isRootScope)
else if (needsAtScope)
{
odataQuery = new ODataQuery<RoleAssignmentFilter>(f => f.AtScope());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public string Scope

public ADObjectFilterOptions ADObjectFilter { get; set; }

public bool AtScope { get; set; }

public bool ExpandPrincipalGroups { get; set; }

public bool IncludeClassicAdministrators { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ public class GetAzureRoleAssignmentCommand : ResourcesBaseCmdlet
[ScopeCompleter]
public string Scope { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.Scope,
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithObjectId,
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithSignInName,
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.ScopeWithSPN,
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleIdWithScopeAndObjectId,
HelpMessage = "If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.")]
public SwitchParameter AtScope { get; set; }

[Parameter(Mandatory = false, ParameterSetName = ParameterSet.ObjectId,
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.")]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet.SignInName,
Expand Down Expand Up @@ -245,8 +257,9 @@ public override void ExecuteCmdlet()
ResourceType = ResourceType,
Subscription = DefaultProfile.DefaultContext.Subscription?.Id?.ToString()
},
ExpandPrincipalGroups = ExpandPrincipalGroups.IsPresent,
IncludeClassicAdministrators = IncludeClassicAdministrators.IsPresent,
AtScope = AtScope,
ExpandPrincipalGroups = ExpandPrincipalGroups,
IncludeClassicAdministrators = IncludeClassicAdministrators,
};

if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
Expand Down
74 changes: 52 additions & 22 deletions src/Resources/Resources/help/Get-AzRoleAssignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,123 +29,123 @@ Please notice that this cmdlet will mark `ObjectType` as `Unknown` in output if
```
Get-AzRoleAssignment [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ObjectIdParameterSet
```
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] [-ExpandPrincipalGroups]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceGroupWithObjectIdParameterSet
```
Get-AzRoleAssignment -ObjectId <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceWithObjectIdParameterSet
```
Get-AzRoleAssignment -ObjectId <String> -ResourceGroupName <String> -ResourceName <String>
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ScopeWithObjectIdParameterSet
```
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] -Scope <String>
Get-AzRoleAssignment -ObjectId <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### RoleIdWithScopeAndObjectIdParameterSet
```
Get-AzRoleAssignment [-ObjectId <String>] -RoleDefinitionId <Guid> [-Scope <String>]
Get-AzRoleAssignment [-ObjectId <String>] -RoleDefinitionId <Guid> [-Scope <String>] [-AtScope]
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceGroupWithSignInNameParameterSet
```
Get-AzRoleAssignment -SignInName <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceWithSignInNameParameterSet
```
Get-AzRoleAssignment -SignInName <String> -ResourceGroupName <String> -ResourceName <String>
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ScopeWithSignInNameParameterSet
```
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] -Scope <String>
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### SignInNameParameterSet
```
Get-AzRoleAssignment -SignInName <String> [-RoleDefinitionName <String>] [-ExpandPrincipalGroups]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceGroupWithSPNParameterSet
```
Get-AzRoleAssignment -ServicePrincipalName <String> -ResourceGroupName <String> [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceWithSPNParameterSet
```
Get-AzRoleAssignment -ServicePrincipalName <String> -ResourceGroupName <String> -ResourceName <String>
-ResourceType <String> [-ParentResource <String>] [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ScopeWithSPNParameterSet
```
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>] -Scope <String>
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>] -Scope <String> [-AtScope]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### SPNParameterSet
```
Get-AzRoleAssignment -ServicePrincipalName <String> [-RoleDefinitionName <String>]
[-IncludeClassicAdministrators] [-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceGroupParameterSet
```
Get-AzRoleAssignment -ResourceGroupName <String> [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ResourceParameterSet
```
Get-AzRoleAssignment -ResourceGroupName <String> -ResourceName <String> -ResourceType <String>
[-ParentResource <String>] [-RoleDefinitionName <String>] [-IncludeClassicAdministrators]
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

### ScopeParameterSet
```
Get-AzRoleAssignment [-RoleDefinitionName <String>] -Scope <String> [-IncludeClassicAdministrators]
Get-AzRoleAssignment [-RoleDefinitionName <String>] -Scope <String> [-AtScope] [-IncludeClassicAdministrators]
[-SkipClientSideScopeValidation] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

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

## PARAMETERS

### -AtScope
If specified, lists role assignments for only the specified scope, not including the role assignments at subscopes.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: ScopeWithObjectIdParameterSet, RoleIdWithScopeAndObjectIdParameterSet, ScopeWithSignInNameParameterSet, ScopeWithSPNParameterSet, ScopeParameterSet
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -DefaultProfile
The credentials, account, tenant, and subscription used for communication with azure

Expand Down Expand Up @@ -303,6 +318,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -ProgressAction
{{ Fill ProgressAction Description }}

```yaml
Type: System.Management.Automation.ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -ResourceGroupName
The resource group name.
Lists role assignments that are effective at the specified resource group.
Expand Down