0

I'm using python sdk to query application insights using azure-applicationinsights==0.1.1. Here is my code:

from azure.identity import DefaultAzureCredential
from azure.applicationinsights import ApplicationInsightsDataClient
from azure.applicationinsights.models import QueryBody

def query_app_insights():
    query = 'myEvents | take 10'
    application = 'my-app'
    creds = DefaultAzureCredential()
    client = ApplicationInsightsDataClient(creds)
    result = client.query.execute(application, QueryBody(query=query))

I get this error:

AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session'

also tried:

    creds = ClientSecretCredential(tenant_id='something',
                                   client_id='something',
                                   client_secret='something')
    client = ApplicationInsightsDataClient(creds)

It gives me the same error.

also I tried:

from azure.identity import DefaultAzureCredential
from azure.applicationinsights import ApplicationInsightsDataClient
from azure.applicationinsights.models import QueryBody
from azure.common.credentials import ServicePrincipalCredentials

def query_app_insights():
    query = 'myEvents | take 10'
    application = 'my-app'
    creds = ServicePrincipalCredentials(tenant='something',
                                        client_id='something',
                                        secret='something')
    client = ApplicationInsightsDataClient(creds)
    result = client.query.execute(application, QueryBody(query=query))

It complaints about my secret, but it is correct. Any ideas?

max
  • 9,708
  • 15
  • 89
  • 144
  • Does this post help fix the issue? https://stackoverflow.com/questions/63384092/exception-attributeerror-defaultazurecredential-object-has-no-attribute-sig – Priya Dec 13 '22 at 00:52
  • Nope. It results in this error: `azure.applicationinsights.models.error_response_py3.ErrorResponseException: (InvalidTokenError) The provided authentication is not valid for this resource` – max Dec 13 '22 at 02:32

1 Answers1

0

AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session':

This error occurs frequently these days and I've also faced this couple of times. It actually means that "no signed session available for the given tenant".

After I've done a workaround, I found there is some issue addressed here with the SDK management libraries.

The DefaultAzureCredential class has been modified, and it no longer has the'signed session' attribute in few versions. To handle this, the most recent versions of the management libraries should be upgraded.

Need to check & resolve:

  • Installed the latest versions of the modules when you are importing in Python code.

     azure-identity 1.12.0
     azure-applicationinsights 0.1.1
    
  • Selected python 3.10 interpreter in visual studio code and ran the same code as yours in a virtual environment.

  • Created a virtual env and ran the project.

      py -m venv env
    .\env\Scripts\activate 
    
  • Sometimes requirements.txt file will not get properly configured, In that scenario also you may face this error.

    To avoid that, Freeze the requirements by using terminal: pip freeze requirements.txt

After meeting all the above conditions, I tried in my environment and was able to proceed without any attribute errors.

Output:

enter image description here

Alternatively, I tried connecting with clientsecretcredential by registering to a new Application in AzureAD.

Note: Prefer to create a new registration instead of using old registrations as client_secrets might be expired.

TENANT_ID = '<TENANT_ID>'
CLIENT_ID = '<CLIENT_ID>'
CLIENT_SECRET = '<CLIENT_SECRET>'
SUBSCRIPTION_ID = '<SUBSCRIPTION_ID>'
from  azure.identity  import  ClientSecretCredential
from  azure.mgmt.keyvault  import  KeyVaultManagementClient
credentials = ClientSecretCredential(tenant_id=TENANT_ID, client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
credentialskeyvault = KeyVaultManagementClient(credentials, SUBSCRIPTION_ID)
print ("Logged")

Output:

enter image description here

To determine the precise status:

I simply deleted specific "rights/permissions" from my application in AzureAD and tested whether it threw an error when logged in.

Why because?

This error is usually thrown only after the user logged in. It states that the defaultazurecredential object was running without problem.

enter image description here

If the issue still persists, then you can wrap credentials by installing msrestazure module and create a credentialwrapperclass detailed by @lmazuel.

Jahnavi
  • 3,076
  • 1
  • 3
  • 10