Skip to content

Commit c595661

Browse files
committedFeb 20, 2023
refactor: traefik configuration for ssl deployment
further onto the work on porting the traefik configuration to completely be dynamic as reported in anomaly/lab-python-server#62 this refactor moves the ssl provisioning configuration to labels and ensures that all security parameters are properly applied to the reverse proxy e.g tls version the api reverse proxies properly from the container and there's configuration for the web client to be proixed from a bucket which at the moment is not working, the configuration does not error but the gateway times out
1 parent 976ae21 commit c595661

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed
 

‎.env.development

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
PROJ_NAME=mock
2-
PROJ_FQDN=mock.local
3-
41
POSTGRES_USER=postgres
52
POSTGRES_PASSWORD=postgres
63
PGADMIN_DEFAULT_PASSWORD=postgres

‎docker-compose.yml

+51-10
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,57 @@
66
# https://docs.docker.com/compose/compose-file/compose-versioning/
77
version: "3.8"
88

9+
# These are volumes managed by Docker
910
volumes:
1011
lab_mock:
12+
letsencrypt:
1113

1214
services:
1315

1416
reverse-proxy:
1517
container_name: reverse-proxy
1618
image: traefik:v3.0
17-
command:
18-
# Remove this for production, this exposes the web UI
19-
- "--api.insecure=true"
20-
- "--providers.docker"
2119
# healthcheck:
2220
# test: ["CMD", "wget", "-q", "-O", "-", "http://localhost:8080/health"]
2321
# interval: 30s
2422
# timeout: 10s
25-
# retries: 3
23+
# retries: 3
24+
command:
25+
# Remove this for production, this exposes the web UI
26+
- "--providers.docker=true"
27+
- "--providers.docker.exposedbydefault=false"
28+
- "--entrypoints.web.address=:80"
29+
- "--entrypoints.http.http.redirections.entryPoint.to=:443"
30+
- "--entrypoints.http.http.redirections.entryPoint.scheme=https"
31+
- "--entrypoints.http.http.redirections.entrypoint.permanent=true"
32+
- "--entrypoints.https.address=:443"
33+
# This allows us to use the staging server for development
34+
# We could potentially move this to a variable name
35+
#- "--certificatesresolvers.letsencrypt.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
36+
- "--certificatesResolvers.letsencrypt.acme.email=${SOA_EMAIL}"
37+
- "--certificatesResolvers.letsencrypt.acme.storage=/letsencrypt/acme.json"
38+
- "--certificatesResolvers.letsencrypt.acme.httpChallenge.entrypoint=http"
2639
ports:
27-
# Remove this for production, this is the web UI
28-
- 8080:8080
29-
- 80:80
30-
- 443:443
40+
- "80:80"
41+
- "443:443"
3142
labels:
43+
- "traefik.enable=true"
44+
# Minimum SSL version set to TLS 1.2
45+
- "traefik.http.routers.${PROJ_NAME}-root.tls"
46+
# The rule host will determine what domain the SSL cert
47+
# will be provisioned for
48+
- "traefik.http.routers.${PROJ_NAME}-root.rule=Host(`${PROJ_FQDN}`)"
49+
- "traefik.http.routers.${PROJ_NAME}-root.tls.certResolver=letsencrypt"
50+
- "traefik.tls.options.default.minVersion=VersionTLS12"
51+
# Proxy the bucket or another container for the web client
52+
- "traefik.http.middlewares.bucket-header.headers.customrequestheaders.host=${BUCKET_FQDN}"
53+
# Declare a service to reverer proxy
54+
- "traefik.http.services.bucket-service.loadbalancer.server.url=http://${BUCKET_FQDN}"
55+
# Declare a router and attach the service to it
56+
- "traefik.http.routers.web-client.entrypoints=https"
57+
- "traefik.http.routers.web-client.rule=Host(`${PROJ_FQDN}`)"
58+
- "traefik.http.routers.web-client.service=bucket-service"
59+
- "traefik.http.routers.web-client.middlewares=bucket-header"
3260
# Send X-Frame-Options to DENY
3361
- "traefik.http.middlewares.testheader.headers.frameDeny=true"
3462
# HSTS security headers
@@ -43,12 +71,12 @@ services:
4371
# This is to expose the docker socker to the reverse proxy
4472
# for it to use the docker provider
4573
- /var/run/docker.sock:/var/run/docker.sock:ro
74+
- letsencrypt:/letsencrypt
4675
# The reverse proxy should be the last thing to be started
4776
# it depends on the entire stack to be healthy
4877
depends_on:
4978
- lab_mock
5079

51-
5280
# Mock application
5381
# - In development we read secrets from .env.development
5482
# - Provides a FastAPI based API that runs using uvicorn in development
@@ -59,8 +87,21 @@ services:
5987
dockerfile: Dockerfile
6088
env_file:
6189
- .env.development
90+
labels:
91+
# Explicitly tell Traefik to expose this container
92+
- "traefik.enable=true"
93+
# Declare a middleware that strips the api prefix, this
94+
# is required for FastaPI to mount on the root and for us
95+
# to proxy the urls on the /api endpoint
96+
- "traefik.http.middlewares.strip-api-prefix.stripprefix.prefixes=/api/"
97+
# The router for this container is going to respond to the host
98+
# of the project and root level url
99+
- "traefik.http.routers.${PROJ_NAME}-api.rule=Host(`${PROJ_FQDN}`) && PathPrefix(`/api/`)"
100+
- "traefik.http.routers.${PROJ_NAME}-api.middlewares=strip-api-prefix"
62101
restart: unless-stopped
63102
ports:
103+
# This is to test if the app is working locally
104+
# In production this would be proxied through traefik
64105
- "8000:80"
65106
volumes:
66107
- ./src/lab_mock:/opt/lab_mock

0 commit comments

Comments
 (0)
Please sign in to comment.