Skip to content

Async credential classes missing from azure.identity.aio #19943

Closed as not planned

Description

azure.identity.aio is missing many Credential classes that exist in the azure.identity namespace.

https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/azure/identity/aio/__init__.py#L7-L20

compared with

https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/azure/identity/__init__.py#L11-L27

missing:

  • AzureApplicationCredential
  • DeviceCodeCredential
  • InteractiveBrowserCredential
  • UsernamePasswordCredential

The implementations don't exist at all. I was trying to implement an asyncio application using InteractiveBrowser and DeviceCodeCredential, only to find they don't exist

Activity

  1. ghost added
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on Jul 27, 2021
  2. added and removed
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on Jul 27, 2021
  3. chlowell commented on Jul 27, 2021

    @chlowell
    Member

    Thanks for opening this issue! An async AzureApplicationCredential does exist but wasn't importable from azure.identity.aio. #19961 fixes that.

    The other credentials you list don't exist because their implementation relies on MSAL Python, which doesn't yet have an async API (AzureAD/microsoft-authentication-library-for-python#88). Our #6354 tracks the SDK work needed to use an async MSAL once it exists. One workaround in the meantime is to wrap a blocking credential, for example:

    class _AsyncDeviceCodeCredential(DeviceCodeCredential):
        async def get_token(self, *scopes, **kwargs):
            return super().get_token(*scopes, **kwargs)
  4. MrNickArcher commented on Jan 24, 2023

    @MrNickArcher

    I had a different work-around for this problem but I see there is a tidier one above; it is useful because the pandas.read_parquet() uses packages ffspec and adlfs which only accept async credentials.

    from azure.identity import InteractiveBrowserCredential
    import pandas
    import functools
    import asyncio
    
    # define an async wrapper
    def run_in_executor(f):
        @functools.wraps(f)
        def inner(*args, **kwargs):
            loop = asyncio.get_running_loop()
            return loop.run_in_executor(None, lambda: f(*args, **kwargs))
        return inner
    
    # apply the wrapper to the get_token function of the credential
    interactive_browser_credential_async = InteractiveBrowserCredential()
    interactive_browser_credential_async.get_token = run_in_executor(interactive_browser_credential_async.get_token)
    
    # use the wrapped credential in async context:
    df = pandas.read_parquet(
        f"abfss://{conainter}@{account_name}.dfs.core.windows.net/{path_to_parquet}",
        storage_options={
            "credential":interactive_browser_credential_async,
        }
    )

    Also works for DeviceCodeCredential. Have not tested the others. Very handy for testing and debugging until scripts are deployed to environments that have better options.

  5. basnijholt commented on May 12, 2023

    @basnijholt
    Contributor

    Over the last 2 years several workaround have been reported and implemented, e.g., #19943 (comment) and microsoft/azure-quantum-python#162. However none of these provide a smooth integration with AzureDefaultCredential.

    In the absence of an async MSAL, one could just provide a class that uses the sync version and runs the methods in an executor like Nicholas Archer (@MrNickArcher) posted above.

    Most users probably don't care about where the asynchronous-ness comes from. As long as it provides any async API, it will work with packages that depend on azure-identity's async auth implementations (such as adlfs, ffspec).

    I see that Xiang Yan (@xiangyan99) has been assigned to this issue. Is solving this any priority at all?

    (edit: the async MSAL issue has been open for almost 4 years, so I wouldn't expect them to implement this any time soon (AzureAD/microsoft-authentication-library-for-python#88))

  6. xiangyan99 commented on May 12, 2023

    @xiangyan99
    Member

    This could be one of the answers. :)

    #30381

  7. basnijholt commented on May 12, 2023

    @basnijholt
    Contributor

    Xiang Yan (@xiangyan99), thanks for your quick reply.

    If that works, great!

    Otherwise one could do something like:

    from azure.identity import InteractiveBrowserCredential
    from azure.core.credentials import AccessToken
    import asyncio
    import functools
    
    def run_in_executor(f):
        @functools.wraps(f)
        def inner(*args, **kwargs):
            loop = asyncio.get_running_loop()
            return loop.run_in_executor(None, functools.partial(f, *args, **kwargs))
        return inner
    
    class AsyncInteractiveBrowserCredential(InteractiveBrowserCredential):
        async def get_token(self, *scopes: str, **kwargs) -> AccessToken:
            sync_get_token = run_in_executor(super().get_token)
            return await sync_get_token(*scopes, **kwargs)
  8. xiangyan99 commented on May 12, 2023

    @xiangyan99
    Member

    Sorry I forgot. In the meanwhile, you can also use this one:
    https://gist.github.com/xiangyan99/79004beae47179f8b17bf5bc61b3fc62

    (This just offers another option. Your sample works too.)

  9. lmazuel commented on May 12, 2023

    @lmazuel
    Member

    Xiang Yan (@xiangyan99) anything we should do for the June release?

  10. xiangyan99 commented on May 12, 2023

    @xiangyan99
    Member

    Let us have a discussion on #30381 and if this one looks good, we can add it into June release.

  11. basnijholt commented on May 22, 2023

    @basnijholt
    Contributor

    Upon having a second look, I do not see how the PR you linked (#30381) solves this problem.

    This issue is about not having async credentials implemented for all credential types (e.g., InteractiveBrowserCredential). The PR you linked allows async credentials in sync contexts IIUC. Am I missing some key point perhaps? 😅

  12. xiangyan99 commented on May 23, 2023

    @xiangyan99
    Member

    w/ this PR, you can use the sync credential in the place that needs async type.

    Does this work?

  13. github-actions commented on Mar 15, 2024

    @github-actions
    Contributor

    Hi Anthony Shaw (@tonybaloney), we deeply appreciate your input into this project. Regrettably, this issue has remained inactive for over 2 years, leading us to the decision to close it. We've implemented this policy to maintain the relevance of our issue queue and facilitate easier navigation for new contributors. If you still believe this topic requires attention, please feel free to create a new issue, referencing this one. Thank you for your understanding and ongoing support.

  14. locked and limited conversation to collaborators on Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Azure.IdentityClientThis issue points to a problem in the data-plane of the library.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions