Skip to content

[RBAC] az ad sp create-for-rbac: refine error message when user specify an invalid scope - #13117

Merged
Xiaojian Xu (arrownj) merged 3 commits into
Azure:devfrom
arrownj:refine_message_for_invalid_scope
Jun 18, 2020
Merged

Xiaojian Xu (arrownj) merged 3 commits into
Azure:devfrom
arrownj:refine_message_for_invalid_scope

Conversation

@arrownj

Copy link
Copy Markdown
Contributor

Description
This PR is to fix #7441

Previously if user specify scope with an invalid resource id, CLI will prompt a strange error message just as described in the issue.

We add a check whether it is a valid resource id here.

The reason why I didn't combine it with scope == '' is that I don't want to change previous check sequence.

Testing Guide
az ad sp create-for-rbac --name myTestSP --role contributor --scopes /subscriptions/<subscription>/<resource_group>

Prompt below error:
Invalid scope. Please use --help to view the valid format.

History Notes

[Component Name 1] BREAKING CHANGE: az command a: Make some customer-facing breaking change.
[Component Name 2] az command b: Add some customer-facing feature.


This checklist is used to make sure that common guidelines for a pull request are followed.

@yonzhan

Copy link
Copy Markdown
Collaborator

az ad

@yonzhan Yong Zhang (yonzhan) added this to the S169 - For Build milestone Apr 22, 2020
Comment thread src/azure-cli/azure/cli/command_modules/role/custom.py Outdated
err = 'Resource group "{}" is redundant because scope is supplied'
raise CLIError(err.format(resource_group_name))
from msrestazure.tools import is_valid_resource_id
if scope.startswith('/subscriptions/') and not is_valid_resource_id(scope):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can / be used as scope?

@arrownj Xiaojian Xu (arrownj) Jun 17, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. If the user specify scope as /, CLI will try to create a role assignment under the global resource provider /providers/Microsoft.Authorization/roleAssignments/{uuid} instead of /subscriptions/{uuid}/providers/Microsoft.Authorization/roleAssignments/{uuid}. Normally server will return error which tells "does not have authorization to perform ... or scope is invalid".

I'm wondering whether there are already some users who take this benefit to create role assignments under global resource provider. So I think it's better to keep this and rely on the service side to do the validation.

Co-authored-by: Jiashuo Li <jiasli@microsoft.com>
err = 'Resource group "{}" is redundant because scope is supplied'
raise CLIError(err.format(resource_group_name))
from azure.mgmt.core.tools import is_valid_resource_id
if scope.startswith('/subscriptions/') and not is_valid_resource_id(scope):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if scope.startswith('/subscriptions/') [](start = 8, length = 38)

why do you check scope.startswith('/subscriptions/') first, can the scope has other value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in some cases, user only provide /provides/... as scope value.

@arrownj
Xiaojian Xu (arrownj) merged commit 4c28536 into Azure:dev Jun 18, 2020
@arrownj
Xiaojian Xu (arrownj) deleted the refine_message_for_invalid_scope branch July 29, 2020 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[role] Confusing service error caused by invalid --scope argument: The api-version '2018-01-01-preview' is invalid.

4 participants