Skip to content

AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token' on resource_groups.list() #16908

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

Closed
alfredopalhares opened this issue Feb 24, 2021 · 4 comments
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. Resource Authorization Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@alfredopalhares
Copy link

  • Package Name: azure.mgmt.resource
  • Package Version: 15.0.0
  • Operating System: Archlinux
  • Python Version: 3.9.1

Describe the bug
Attribute Error duing resource list

To Reproduce
Steps to reproduce the behavior:
main.py:

#!/usr/bin/python

import os
import json
from datetime import datetime
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient

### Gets Azure resource groups and virtual machines
# I assume the following variables exist
# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain
# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id

def main():
    subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
    client_id = os.environ.get("AZURE_CLIENT_ID")
    client_secret = os.environ.get("AZURE_CLIENT_SECRET")
    tenant_id = os.environ.get("AZURE_TENANT_ID")

    credentials = ServicePrincipalCredentials(
        client_id = client_id,
        secret = client_secret,
        tenant = tenant_id
    )

    print(credentials)
    client = ResourceManagementClient(credentials, subscription_id)

    print(client.resource_groups.list())
    for item in client.resource_groups.list():
        print(item)

main()

This produces:

<msrestazure.azure_active_directory.ServicePrincipalCredentials object at 0x7f556e3dc520>
<iterator object azure.core.paging.ItemPaged at 0x7f556d067790>
Traceback (most recent call last):
  File "/home/masterkorp/Documents/RenaltyXAi/repos/docker-grafana/./get-az-resouces.py", line 35, in <module>
    main()
  File "/home/masterkorp/Documents/RenaltyXAi/repos/docker-grafana/./get-az-resouces.py", line 32, in main
    for item in client.resource_groups.list():
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/paging.py", line 129, in __next__
    return next(self._page_iterator)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/paging.py", line 76, in __next__
    self._response = self._get_next(self.continuation_token)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/mgmt/resource/resources/v2020_06_01/operations/_resource_groups_operations.py", line 580, in get_next
    pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 211, in run
    return first_node.send(pipeline_request)  # type: ignore
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/mgmt/core/policies/_base.py", line 47, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  [Previous line repeated 1 more time]
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_redirect.py", line 158, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_retry.py", line 435, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 69, in send
    _await_result(self._policy.on_request, request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_tools.py", line 29, in await_result
    result = func(*args, **kwargs)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_authentication.py", line 93, in on_request
    self._token = self._credential.get_token(*self._scopes)
AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'

Expected behavior
Should print the item without crashing.

Additional context
I am following the example in https://github.com/Azure-Samples/resource-manager-python-resources-and-groups/blob/master/example.py#L48

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 24, 2021
@xiangyan99 xiangyan99 added bug This issue requires a change to an existing behavior in the product in order to be resolved. Mgmt This issue is related to a management-plane library. labels Feb 24, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Feb 24, 2021
@xiangyan99 xiangyan99 added the Service Attention Workflow: This issue is responsible by Azure service team. label Feb 24, 2021
@chlowell
Copy link
Member

chlowell commented Feb 24, 2021

Thanks for opening this issue! That sample code is out of date (@jongio, are you still tracking old samples?). The latest azure-mgmt-resource (15.x) expects a credential from azure-identity, whose ClientSecretCredential is the equivalent of azure.common.credentials.ServicePrincipalCredentials. It takes the same information but its parameters are slightly different:

import os
from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient

credential = ClientSecretCredential(
    tenant_id=os.environ["AZURE_TENANT_ID"],
    client_id=os.environ["AZURE_CLIENT_ID"],
    client_secret=os.environ["AZURE_CLIENT_SECRET"]
)

client = ResourceManagementClient(credential, os.environ["AZURE_SUBSCRIPTION_ID"])

print(client.resource_groups.list())
for item in client.resource_groups.list():
    print(item)

I showed creating ClientSecretCredential above to demonstrate the difference vs. ServicePrincipalCredentials. However, using EnvironmentCredential is simpler (it will read the same variables):

from azure.identity import EnvironmentCredential

credential = EnvironmentCredential()

(Related: #14919)

@xiangyan99 xiangyan99 added Resource Authorization and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 24, 2021
@ghost
Copy link

ghost commented Feb 24, 2021

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @darshanhs90, @AshishGargMicrosoft.

Issue Details
  • Package Name: azure.mgmt.resource
  • Package Version: 15.0.0
  • Operating System: Archlinux
  • Python Version: 3.9.1

Describe the bug
Attribute Error duing resource list

To Reproduce
Steps to reproduce the behavior:
main.py:

#!/usr/bin/python

import os
import json
from datetime import datetime
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient

### Gets Azure resource groups and virtual machines
# I assume the following variables exist
# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain
# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id

def main():
    subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
    client_id = os.environ.get("AZURE_CLIENT_ID")
    client_secret = os.environ.get("AZURE_CLIENT_SECRET")
    tenant_id = os.environ.get("AZURE_TENANT_ID")

    credentials = ServicePrincipalCredentials(
        client_id = client_id,
        secret = client_secret,
        tenant = tenant_id
    )

    print(credentials)
    client = ResourceManagementClient(credentials, subscription_id)

    print(client.resource_groups.list())
    for item in client.resource_groups.list():
        print(item)

main()

This produces:

<msrestazure.azure_active_directory.ServicePrincipalCredentials object at 0x7f556e3dc520>
<iterator object azure.core.paging.ItemPaged at 0x7f556d067790>
Traceback (most recent call last):
  File "/home/masterkorp/Documents/RenaltyXAi/repos/docker-grafana/./get-az-resouces.py", line 35, in <module>
    main()
  File "/home/masterkorp/Documents/RenaltyXAi/repos/docker-grafana/./get-az-resouces.py", line 32, in main
    for item in client.resource_groups.list():
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/paging.py", line 129, in __next__
    return next(self._page_iterator)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/paging.py", line 76, in __next__
    self._response = self._get_next(self.continuation_token)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/mgmt/resource/resources/v2020_06_01/operations/_resource_groups_operations.py", line 580, in get_next
    pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 211, in run
    return first_node.send(pipeline_request)  # type: ignore
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/mgmt/core/policies/_base.py", line 47, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  [Previous line repeated 1 more time]
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_redirect.py", line 158, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_retry.py", line 435, in send
    response = self.next.send(request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 69, in send
    _await_result(self._policy.on_request, request)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/_tools.py", line 29, in await_result
    result = func(*args, **kwargs)
  File "/home/masterkorp/.local/lib/python3.9/site-packages/azure/core/pipeline/policies/_authentication.py", line 93, in on_request
    self._token = self._credential.get_token(*self._scopes)
AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'

Expected behavior
Should print the item without crashing.

Additional context
I am following the example in https://github.com/Azure-Samples/resource-manager-python-resources-and-groups/blob/master/example.py#L48

Author: alfredopalhares
Assignees: msyyc
Labels:

Mgmt, Resource Authorization, Service Attention, bug, customer-reported

Milestone: -

@jongio
Copy link
Member

jongio commented Mar 1, 2021

I'm looking into this for an update.

@jsntcy
Copy link
Member

jsntcy commented Apr 13, 2021

Close this as it's duplicated issue with #14919.

@jsntcy jsntcy closed this as completed Apr 13, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. Resource Authorization Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

6 participants