generated from pamelafox/simple-flask-server-example
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import logging | ||
import os | ||
|
||
import psycopg2 | ||
from azure.identity import DefaultAzureCredential | ||
|
||
logger = logging.getLogger("scripts") | ||
|
||
|
||
def assign_role_for_webapp(postgres_host, postgres_username, app_identity_name): | ||
if not postgres_host.endswith(".database.azure.com"): | ||
logger.info("This script is intended to be used with Azure Database for PostgreSQL.") | ||
logger.info("Please set the environment variable DBHOST to the Azure Database for PostgreSQL server hostname.") | ||
return | ||
|
||
logger.info("Authenticating to Azure Database for PostgreSQL using Azure Identity...") | ||
azure_credential = DefaultAzureCredential() | ||
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default") | ||
conn = psycopg2.connect( | ||
database="postgres", # You must connect to postgres database when assigning roles | ||
user=postgres_username, | ||
password=token.token, | ||
host=postgres_host, | ||
sslmode="require", | ||
) | ||
|
||
conn.autocommit = True | ||
cur = conn.cursor() | ||
|
||
cur.execute(f"select * from pgaadauth_list_principals(false) WHERE rolname = '{app_identity_name}'") | ||
|
||
# count number of rows in cur | ||
if len(cur.fetchall()) == 1: | ||
logger.info(f"Found an existing PostgreSQL role for identity {app_identity_name}") | ||
else: | ||
logger.info(f"Creating a PostgreSQL role for identity {app_identity_name}") | ||
cur.execute(f"SELECT * FROM pgaadauth_create_principal('{app_identity_name}', false, false)") | ||
cur.execute(f'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{app_identity_name}"') | ||
cur.execute( | ||
f"ALTER DEFAULT PRIVILEGES IN SCHEMA public" | ||
f'GRANT SELECT, UPDATE, INSERT, DELETE ON TABLES TO "{app_identity_name}"' | ||
) | ||
cur.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.WARNING) | ||
logger.setLevel(logging.INFO) | ||
|
||
POSTGRES_HOST = os.getenv("POSTGRES_DOMAIN_NAME") | ||
POSTGRES_USERNAME = os.getenv("POSTGRES_ADMIN_USERNAME") | ||
APP_IDENTITY_NAME = os.getenv("WEB_APP_NAME") | ||
if not POSTGRES_HOST or not POSTGRES_USERNAME or not APP_IDENTITY_NAME: | ||
logger.error( | ||
"Can't find POSTGRES_DOMAIN_NAME, POSTGRES_ADMIN_USERNAME, and WEB_APP_NAME environment variables." | ||
"Make sure you run azd up first." | ||
) | ||
else: | ||
assign_role_for_webapp(POSTGRES_HOST, POSTGRES_USERNAME, APP_IDENTITY_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/sh | ||
|
||
while IFS='=' read -r key value; do | ||
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//') | ||
export "$key=$value" | ||
done <<EOF | ||
$(azd env get-values) | ||
EOF | ||
|
||
python ./scripts/assign_role.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import os | ||
|
||
from azure.identity import DefaultAzureCredential | ||
|
||
DEBUG = False | ||
|
||
if "WEBSITE_HOSTNAME" in os.environ: | ||
ALLOWED_HOSTS = [os.environ["WEBSITE_HOSTNAME"]] | ||
else: | ||
ALLOWED_HOSTS = [] | ||
|
||
# Configure Postgres database; the full username for PostgreSQL flexible server is | ||
# username (not @sever-name). | ||
dbuser = os.environ["DBUSER"] | ||
dbpass = os.environ["DBPASS"] | ||
azure_credential = DefaultAzureCredential() | ||
dbpass = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default").token | ||
dbhost = os.environ["DBHOST"] + ".postgres.database.azure.com" | ||
dbname = os.environ["DBNAME"] | ||
|
||
DATABASE_URI = f"postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}" | ||
|
||
# TODO: SSL not needed? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ psycopg2-binary==2.9.9 | |
python-dotenv==1.0.1 | ||
SQLAlchemy==2.0.29 | ||
gunicorn==21.2.0 | ||
azure-identity==1.16.0 |