pulumi-django-azure 1.0.35__tar.gz → 1.0.37__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pulumi-django-azure might be problematic. Click here for more details.

Files changed (19) hide show
  1. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/PKG-INFO +246 -244
  2. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/pyproject.toml +16 -22
  3. pulumi_django_azure-1.0.35/setup.cfg +0 -8
  4. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/PKG-INFO +0 -244
  5. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/SOURCES.txt +0 -18
  6. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/dependency_links.txt +0 -1
  7. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/requires.txt +0 -14
  8. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/top_level.txt +0 -1
  9. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/README.md +0 -0
  10. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/__init__.py +0 -0
  11. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/azure_helper.py +0 -0
  12. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/context_processors.py +0 -0
  13. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/django_deployment.py +0 -0
  14. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/management/commands/__init__.py +0 -0
  15. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/management/commands/purge_cache.py +0 -0
  16. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/management/commands/purge_cdn.py +0 -0
  17. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/management/commands/test_redis.py +0 -0
  18. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/middleware.py +0 -0
  19. {pulumi_django_azure-1.0.35 → pulumi_django_azure-1.0.37}/src/pulumi_django_azure/settings.py +0 -0
@@ -1,244 +1,246 @@
1
- Metadata-Version: 2.4
2
- Name: pulumi-django-azure
3
- Version: 1.0.35
4
- Summary: Simply deployment of Django on Azure with Pulumi
5
- Author-email: Maarten Ureel <maarten@youreal.eu>
6
- License-Expression: MIT
7
- Project-URL: Homepage, https://gitlab.com/MaartenUreel/pulumi-django-azure
8
- Keywords: django,pulumi,azure
9
- Classifier: Programming Language :: Python
10
- Classifier: Programming Language :: Python :: 3
11
- Requires-Python: <3.14,>=3.11
12
- Description-Content-Type: text/markdown
13
- Requires-Dist: azure-identity<2.0.0,>=1.24.0
14
- Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.10.0
15
- Requires-Dist: azure-mgmt-cdn<14.0.0,>=13.1.1
16
- Requires-Dist: azure-mgmt-resource<25.0.0,>=24.0.0
17
- Requires-Dist: django<6.0.0,>=5.2.5
18
- Requires-Dist: django-azure-communication-email<2.0.0,>=1.3.2
19
- Requires-Dist: django-environ<0.13.0,>=0.12.0
20
- Requires-Dist: django-redis<7.0.0,>=6.0.0
21
- Requires-Dist: django-storages[azure]<2.0.0,>=1.14.6
22
- Requires-Dist: pulumi>=3.189.0
23
- Requires-Dist: pulumi-azure-native>=3.7.1
24
- Requires-Dist: pulumi-random>=4.18.3
25
- Requires-Dist: redis[hiredis]<7.0.0,>=6.4.0
26
- Requires-Dist: tenacity<10.0.0,>=9.1.2
27
-
28
- # Pulumi Django Deployment
29
-
30
- This project aims to make a simple Django deployment on Azure easier.
31
-
32
- To have a proper and secure environment, we need these components:
33
- * Storage account for media and static files
34
- * CDN endpoint in front with a domain name of our choosing
35
- * PostgreSQL server
36
- * Azure Communication Services to send e-mails
37
- * Webapp with multiple custom host names and managed SSL for the website itself
38
- * Azure Key Vault per application
39
- * Webapp running pgAdmin
40
-
41
- ## Project requirements
42
-
43
- ## Installation
44
- This package is published on PyPi, so you can just add pulumi-django-azure to your requirements file.
45
-
46
- To use a specific branch in your project, add to pyproject.toml dependencies:
47
- ```
48
- pulumi-django-azure = { git = "git@gitlab.com:MaartenUreel/pulumi-django-azure.git", branch = "dev" }
49
- ```
50
-
51
- A simple project could look like this:
52
- ```python
53
- import pulumi
54
- import pulumi_azure_native as azure
55
- from pulumi_django_azure import DjangoDeployment
56
-
57
- stack = pulumi.get_stack()
58
- config = pulumi.Config()
59
-
60
-
61
- # Create resource group
62
- rg = azure.resources.ResourceGroup(f"rg-{stack}")
63
-
64
- # Create VNet
65
- vnet = azure.network.VirtualNetwork(
66
- f"vnet-{stack}",
67
- resource_group_name=rg.name,
68
- address_space=azure.network.AddressSpaceArgs(
69
- address_prefixes=["10.0.0.0/16"],
70
- ),
71
- )
72
-
73
- # Deploy the website and all its components
74
- django = DjangoDeployment(
75
- stack,
76
- tenant_id="abc123...",
77
- resource_group_name=rg.name,
78
- vnet=vnet,
79
- pgsql_ip_prefix="10.0.10.0/24",
80
- appservice_ip_prefix="10.0.20.0/24",
81
- app_service_sku=azure.web.SkuDescriptionArgs(
82
- name="B2",
83
- tier="Basic",
84
- ),
85
- storage_account_name="mystorageaccount",
86
- cdn_host="cdn.example.com",
87
- )
88
-
89
- django.add_django_website(
90
- name="web",
91
- db_name="mywebsite",
92
- repository_url="git@gitlab.com:project/website.git",
93
- repository_branch="main",
94
- website_hosts=["example.com", "www.example.com"],
95
- django_settings_module="mywebsite.settings.production",
96
- comms_data_location="europe",
97
- comms_domains=["mydomain.com"],
98
- )
99
-
100
- django.add_database_administrator(
101
- object_id="a1b2c3....",
102
- user_name="user@example.com",
103
- tenant_id="a1b2c3....",
104
- )
105
- ```
106
-
107
- ## Changes to your Django project
108
- 1. Add `pulumi_django_azure` to your `INSTALLED_APPS`
109
- 2. Add to your settings file:
110
- ```python
111
- from pulumi_django_azure.settings import * # noqa: F403
112
-
113
- # This will provide the management command to purge the CDN and cache
114
- INSTALLED_APPS += ["pulumi_django_azure"]
115
-
116
- # This will provide the health check middleware that will also take care of credential rotation.
117
- MIDDLEWARE += ["pulumi_django_azure.middleware.HealthCheckMiddleware"]
118
- ```
119
- This will pre-configure most settings to make your app work on Azure. You can check the source for details,
120
- and ofcourse override any value after importing them.
121
-
122
-
123
- ## Deployment steps
124
- 1. Deploy without custom hosts (for CDN and websites)
125
- 2. Configure the PostgreSQL server (create and grant permissions to role for your websites)
126
- 3. Retrieve the deployment SSH key and configure your remote GIT repository with it
127
- 4. Configure your CDN host (add the CNAME record)
128
- 5. Configure your custom website domains (add CNAME/A record and TXT validation records)
129
- 6. Re-deploy with custom hosts
130
- 7. Re-deploy once more to enable HTTPS on website domains
131
- 8. Manually activate HTTPS on the CDN host
132
- 9. Go to the e-mail communications service on Azure and configure DKIM, SPF,... for your custom domains.
133
-
134
- ## Custom domain name for CDN
135
- When deploying the first time, you will get a `cdn_cname` output. You need to create a CNAME to this domain before the deployment of the custom domain will succeed.
136
-
137
- You can safely deploy with the failing CustomDomain to get the CNAME, create the record and then deploy again.
138
-
139
- To enable HTTPS, you need to do this manually in the console. This is because of a limitation in the Azure API:
140
- https://github.com/Azure/azure-rest-api-specs/issues/17498
141
-
142
- ## Custom domain names for web application
143
- Because of a circular dependency in custom domain name bindings and certificates that is out of our control, you need to deploy the stack twice.
144
-
145
- The first time will create the bindings without a certificate.
146
- The second deployment will then create the certificate for the domain (which is only possible if the binding exists), but also set the fingerprint of that certificate on the binding.
147
-
148
- To make the certificate work, you need to create a TXT record named `asuid` point to the output of `{your_app}_site_domain_verification_id`. For example:
149
-
150
- ```
151
- asuid.mywebsite.com. TXT "A1B2C3D4E5..."
152
- asuid.www.mywebsite.com. TXT "A1B2C3D4E5..."
153
- ```
154
-
155
- ## Database authentication
156
- The PostgreSQL uses Entra ID authentication only, no passwords.
157
-
158
- ### Administrator login
159
- If you want to log in to the database yourself, you can add yourself as an administrator with the `add_database_administrator` function.
160
- Your username is your e-mailaddress, a temporary password can be obtained using `az account get-access-token`.
161
-
162
- You can use this method to log in to pgAdmin.
163
-
164
- ### Application
165
- Refer to this documentation:
166
- https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-manage-azure-ad-users#create-a-role-using-microsoft-entra-object-identifier
167
-
168
- In short, run something like this in the `postgres` database:
169
- ```
170
- SELECT * FROM pgaadauth_create_principal_with_oid('web_managed_identity', 'c8b25b85-d060-4cfc-bad4-b8581cfdf946', 'service', false, false);
171
- ```
172
- Replace the GUID of course with the managed identity our web app gets.
173
-
174
- The name of the role is outputted by `{your_app}_site_db_user`
175
-
176
- Be sure to grant this role the correct permissions too.
177
-
178
- ## pgAdmin specifics
179
- pgAdmin will be created with a default login:
180
- * Login: dbadmin@dbadmin.net
181
- * Password: dbadmin
182
-
183
- Best practice is to log in right away, create a user for yourself and delete this default user.
184
-
185
- ## Azure OAuth2 / Django Social Auth
186
- If you want to set up login with Azure, which would make sense since you are in the ecosystem, you need to create an App Registration in Entra ID, create a secret and then register these settings in your stack:
187
- ```
188
- pulumi config set --secret --path 'mywebsite_social_auth_azure.key' secret_ID
189
- pulumi config set --secret --path 'mywebsite_social_auth_azure.secret' secret_value
190
- pulumi config set --secret --path 'mywebsite_social_auth_azure.tenant_id' directory_tenant_id
191
- pulumi config set --secret --path 'mywebsite_social_auth_azure.client_id' application_id
192
- ```
193
-
194
- Then in your Django deployment, pass to the `add_django_website` command:
195
- ```
196
- secrets={
197
- "mywebsite_social_auth_azure": "AZURE_OAUTH",
198
- },
199
- ```
200
-
201
- The value will be automatically stored in the vault where the application has access to.
202
- The environment variable will be suffixed with `_SECRET_NAME`.
203
-
204
- Then, in your application, retrieve this data from the vault, e.g.:
205
- ```python
206
- # Social Auth settings
207
- oauth_secret = AZURE_KEY_VAULT_CLIENT.get_secret(env("AZURE_OAUTH_SECRET_NAME"))
208
- oauth_secret = json.loads(oauth_secret.value)
209
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = oauth_secret["client_id"]
210
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = oauth_secret["secret"]
211
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = oauth_secret["tenant_id"]
212
- SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ["username", "first_name", "last_name", "email"]
213
- SOCIAL_AUTH_POSTGRES_JSONFIELD = True
214
-
215
- AUTHENTICATION_BACKENDS = (
216
- "social_core.backends.azuread_tenant.AzureADTenantOAuth2",
217
- "django.contrib.auth.backends.ModelBackend",
218
- )
219
- ```
220
-
221
- And of course add the login button somewhere, following Django Social Auth instructions.
222
-
223
- ## Automate deployments
224
- When using a service like GitLab, you can configure a Webhook to fire upon a push to your branch.
225
-
226
- You need to download the deployment profile to obtain the deployment username and password, and then you can construct a URL like this:
227
-
228
- ```
229
- https://{user}:{pass}@{appname}.scm.azurewebsites.net/deploy
230
-
231
- ```
232
-
233
- ```
234
- https://{appname}.scm.azurewebsites.net/api/sshkey?ensurePublicKey=1
235
- ```
236
-
237
- Be sure to configure the SSH key that Azure will use on GitLab side. You can obtain it using:
238
-
239
- This would then trigger a redeploy everytime you make a commit to your live branch.
240
-
241
-
242
- ## Change requests
243
- I created this for internal use but since it took me a while to puzzle all the things together I decided to share it.
244
- Therefore this project is not super generic, but tailored to my needs. I am however open to pull or change requests to improve this project or to make it more usable for others.
1
+ Metadata-Version: 2.4
2
+ Name: pulumi-django-azure
3
+ Version: 1.0.37
4
+ Summary: Simply deployment of Django on Azure with Pulumi
5
+ License-Expression: MIT
6
+ Keywords: django,pulumi,azure
7
+ Author: Maarten Ureel
8
+ Author-email: maarten@youreal.eu
9
+ Requires-Python: >=3.11,<3.14
10
+ Classifier: Programming Language :: Python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Dist: azure-identity (>=1.25.0,<2.0.0)
13
+ Requires-Dist: azure-keyvault-secrets (>=4.10.0,<5.0.0)
14
+ Requires-Dist: azure-mgmt-cdn (>=13.1.1,<14.0.0)
15
+ Requires-Dist: azure-mgmt-resource (>=24.0.0,<25.0.0)
16
+ Requires-Dist: django (>=5.2.7,<6.0.0)
17
+ Requires-Dist: django-azure-communication-email (>=1.4.0,<2.0.0)
18
+ Requires-Dist: django-environ (>=0.12.0,<0.13.0)
19
+ Requires-Dist: django-redis (>=6.0.0,<7.0.0)
20
+ Requires-Dist: django-storages[azure] (>=1.14.6,<2.0.0)
21
+ Requires-Dist: pulumi (>=3.199.0)
22
+ Requires-Dist: pulumi-azure-native (>=3.8.0)
23
+ Requires-Dist: pulumi-random (>=4.18.3)
24
+ Requires-Dist: redis[hiredis] (>=6.4.0,<7.0.0)
25
+ Requires-Dist: tenacity (>=9.1.2,<10.0.0)
26
+ Project-URL: Homepage, https://gitlab.com/MaartenUreel/pulumi-django-azure
27
+ Description-Content-Type: text/markdown
28
+
29
+ # Pulumi Django Deployment
30
+
31
+ This project aims to make a simple Django deployment on Azure easier.
32
+
33
+ To have a proper and secure environment, we need these components:
34
+ * Storage account for media and static files
35
+ * CDN endpoint in front with a domain name of our choosing
36
+ * PostgreSQL server
37
+ * Azure Communication Services to send e-mails
38
+ * Webapp with multiple custom host names and managed SSL for the website itself
39
+ * Azure Key Vault per application
40
+ * Webapp running pgAdmin
41
+
42
+ ## Project requirements
43
+
44
+ ## Installation
45
+ This package is published on PyPi, so you can just add pulumi-django-azure to your requirements file.
46
+
47
+ To use a specific branch in your project, add to pyproject.toml dependencies:
48
+ ```
49
+ pulumi-django-azure = { git = "git@gitlab.com:MaartenUreel/pulumi-django-azure.git", branch = "dev" }
50
+ ```
51
+
52
+ A simple project could look like this:
53
+ ```python
54
+ import pulumi
55
+ import pulumi_azure_native as azure
56
+ from pulumi_django_azure import DjangoDeployment
57
+
58
+ stack = pulumi.get_stack()
59
+ config = pulumi.Config()
60
+
61
+
62
+ # Create resource group
63
+ rg = azure.resources.ResourceGroup(f"rg-{stack}")
64
+
65
+ # Create VNet
66
+ vnet = azure.network.VirtualNetwork(
67
+ f"vnet-{stack}",
68
+ resource_group_name=rg.name,
69
+ address_space=azure.network.AddressSpaceArgs(
70
+ address_prefixes=["10.0.0.0/16"],
71
+ ),
72
+ )
73
+
74
+ # Deploy the website and all its components
75
+ django = DjangoDeployment(
76
+ stack,
77
+ tenant_id="abc123...",
78
+ resource_group_name=rg.name,
79
+ vnet=vnet,
80
+ pgsql_ip_prefix="10.0.10.0/24",
81
+ appservice_ip_prefix="10.0.20.0/24",
82
+ app_service_sku=azure.web.SkuDescriptionArgs(
83
+ name="B2",
84
+ tier="Basic",
85
+ ),
86
+ storage_account_name="mystorageaccount",
87
+ cdn_host="cdn.example.com",
88
+ )
89
+
90
+ django.add_django_website(
91
+ name="web",
92
+ db_name="mywebsite",
93
+ repository_url="git@gitlab.com:project/website.git",
94
+ repository_branch="main",
95
+ website_hosts=["example.com", "www.example.com"],
96
+ django_settings_module="mywebsite.settings.production",
97
+ comms_data_location="europe",
98
+ comms_domains=["mydomain.com"],
99
+ )
100
+
101
+ django.add_database_administrator(
102
+ object_id="a1b2c3....",
103
+ user_name="user@example.com",
104
+ tenant_id="a1b2c3....",
105
+ )
106
+ ```
107
+
108
+ ## Changes to your Django project
109
+ 1. Add `pulumi_django_azure` to your `INSTALLED_APPS`
110
+ 2. Add to your settings file:
111
+ ```python
112
+ from pulumi_django_azure.settings import * # noqa: F403
113
+
114
+ # This will provide the management command to purge the CDN and cache
115
+ INSTALLED_APPS += ["pulumi_django_azure"]
116
+
117
+ # This will provide the health check middleware that will also take care of credential rotation.
118
+ MIDDLEWARE += ["pulumi_django_azure.middleware.HealthCheckMiddleware"]
119
+ ```
120
+ This will pre-configure most settings to make your app work on Azure. You can check the source for details,
121
+ and ofcourse override any value after importing them.
122
+
123
+
124
+ ## Deployment steps
125
+ 1. Deploy without custom hosts (for CDN and websites)
126
+ 2. Configure the PostgreSQL server (create and grant permissions to role for your websites)
127
+ 3. Retrieve the deployment SSH key and configure your remote GIT repository with it
128
+ 4. Configure your CDN host (add the CNAME record)
129
+ 5. Configure your custom website domains (add CNAME/A record and TXT validation records)
130
+ 6. Re-deploy with custom hosts
131
+ 7. Re-deploy once more to enable HTTPS on website domains
132
+ 8. Manually activate HTTPS on the CDN host
133
+ 9. Go to the e-mail communications service on Azure and configure DKIM, SPF,... for your custom domains.
134
+
135
+ ## Custom domain name for CDN
136
+ When deploying the first time, you will get a `cdn_cname` output. You need to create a CNAME to this domain before the deployment of the custom domain will succeed.
137
+
138
+ You can safely deploy with the failing CustomDomain to get the CNAME, create the record and then deploy again.
139
+
140
+ To enable HTTPS, you need to do this manually in the console. This is because of a limitation in the Azure API:
141
+ https://github.com/Azure/azure-rest-api-specs/issues/17498
142
+
143
+ ## Custom domain names for web application
144
+ Because of a circular dependency in custom domain name bindings and certificates that is out of our control, you need to deploy the stack twice.
145
+
146
+ The first time will create the bindings without a certificate.
147
+ The second deployment will then create the certificate for the domain (which is only possible if the binding exists), but also set the fingerprint of that certificate on the binding.
148
+
149
+ To make the certificate work, you need to create a TXT record named `asuid` point to the output of `{your_app}_site_domain_verification_id`. For example:
150
+
151
+ ```
152
+ asuid.mywebsite.com. TXT "A1B2C3D4E5..."
153
+ asuid.www.mywebsite.com. TXT "A1B2C3D4E5..."
154
+ ```
155
+
156
+ ## Database authentication
157
+ The PostgreSQL uses Entra ID authentication only, no passwords.
158
+
159
+ ### Administrator login
160
+ If you want to log in to the database yourself, you can add yourself as an administrator with the `add_database_administrator` function.
161
+ Your username is your e-mailaddress, a temporary password can be obtained using `az account get-access-token`.
162
+
163
+ You can use this method to log in to pgAdmin.
164
+
165
+ ### Application
166
+ Refer to this documentation:
167
+ https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-manage-azure-ad-users#create-a-role-using-microsoft-entra-object-identifier
168
+
169
+ In short, run something like this in the `postgres` database:
170
+ ```
171
+ SELECT * FROM pgaadauth_create_principal_with_oid('web_managed_identity', 'c8b25b85-d060-4cfc-bad4-b8581cfdf946', 'service', false, false);
172
+ ```
173
+ Replace the GUID of course with the managed identity our web app gets.
174
+
175
+ The name of the role is outputted by `{your_app}_site_db_user`
176
+
177
+ Be sure to grant this role the correct permissions too.
178
+
179
+ ## pgAdmin specifics
180
+ pgAdmin will be created with a default login:
181
+ * Login: dbadmin@dbadmin.net
182
+ * Password: dbadmin
183
+
184
+ Best practice is to log in right away, create a user for yourself and delete this default user.
185
+
186
+ ## Azure OAuth2 / Django Social Auth
187
+ If you want to set up login with Azure, which would make sense since you are in the ecosystem, you need to create an App Registration in Entra ID, create a secret and then register these settings in your stack:
188
+ ```
189
+ pulumi config set --secret --path 'mywebsite_social_auth_azure.key' secret_ID
190
+ pulumi config set --secret --path 'mywebsite_social_auth_azure.secret' secret_value
191
+ pulumi config set --secret --path 'mywebsite_social_auth_azure.tenant_id' directory_tenant_id
192
+ pulumi config set --secret --path 'mywebsite_social_auth_azure.client_id' application_id
193
+ ```
194
+
195
+ Then in your Django deployment, pass to the `add_django_website` command:
196
+ ```
197
+ secrets={
198
+ "mywebsite_social_auth_azure": "AZURE_OAUTH",
199
+ },
200
+ ```
201
+
202
+ The value will be automatically stored in the vault where the application has access to.
203
+ The environment variable will be suffixed with `_SECRET_NAME`.
204
+
205
+ Then, in your application, retrieve this data from the vault, e.g.:
206
+ ```python
207
+ # Social Auth settings
208
+ oauth_secret = AZURE_KEY_VAULT_CLIENT.get_secret(env("AZURE_OAUTH_SECRET_NAME"))
209
+ oauth_secret = json.loads(oauth_secret.value)
210
+ SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = oauth_secret["client_id"]
211
+ SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = oauth_secret["secret"]
212
+ SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = oauth_secret["tenant_id"]
213
+ SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ["username", "first_name", "last_name", "email"]
214
+ SOCIAL_AUTH_POSTGRES_JSONFIELD = True
215
+
216
+ AUTHENTICATION_BACKENDS = (
217
+ "social_core.backends.azuread_tenant.AzureADTenantOAuth2",
218
+ "django.contrib.auth.backends.ModelBackend",
219
+ )
220
+ ```
221
+
222
+ And of course add the login button somewhere, following Django Social Auth instructions.
223
+
224
+ ## Automate deployments
225
+ When using a service like GitLab, you can configure a Webhook to fire upon a push to your branch.
226
+
227
+ You need to download the deployment profile to obtain the deployment username and password, and then you can construct a URL like this:
228
+
229
+ ```
230
+ https://{user}:{pass}@{appname}.scm.azurewebsites.net/deploy
231
+
232
+ ```
233
+
234
+ ```
235
+ https://{appname}.scm.azurewebsites.net/api/sshkey?ensurePublicKey=1
236
+ ```
237
+
238
+ Be sure to configure the SSH key that Azure will use on GitLab side. You can obtain it using:
239
+
240
+ This would then trigger a redeploy everytime you make a commit to your live branch.
241
+
242
+
243
+ ## Change requests
244
+ I created this for internal use but since it took me a while to puzzle all the things together I decided to share it.
245
+ Therefore this project is not super generic, but tailored to my needs. I am however open to pull or change requests to improve this project or to make it more usable for others.
246
+
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["setuptools>=61.0.0", "wheel"]
3
- build-backend = "setuptools.build_meta"
2
+ requires = ["poetry-core"]
3
+ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [project]
6
6
  name = "pulumi-django-azure"
