-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_conn.py
18 lines (17 loc) · 805 Bytes
/
get_conn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
from flask import current_app
from azure.identity import DefaultAzureCredential
def get_conn():
if 'WEBSITE_HOSTNAME' in os.environ:
# Azure hosted, refresh token that becomes password.
azure_credential = DefaultAzureCredential()
# Get token for Azure Database for PostgreSQL
print("Get password token.")
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net")
conn = str(current_app.config.get('DATABASE_URI')).replace('PASSWORDORTOKEN', token.token)
return conn
else:
# Locally, read password from environment variable.
print("Read password env variable.")
conn = str(current_app.config.get('DATABASE_URI')).replace('PASSWORDORTOKEN', os.environ['DBPASS'])
return conn