SubscriptionClient does not include tags
- Package Name: azure-mgmt-resource
- Package Version: azure-mgmt-resource==20.0.0
- Operating System: Python docker image
- Python Version: 3.9.6
Describe the bug The SubscriptionClient does not return tags
To Reproduce Steps to reproduce the behavior:
- Navigate to this website: https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate-service-principals
- Follow the example here: https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate-service-principals#authenticate-using-environment-variables
- for each subscription returned in the list try
print(subscription.tags)and witness it say None.
In [26]: for sub in sub_list:
...: print(sub.tags)
...:
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
Expected behavior I would expect to see key value listing of the tags I've assigned on to each subscription.
thanks @fxdgear for reaching out! we'll investigate it ASAP.
@yunhaoling Any progress on this?
or at least a suggestion for a workaround?
Looking at the docs and community posts, it's not clear how to do things in the MS ecosystem.
I'm currently blocked by not being able to list subscriptions with their tags.
@fxdgear apologize that I'm not familiar with the subscription mgmt sdk..
@msyyc @SaurabhSharma-MSFT could you take a look and give suggestion?
@fxdgear Do you have any tags assigned to these subscriptions. I have tested this in my environment by adding some tags to the subscription and I am getting back the results. (Please check below)
Result:

Yes I do.
I didn't include a screenshot in the issue.
Is there else I can help debug this issue? Is there a permission I need to ensure exists?
What is the minimum required permissions needed to make this work?
This service account does one thing and one thing only. Read subs and their tags. It doesn't need and shouldn't need excess permissions.

Screenshot showing that there's a tag on this subscription.
FYI the role assignment for my app is "Reader".
Is there a different role I should be using?
I've since added Tag Contributer to my service principal

But now when I try to use the above example I am now getting the following error:
AttributeError: 'EnvironmentCredential' object has no attribute 'signed_session'
I've even added Owner now to my service principal and I'm still getting nothing for tags.
Since I've started getting errors using the EnvironmentCredential I've switched over to using the ServicePrincipalCredentials: https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate-service-principals#serviceprincipalcredentials-azurecommon
AND I'm still getting None for the tags.
@fxdgear I have given contributor access to the service principal but it worked with only Tag Contributor access as well.
Can you check with only Reader?
I've changed my service principal to only Tag Contributer and I'm still getting None for tags.
Python 3.9.6 (default, Jun 29 2021, 19:18:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.30.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from azure.identity import EnvironmentCredential
...: from azure.mgmt.resource import SubscriptionClient
...:
...: credential = EnvironmentCredential()
...: subscription_client = SubscriptionClient(credential)
...:
...: sub_list = subscription_client.subscriptions.list()
In [2]: for sub in sub_list:
...: print(sub.tags)
...:
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
when I use the az cli tool I can get the tag for a specific sub:
❯ az tag list --resource-id /subscriptions/fc58a142-2e74-48ff-974c-cde6e008f8f5
{
"id": "/subscriptions/fc58a142-2e74-48ff-974c-cde6e008f8f5/providers/Microsoft.Resources/tags/default",
"name": "default",
"properties": {
"tags": {
"test": "bar"
}
},
"type": "Microsoft.Resources/tags"
}
When I then change my python script to only look for the previoysly mentioned sub:
In [5]: for sub in sub_list:
...: if sub.id == "/subscriptions/fc58a142-2e74-48ff-974c-cde6e008f8f5":
...: print(sub.tags)
...:
None
There is clearly something different between my environment and your environment. I'm trying to understand what this difference is.
@fxdgear What is the OS version of your docker image you are using?
I am using the default python3.9 base image from DockerHub.
FROM python:3.9
RUN pip install boto3 ipdb azure-mgmt-subscription azure-identity azure-mgmt-resource google-cloud-resource-manager
edit: let me be more clear.
@fxdgear Thanks, I have checked with Reader access, and it worked fine to me. I am however using Python 3.7.9 but I don't think that is going to make much difference. Also, azure-mgmt-resource is 20.0.0 and azure-mgmt-subscription version is 2.0.0.
Dockerfile:
FROM python:3.7
RUN pip install boto3 ipdb azure-mgmt-subscription azure-identity azure-mgmt-resource google-cloud-resource-manager
WORKDIR /app
COPY . ./
CMD ["python", "azure_utils.py"]
azure_utils.py:
from azure.identity import EnvironmentCredential
from azure.mgmt.resource import SubscriptionClient
credential = EnvironmentCredential()
subscription_client = SubscriptionClient(credential)
sub_list = subscription_client.subscriptions.list()
for sub in sub_list:
print(sub.tags)
run command: docker build -t azure-fails:latest .
then run docker run --rm -it -e AZURE_CLIENT_SECRET=<client_secret> -e AZURE_TENANT_ID=<tenant_id> -e AZURE_CLIENT_ID=<client_id> azure-fails:latest
When I run the following I get
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
as my output.
This says that
- my auth is correct, cause I am able to get a list of subscriptions.
- confirms my issue cause the tags are all None.
I have verified above with
- a screen shot that I should have AT LEAST 1 tag.
- a copy of my CLI output that verifies that there is a tag on at least 1 subscription.
@SaurabhSharma-MSFT So what I've done now is updated my python script to be like the following:
for sub in sub_list:
print(subscription_client.subscriptions.get(sub.subscription_id).tags)
And by fetching each sub individually i can get the tags.
{'team': 'Cloud'}
{'team': 'Observability'}
{'team': 'Stack'}
This seems like incorrect behavior. I shouldn't have to make an additional request for each subscription to fetch the tags. They should show up in the original request to the sub list.
But the tags do not show up when i am iterating through the object list.