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

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

Merged
merged 3 commits into from
Jun 18, 2020
Merged

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

merged 3 commits into from
Jun 18, 2020

Conversation

arrownj
Copy link
Contributor

@arrownj arrownj commented Apr 22, 2020

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.

@arrownj arrownj requested a review from jiasli as a code owner April 22, 2020 07:05
@arrownj arrownj self-assigned this Apr 22, 2020
@yonzhan
Copy link
Collaborator

yonzhan commented Apr 22, 2020

az ad

@yonzhan yonzhan added this to the S169 - For Build milestone Apr 22, 2020
src/azure-cli/azure/cli/command_modules/role/custom.py Outdated Show resolved Hide resolved
@@ -495,6 +495,9 @@ def _build_role_scope(resource_group_name, scope, subscription_id):
if resource_group_name:
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
Member

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?

Copy link
Contributor Author

@arrownj arrownj Jun 17, 2020

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.

@yonzhan yonzhan requested a review from qianwens June 17, 2020 10:56
@@ -495,6 +495,9 @@ def _build_role_scope(resource_group_name, scope, subscription_id):
if resource_group_name:
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
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
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 arrownj merged commit 4c28536 into Azure:dev Jun 18, 2020
@arrownj 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
4 participants