Skip to content

Commit 9001b0c

Browse files
committed
convert to service connector and kv secrets
1 parent 7db2b4e commit 9001b0c

12 files changed

+378
-225
lines changed

.deployment

Lines changed: 0 additions & 2 deletions
This file was deleted.

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/vscode/devcontainers/python:0-3.10
1+
FROM mcr.microsoft.com/vscode/devcontainers/python:3.12
22

33
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
44
&& apt-get -y install --no-install-recommends postgresql-client \

.devcontainer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
This `.devcontainer` directory contains the configuration for a [dev container](https://docs.github.com/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers) and isn't used by the sample application.
44

5-
The dev container configuration lets you open the repository in a [GitHub codespace](https://docs.github.com/codespaces/overview) or a dev container in Visual Studio Code. For your convenience, the dev container is configured with the following:
5+
The dev container configuration lets you open therepository in a [GitHub codespace](https://docs.github.com/codespaces/overview) or a dev container in Visual Studio Code. For your convenience, the dev container is configured with the following:
66

77
- Python
8+
- Running `pip install -r requirements.txt` from the project at container start.
89
- PostgreSQL
910
- Redis
1011
- [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview) (so you can run `azd` commands directly).

.devcontainer/devcontainer.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "python-app-service-postgresql-redis-infra",
33
"dockerComposeFile": "docker-compose.yml",
44
"service": "app",
5-
"workspaceFolder": "/workspace",
5+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
66
"features": {
77
"ghcr.io/azure/azure-dev/azd:latest": {}
88
},
@@ -14,7 +14,8 @@
1414
"ms-python.python",
1515
"ms-python.vscode-pylance",
1616
"mtxr.sqltools",
17-
"mtxr.sqltools-driver-pg"
17+
"mtxr.sqltools-driver-pg",
18+
"GitHub.copilot"
1819
],
1920
"settings": {
2021
"sqltools.connections": [
@@ -29,7 +30,6 @@
2930
"password": "app_password"
3031
}
3132
],
32-
"python.pythonPath": "/usr/local/bin/python",
3333
"python.languageServer": "Pylance",
3434
"python.linting.enabled": true,
3535
"python.linting.mypyEnabled": true,
@@ -51,12 +51,10 @@
5151
}
5252
}
5353
},
54-
// Use 'forwardPorts' to make a list of ports inside the container available locally. 5000 is for Flask, 8000 is for Django, and 5432 is for PostgreSQL.
55-
"forwardPorts": [
56-
8000, 5000, 5432
57-
],
54+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
55+
// "forwardPorts": [],
5856
// Use 'postCreateCommand' to run commands after the container is created.
59-
// "postCreateCommand": "",
57+
"postCreateCommand": "pip install -r requirements.txt",
6058
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
6159
"remoteUser": "vscode"
6260
}

.env.sample

Lines changed: 0 additions & 6 deletions
This file was deleted.

.env.sample.devcontainer

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ This project has a [dev container configuration](.devcontainer/), which makes it
4545
1. In the codespace terminal, run the following commands:
4646

4747
```shell
48-
# Install requirements
49-
python3 -m pip install -r requirements.txt
50-
# Create .env with environment variables
51-
cp .env.sample.devcontainer .env
5248
# Run database migrations
5349
python3 manage.py migrate
5450
# Start the development server

azure.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ hooks:
1313
postprovision:
1414
posix:
1515
shell: sh
16-
run: echo $'\n\nApp Service app has the following settings:\n' && echo "$WEB_APP_SETTINGS" | jq -r '.[]' | sed 's/\(.*\)/\t- \1/' && echo -e $"\nSee the settings in the portal:\033[1;36m $WEB_APP_CONFIG"
16+
run: printf '\nApp Service app has the following connection settings:\n' && printf "$CONNECTION_SETTINGS" | jq -r '.[]' | sed 's/\(.*\)/\t- \1/' && printf "\nSee the settings in the portal:\033[1;36m $WEB_APP_CONFIG\n"
1717
interactive: true
1818
continueOnError: true
1919
windows:
2020
shell: pwsh
21-
run: Write-Host "`n`nApp Service app has the following settings:`n" $WEB_APP_SETTINGS | ConvertFrom-Json | ForEach-Object { Write-Host "\t- $_" }
21+
run: Write-Host "`n`nApp Service app has the following connection settings:`n" $CONNECTION_SETTINGS | ConvertFrom-Json | ForEach-Object { Write-Host "\t- $_" }
2222
interactive: true
2323
continueOnError: true
2424
postdeploy:
2525
posix:
2626
shell: sh
27-
run: echo -e $"\n\nOpen SSH session to App Service container at:\033[1;36m $WEB_APP_SSH\033[0m" && echo -e $"Stream App Service logs at:\033[1;36m $WEB_APP_LOG_STREAM"
27+
run: printf "Open SSH session to App Service container at:\033[1;36m $WEB_APP_SSH\033[0m\nStream App Service logs at:\033[1;36m $WEB_APP_LOG_STREAM\n"
2828
interactive: true
2929
continueOnError: true
3030
windows:

