question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KustoClient requesting a new token on every request when using Managed Service Identity

See original GitHub issue

Code Sample, a copy-pastable example if possible

cluster_url = "myclusterurl"
kcsb = KustoConnectionStringBuilder.with_aad_managed_service_identity_authentication(           cluster_url)
client = KustoClient(kcsb)

# Run a query. A call to the /msi/token endpoint is made.
# Then, a few lines below, run another query. Another call to /msi/token endpoint is made.
# Then, again, another query. Another call to /msi/token endpoint is made.

Problem description

I have a Python Azure Function which is instantiating a new kusto client using managed service identity and running some queries with it.

The problem is that the library is requesting a token on each call instead of just on the first one and then reusing the token. In my logs I can see the following request being made before every query I make on the cluster:

GET http://localhost:8081/msi/token/?resource=https://myclustername.westus2.kusto.windows.net&api-version=2017-09-01 [200]

Every once in a while I get the following error:

MSI: Failed to retrieve a token from 'http://localhost:8081/msi/token/?resource=https://myclustername.westus2.kusto.windows.net&api-version=2017-09-01' with an error of '{"statusCode":503,"message":"An unexpected error occured while fetching the AAD Token.","correlationId":"431d234c-678d-4ce7-be3a-06da7482b547"}'.

Azure supports says this error is caused by a lot of requests to that endpoint in a short period of time.

Just to clarify, the library is requesting a new token before every kusto query call inside the same function invocation. So this is not an issue caused by multiple function invocations instantiating a new kusto client and exceeding the MSI token endpoint rate limit.

Output of pip freeze

adal==1.2.2 astroid==2.3.3 autopep8==1.5 azure-common==1.1.25 azure-core==1.2.1 azure-functions==1.2.0 azure-kusto-data==0.0.44 azure-kusto-ingest==0.0.44 azure-storage-blob==2.1.0 azure-storage-common==2.1.0 azure-storage-queue==2.1.0 certifi==2019.11.28 cffi==1.13.2 chardet==3.0.4 colorama==0.4.3 cryptography==2.8 idna==2.8 isodate==0.6.0 isort==4.3.21 lazy-object-proxy==1.4.3 mccabe==0.6.1 msrest==0.6.11 msrestazure==0.6.2 numpy==1.18.2 oauthlib==3.1.0 pandas==1.0.3 pycodestyle==2.5.0 pycparser==2.19 PyJWT==1.7.1 pylint==2.4.4 python-dateutil==2.8.1 pytz==2019.3 requests==2.22.0 requests-oauthlib==1.3.0 sentry-sdk==0.14.3 six==1.14.0 tenacity==6.1.0 typed-ast==1.4.1 urllib3==1.25.8 wrapt==1.11.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
yogiladcommented, May 5, 2020

Update: I opened an issue with msrestazure () to see if and how the tokens can be cached by them. As discussed, I’ll provide an option to authenticate with a token provider, which will allow to workaround the issue.

0reactions
yogiladcommented, Jul 12, 2020

In version 1.0.0 we switched to azure.identity library which caches the tokens. This should resolve this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authenticate with Azure AD for access - Azure Data Explorer
In the first step, the client: Communicates with the Azure AD service. Authenticates to the Azure AD service. Requests an access token issued ......
Read more >
Options for obtaining an access token with Azure application ...
In this post we will take a look at a few different options that we can use, to get an access token so...
Read more >
OAuth 2.0 with Managed Identities - AzToso.com
The client application authenticates to the Azure AD token issuance endpoint and requests an access token. The Azure AD token issuance endpoint ...
Read more >
Access Azure AD protected API with managed Identity
Managed identities are a great way to improve the security of your application. ... Request a token using the managed identity.
Read more >
kusto - Go Packages
Every query is done using a Stmt. A Stmt is built with Go string ... using an application token obtained from a Microsoft...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found