Skip to content

[azure-mgmt-core] is_valid_resource_id returns True for invalid resource IDs #11658

@jiasli

Description

@jiasli

Is your feature request related to a problem? Please describe.
In Azure/azure-cli#10641 (comment), Azure CLI receives an incorrect --scope /subscriptions/{subscription_id}/resourceGroups/ which lacks the resource group name. It gets injected into the resource URL, thus corrupting the command.

In order to fix it, I try to use azure.mgmt.core.tools.is_valid_resource_id to detect whether a scope is valid:

def is_valid_resource_id(rid, exception_type=None):

Unfortunately, is_valid_resource_id can't return the correct value for some edge cases like this.

A code snippet:

from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id

res_ids = ['/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/rg1',
           '/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups',
           '/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/',
           '/subscriptions//resourceGroups/']

for res_id in res_ids:
    print(res_id)
    print(parse_resource_id(res_id))
    print(is_valid_resource_id(res_id))
    print()

Output:

/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/rg1
{'subscription': '0b1f6471-1bf0-4dda-aec3-cb9272f09590', 'resource_group': 'rg1'}
True

/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups
{'subscription': '0b1f6471-1bf0-4dda-aec3-cb9272f09590'}
False

# The trailing slash makes is_valid_resource_id return True
/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/
{'subscription': '0b1f6471-1bf0-4dda-aec3-cb9272f09590', 'resource_group': ''}
True

# Empty subscription ID shouldn't be allowed
/subscriptions//resourceGroups/
{'subscription': '', 'resource_group': ''}
True

Describe the solution you'd like

  1. No empty name '' should be allowed when parsing a resource ID
  2. is_valid_resource_id should return False for /subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/

Metadata

Metadata

Assignees

Labels

Azure.Mgmt.CoreAzure management coreMgmtThis issue is related to a management-plane library.bugThis issue requires a change to an existing behavior in the product in order to be resolved.issue-addressedWorkflow: The Azure SDK team believes it to be addressed and ready to close.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions