forked from mtusi2026/mtusi2026lab7
Implement lab 7 security and mTLS
This commit is contained in:
+343
@@ -0,0 +1,343 @@
|
||||
name: mtusi2026lab7
|
||||
|
||||
services:
|
||||
lb:
|
||||
image: nginx:1.27.2
|
||||
ports:
|
||||
- "8443:8443"
|
||||
volumes:
|
||||
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./certs:/etc/nginx/certs:ro
|
||||
depends_on:
|
||||
api1:
|
||||
condition: service_healthy
|
||||
api2:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
|
||||
api1: &api
|
||||
build: .
|
||||
image: mtusi2026lab7-app:local
|
||||
environment: &api-env
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
APP_ENV: lab
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
S3_ENDPOINT: http://seaweedfs:8333
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
OTEL_SERVICE_NAME: messenger-api
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /run/certs/ca.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /run/certs/api1-client.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_KEY: /run/certs/api1-client.key
|
||||
DEPLOYMENT_ENVIRONMENT: lab
|
||||
INSTANCE_ID: api1
|
||||
OPENBAO_ENABLED: ${OPENBAO_ENABLED:-false}
|
||||
OPENBAO_ADDR: http://openbao:8200
|
||||
OPENBAO_TOKEN_FILE: /run/secrets/openbao_token
|
||||
REDIS_MTLS: "true"
|
||||
REDIS_TLS_CA: /run/certs/ca.crt
|
||||
REDIS_TLS_CERT: /run/certs/api1-client.crt
|
||||
REDIS_TLS_KEY: /run/certs/api1-client.key
|
||||
secrets: &api-secrets
|
||||
- database_url
|
||||
- cache_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- openbao_token
|
||||
volumes:
|
||||
- ./certs:/run/certs:ro
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
cap_drop: ["ALL"]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
seaweedfs:
|
||||
condition: service_healthy
|
||||
otel-collector:
|
||||
condition: service_started
|
||||
healthcheck: &api-healthcheck
|
||||
test: ["CMD", "python", "-c", "import ssl,urllib.request; c=ssl._create_unverified_context(); c.load_cert_chain('/run/certs/lb-client.crt','/run/certs/lb-client.key'); urllib.request.urlopen('https://127.0.0.1:8080/health/ready', context=c, timeout=2)"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 320M
|
||||
command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "5", "--ssl-keyfile", "/run/certs/api1.key", "--ssl-certfile", "/run/certs/api1.crt", "--ssl-ca-certs", "/run/certs/ca.crt", "--ssl-cert-reqs", "2"]
|
||||
|
||||
api2:
|
||||
<<: *api
|
||||
environment:
|
||||
<<: *api-env
|
||||
INSTANCE_ID: api2
|
||||
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /run/certs/api2-client.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_KEY: /run/certs/api2-client.key
|
||||
REDIS_TLS_CERT: /run/certs/api2-client.crt
|
||||
REDIS_TLS_KEY: /run/certs/api2-client.key
|
||||
command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "5", "--ssl-keyfile", "/run/certs/api2.key", "--ssl-certfile", "/run/certs/api2.crt", "--ssl-ca-certs", "/run/certs/ca.crt", "--ssl-cert-reqs", "2"]
|
||||
|
||||
worker:
|
||||
build: .
|
||||
image: mtusi2026lab7-app:local
|
||||
command: ["python3", "-m", "app.worker"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
S3_ENDPOINT: http://seaweedfs:8333
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
OTEL_SERVICE_NAME: messenger-worker
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /run/certs/ca.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /run/certs/worker-client.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_KEY: /run/certs/worker-client.key
|
||||
DEPLOYMENT_ENVIRONMENT: lab
|
||||
INSTANCE_ID: worker1
|
||||
OPENBAO_ENABLED: ${OPENBAO_ENABLED:-false}
|
||||
OPENBAO_ADDR: http://openbao:8200
|
||||
OPENBAO_TOKEN_FILE: /run/secrets/openbao_token
|
||||
REDIS_MTLS: "true"
|
||||
REDIS_TLS_CA: /run/certs/ca.crt
|
||||
REDIS_TLS_CERT: /run/certs/worker-client.crt
|
||||
REDIS_TLS_KEY: /run/certs/worker-client.key
|
||||
secrets: *api-secrets
|
||||
volumes:
|
||||
- ./certs:/run/certs:ro
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
cap_drop: ["ALL"]
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import pathlib; assert b'app.worker' in pathlib.Path('/proc/1/cmdline').read_bytes()"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
seaweedfs:
|
||||
condition: service_healthy
|
||||
otel-collector:
|
||||
condition: service_started
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 384M
|
||||
|
||||
postgres:
|
||||
image: postgres:16.4
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
redis:
|
||||
image: redis:7.4.1
|
||||
user: "999:999"
|
||||
command: ["redis-server", "--port", "0", "--tls-port", "6379", "--tls-cert-file", "/run/certs/redis.crt", "--tls-key-file", "/run/certs/redis.key", "--tls-ca-cert-file", "/run/certs/ca.crt", "--tls-auth-clients", "yes", "--appendonly", "yes", "--appendfsync", "everysec"]
|
||||
volumes:
|
||||
- redisdata:/data
|
||||
- ./certs:/run/certs:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "--tls", "--cacert", "/run/certs/ca.crt", "--cert", "/run/certs/redis-health-client.crt", "--key", "/run/certs/redis-health-client.key", "-h", "redis", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
cap_drop: ["ALL"]
|
||||
|
||||
seaweedfs:
|
||||
image: chrislusf/seaweedfs:3.73
|
||||
command:
|
||||
- server
|
||||
- -dir=/data
|
||||
- -s3
|
||||
- -s3.port=8333
|
||||
- -s3.config=/etc/seaweedfs/s3.json
|
||||
volumes:
|
||||
- seaweed_data:/data
|
||||
- ./secrets/s3.json:/etc/seaweedfs/s3.json:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "nc", "-z", "127.0.0.1", "8333"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:0.114.0
|
||||
user: "0:0"
|
||||
command: ["--config=/etc/otel-collector/config.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel-collector/config.yaml:/etc/otel-collector/config.yaml:ro
|
||||
- ./certs:/etc/otel/certs:ro
|
||||
depends_on:
|
||||
- prometheus
|
||||
- loki
|
||||
- tempo
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 200M
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v3.0.1
|
||||
command:
|
||||
- --config.file=/etc/prometheus/prometheus.yml
|
||||
- --storage.tsdb.path=/prometheus
|
||||
- --storage.tsdb.retention.time=7d
|
||||
volumes:
|
||||
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
|
||||
- prometheus_data:/prometheus
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
loki:
|
||||
image: grafana/loki:3.2.1
|
||||
command: ["-config.file=/etc/loki/loki-config.yaml"]
|
||||
volumes:
|
||||
- ./infra/loki/loki-config.yaml:/etc/loki/loki-config.yaml:ro
|
||||
- loki_data:/loki
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
tempo:
|
||||
image: grafana/tempo:2.6.1
|
||||
command: ["-config.file=/etc/tempo/tempo.yaml"]
|
||||
volumes:
|
||||
- ./infra/tempo/tempo.yaml:/etc/tempo/tempo.yaml:ro
|
||||
- tempo_data:/var/tempo
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:11.3.1
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD}
|
||||
GF_AUTH_ANONYMOUS_ENABLED: "false"
|
||||
GF_FEATURE_TOGGLES_ENABLE: traceqlEditor
|
||||
volumes:
|
||||
- ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
|
||||
- ./infra/grafana/dashboards:/etc/grafana/dashboards:ro
|
||||
- grafana_data:/var/lib/grafana
|
||||
depends_on:
|
||||
- prometheus
|
||||
- loki
|
||||
- tempo
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
openbao:
|
||||
image: openbao/openbao:2.2.0
|
||||
command: ["server", "-config=/bao/config/config.hcl"]
|
||||
volumes:
|
||||
- ./infra/openbao/config.hcl:/bao/config/config.hcl:ro
|
||||
- ./infra/openbao/policies:/bao/config/policies:ro
|
||||
- bao_data:/bao/data
|
||||
cap_add: ["IPC_LOCK"]
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "--server-response", "http://127.0.0.1:8200/v1/sys/health"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
redisdata:
|
||||
seaweed_data:
|
||||
prometheus_data:
|
||||
loki_data:
|
||||
tempo_data:
|
||||
grafana_data:
|
||||
bao_data:
|
||||
|
||||
secrets:
|
||||
database_url:
|
||||
file: ./secrets/database_url.txt
|
||||
cache_url:
|
||||
file: ./secrets/cache_url.txt
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password.txt
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id.txt
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key.txt
|
||||
openbao_token:
|
||||
file: ./secrets/openbao_token.txt
|
||||
Reference in New Issue
Block a user