-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[REST] Fix #14152: az rest: Accept ARM URLs without subscription ID
#14370
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -735,16 +735,17 @@ def send_raw_request(cli_ctx, method, url, headers=None, uri_parameters=None, # | |||
| # Replace common tokens with real values. It is for smooth experience if users copy and paste the url from | ||||
| # Azure Rest API doc | ||||
| from azure.cli.core._profile import Profile | ||||
| profile = Profile() | ||||
| profile = Profile(cli_ctx=cli_ctx) | ||||
|
Contributor
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. Pass the existing
|
||||
| if '{subscriptionId}' in url: | ||||
| url = url.replace('{subscriptionId}', cli_ctx.data['subscription_id'] or profile.get_subscription_id()) | ||||
|
|
||||
| # Prepare the Bearer token for `Authorization` header | ||||
| if not skip_authorization_header and url.lower().startswith('https://'): | ||||
| # Prepare `resource` | ||||
| # Prepare `resource` for `get_raw_token` | ||||
| if not resource: | ||||
| # If url starts with ARM endpoint, like https://management.azure.com/, | ||||
| # use active_directory_resource_id for resource. | ||||
| # This follows the same behavior as azure.cli.core.commands.client_factory._get_mgmt_service_client | ||||
| # If url starts with ARM endpoint, like `https://management.azure.com/`, | ||||
| # use `active_directory_resource_id` for resource, like `https://management.core.windows.net/`. | ||||
| # This follows the same behavior as `azure.cli.core.commands.client_factory._get_mgmt_service_client` | ||||
| if url.lower().startswith(endpoints.resource_manager.rstrip('/')): | ||||
| resource = endpoints.active_directory_resource_id | ||||
| else: | ||||
|
|
@@ -758,11 +759,15 @@ def send_raw_request(cli_ctx, method, url, headers=None, uri_parameters=None, # | |||
| resource = value | ||||
| break | ||||
| if resource: | ||||
| # If this is an ARM request, extract subscription ID from the URL. | ||||
| # In the future when multi-tenant subscription is supported, we won't be able to uniquely identity the token | ||||
| # from subscription anymore. | ||||
| # Prepare `subscription` for `get_raw_token` | ||||
| # If this is an ARM request, try to extract subscription ID from the URL. | ||||
| # But there are APIs which don't require subscription ID, like /subscriptions, /tenants | ||||
| # TODO: In the future when multi-tenant subscription is supported, we won't be able to uniquely identify | ||||
| # the token from subscription anymore. | ||||
| token_subscription = None | ||||
| if url.lower().startswith(endpoints.resource_manager.rstrip('/')): | ||||
| token_subscription = _extract_subscription_id(url) | ||||
| if token_subscription: | ||||
|
jsntcy marked this conversation as resolved.
|
||||
| logger.debug('Retrieving token for resource %s, subscription %s', resource, token_subscription) | ||||
| token_info, _, _ = profile.get_raw_token(resource, subscription=token_subscription) | ||||
| else: | ||||
|
|
@@ -804,9 +809,12 @@ def _extract_subscription_id(url): | |||
| """Extract the subscription ID from an ARM request URL.""" | ||||
| subscription_regex = '/subscriptions/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})' | ||||
| match = re.search(subscription_regex, url, re.IGNORECASE) | ||||
| if not match: | ||||
| raise CLIError('No subscription ID specified in the URL') | ||||
| return match.groups()[0] | ||||
| if match: | ||||
| subscription_id = match.groups()[0] | ||||
| logger.debug('Found subscription ID %s in the URL %s', subscription_id, url) | ||||
| return subscription_id | ||||
| logger.debug('No subscription ID specified in the URL %s', url) | ||||
| return None | ||||
|
Comment on lines
+816
to
+817
Contributor
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. Stop raising an exception but return |
||||
|
|
||||
|
|
||||
| def _log_request(request): | ||||
|
|
||||
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.
Unit tests are added for APIs without subscription ID.