Is your feature request related to a problem? Please describe.
enable_entra_authentication_async configures an asynchronous SQLAlchemy engine,
but its do_connect event listener is synchronous and requires a synchronous
TokenCredential. As a result, applications using
postgresql+asyncpg and azure.identity.aio.DefaultAzureCredential cannot obtain
Microsoft Entra tokens without introducing custom token caching and connection-event
workarounds. Calling the synchronous credential path from an async application can
also block the event loop.
Describe the solution you'd like
Add a SQLAlchemy integration path for postgresql+asyncpg that accepts an
AsyncTokenCredential and acquires Entra connection information through the
existing get_entra_conninfo_async API.
Because SQLAlchemy's do_connect listener cannot await a coroutine, the integration
should use SQLAlchemy's asynchronous connection-creation hook (async_creator) to:
- acquire the token and PostgreSQL principal asynchronously;
- open an
asyncpg connection with the Entra token as its password;
- return the driver connection to SQLAlchemy's async pool.
The existing enable_entra_authentication_async API must remain compatible with
synchronous TokenCredential implementations and async engines backed by drivers
that use the synchronous event hook.
The package should provide:
- a documented public helper or factory for the asyncpg integration;
- an optional dependency extra for
asyncpg, if required;
- unit tests covering async credential validation, token injection, failures, and
concurrent connection creation;
- a live test and a README example using
azure.identity.aio.DefaultAzureCredential.
Describe alternatives you've considered
Applications can manually cache tokens, inject them into asyncpg connection
parameters, and refresh them before creating connections. This duplicates
azure-postgresql-auth behavior, risks stale tokens and secret exposure in URLs,
and does not provide a reusable solution for SQLAlchemy users.
Using enable_entra_authentication_async with a synchronous
DefaultAzureCredential works for supported drivers, but token acquisition still
runs synchronously in the connection event and is unsuitable when the application
requires an end-to-end asynchronous credential flow.
Additional context
The package already exposes get_entra_conninfo_async and supports
AsyncTokenCredential in psycopg3.AsyncEntraConnection; this feature would extend
that capability to SQLAlchemy applications using asyncpg.
The change must not attempt to make SQLAlchemy's do_connect listener awaitable:
SQLAlchemy invokes that listener synchronously.
Is your feature request related to a problem? Please describe.
enable_entra_authentication_asyncconfigures an asynchronous SQLAlchemy engine,but its
do_connectevent listener is synchronous and requires a synchronousTokenCredential. As a result, applications usingpostgresql+asyncpgandazure.identity.aio.DefaultAzureCredentialcannot obtainMicrosoft Entra tokens without introducing custom token caching and connection-event
workarounds. Calling the synchronous credential path from an async application can
also block the event loop.
Describe the solution you'd like
Add a SQLAlchemy integration path for
postgresql+asyncpgthat accepts anAsyncTokenCredentialand acquires Entra connection information through theexisting
get_entra_conninfo_asyncAPI.Because SQLAlchemy's
do_connectlistener cannot await a coroutine, the integrationshould use SQLAlchemy's asynchronous connection-creation hook (
async_creator) to:asyncpgconnection with the Entra token as its password;The existing
enable_entra_authentication_asyncAPI must remain compatible withsynchronous
TokenCredentialimplementations and async engines backed by driversthat use the synchronous event hook.
The package should provide:
asyncpg, if required;concurrent connection creation;
azure.identity.aio.DefaultAzureCredential.Describe alternatives you've considered
Applications can manually cache tokens, inject them into
asyncpgconnectionparameters, and refresh them before creating connections. This duplicates
azure-postgresql-authbehavior, risks stale tokens and secret exposure in URLs,and does not provide a reusable solution for SQLAlchemy users.
Using
enable_entra_authentication_asyncwith a synchronousDefaultAzureCredentialworks for supported drivers, but token acquisition stillruns synchronously in the connection event and is unsuitable when the application
requires an end-to-end asynchronous credential flow.
Additional context
The package already exposes
get_entra_conninfo_asyncand supportsAsyncTokenCredentialinpsycopg3.AsyncEntraConnection; this feature would extendthat capability to SQLAlchemy applications using
asyncpg.The change must not attempt to make SQLAlchemy's
do_connectlistener awaitable:SQLAlchemy invokes that listener synchronously.