7
- version = "1.0.35"
7
+ version = "1.0.37"
8
8
  description = "Simply deployment of Django on Azure with Pulumi"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Maarten Ureel", email = "maarten@youreal.eu" }]
@@ -15,17 +15,17 @@ classifiers = [
15
15
  ]
16
16
  keywords = ["django", "pulumi", "azure"]
17
17
  dependencies = [
18
- "azure-identity (>=1.24.0,<2.0.0)",
18
+ "azure-identity (>=1.25.0,<2.0.0)",
19
19
  "azure-keyvault-secrets (>=4.10.0,<5.0.0)",
20
20
  "azure-mgmt-cdn (>=13.1.1,<14.0.0)",
21
21
  "azure-mgmt-resource (>=24.0.0,<25.0.0)",
22
- "django (>=5.2.5,<6.0.0)",
23
- "django-azure-communication-email (>=1.3.2,<2.0.0)",
22
+ "django (>=5.2.7,<6.0.0)",
23
+ "django-azure-communication-email (>=1.4.0,<2.0.0)",
24
24
  "django-environ (>=0.12.0,<0.13.0)",
25
25
  "django-redis (>=6.0.0,<7.0.0)",
26
26
  "django-storages[azure] (>=1.14.6,<2.0.0)",
27
- "pulumi (>=3.189.0)",
28
- "pulumi-azure-native (>=3.7.1)",
27
+ "pulumi (>=3.199.0)",
28
+ "pulumi-azure-native (>=3.8.0)",
29
29
  "pulumi-random (>=4.18.3)",
30
30
  "redis[hiredis] (>=6.4.0,<7.0.0)",
31
31
  "tenacity (>=9.1.2,<10.0.0)"
@@ -35,35 +35,29 @@ requires-python = ">=3.11,<3.14"
35
35
  [project.urls]
36
36
  Homepage = "https://gitlab.com/MaartenUreel/pulumi-django-azure"
37
37
 
38
- [tool.poetry]
39
- name = "pulumi-django-azure"
40
- version = "1.0.35"
41
- description = "Simplify deployment of Django websites on Azure with Pulumi"
42
- authors = ["Maarten Ureel <maarten@youreal.eu>"]
43
-
44
38
  [tool.poetry.dependencies]
45
39
  python = "^3.11,<3.14"
46
- azure-identity = "^1.24.0"
40
+ azure-identity = "^1.25.0"
47
41
  azure-keyvault-secrets = "^4.10.0"
48
42
  azure-mgmt-cdn = "^13.1.1"
49
43
  azure-mgmt-resource = "^24.0.0"
50
- django = "^5.2.5"
51
- django-azure-communication-email = "^1.3.2"
44
+ django = "^5.2.7"
45
+ django-azure-communication-email = "^1.4.0"
52
46
  django-environ = "^0.12.0"
53
47
  django-redis = "^6.0.0"
54
48
  django-storages = {extras = ["azure"], version = "^1.14.6"}
55
- pulumi = ">=3.189.0"
56
- pulumi-azure-native = ">=3.7.1"
49
+ pulumi = ">=3.199.0"
50
+ pulumi-azure-native = ">=3.8.0"
57
51
  pulumi-random = ">=4.18.3"
58
52
  redis = {extras = ["hiredis"], version = "^6.4.0"}
59
53
  tenacity = "^9.1.2"
60
54
 
61
55
  [tool.poetry.group.dev.dependencies]
62
56
  build = "^1.3.0"
63
- mkdocs-material = "^9.6.16"
57
+ mkdocs-material = "^9.6.21"
64
58
  pre-commit = "^4.3.0"
65
- ruff = "^0.12.8"
66
- twine = "^6.1.0"
59
+ ruff = "^0.13.2"
60
+ twine = "^6.2.0"
67
61
 
68
62
  [tool.ruff]
69
63
  line-length = 140
@@ -1,8 +0,0 @@
1
- [flake8]
2
- max-line-length = 140
3
- exclude = .git
4
-
5
- [egg_info]
6
- tag_build =
7
- tag_date = 0
8
-
@@ -1,244 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: pulumi-django-azure
3
- Version: 1.0.35
4
- Summary: Simply deployment of Django on Azure with Pulumi
5
- Author-email: Maarten Ureel <maarten@youreal.eu>
6
- License-Expression: MIT
7
- Project-URL: Homepage, https://gitlab.com/MaartenUreel/pulumi-django-azure
8
- Keywords: django,pulumi,azure
9
- Classifier: Programming Language :: Python
10
- Classifier: Programming Language :: Python :: 3
11
- Requires-Python: <3.14,>=3.11
12
- Description-Content-Type: text/markdown
13
- Requires-Dist: azure-identity<2.0.0,>=1.24.0
14
- Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.10.0
15
- Requires-Dist: azure-mgmt-cdn<14.0.0,>=13.1.1
16
- Requires-Dist: azure-mgmt-resource<25.0.0,>=24.0.0
17
- Requires-Dist: django<6.0.0,>=5.2.5
18
- Requires-Dist: django-azure-communication-email<2.0.0,>=1.3.2
19
- Requires-Dist: django-environ<0.13.0,>=0.12.0
20
- Requires-Dist: django-redis<7.0.0,>=6.0.0
21
- Requires-Dist: django-storages[azure]<2.0.0,>=1.14.6
22
- Requires-Dist: pulumi>=3.189.0
23
- Requires-Dist: pulumi-azure-native>=3.7.1
24
- Requires-Dist: pulumi-random>=4.18.3
25
- Requires-Dist: redis[hiredis]<7.0.0,>=6.4.0
26
- Requires-Dist: tenacity<10.0.0,>=9.1.2
27
-
28
- # Pulumi Django Deployment
29
-
30
- This project aims to make a simple Django deployment on Azure easier.
31
-
32
- To have a proper and secure environment, we need these components:
33
- * Storage account for media and static files
34
- * CDN endpoint in front with a domain name of our choosing
35
- * PostgreSQL server
36
- * Azure Communication Services to send e-mails
37
- * Webapp with multiple custom host names and managed SSL for the website itself
38
- * Azure Key Vault per application
39
- * Webapp running pgAdmin
40
-
41
- ## Project requirements
42
-
43
- ## Installation
44
- This package is published on PyPi, so you can just add pulumi-django-azure to your requirements file.
45
-
46
- To use a specific branch in your project, add to pyproject.toml dependencies:
47
- ```
48
- pulumi-django-azure = { git = "git@gitlab.com:MaartenUreel/pulumi-django-azure.git", branch = "dev" }
49
- ```
50
-
51
- A simple project could look like this:
52
- ```python
53
- import pulumi
54
- import pulumi_azure_native as azure
55
- from pulumi_django_azure import DjangoDeployment
56
-
57
- stack = pulumi.get_stack()
58
- config = pulumi.Config()
59
-
60
-
61
- # Create resource group
62
- rg = azure.resources.ResourceGroup(f"rg-{stack}")
63
-
64
- # Create VNet
65
- vnet = azure.network.VirtualNetwork(
66
- f"vnet-{stack}",
67
- resource_group_name=rg.name,
68
- address_space=azure.network.AddressSpaceArgs(
69
- address_prefixes=["10.0.0.0/16"],
70
- ),
71
- )
72
-
73
- # Deploy the website and all its components
74
- django = DjangoDeployment(
75
- stack,
76
- tenant_id="abc123...",
77
- resource_group_name=rg.name,
78
- vnet=vnet,
79
- pgsql_ip_prefix="10.0.10.0/24",
80
- appservice_ip_prefix="10.0.20.0/24",
81
- app_service_sku=azure.web.SkuDescriptionArgs(
82
- name="B2",
83
- tier="Basic",
84
- ),
85
- storage_account_name="mystorageaccount",
86
- cdn_host="cdn.example.com",
87
- )
88
-
89
- django.add_django_website(
90
- name="web",
91
- db_name="mywebsite",
92
- repository_url="git@gitlab.com:project/website.git",
93
- repository_branch="main",
94
- website_hosts=["example.com", "www.example.com"],
95
- django_settings_module="mywebsite.settings.production",
96
- comms_data_location="europe",
97
- comms_domains=["mydomain.com"],
98
- )
99
-
100
- django.add_database_administrator(
101
- object_id="a1b2c3....",
102
- user_name="user@example.com",
103
- tenant_id="a1b2c3....",
104
- )
105
- ```
106
-
107
- ## Changes to your Django project
108
- 1. Add `pulumi_django_azure` to your `INSTALLED_APPS`
109
- 2. Add to your settings file:
110
- ```python
111
- from pulumi_django_azure.settings import * # noqa: F403
112
-
113
- # This will provide the management command to purge the CDN and cache
114
- INSTALLED_APPS += ["pulumi_django_azure"]
115
-
116
- # This will provide the health check middleware that will also take care of credential rotation.
117
- MIDDLEWARE += ["pulumi_django_azure.middleware.HealthCheckMiddleware"]
118
- ```
119
- This will pre-configure most settings to make your app work on Azure. You can check the source for details,
120
- and ofcourse override any value after importing them.
121
-
122
-
123
- ## Deployment steps
124
- 1. Deploy without custom hosts (for CDN and websites)
125
- 2. Configure the PostgreSQL server (create and grant permissions to role for your websites)
126
- 3. Retrieve the deployment SSH key and configure your remote GIT repository with it
127
- 4. Configure your CDN host (add the CNAME record)
128
- 5. Configure your custom website domains (add CNAME/A record and TXT validation records)
129
- 6. Re-deploy with custom hosts
130
- 7. Re-deploy once more to enable HTTPS on website domains
131
- 8. Manually activate HTTPS on the CDN host
132
- 9. Go to the e-mail communications service on Azure and configure DKIM, SPF,... for your custom domains.
133
-
134
- ## Custom domain name for CDN
135
- When deploying the first time, you will get a `cdn_cname` output. You need to create a CNAME to this domain before the deployment of the custom domain will succeed.
136
-
137
- You can safely deploy with the failing CustomDomain to get the CNAME, create the record and then deploy again.
138
-
139
- To enable HTTPS, you need to do this manually in the console. This is because of a limitation in the Azure API:
140
- https://github.com/Azure/azure-rest-api-specs/issues/17498
141
-
142
- ## Custom domain names for web application
143
- Because of a circular dependency in custom domain name bindings and certificates that is out of our control, you need to deploy the stack twice.
144
-
145
- The first time will create the bindings without a certificate.
146
- The second deployment will then create the certificate for the domain (which is only possible if the binding exists), but also set the fingerprint of that certificate on the binding.
147
-
148
- To make the certificate work, you need to create a TXT record named `asuid` point to the output of `{your_app}_site_domain_verification_id`. For example:
149
-
150
- ```
151
- asuid.mywebsite.com. TXT "A1B2C3D4E5..."
152
- asuid.www.mywebsite.com. TXT "A1B2C3D4E5..."
153
- ```
154
-
155
- ## Database authentication
156
- The PostgreSQL uses Entra ID authentication only, no passwords.
157
-
158
- ### Administrator login
159
- If you want to log in to the database yourself, you can add yourself as an administrator with the `add_database_administrator` function.
160
- Your username is your e-mailaddress, a temporary password can be obtained using `az account get-access-token`.
161
-
162
- You can use this method to log in to pgAdmin.
163
-
164
- ### Application
165
- Refer to this documentation:
166
- https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-manage-azure-ad-users#create-a-role-using-microsoft-entra-object-identifier
167
-
168
- In short, run something like this in the `postgres` database:
169
- ```
170
- SELECT * FROM pgaadauth_create_principal_with_oid('web_managed_identity', 'c8b25b85-d060-4cfc-bad4-b8581cfdf946', 'service', false, false);
171
- ```
172
- Replace the GUID of course with the managed identity our web app gets.
173
-
174
- The name of the role is outputted by `{your_app}_site_db_user`
175
-
176
- Be sure to grant this role the correct permissions too.
177
-
178
- ## pgAdmin specifics
179
- pgAdmin will be created with a default login:
180
- * Login: dbadmin@dbadmin.net
181
- * Password: dbadmin
182
-
183
- Best practice is to log in right away, create a user for yourself and delete this default user.
184
-
185
- ## Azure OAuth2 / Django Social Auth
186
- If you want to set up login with Azure, which would make sense since you are in the ecosystem, you need to create an App Registration in Entra ID, create a secret and then register these settings in your stack:
187
- ```
188
- pulumi config set --secret --path 'mywebsite_social_auth_azure.key' secret_ID
189
- pulumi config set --secret --path 'mywebsite_social_auth_azure.secret' secret_value
190
- pulumi config set --secret --path 'mywebsite_social_auth_azure.tenant_id' directory_tenant_id
191
- pulumi config set --secret --path 'mywebsite_social_auth_azure.client_id' application_id
192
- ```
193
-
194
- Then in your Django deployment, pass to the `add_django_website` command:
195
- ```
196
- secrets={
197
- "mywebsite_social_auth_azure": "AZURE_OAUTH",
198
- },
199
- ```
200
-
201
- The value will be automatically stored in the vault where the application has access to.
202
- The environment variable will be suffixed with `_SECRET_NAME`.
203
-
204
- Then, in your application, retrieve this data from the vault, e.g.:
205
- ```python
206
- # Social Auth settings
207
- oauth_secret = AZURE_KEY_VAULT_CLIENT.get_secret(env("AZURE_OAUTH_SECRET_NAME"))
208
- oauth_secret = json.loads(oauth_secret.value)
209
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = oauth_secret["client_id"]
210
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = oauth_secret["secret"]
211
- SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = oauth_secret["tenant_id"]
212
- SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ["username", "first_name", "last_name", "email"]
213
- SOCIAL_AUTH_POSTGRES_JSONFIELD = True
214
-
215
- AUTHENTICATION_BACKENDS = (
216
- "social_core.backends.azuread_tenant.AzureADTenantOAuth2",
217
- "django.contrib.auth.backends.ModelBackend",
218
- )
219
- ```
220
-
221
- And of course add the login button somewhere, following Django Social Auth instructions.
222
-
223
- ## Automate deployments
224
- When using a service like GitLab, you can configure a Webhook to fire upon a push to your branch.
225
-
226
- You need to download the deployment profile to obtain the deployment username and password, and then you can construct a URL like this:
227
-
228
- ```
229
- https://{user}:{pass}@{appname}.scm.azurewebsites.net/deploy
230
-
231
- ```
232
-
233
- ```
234
- https://{appname}.scm.azurewebsites.net/api/sshkey?ensurePublicKey=1
235
- ```
236
-
237
- Be sure to configure the SSH key that Azure will use on GitLab side. You can obtain it using:
238
-
239
- This would then trigger a redeploy everytime you make a commit to your live branch.
240
-
241
-
242
- ## Change requests
243
- I created this for internal use but since it took me a while to puzzle all the things together I decided to share it.
244
- Therefore this project is not super generic, but tailored to my needs. I am however open to pull or change requests to improve this project or to make it more usable for others.
@@ -1,18 +0,0 @@
1
- README.md
2
- pyproject.toml
3
- setup.cfg
4
- src/pulumi_django_azure/__init__.py
5
- src/pulumi_django_azure/azure_helper.py
6
- src/pulumi_django_azure/context_processors.py
7
- src/pulumi_django_azure/django_deployment.py
8
- src/pulumi_django_azure/middleware.py
9
- src/pulumi_django_azure/settings.py
10
- src/pulumi_django_azure.egg-info/PKG-INFO
11
- src/pulumi_django_azure.egg-info/SOURCES.txt
12
- src/pulumi_django_azure.egg-info/dependency_links.txt
13
- src/pulumi_django_azure.egg-info/requires.txt
14
- src/pulumi_django_azure.egg-info/top_level.txt
15
- src/pulumi_django_azure/management/commands/__init__.py
16
- src/pulumi_django_azure/management/commands/purge_cache.py
17
- src/pulumi_django_azure/management/commands/purge_cdn.py
18
- src/pulumi_django_azure/management/commands/test_redis.py
@@ -1,14 +0,0 @@
1
- azure-identity<2.0.0,>=1.24.0
2
- azure-keyvault-secrets<5.0.0,>=4.10.0
3
- azure-mgmt-cdn<14.0.0,>=13.1.1
4
- azure-mgmt-resource<25.0.0,>=24.0.0
5
- django<6.0.0,>=5.2.5
6
- django-azure-communication-email<2.0.0,>=1.3.2
7
- django-environ<0.13.0,>=0.12.0
8
- django-redis<7.0.0,>=6.0.0
9
- django-storages[azure]<2.0.0,>=1.14.6
10
- pulumi>=3.189.0
11
- pulumi-azure-native>=3.7.1
12
- pulumi-random>=4.18.3
13
- redis[hiredis]<7.0.0,>=6.4.0
14
- tenacity<10.0.0,>=9.1.2
@@ -1 +0,0 @@
1
- pulumi_django_azure