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.

Cannot get InteractiveBrowserCredential() to cache credentials

See original GitHub issue
  • Package Name: azure-identity
  • Package Version: 1.7.0
  • Operating System: Windows 11
  • Python Version: 3.8.0

Describe the bug Using InteractiveBrowserCredential() with enable_persistent_cache=True does not seem to cache the credentials over subsequent runs (even for as little as 30 seconds). When I also specify the authenication_record parameter, it still prompts for credentials in the browse on each run.

To Reproduce Steps to reproduce the behavior:

Run the following python program:

# auth_with_cach_test.py: test out ability to get credentials without having to authenticate each time
import os
from azure.identity import InteractiveBrowserCredential, AuthenticationRecord, TokenCachePersistenceOptions

fn = "cred_cache.txt"
deserialized_record = None

found_cc = os.path.exists(fn)
if found_cc:
    with open(fn, "rt") as infile:
        cred_json = infile.read()
        deserialized_record = AuthenticationRecord.deserialize(cred_json)
        print("record read from:", fn)

credential = InteractiveBrowserCredential(enable_persistent_cache=True, authenication_record=deserialized_record)
record = credential.authenticate()

if not found_cc:
    # serialize to our cache
    cred_json = record.serialize()
    with open(fn, "wt") as outfile:
        outfile.write(cred_json)

    print("record written to:", fn)

Expected behavior The first time the program is run, I expect it to prompt for credentials in the browser. The second time the program is run, I expect it to run to completion without prompting for credentials in the browser, but it always prompts.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context See original issue: #9744. Note: I am looking for the simplest way to get an “auth” to pass to the Azure Workspace.get() method, since it seems to require one with for certain Singularity workspaces. I would prefer, in the following order:

  • to just log-in using my Azure portal login cached creds - is there a way to do this yet?
  • to have Azure cache my credentials itself (in the cloud, or on my local computer) - is there a way to do this yet?
  • to be able to cache some bit of information that I can use to get the “auth” without having to make the user authenticate again The above test program represents the suggested code from #9744, as I interpret it. Did I miss something?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rfernand2commented, Mar 30, 2022

Thanks to help from @xiangyan99, the below, updated program now works correctly (Azure authentication in browser is only done on first run):

# creds_cache_test.py: test out ability to use cached creds to avoid authenticating on every run
import os
from azure.identity import InteractiveBrowserCredential, AuthenticationRecord, TokenCachePersistenceOptions 

# actual credential caching is handling by azure, but our app must supply the authentication record 
# which identifies the tentant_id, username, etc.
fn = "cred_cache.txt"
deserialized_record = None

if os.path.exists(fn):
    # read serialized authentication_record from local file
    with open(fn, "rt") as infile:
        cred_json = infile.read()
        deserialized_record = AuthenticationRecord.deserialize(cred_json)

cpo = TokenCachePersistenceOptions()
credential = InteractiveBrowserCredential(cache_persistence_options=cpo,
    authentication_record=deserialized_record)

if not os.path.exists(fn):
    # serialize authentication_record to local file
    record = credential.authenticate()
    cred_json = record.serialize()

    with open(fn, "wt") as outfile:
        outfile.write(cred_json)

# prove the credential is valid
scope = "https://management.core.windows.net/.default"
token = credential.get_token(scope)
print(token)
0reactions
xiangyan99commented, May 9, 2022

If you don’t mind, could you share your code snippets how you use those credentials?

My guess is you did not specify tenant_id for them. If it is not specified, SharedTokenCacheCredential gives a default one but InteractiveBrowserCredential cannot give a default one hence it prompts you to specify.

Read more comments on GitHub >

github_iconTop Results From Across the Web

azure.identity.InteractiveBrowserCredential class
Authenticates as a service principal using a certificate. The certificate must have an RSA private key, because this credential signs assertions using RS256....
Read more >
azure.identity package — Azure SDK for Python 2.0.0 ... - NET
This class enables DeviceCodeCredential and InteractiveBrowserCredential to access previously cached authentication data. Applications shouldn't construct ...
Read more >
azidentity - Go Packages
This credential doesn't cache tokens, so every call invokes the CLI. This method is called automatically by Azure SDK clients.
Read more >
node.js - Azure Identity Authentication DefaultAzureCredential ...
But the authentication is getting failed and I am not able to generate credentials. when I consoled the new DefaultAzureCredential(); ...
Read more >
Cached Credentials: Important Facts That You Cannot Miss
The second thing, however, is if our cached logon data password is really safe. So, let's have a look. First of all, at...
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