Skip to content

Commit 522e165

Browse files
committed
ran isort and moved create managed id up in create_resources.sh
1 parent a2567be commit 522e165

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

azureproject/development.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
2-
from .settings import *
2+
33
from .get_token import get_token
4+
from .settings import *
45

56
# SECURITY WARNING: don't run with debug turned on in production!
67
DEBUG = True
@@ -37,4 +38,4 @@
3738
'PASSWORD': DBPASS,
3839
}
3940
}
40-
get_token()
41+
get_token()

azureproject/get_token.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import os
2-
from azure.identity import DefaultAzureCredential
2+
33
import django.conf as conf
4+
from azure.identity import DefaultAzureCredential
45

56
# This is for demo purposes. Consider using Django middleware to hook into req/resp processing.
67

78
def get_token():
89
if 'RUNNING_IN_PRODUCTION' in os.environ or 'USE_REMOTE_POSTGRESQL' in os.environ:
9-
# Azure hosted PostgreSQL server, refresh token that becomes password.
10+
# Azure hosted PostgreSQL server, refresh token that is used as the PostgreSQL password.
1011
# Get token for Azure Database for PostgreSQL
1112
azure_credential = DefaultAzureCredential()
1213
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default")
1314
conf.settings.DATABASES['default']['PASSWORD'] = token.token
1415
else:
1516
# Local PostgreSQL server, read password from environment variable.
1617
conf.settings.DATABASES['default']['PASSWORD'] = os.environ['DBPASS']
17-
return
18+
return

azureproject/production.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
2-
from .settings import *
2+
import secrets
3+
34
from .get_token import get_token
5+
from .settings import *
46

57
# Configure allowed host names that can be served and trusted origins for Azure Container Apps.
68
ALLOWED_HOSTS = ['.azurecontainerapps.io'] if 'RUNNING_IN_PRODUCTION' in os.environ else []
@@ -11,7 +13,7 @@
1113
# SECURITY WARNING: keep the secret key used in production secret!
1214
# Use this py command to create secret
1315
# python -c 'import secrets; print(secrets.token_hex())'
14-
SECRET_KEY = os.getenv('AZURE_SECRET_KEY')
16+
SECRET_KEY = os.getenv('AZURE_SECRET_KEY') or secrets.token_hex()
1517

1618
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
1719

@@ -25,4 +27,4 @@
2527
'PASSWORD': 'set with get_token()'
2628
}
2729
}
28-
get_token()
30+
get_token()

azureproject/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import os
12
from pathlib import Path
2-
import os
33

44
# Build paths inside the project like this: BASE_DIR / 'subdir'.
55
BASE_DIR = Path(__file__).resolve().parent.parent

create_resources.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ az acr build \
6363
--image $IMAGE_NAME $CODE_LOCATION
6464
echo "INFO:: Completed building image: $IMAGE_NAME."
6565

66+
# Create a user-assigned managed identity named my-ua-managed-id to access database
67+
68+
az identity create \
69+
--name my-ua-managed-id \
70+
--resource-group $RESOURCE_GROUP
71+
echo "INFO:: Created user-assigned managed identity named my-ua-managed-id in resource group: $RESOURCE_GROUP."
72+
6673
# Create PostgreSQL database server.
6774

6875
az postgres flexible-server create \
@@ -94,13 +101,6 @@ az postgres flexible-server db create \
94101
--database-name restaurants_reviews
95102
echo "INFO:: Completed creating database restaurants_reviews on PostgreSQL server: $POSTGRESQL_NAME."
96103

97-
# Create a user-assigned managed identity named my-ua-managed-id to access database
98-
99-
az identity create \
100-
--name my-ua-managed-id \
101-
--resource-group $RESOURCE_GROUP
102-
echo "INFO:: Created user-assigned managed identity named my-ua-managed-id in resource group: $RESOURCE_GROUP."
103-
104104
# Add user assigned managed identity as role on server (requires rdbms-connect extension for token)
105105

106106
az postgres flexible-server execute \

restaurant_review/views.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import uuid
21
import os
2+
import uuid
3+
4+
from azureproject.get_token import get_token
5+
from django.contrib import messages
6+
from django.db.models import Avg, Count
37
from django.http import Http404, HttpResponseRedirect
48
from django.shortcuts import render
5-
from django.db.models import Avg, Count
69
from django.urls import reverse
710
from django.utils import timezone
8-
from django.contrib import messages
911
from requests import RequestException, exceptions
10-
from azureproject.get_token import get_token
1112

1213
from restaurant_review.models import Restaurant, Review
1314

0 commit comments

Comments
 (0)