-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Role] az role assignment list: Add --at-scope
#31179
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
Open
jiasli
wants to merge
1
commit into
Azure:dev
Choose a base branch
from
jiasli:at-scope
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,7 +227,8 @@ def _create_role_assignment(cli_ctx, role, assignee, resource_group_name=None, s | |
| def list_role_assignments(cmd, # pylint: disable=too-many-locals, too-many-branches | ||
| assignee=None, assignee_object_id=None, | ||
| role=None, | ||
| resource_group_name=None, scope=None, | ||
| scope=None, at_scope=True, | ||
| resource_group_name=None, | ||
| include_inherited=False, | ||
| show_all=False, include_groups=False, | ||
| fill_role_definition_name=True, fill_principal_name=True): | ||
|
|
@@ -251,7 +252,8 @@ def list_role_assignments(cmd, # pylint: disable=too-many-locals, too-many-bran | |
| assignee_object_id = _resolve_object_id(cmd.cli_ctx, assignee, fallback_to_object_id=True) | ||
| assignments = _search_role_assignments(assignments_client, definitions_client, | ||
| scope, assignee_object_id, role, | ||
| include_inherited, include_groups) | ||
| include_inherited, include_groups, | ||
| at_scope=at_scope) | ||
|
|
||
| results = todict(assignments) if assignments else [] | ||
|
|
||
|
|
@@ -499,7 +501,7 @@ def delete_role_assignments(cmd, ids=None, | |
| assignee_object_id = _resolve_object_id(cmd.cli_ctx, assignee, fallback_to_object_id=True) | ||
| assignments = _search_role_assignments(assignments_client, definitions_client, | ||
| scope, assignee_object_id, role, include_inherited, | ||
| include_groups=False) | ||
| include_groups=False, at_scope=True) | ||
|
|
||
| if assignments: | ||
| for a in assignments: | ||
|
|
@@ -509,14 +511,20 @@ def delete_role_assignments(cmd, ids=None, | |
|
|
||
|
|
||
| def _search_role_assignments(assignments_client, definitions_client, | ||
| scope, assignee_object_id, role, include_inherited, include_groups): | ||
| scope, assignee_object_id, role, include_inherited, include_groups, | ||
| at_scope=None): | ||
| # https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest | ||
| # "atScope()" and "principalId eq '{value}'" query cannot be used together (API limitation). | ||
| # always use "scope" if provided, so we can get assignments beyond subscription e.g. management groups | ||
| if scope: | ||
| f = 'atScope()' # atScope() excludes role assignments at subscopes | ||
| filters = [] | ||
| if at_scope: | ||
| filters.append('atScope()') # atScope() excludes role assignments at subscopes | ||
| if assignee_object_id and not include_groups and not at_scope: | ||
| filters.append("principalId eq '{}'".format(assignee_object_id)) | ||
|
Comment on lines
+523
to
+524
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now with the ability to turn off |
||
| if assignee_object_id and include_groups: | ||
| f = f + " and assignedTo('{}')".format(assignee_object_id) | ||
| filters.append("assignedTo('{}')".format(assignee_object_id)) | ||
| f = ' and '.join(filters) if filters else None | ||
| assignments = list(assignments_client.list_for_scope(scope=scope, filter=f)) | ||
| elif assignee_object_id: | ||
| if include_groups: | ||
|
|
@@ -529,8 +537,10 @@ def _search_role_assignments(assignments_client, definitions_client, | |
|
|
||
| if assignments: | ||
| assignments = [ra for ra in assignments if ( | ||
| # If no scope, list all assignments | ||
| # If no scope (--all), list all assignments | ||
| not scope or | ||
| # If --at-scope false, list all assignments | ||
| not at_scope or | ||
| # If scope is provided with include_inherited, list assignments at and above the scope. | ||
| # Note that assignments below the scope are already excluded by atScope() | ||
| include_inherited or | ||
|
|
||
209 changes: 209 additions & 0 deletions
209
...azure/cli/command_modules/role/tests/latest/recordings/test_role_assignment_at_scope.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"location": "westus"}' | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| CommandName: | ||
| - identity create | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '22' | ||
| Content-Type: | ||
| - application/json | ||
| ParameterSetName: | ||
| - -g -n --location | ||
| User-Agent: | ||
| - AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.12.10 (Windows-11-10.0.26100-SP0) | ||
| method: PUT | ||
| uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.ManagedIdentity/userAssignedIdentities/clitest000002?api-version=2023-01-31 | ||
| response: | ||
| body: | ||
| string: '{"location":"westus","tags":{},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli_role_assign000001/providers/Microsoft.ManagedIdentity/userAssignedIdentities/clitest000002","name":"clitest000002","type":"Microsoft.ManagedIdentity/userAssignedIdentities","properties":{"tenantId":"54826b22-38d6-4fb2-bad9-b7b93a3e9c5a","principalId":"88c53162-584e-4c98-984b-62149c2e9bca","clientId":"8cc2fab2-7cc6-4863-aae8-9ee986d5a5ac"}}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '449' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| date: | ||
| - Mon, 14 Apr 2025 08:54:23 GMT | ||
| expires: | ||
| - '-1' | ||
| location: | ||
| - /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/cli_role_assign000001/providers/Microsoft.ManagedIdentity/userAssignedIdentities/clitest000002 | ||
| pragma: | ||
| - no-cache | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-cache: | ||
| - CONFIG_NOCACHE | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-ms-operation-identifier: | ||
| - tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/japanwest/90ea6275-c25c-4d73-ba0a-55e6fbe522bb | ||
| x-ms-ratelimit-remaining-subscription-global-writes: | ||
| - '2999' | ||
| x-ms-ratelimit-remaining-subscription-writes: | ||
| - '199' | ||
| x-msedge-ref: | ||
| - 'Ref A: 4359A8C8A1DE4A538CC6C54BFB82E1A0 Ref B: TYO201100114011 Ref C: 2025-04-14T08:54:21Z' | ||
| status: | ||
| code: 201 | ||
| message: Created | ||
| - request: | ||
| body: '{"properties": {"roleDefinitionId": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", | ||
| "principalId": "88c53162-584e-4c98-984b-62149c2e9bca", "principalType": "ServicePrincipal"}}' | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| CommandName: | ||
| - role assignment create | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '270' | ||
| Content-Type: | ||
| - application/json | ||
| ParameterSetName: | ||
| - --assignee-object-id --assignee-principal-type --role --scope | ||
| User-Agent: | ||
| - AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.12.10 (Windows-11-10.0.26100-SP0) | ||
| method: PUT | ||
| uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.Authorization/roleAssignments/88888888-0000-0000-0000-000000000001?api-version=2022-04-01 | ||
| response: | ||
| body: | ||
| string: '{"properties":{"roleDefinitionId":"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7","principalId":"88c53162-584e-4c98-984b-62149c2e9bca","principalType":"ServicePrincipal","scope":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001","condition":null,"conditionVersion":null,"createdOn":"2025-04-14T08:54:25.8958593Z","updatedOn":"2025-04-14T08:54:26.7153899Z","createdBy":null,"updatedBy":"0d504196-1423-4569-9a6e-15149656f0ee","delegatedManagedIdentityResourceId":null,"description":null},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.Authorization/roleAssignments/88888888-0000-0000-0000-000000000001","type":"Microsoft.Authorization/roleAssignments","name":"88888888-0000-0000-0000-000000000001"}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '897' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| date: | ||
| - Mon, 14 Apr 2025 08:54:30 GMT | ||
| expires: | ||
| - '-1' | ||
| pragma: | ||
| - no-cache | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-cache: | ||
| - CONFIG_NOCACHE | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-ms-operation-identifier: | ||
| - tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/japaneast/5aef1660-efc8-4259-ada4-ff3377587bdb | ||
| x-ms-ratelimit-remaining-subscription-global-writes: | ||
| - '2999' | ||
| x-ms-ratelimit-remaining-subscription-writes: | ||
| - '199' | ||
| x-msedge-ref: | ||
| - 'Ref A: 06485B5DC8264B9C917E7D0942EDE412 Ref B: TYO201151002025 Ref C: 2025-04-14T08:54:25Z' | ||
| status: | ||
| code: 201 | ||
| message: Created | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| CommandName: | ||
| - role assignment list | ||
| Connection: | ||
| - keep-alive | ||
| ParameterSetName: | ||
| - --scope --at-scope --assignee-object-id --fill-role-definition-name --fill-principal-name | ||
| User-Agent: | ||
| - AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.12.10 (Windows-11-10.0.26100-SP0) | ||
| method: GET | ||
| uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments?$filter=principalId%20eq%20'88c53162-584e-4c98-984b-62149c2e9bca'&api-version=2022-04-01 | ||
| response: | ||
| body: | ||
| string: '{"value":[{"properties":{"roleDefinitionId":"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7","principalId":"88c53162-584e-4c98-984b-62149c2e9bca","principalType":"ServicePrincipal","scope":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001","condition":null,"conditionVersion":null,"createdOn":"2025-04-14T08:54:26.7153899Z","updatedOn":"2025-04-14T08:54:26.7153899Z","createdBy":"0d504196-1423-4569-9a6e-15149656f0ee","updatedBy":"0d504196-1423-4569-9a6e-15149656f0ee","delegatedManagedIdentityResourceId":null,"description":null},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.Authorization/roleAssignments/88888888-0000-0000-0000-000000000001","type":"Microsoft.Authorization/roleAssignments","name":"88888888-0000-0000-0000-000000000001"}]}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '943' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| date: | ||
| - Mon, 14 Apr 2025 08:54:31 GMT | ||
| expires: | ||
| - '-1' | ||
| pragma: | ||
| - no-cache | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-cache: | ||
| - CONFIG_NOCACHE | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-ms-operation-identifier: | ||
| - tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/japaneast/4f3785c6-7da5-4a37-9e9a-37b877a366e0 | ||
| x-ms-ratelimit-remaining-subscription-global-reads: | ||
| - '3749' | ||
| x-msedge-ref: | ||
| - 'Ref A: F01D790831084190BDA2FD89661CF517 Ref B: TYO201151001054 Ref C: 2025-04-14T08:54:31Z' | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| CommandName: | ||
| - role assignment list | ||
| Connection: | ||
| - keep-alive | ||
| ParameterSetName: | ||
| - --scope --at-scope --assignee-object-id --fill-role-definition-name --fill-principal-name | ||
| User-Agent: | ||
| - AZURECLI/2.71.0 azsdk-python-core/1.31.0 Python/3.12.10 (Windows-11-10.0.26100-SP0) | ||
| method: GET | ||
| uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.Authorization/roleAssignments?$filter=principalId%20eq%20'88c53162-584e-4c98-984b-62149c2e9bca'&api-version=2022-04-01 | ||
| response: | ||
| body: | ||
| string: '{"value":[{"properties":{"roleDefinitionId":"/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7","principalId":"88c53162-584e-4c98-984b-62149c2e9bca","principalType":"ServicePrincipal","scope":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001","condition":null,"conditionVersion":null,"createdOn":"2025-04-14T08:54:26.7153899Z","updatedOn":"2025-04-14T08:54:26.7153899Z","createdBy":"0d504196-1423-4569-9a6e-15149656f0ee","updatedBy":"0d504196-1423-4569-9a6e-15149656f0ee","delegatedManagedIdentityResourceId":null,"description":null},"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli_role_assign000001/providers/Microsoft.Authorization/roleAssignments/88888888-0000-0000-0000-000000000001","type":"Microsoft.Authorization/roleAssignments","name":"88888888-0000-0000-0000-000000000001"}]}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '943' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| date: | ||
| - Mon, 14 Apr 2025 08:54:31 GMT | ||
| expires: | ||
| - '-1' | ||
| pragma: | ||
| - no-cache | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-cache: | ||
| - CONFIG_NOCACHE | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-ms-operation-identifier: | ||
| - tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/japaneast/b891f240-fe35-43fb-a8ef-cd9597db7f7e | ||
| x-ms-ratelimit-remaining-subscription-global-reads: | ||
| - '3749' | ||
| x-msedge-ref: | ||
| - 'Ref A: 0DD953D8592F4F9796E6678A325D0EAB Ref B: TYO201151002025 Ref C: 2025-04-14T08:54:32Z' | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am hesitating on whether
--at-scopeshould be exposed byaz role assignment delete, as setting--at-scope falsewill include subscope assignments, causing accidental deletion.