Skip to content

Commit d13ae74

Browse files
committedJul 4, 2024
🐳 Fix proxy-setup with docker-compose
We need to rewrite the Host header when we make API calls to localhost:8001 and localhost:8002 from the test suite, since the URLs need to be resolvable from container-to-container. However, the previous setup broke the local docker-compose setup for login to the admin because the cookie domain is set to the internal service names, so logging in was no longer possible due to a missing csrftoken cookie (as we log in on localhost, not the internal service names). Only overwriting the Host header in /api/ subpaths is a pragmatic approach that should solve this, since CSRF cookies are not used in the API. Furthermore, the browser sends an Oigin header of localhost:800x, but this is different from the Host seen by Django due to the proxy_pass directive, which also results in CSRF issues. The solution is to enable X-Forwarded-Host header and calculate it in nginx, so that it matches. For this match to happen, we also need to add the port number and keep the mapped docker-compose port and nginx server block ports in sync, since different ports lead to different origins.
1 parent a6586c6 commit d13ae74

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed
 

‎docker/docker-compose.objects-apis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525
- DJANGO_SETTINGS_MODULE=objecttypes.conf.docker
2626
- SECRET_KEY=${SECRET_KEY:-fgv=c0hz&tl*8*3m3893@m+1pstrvidc9e^5@fpspmg%cy$15d}
2727
- ALLOWED_HOSTS=*
28+
- USE_X_FORWARDED_HOST=true
2829
- DB_HOST=objects-objecttypes-db
2930
- DB_NAME=objecttypes
3031
- DB_USER=objecttypes
@@ -44,11 +45,12 @@ services:
4445
- open-forms-dev
4546

4647
objects-web:
47-
image: maykinmedia/objects-api:${OBJECTS_VERSION:-2.3.1}
48+
image: maykinmedia/objects-api:${OBJECTS_VERSION:-2.3.2}
4849
environment: &objects_web_env
4950
- DJANGO_SETINGS_MODULE=objects.conf.docker
5051
- SECRET_KEY=${SECRET_KEY:-fgv=c0hz&tl*8*3m3893@m+1pstrvidc9e^5@fpspmg%cy$15d}
5152
- ALLOWED_HOSTS=*
53+
- USE_X_FORWARDED_HOST=true
5254
- DB_HOST=objects-objecttypes-db
5355
- DB_NAME=objects
5456
- DB_USER=objects
@@ -86,8 +88,8 @@ services:
8688
volumes:
8789
- ./objects-apis/nginx.conf:/etc/nginx/conf.d/default.conf
8890
ports:
89-
- '8001:80'
90-
- '8002:81'
91+
- '8001:8001'
92+
- '8002:8002'
9193
depends_on:
9294
- objects-web
9395
- objecttypes-web

‎docker/objects-apis/fixtures/objects_api_fixtures.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
},
2929
{
3030
"model": "token.tokenauth",
31-
"pk": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
31+
"pk": 1,
3232
"fields": {
33+
"token": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
3334
"contact_person": "Admin",
3435
"email": "admin@example.com",
3536
"organization": "",
@@ -43,7 +44,7 @@
4344
"model": "token.permission",
4445
"pk": 1,
4546
"fields": {
46-
"token_auth": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
47+
"token_auth": 1,
4748
"object_type": 1,
4849
"mode": "read_and_write",
4950
"use_fields": false,
@@ -54,7 +55,7 @@
5455
"model": "token.permission",
5556
"pk": 2,
5657
"fields": {
57-
"token_auth": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
58+
"token_auth": 1,
5859
"object_type": 2,
5960
"mode": "read_and_write",
6061
"use_fields": false,
@@ -65,7 +66,7 @@
6566
"model": "token.permission",
6667
"pk": 3,
6768
"fields": {
68-
"token_auth": "7657474c3d75f56ae0abd0d1bf7994b09964dca9",
69+
"token_auth": 1,
6970
"object_type": 3,
7071
"mode": "read_and_write",
7172
"use_fields": false,

‎docker/objects-apis/nginx.conf

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
server {
2-
listen 80;
2+
listen 8001;
33
server_name localhost;
44

55
location / {
6+
proxy_pass http://objecttypes-web:8000;
7+
proxy_set_header X-Forwarded-Host $host:$server_port;
8+
}
9+
10+
location /api/ {
611
proxy_pass http://objecttypes-web:8000;
712
proxy_set_header Host objecttypes-web:8000;
813
}
914
}
1015

1116
server {
12-
listen 81;
17+
listen 8002;
1318
server_name localhost;
1419

1520
location / {
21+
proxy_pass http://objects-web:8000;
22+
proxy_set_header X-Forwarded-Host $host:$server_port;
23+
}
24+
25+
location /api/ {
1626
proxy_pass http://objects-web:8000;
1727
proxy_set_header Host objects-web:8000;
1828
}

0 commit comments

Comments
 (0)