azureproject/production.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@
2626
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
2727
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
2828

29-
# Configure Postgres database based on connection string of the libpq Keyword/Value form
30-
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
31-
conn_str = os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']
32-
conn_str_params = {pair.split('=')[0]: pair.split('=')[1] for pair in conn_str.split(' ')}
3329
DATABASES = {
3430
'default': {
3531
'ENGINE': 'django.db.backends.postgresql',
36-
'NAME': conn_str_params['dbname'],
37-
'HOST': conn_str_params['host'],
38-
'USER': conn_str_params['user'],
39-
'PASSWORD': conn_str_params['password'],
32+
'NAME': os.environ['AZURE_POSTGRESQL_NAME'],
33+
'HOST': os.environ['AZURE_POSTGRESQL_HOST'],
34+
'USER': os.environ['AZURE_POSTGRESQL_USER'],
35+
'PASSWORD': os.environ['AZURE_POSTGRESQL_PASSWORD'],
4036
}
4137
}
4238

4339
CACHES = {
4440
"default": {
4541
"BACKEND": "django_redis.cache.RedisCache",
46-
"LOCATION": os.environ.get('AZURE_REDIS_CONNECTIONSTRING'),
42+
"LOCATION": os.environ['AZURE_REDIS_CONNECTIONSTRING'],
4743
"OPTIONS": {
4844
"CLIENT_CLASS": "django_redis.client.DefaultClient",
4945
"COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",

infra/main.bicep

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ param databasePassword string
1717
@description('Django SECRET_KEY for securing signed data')
1818
param secretKey string
1919

20+
param principalId string = ''
21+
2022
var resourceToken = toLower(uniqueString(subscription().id, name, location))
2123
var tags = { 'azd-env-name': name }
2224

@@ -35,14 +37,15 @@ module resources 'resources.bicep' = {
3537
resourceToken: resourceToken
3638
tags: tags
3739
databasePassword: databasePassword
40+
principalId: principalId
3841
secretKey: secretKey
3942
}
4043
}
4144

4245
output AZURE_LOCATION string = location
4346
output APPLICATIONINSIGHTS_CONNECTION_STRING string = resources.outputs.APPLICATIONINSIGHTS_CONNECTION_STRING
4447
output WEB_URI string = resources.outputs.WEB_URI
45-
output WEB_APP_SETTINGS array = resources.outputs.WEB_APP_SETTINGS
48+
output CONNECTION_SETTINGS array = resources.outputs.CONNECTION_SETTINGS
4649
output WEB_APP_LOG_STREAM string = resources.outputs.WEB_APP_LOG_STREAM
4750
output WEB_APP_SSH string = resources.outputs.WEB_APP_SSH
48-
output WEB_APP_CONFIG string = resources.outputs.WEB_APP_CONFIG
51+
output WEB_APP_CONFIG string = resources.outputs.WEB_APP_CONFIG

infra/main.parameters.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
2-
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3-
"contentVersion": "1.0.0.0",
4-
"parameters": {
5-
"name": {
6-
"value": "${AZURE_ENV_NAME}"
7-
},
8-
"location": {
9-
"value": "${AZURE_LOCATION}"
10-
},
11-
"databasePassword": {
12-
"value": "$(secretOrRandomPassword)"
13-
},
14-
"secretKey": {
15-
"value": "$(secretOrRandomPassword)"
16-
}
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"name": {
6+
"value": "${AZURE_ENV_NAME}"
7+
},
8+
"location": {
9+
"value": "${AZURE_LOCATION}"
10+
},
11+
"databasePassword": {
12+
"value": "$(secretOrRandomPassword)"
13+
},
14+
"principalId": {
15+
"value": "${AZURE_PRINCIPAL_ID}"
16+
},
17+
"secretKey": {
18+
"value": "$(secretOrRandomPassword)"
1719
}
1820
}
21+
}

0 commit comments

Comments
 (0)