azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

SubscriptionClient does not include tags

Open fxdgear opened this issue 4 years ago • 35 comments

  • 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:

  1. Navigate to this website: https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate-service-principals
  2. Follow the example here: https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate-service-principals#authenticate-using-environment-variables
  3. 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.

fxdgear avatar Dec 15 '21 04:12 fxdgear

thanks @fxdgear for reaching out! we'll investigate it ASAP.

yunhaoling avatar Dec 15 '21 18:12 yunhaoling

@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 avatar Dec 16 '21 15:12 fxdgear

@fxdgear apologize that I'm not familiar with the subscription mgmt sdk..

@msyyc @SaurabhSharma-MSFT could you take a look and give suggestion?

yunhaoling avatar Dec 16 '21 18:12 yunhaoling

@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) image Result: image

SaurabhSharma-MSFT avatar Dec 17 '21 01:12 SaurabhSharma-MSFT

Yes I do.

I didn't include a screenshot in the issue.

fxdgear avatar Dec 17 '21 01:12 fxdgear

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.

fxdgear avatar Dec 17 '21 03:12 fxdgear

Screenshot from 2021-12-17 10-08-11

Screenshot showing that there's a tag on this subscription.

fxdgear avatar Dec 17 '21 17:12 fxdgear

FYI the role assignment for my app is "Reader".

Is there a different role I should be using?

fxdgear avatar Dec 17 '21 17:12 fxdgear

I've since added Tag Contributer to my service principal

Screenshot from 2021-12-17 10-55-17

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'

fxdgear avatar Dec 17 '21 17:12 fxdgear

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 avatar Dec 17 '21 18:12 fxdgear

@fxdgear I have given contributor access to the service principal but it worked with only Tag Contributor access as well.

SaurabhSharma-MSFT avatar Dec 17 '21 19:12 SaurabhSharma-MSFT

Can you check with only Reader?

fxdgear avatar Dec 17 '21 19:12 fxdgear

I've changed my service principal to only Tag Contributer and I'm still getting None for tags.

fxdgear avatar Dec 17 '21 20:12 fxdgear

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

fxdgear avatar Dec 17 '21 20:12 fxdgear

There is clearly something different between my environment and your environment. I'm trying to understand what this difference is.

fxdgear avatar Dec 17 '21 20:12 fxdgear

@fxdgear What is the OS version of your docker image you are using?

SaurabhSharma-MSFT avatar Dec 21 '21 03:12 SaurabhSharma-MSFT

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 avatar Dec 21 '21 03:12 fxdgear

@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.

SaurabhSharma-MSFT avatar Dec 21 '21 03:12 SaurabhSharma-MSFT

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

  1. my auth is correct, cause I am able to get a list of subscriptions.
  2. confirms my issue cause the tags are all None.

I have verified above with

  1. a screen shot that I should have AT LEAST 1 tag.
  2. a copy of my CLI output that verifies that there is a tag on at least 1 subscription.

fxdgear avatar Dec 21 '21 03:12 fxdgear

@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.

fxdgear avatar Dec 21 '21 16:12 fxdgear