Skip to content
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

AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token' #16923

Closed
sanju918 opened this issue Feb 25, 2021 · 3 comments
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@sanju918
Copy link

sanju918 commented Feb 25, 2021

Script for China cloud
I'm getting the error AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token' what am I doing wrong.

from azure.common.credentials import ServicePrincipalCredentials
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD
from azure.mgmt.resource.resources import ResourceManagementClient
from my_az_keyvault import KeyVault

def get_cred():
    china_tenant_id = 'xyz'
    china_cloud_mgr_id = 'abcd'
    vault = KeyVault(vault_name='my-valut')

    china_cloud_key = vault.get('azure-china-resource-reader')
    resource_mgr_cred = ServicePrincipalCredentials(
        client_id=china_cloud_mgr_id,
        secret=china_cloud_key,
        tenant=china_tenant_id,
        china=True,
        base_url=AZURE_CHINA_CLOUD.endpoints.resource_manager,
        credential_scope=[AZURE_CHINA_CLOUD.endpoints.resource_manager + "/.default"]
    )
    return resource_mgr_cred

def get_tags_subs(subscription_id):
    sub_scope = 'subscriptions/' + subscription_id
    resource_mgmt_client = ResourceManagementClient(
        credential=get_cred(),
        subscription_id=subscription_id,
        base_url=AZURE_CHINA_CLOUD.endpoints.resource_manager
    )

    tags = resource_mgmt_client.tags.get_at_scope(sub_scope)
    return tags


def main():
    print(get_tags_subs('abcd-efgh-igk-lmnopqr'))


if __name__ == '__main__':
    main()

These are the requirements I installed

my-az-keyvault==1.0.0.post332
azure-mgmt-subscription==0.6.0
azure-mgmt-resource>=1.15.0

Complete error

Retrieving Secret: 'azure-china-resource-reader' with parameters:
{
  "version": null
}
Traceback (most recent call last):
  File "main.py", line 80, in <module>
    main()
  File "main.py", line 76, in main
    print(get_tags_subs('abcd-efgh-igk-lmnopqr'))
  File "main.py", line 71, in get_tags_subs
    tags = resource_mgmt_client.tags.get_at_scope(sub_scope)
  File "/Users/sanjayq/azure/Azure-China/env/lib/python3.7/site-packages/azure/mgmt/resource/resources/v2020_06_01/operations/_tags_operations.py", line 541, in get_at_scope
    pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 211, in run
    return first_node.send(pipeline_request)  # type: ignore
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/mgmt/core/policies/_base.py", line 47, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send
    response = self.next.send(request)
  [Previous line repeated 1 more time]
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/policies/_redirect.py", line 158, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/policies/_retry.py", line 435, in send
    response = self.next.send(request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 69, in send
    _await_result(self._policy.on_request, request)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/site-packages/azure/core/pipeline/_tools.py", line 29, in await_result
    result = func(*args, **kwargs)
  File "/Users/sanjay/azure/Azure-China/env/lib/python3.7/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'

@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 25, 2021
@xiangyan99 xiangyan99 added Mgmt This issue is related to a management-plane library. Service Attention Workflow: This issue is responsible by Azure service team. labels Feb 25, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Feb 25, 2021
@xiangyan99
Copy link
Member

Thanks for the feedback, we’ll investigate asap.

@chlowell
Copy link
Member

The latest azure-mgmt-resource (15.x) expects a credential from azure-identity, whose ClientSecretCredential is the equivalent of azure.common.credentials.ServicePrincipalCredentials. You will also need to configure the credential and client to use Azure China Cloud. The relevant snippets of your code above would look something like this afterward:

from azure.identity import ClientSecretCredential

credential = ClientSecretCredential(
    tenant_id=china_tenant_id,
    client_id=china_cloud_mgr_id,
    client_secret=china_cloud_key,
    authority=AZURE_CHINA_CLOUD.endpoints.active_directory
)

resource_mgmt_client = ResourceManagementClient(
    credential=credential,
    subscription_id=subscription_id,
    base_url=AZURE_CHINA_CLOUD.endpoints.resource_manager,
    credential_scopes=[AZURE_CHINA_CLOUD.endpoints.active_directory_resource_id + "/.default"]
)

(Related: #14919, #16908)

@jsntcy
Copy link
Member

jsntcy commented Apr 13, 2021

Close this as solution is provided.

@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
customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

6 participants