Skip to content

Commit 36d54c6

Browse files
committed
Use async cred for pg in prod
1 parent 46c371b commit 36d54c6

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/backend/fastapi_app/postgres_engine.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import logging
23
import os
34

@@ -13,8 +14,8 @@
1314

1415

1516
async def create_postgres_engine(*, host, username, database, password, sslmode, azure_credential) -> AsyncEngine:
16-
def get_password_from_azure_credential():
17-
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default")
17+
async def get_password_from_azure_credential():
18+
token = await azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default")
1819
return token.token
1920

2021
token_based_password = False
@@ -23,7 +24,7 @@ def get_password_from_azure_credential():
2324
logger.info("Authenticating to Azure Database for PostgreSQL using Azure Identity...")
2425
if azure_credential is None:
2526
raise ValueError("Azure credential must be provided for Azure Database for PostgreSQL")
26-
password = get_password_from_azure_credential()
27+
password = await get_password_from_azure_credential()
2728
else:
2829
logger.info("Authenticating to PostgreSQL using password...")
2930

@@ -46,7 +47,8 @@ def register_custom_types(dbapi_connection: AdaptedConnection, *args):
4647
def update_password_token(dialect, conn_rec, cargs, cparams):
4748
if token_based_password:
4849
logger.info("Updating password token for Azure Database for PostgreSQL")
49-
cparams["password"] = get_password_from_azure_credential()
50+
loop = asyncio.get_event_loop()
51+
cparams["password"] = loop.run_until_complete(get_password_from_azure_credential())
5052

5153
return engine
5254

0 commit comments

Comments
 (0)