Закрыть внешний вход TLS и выдавать секреты через OpenBao.
LB ходит в API только с клиентским сертификатом нужной identity, остальные связи проверяют серверный сертификат, root token в приложение не попадает. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+18
-26
@@ -1,41 +1,33 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
# Copy is not required. scripts/bootstrap.ps1 creates .env and local secrets.
|
||||
# Nothing in this file is a credential.
|
||||
APP_ENV=lab
|
||||
LOG_LEVEL=info
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
CACHE_TTL_SECONDS=60
|
||||
SESSION_STORE=redis
|
||||
API_REPLICAS=2
|
||||
API_CPUS=0.50
|
||||
API_MEM=384m
|
||||
POSTGRES_CPUS=1.0
|
||||
POSTGRES_MEM=512m
|
||||
DB_POOL_MAX=4
|
||||
LB_DRAIN_SECONDS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=https://s3:9000
|
||||
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
|
||||
S3_PUBLIC_ENDPOINT=https://127.0.0.1:9000
|
||||
PRESIGN_TTL_SECONDS=60
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
BROKER_URL_FILE=/run/secrets/broker_url
|
||||
WORKER_CONCURRENCY=2
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
STAGING_RETENTION_SECONDS=86400
|
||||
OTEL_SERVICE_NAME=messenger-api
|
||||
# Worker must set a distinct service.name.
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
||||
OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
|
||||
# TLS and OpenBao: actual paths/ports depend on your implemented trust matrix.
|
||||
PUBLIC_BASE_URL=https://localhost:8443
|
||||
TLS_CA_FILE=/run/secrets/ca.crt
|
||||
TLS_CERT_FILE=/run/secrets/service.crt
|
||||
TLS_KEY_FILE=/run/secrets/service.key
|
||||
BAO_ADDR=https://openbao:8200
|
||||
BAO_CACERT=/run/secrets/bao-ca.crt
|
||||
# No root token here. Bootstrap operator -> short-lived service auth -> scoped token.
|
||||
TELEMETRY_ENABLED=1
|
||||
TRACE_SAMPLE_RATIO=1
|
||||
ARGON2_MEMORY_KIB=19456
|
||||
ARGON2_ITERATIONS=2
|
||||
ARGON2_PARALLELISM=1
|
||||
LOGIN_RATE_LIMIT=8
|
||||
LOGIN_RATE_WINDOW_SECONDS=60
|
||||
PUBLIC_BASE_URL=https://localhost:8443
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
*.sh text eol=lf
|
||||
*.cfg text eol=lf
|
||||
*.hcl text eol=lf
|
||||
*.yml text eol=lf
|
||||
*.yaml text eol=lf
|
||||
*.py text eol=lf
|
||||
Dockerfile text eol=lf
|
||||
@@ -3,6 +3,7 @@
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets/
|
||||
certs/
|
||||
certs/private/
|
||||
*.key
|
||||
*.p12
|
||||
|
||||
@@ -71,6 +71,8 @@ MAX_JSON_BYTES = int(_env("MAX_JSON_BYTES", "1048576"))
|
||||
TLS_CA_FILE = _env("TLS_CA_FILE", "")
|
||||
TLS_CERT_FILE = _env("TLS_CERT_FILE", "")
|
||||
TLS_KEY_FILE = _env("TLS_KEY_FILE", "")
|
||||
TLS_SERVER_CERT_FILE = _env("TLS_SERVER_CERT_FILE", "")
|
||||
TLS_SERVER_KEY_FILE = _env("TLS_SERVER_KEY_FILE", "")
|
||||
MTLS_PORT = int(_env("MTLS_PORT", "8443"))
|
||||
MTLS_ALLOW_FILE = _env("MTLS_ALLOW_FILE", "")
|
||||
BAO_ADDR = _env("BAO_ADDR", "")
|
||||
|
||||
+9
-2
@@ -11,12 +11,19 @@ from botocore.exceptions import BotoCoreError, ClientError
|
||||
from app import bao, config
|
||||
|
||||
|
||||
def _secret(name: str) -> str:
|
||||
path = os.environ.get(name + "_FILE")
|
||||
if path:
|
||||
return open(path, encoding="utf-8").read().strip()
|
||||
return os.environ.get(name, "")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
bao.apply()
|
||||
endpoint = os.environ.get("S3_ENDPOINT") or config.S3_ENDPOINT
|
||||
bucket = os.environ.get("S3_BUCKET") or config.S3_BUCKET
|
||||
access_key = os.environ.get("S3_ACCESS_KEY_ID") or config.S3_ACCESS_KEY_ID
|
||||
secret_key = os.environ.get("S3_SECRET_ACCESS_KEY") or config.S3_SECRET_ACCESS_KEY
|
||||
access_key = _secret("S3_ACCESS_KEY_ID") or config.S3_ACCESS_KEY_ID
|
||||
secret_key = _secret("S3_SECRET_ACCESS_KEY") or config.S3_SECRET_ACCESS_KEY
|
||||
verify = config.TLS_CA_FILE if endpoint.startswith("https://") and config.TLS_CA_FILE else True
|
||||
client = boto3.client(
|
||||
"s3",
|
||||
|
||||
+7
-3
@@ -80,17 +80,21 @@ async def _handle(client_reader: asyncio.StreamReader, client_writer: asyncio.St
|
||||
|
||||
|
||||
def _context() -> ssl.SSLContext:
|
||||
cert = config.TLS_SERVER_CERT_FILE or config.TLS_CERT_FILE
|
||||
key = config.TLS_SERVER_KEY_FILE or config.TLS_KEY_FILE
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
||||
context.load_cert_chain(config.TLS_CERT_FILE, config.TLS_KEY_FILE)
|
||||
context.load_cert_chain(cert, key)
|
||||
context.load_verify_locations(config.TLS_CA_FILE)
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
return context
|
||||
|
||||
|
||||
async def serve() -> None:
|
||||
if not (config.TLS_CERT_FILE and config.TLS_KEY_FILE and config.TLS_CA_FILE):
|
||||
raise SystemExit("TLS_CERT_FILE, TLS_KEY_FILE and TLS_CA_FILE are required")
|
||||
cert = config.TLS_SERVER_CERT_FILE or config.TLS_CERT_FILE
|
||||
key = config.TLS_SERVER_KEY_FILE or config.TLS_KEY_FILE
|
||||
if not (cert and key and config.TLS_CA_FILE and config.MTLS_ALLOW_FILE):
|
||||
raise SystemExit("server certificate, CA and MTLS_ALLOW_FILE are required")
|
||||
server = await asyncio.start_server(_handle, "0.0.0.0", config.MTLS_PORT, ssl=_context())
|
||||
logger.info("mTLS proxy listening on %s", config.MTLS_PORT)
|
||||
async with server:
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
"""Operator bootstrap for OpenBao. Prints status only, never tokens or secret values."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import ssl
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ADDR = os.environ["BAO_ADDR"].rstrip("/")
|
||||
CAFILE = os.environ["TLS_CA_FILE"]
|
||||
INIT_PATH = Path(os.environ.get("BAO_INIT_FILE", "/secrets/openbao-init.json"))
|
||||
SECRETS = Path("/secrets")
|
||||
POLICIES = Path("/policies")
|
||||
|
||||
|
||||
def _ctx() -> ssl.SSLContext:
|
||||
context = ssl.create_default_context(cafile=CAFILE)
|
||||
context.check_hostname = True
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
||||
return context
|
||||
|
||||
|
||||
def call(method: str, path: str, body: dict | None = None, token: str = "", accept: tuple[int, ...] = (200, 204)) -> tuple[int, dict]:
|
||||
data = None if body is None else json.dumps(body).encode("utf-8")
|
||||
request = urllib.request.Request(ADDR + path, data=data, method=method)
|
||||
request.add_header("Content-Type", "application/json")
|
||||
if token:
|
||||
request.add_header("X-Vault-Token", token)
|
||||
try:
|
||||
with urllib.request.urlopen(request, context=_ctx(), timeout=5) as response:
|
||||
raw = response.read()
|
||||
code = response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw = exc.read()
|
||||
code = exc.code
|
||||
if code not in accept:
|
||||
raise SystemExit("OpenBao %s %s failed with HTTP %s" % (method, path, code))
|
||||
if not raw:
|
||||
return code, {}
|
||||
return code, json.loads(raw.decode("utf-8"))
|
||||
|
||||
|
||||
def wait_until_reachable() -> None:
|
||||
for _ in range(60):
|
||||
try:
|
||||
call("GET", "/v1/sys/seal-status", accept=(200,))
|
||||
return
|
||||
except (SystemExit, urllib.error.URLError, TimeoutError, OSError):
|
||||
time.sleep(1)
|
||||
raise SystemExit("OpenBao did not become reachable")
|
||||
|
||||
|
||||
def read_text(name: str) -> str:
|
||||
return (SECRETS / name).read_text(encoding="utf-8").strip()
|
||||
|
||||
|
||||
def ensure_init() -> dict:
|
||||
code, status = call("GET", "/v1/sys/seal-status", accept=(200,))
|
||||
del code
|
||||
if not status.get("initialized"):
|
||||
_, payload = call(
|
||||
"POST",
|
||||
"/v1/sys/init",
|
||||
{"secret_shares": 1, "secret_threshold": 1},
|
||||
accept=(200,),
|
||||
)
|
||||
INIT_PATH.write_text(json.dumps(payload), encoding="utf-8")
|
||||
try:
|
||||
os.chmod(INIT_PATH, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
print("openbao initialized")
|
||||
return payload
|
||||
if not INIT_PATH.is_file():
|
||||
raise SystemExit("OpenBao is already initialized but the operator init file is missing")
|
||||
print("openbao already initialized")
|
||||
return json.loads(INIT_PATH.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def ensure_unsealed(material: dict) -> str:
|
||||
_, status = call("GET", "/v1/sys/seal-status", accept=(200,))
|
||||
if status.get("sealed", True):
|
||||
key = (material.get("keys") or [""])[0]
|
||||
call("POST", "/v1/sys/unseal", {"key": key}, accept=(200,))
|
||||
print("openbao unsealed")
|
||||
else:
|
||||
print("openbao already unsealed")
|
||||
token = material.get("root_token") or ""
|
||||
if not token:
|
||||
raise SystemExit("operator init file has no root token")
|
||||
return token
|
||||
|
||||
|
||||
def ensure_mounts(token: str) -> None:
|
||||
code, _ = call(
|
||||
"POST",
|
||||
"/v1/sys/mounts/secret",
|
||||
{"type": "kv", "options": {"version": "2"}},
|
||||
token=token,
|
||||
accept=(200, 204, 400),
|
||||
)
|
||||
if code == 400:
|
||||
print("kv mount already present")
|
||||
else:
|
||||
print("kv mount enabled")
|
||||
code, _ = call("POST", "/v1/sys/auth/approle", {"type": "approle"}, token=token, accept=(200, 204, 400))
|
||||
if code == 400:
|
||||
print("approle already present")
|
||||
else:
|
||||
print("approle enabled")
|
||||
|
||||
|
||||
def put_policy(token: str, name: str) -> None:
|
||||
text = (POLICIES / (name + ".hcl")).read_text(encoding="utf-8")
|
||||
call("PUT", "/v1/sys/policies/acl/messenger-" + name, {"policy": text}, token=token, accept=(200, 204))
|
||||
|
||||
|
||||
def put_kv(token: str, name: str, data: dict) -> None:
|
||||
call("POST", "/v1/secret/data/messenger/" + name, {"data": data}, token=token, accept=(200,))
|
||||
|
||||
|
||||
def ensure_role(token: str, name: str) -> None:
|
||||
call(
|
||||
"POST",
|
||||
"/v1/auth/approle/role/" + name,
|
||||
{
|
||||
"token_policies": "messenger-" + name,
|
||||
"token_ttl": "20m",
|
||||
"token_max_ttl": "2h",
|
||||
"secret_id_ttl": "24h",
|
||||
},
|
||||
token=token,
|
||||
accept=(200, 204),
|
||||
)
|
||||
role_path = SECRETS / (name + ".role_id")
|
||||
secret_path = SECRETS / (name + ".secret_id")
|
||||
_, role = call("GET", "/v1/auth/approle/role/" + name + "/role-id", token=token, accept=(200,))
|
||||
role_path.write_text(role["data"]["role_id"], encoding="utf-8")
|
||||
_, secret = call(
|
||||
"POST",
|
||||
"/v1/auth/approle/role/" + name + "/secret-id",
|
||||
{},
|
||||
token=token,
|
||||
accept=(200,),
|
||||
)
|
||||
secret_path.write_text(secret["data"]["secret_id"], encoding="utf-8")
|
||||
print("approle ready:", name)
|
||||
|
||||
|
||||
def deny_check() -> None:
|
||||
_, login = call(
|
||||
"POST",
|
||||
"/v1/auth/approle/login",
|
||||
{"role_id": read_text("api.role_id"), "secret_id": read_text("api.secret_id")},
|
||||
accept=(200,),
|
||||
)
|
||||
token = login["auth"]["client_token"]
|
||||
code, _ = call("GET", "/v1/secret/data/messenger/worker", token=token, accept=(200, 403))
|
||||
if code != 403:
|
||||
raise SystemExit("api policy was able to read the worker secret")
|
||||
evidence = Path("/evidence")
|
||||
if evidence.is_dir():
|
||||
(evidence / "openbao-deny.txt").write_text("PASS api token denied for messenger/worker\n", encoding="utf-8")
|
||||
print("api policy deny check passed")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
wait_until_reachable()
|
||||
material = ensure_init()
|
||||
token = ensure_unsealed(material)
|
||||
ensure_mounts(token)
|
||||
for name in ("api", "worker", "migrate"):
|
||||
put_policy(token, name)
|
||||
super_pass = read_text("db_super_password")
|
||||
api_pass = read_text("db_api_password")
|
||||
worker_pass = read_text("db_worker_password")
|
||||
redis_pass = read_text("redis_password")
|
||||
rabbit_api = read_text("rabbit_api_password")
|
||||
rabbit_worker = read_text("rabbit_worker_password")
|
||||
put_kv(
|
||||
token,
|
||||
"migrate",
|
||||
{"database_url": "postgresql://messenger:%s@postgres:5432/messenger" % super_pass},
|
||||
)
|
||||
put_kv(
|
||||
token,
|
||||
"api",
|
||||
{
|
||||
"database_url": "postgresql://messenger_api:%s@postgres:5432/messenger" % api_pass,
|
||||
"redis_url": "rediss://api:%s@redis:6379/0" % redis_pass,
|
||||
"broker_url": "amqps://messenger_api:%s@rabbitmq:5671/" % rabbit_api,
|
||||
"s3_access_key_id": read_text("s3_api_access"),
|
||||
"s3_secret_access_key": read_text("s3_api_secret"),
|
||||
},
|
||||
)
|
||||
put_kv(
|
||||
token,
|
||||
"worker",
|
||||
{
|
||||
"database_url": "postgresql://messenger_worker:%s@postgres:5432/messenger" % worker_pass,
|
||||
"broker_url": "amqps://messenger_worker:%s@rabbitmq:5671/" % rabbit_worker,
|
||||
"s3_access_key_id": read_text("s3_worker_access"),
|
||||
"s3_secret_access_key": read_text("s3_worker_secret"),
|
||||
},
|
||||
)
|
||||
print("kv documents written")
|
||||
for name in ("api", "worker", "migrate"):
|
||||
ensure_role(token, name)
|
||||
deny_check()
|
||||
print("bootstrap complete")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,9 +1,8 @@
|
||||
# Local-only overlay so this stand can run beside lab4 (8080/9000) and lab5 (8081/9001).
|
||||
# Not part of the submission. Delete it and use compose.yaml when those ports are free.
|
||||
# Local-only overlay so this stand can run beside another copy.
|
||||
services:
|
||||
lb:
|
||||
ports: !override
|
||||
- "127.0.0.1:8082:8080"
|
||||
- "127.0.0.1:8444:8443"
|
||||
s3:
|
||||
ports: !override
|
||||
- "127.0.0.1:9002:9000"
|
||||
|
||||
+465
-150
@@ -1,15 +1,225 @@
|
||||
# Client -> lb:8080 -> api x2 -> Postgres + Redis + private SeaweedFS S3 + RabbitMQ.
|
||||
# Worker is a separate process and is the only place that resizes images.
|
||||
# API and worker export OTLP to the collector. Prometheus, Loki, Tempo and Grafana
|
||||
# keep metrics, logs and traces. The collector is not required for the API to start.
|
||||
# API replicas do not publish host ports. MinIO console is not published.
|
||||
# API replicas do not publish host ports. Migrations run once in `migrate`.
|
||||
# Check period: HAProxy inter 1s, fall 2, rise 1. Worst detection of a dead
|
||||
# or not-ready replica is about 2s, plus the 1s DNS hold. API on SIGTERM
|
||||
# fails /health/ready immediately and exits after LB_DRAIN_SECONDS (default 2).
|
||||
# Client -TLS-> lb:8443 -mTLS-> api x2. API listens on 127.0.0.1 only.
|
||||
# Postgres, Redis, RabbitMQ (via local mTLS proxy), S3 (via local TLS proxy),
|
||||
# the collector and OpenBao are TLS. Secrets are read from OpenBao with AppRole.
|
||||
# Run scripts/bootstrap.ps1 before the first start: it creates certs, unseals
|
||||
# OpenBao and does not print secret values.
|
||||
|
||||
x-app-hardening: &app-hardening
|
||||
read_only: true
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
cap_drop:
|
||||
- ALL
|
||||
tmpfs:
|
||||
- /tmp:rw,nosuid,size=64m
|
||||
pids_limit: 128
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.6-alpine
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db_super_password
|
||||
POSTGRES_DB: messenger
|
||||
entrypoint: ["/bin/sh", "/entrypoint.sh"]
|
||||
volumes:
|
||||
- pg_data:/var/lib/postgresql/data
|
||||
- ./infra/postgres/entrypoint.sh:/entrypoint.sh:ro
|
||||
- ./infra/postgres/pg_hba.conf:/etc/postgres/pg_hba.conf:ro
|
||||
- ./infra/postgres/grant.sh:/etc/postgres/grant.sh:ro
|
||||
- ./certs/postgres.crt:/certs/postgres.crt:ro
|
||||
- ./certs/postgres.key:/certs/postgres.key:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
secrets:
|
||||
- db_super_password
|
||||
- db_api_password
|
||||
- db_worker_password
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
mem_limit: ${POSTGRES_MEM:-512m}
|
||||
cpus: ${POSTGRES_CPUS:-1.0}
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [data]
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
entrypoint: ["/bin/sh", "/entrypoint.sh"]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
- ./infra/redis/entrypoint.sh:/entrypoint.sh:ro
|
||||
- ./certs/redis.crt:/certs/redis.crt:ro
|
||||
- ./certs/redis.key:/certs/redis.key:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
secrets:
|
||||
- redis_password
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"redis-cli --tls --cacert /tmp/ca.crt -p 6379 --user api --pass \"$$(cat /run/secrets/redis_password)\" ping | grep -q PONG",
|
||||
]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
mem_limit: 128m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [data]
|
||||
|
||||
rabbitmq:
|
||||
image: rabbitmq:3.13.7-alpine
|
||||
hostname: rabbitmq
|
||||
entrypoint: ["/bin/sh", "/entrypoint.sh"]
|
||||
environment:
|
||||
RABBITMQ_DEFAULT_USER: messenger_api
|
||||
RABBITMQ_DEFAULT_PASS_FILE: /run/secrets/rabbit_api_password
|
||||
volumes:
|
||||
- rabbitmq_data:/var/lib/rabbitmq
|
||||
- ./infra/rabbitmq/entrypoint.sh:/entrypoint.sh:ro
|
||||
- ./certs/rabbitmq.crt:/certs/rabbitmq.crt:ro
|
||||
- ./certs/rabbitmq.key:/certs/rabbitmq.key:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
secrets:
|
||||
- rabbit_api_password
|
||||
- rabbit_worker_password
|
||||
healthcheck:
|
||||
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
start_period: 20s
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [data]
|
||||
|
||||
rabbit-mtls:
|
||||
image: haproxy:3.1.7-alpine
|
||||
network_mode: service:rabbitmq
|
||||
depends_on:
|
||||
rabbitmq:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ./infra/haproxy-rabbit.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
- ./certs/rabbitmq.pem:/certs/rabbitmq.pem:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:rw,nosuid,size=16m
|
||||
- /var/run:rw,nosuid,size=8m
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
restart: unless-stopped
|
||||
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:3.85
|
||||
entrypoint: ["/bin/sh", "/seaweed-entrypoint.sh"]
|
||||
environment:
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
ports:
|
||||
- "127.0.0.1:9000:9000"
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./infra/s3/seaweed-entrypoint.sh:/seaweed-entrypoint.sh:ro
|
||||
secrets:
|
||||
- s3_admin_access
|
||||
- s3_admin_secret
|
||||
- s3_api_access
|
||||
- s3_api_secret
|
||||
- s3_worker_access
|
||||
- s3_worker_secret
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9333/cluster/status || exit 1"]
|
||||
interval: 3s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
start_period: 8s
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [data]
|
||||
|
||||
s3-tls:
|
||||
image: haproxy:3.1.7-alpine
|
||||
network_mode: service:s3
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ./infra/haproxy-s3.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
- ./certs/s3.pem:/certs/s3.pem:ro
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:rw,nosuid,size=16m
|
||||
- /var/run:rw,nosuid,size=8m
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
restart: unless-stopped
|
||||
|
||||
openbao:
|
||||
image: openbao/openbao:2.2.0
|
||||
entrypoint: ["bao"]
|
||||
command: ["server", "-config=/openbao/config/openbao.hcl"]
|
||||
volumes:
|
||||
- bao_data:/openbao/file
|
||||
- ./infra/openbao/openbao.hcl:/openbao/config/openbao.hcl:ro
|
||||
- ./certs/openbao.crt:/certs/openbao.crt:ro
|
||||
- ./certs/openbao.key:/certs/openbao.key:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "bao", "status", "-address=https://127.0.0.1:8200", "-ca-cert=/certs/ca.crt"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 40
|
||||
start_period: 5s
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [data]
|
||||
|
||||
openbao-bootstrap:
|
||||
profiles: ["bootstrap"]
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab7
|
||||
command: ["python", "-m", "app.tools.openbao_bootstrap"]
|
||||
environment:
|
||||
BAO_ADDR: https://openbao:8200
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
BAO_INIT_FILE: /secrets/openbao-init.json
|
||||
volumes:
|
||||
- ./secrets:/secrets
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
- ./infra/openbao/policies:/policies:ro
|
||||
- ./evidence:/evidence
|
||||
networks: [data]
|
||||
depends_on:
|
||||
openbao:
|
||||
condition: service_started
|
||||
restart: "no"
|
||||
|
||||
migrate:
|
||||
<<: *app-hardening
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
@@ -18,31 +228,76 @@ services:
|
||||
environment:
|
||||
APP_ENV: lab
|
||||
LOG_LEVEL: info
|
||||
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
BAO_ADDR: https://openbao:8200
|
||||
BAO_CACERT: /certs/ca.crt
|
||||
BAO_ROLE_ID_FILE: /run/secrets/migrate_role_id
|
||||
BAO_SECRET_ID_FILE: /run/secrets/migrate_secret_id
|
||||
BAO_SECRET_PATH: secret/data/messenger/migrate
|
||||
DB_POOL_MIN: "1"
|
||||
DB_POOL_MAX: "2"
|
||||
MIGRATIONS_ON_START: "1"
|
||||
secrets:
|
||||
- source: migrate_role_id
|
||||
target: migrate_role_id
|
||||
- source: migrate_secret_id
|
||||
target: migrate_secret_id
|
||||
volumes:
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
openbao:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
networks: [data]
|
||||
|
||||
s3-init:
|
||||
<<: *app-hardening
|
||||
image: mtusi-messenger:lab7
|
||||
command: ["python", "-m", "app.s3bootstrap"]
|
||||
environment:
|
||||
S3_ENDPOINT: https://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret
|
||||
secrets:
|
||||
- source: s3_admin_access
|
||||
target: s3_admin_access
|
||||
- source: s3_admin_secret
|
||||
target: s3_admin_secret
|
||||
volumes:
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
s3-tls:
|
||||
condition: service_started
|
||||
restart: "no"
|
||||
networks: [data]
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
<<: *app-hardening
|
||||
image: mtusi-messenger:lab7
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
command: ["sh", "/scripts/api-entrypoint.sh"]
|
||||
environment: &api-env
|
||||
APP_HOST: 127.0.0.1
|
||||
APP_PORT: "8080"
|
||||
MTLS_PORT: "8443"
|
||||
APP_ENV: lab
|
||||
LOG_LEVEL: info
|
||||
TELEMETRY_ENABLED: ${TELEMETRY_ENABLED:-1}
|
||||
TRACE_SAMPLE_RATIO: ${TRACE_SAMPLE_RATIO:-1}
|
||||
OTEL_SERVICE_NAME: messenger-api
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /certs/ca.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /certs/api-client.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_KEY: /certs/api-client.key
|
||||
SERVICE_VERSION: lab7
|
||||
SHUTDOWN_GRACE_SECONDS: "3"
|
||||
LB_DRAIN_SECONDS: ${LB_DRAIN_SECONDS:-2}
|
||||
@@ -50,28 +305,52 @@ services:
|
||||
CACHE_TTL_SECONDS: ${CACHE_TTL_SECONDS:-60}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
SESSION_STORE: redis
|
||||
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger
|
||||
REDIS_URL: redis://redis:6379/0
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_ENDPOINT: https://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY}
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-https://127.0.0.1:9000}
|
||||
PRESIGN_TTL_SECONDS: ${PRESIGN_TTL_SECONDS:-60}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-120}
|
||||
BROKER_URL: amqp://messenger:${RABBITMQ_PASSWORD}@rabbitmq:5672/
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
|
||||
S3_SWEEP_INTERVAL_SECONDS: ${S3_SWEEP_INTERVAL_SECONDS:-30}
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:9000}
|
||||
PRESIGN_TTL_SECONDS: ${PRESIGN_TTL_SECONDS:-60}
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
TLS_CERT_FILE: /certs/api-client.crt
|
||||
TLS_KEY_FILE: /certs/api-client.key
|
||||
TLS_SERVER_CERT_FILE: /certs/api.crt
|
||||
TLS_SERVER_KEY_FILE: /certs/api.key
|
||||
MTLS_ALLOW_FILE: /certs/allowed-lb.sha256
|
||||
BAO_ADDR: https://openbao:8200
|
||||
BAO_CACERT: /certs/ca.crt
|
||||
BAO_ROLE_ID_FILE: /run/secrets/api_role_id
|
||||
BAO_SECRET_ID_FILE: /run/secrets/api_secret_id
|
||||
BAO_SECRET_PATH: secret/data/messenger/api
|
||||
ARGON2_MEMORY_KIB: ${ARGON2_MEMORY_KIB:-19456}
|
||||
ARGON2_ITERATIONS: ${ARGON2_ITERATIONS:-2}
|
||||
ARGON2_PARALLELISM: ${ARGON2_PARALLELISM:-1}
|
||||
LOGIN_RATE_LIMIT: ${LOGIN_RATE_LIMIT:-8}
|
||||
LOGIN_RATE_WINDOW_SECONDS: ${LOGIN_RATE_WINDOW_SECONDS:-60}
|
||||
DB_POOL_MIN: "1"
|
||||
DB_POOL_MAX: ${DB_POOL_MAX:-4}
|
||||
DB_COMMAND_TIMEOUT: "5"
|
||||
DB_CONNECT_TIMEOUT: "5"
|
||||
MIGRATIONS_ON_START: "0"
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
|
||||
secrets:
|
||||
- source: api_role_id
|
||||
target: api_role_id
|
||||
- source: api_secret_id
|
||||
target: api_secret_id
|
||||
volumes:
|
||||
- ./scripts/api-entrypoint.sh:/scripts/api-entrypoint.sh:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
- ./certs/api.crt:/certs/api.crt:ro
|
||||
- ./certs/api.key:/certs/api.key:ro
|
||||
- ./certs/api-client.crt:/certs/api-client.crt:ro
|
||||
- ./certs/api-client.key:/certs/api-client.key:ro
|
||||
- ./certs/allowed-lb.sha256:/certs/allowed-lb.sha256:ro
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
@@ -83,10 +362,14 @@ services:
|
||||
condition: service_completed_successfully
|
||||
rabbitmq:
|
||||
condition: service_healthy
|
||||
rabbit-mtls:
|
||||
condition: service_started
|
||||
openbao:
|
||||
condition: service_healthy
|
||||
expose:
|
||||
- "8080"
|
||||
- "8443"
|
||||
restart: unless-stopped
|
||||
mem_limit: ${API_MEM:-256m}
|
||||
mem_limit: ${API_MEM:-384m}
|
||||
cpus: ${API_CPUS:-0.50}
|
||||
stop_grace_period: 8s
|
||||
healthcheck:
|
||||
@@ -99,105 +382,37 @@ services:
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 6
|
||||
start_period: 5s
|
||||
retries: 12
|
||||
start_period: 15s
|
||||
networks: [app, data, signals]
|
||||
|
||||
lb:
|
||||
image: haproxy:2.8.14-alpine
|
||||
image: haproxy:3.1.7-alpine
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
- ./certs/lb.pem:/certs/lb.pem:ro
|
||||
- ./certs/lb-client.pem:/certs/lb-client.pem:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
- "127.0.0.1:8443:8443"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_started
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:rw,nosuid,size=16m
|
||||
- /var/run:rw,nosuid,size=8m
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
restart: unless-stopped
|
||||
mem_limit: 128m
|
||||
cpus: "0.25"
|
||||
|
||||
postgres:
|
||||
image: postgres:16.6-alpine
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: messenger
|
||||
volumes:
|
||||
- pg_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
mem_limit: ${POSTGRES_MEM:-512m}
|
||||
cpus: ${POSTGRES_CPUS:-1.0}
|
||||
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:3.85
|
||||
entrypoint: ["/bin/sh", "/seaweed-entrypoint.sh"]
|
||||
environment:
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY}
|
||||
ports:
|
||||
- "127.0.0.1:9000:9000"
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./infra/s3/seaweed-entrypoint.sh:/seaweed-entrypoint.sh:ro
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"wget -q -O /dev/null http://127.0.0.1:9333/cluster/status || exit 1; wget -q -O /dev/null http://127.0.0.1:9000/ || test $$? -ne 4",
|
||||
]
|
||||
interval: 3s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
start_period: 8s
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
|
||||
s3-init:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab7
|
||||
command: ["python", "-m", "app.s3bootstrap"]
|
||||
environment:
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY}
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
rabbitmq:
|
||||
image: rabbitmq:3.13.7-alpine
|
||||
hostname: rabbitmq
|
||||
environment:
|
||||
RABBITMQ_DEFAULT_USER: messenger
|
||||
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD}
|
||||
RABBITMQ_DEFAULT_VHOST: /
|
||||
volumes:
|
||||
- rabbitmq_data:/var/lib/rabbitmq
|
||||
healthcheck:
|
||||
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
start_period: 15s
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
networks: [app]
|
||||
|
||||
worker:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
<<: *app-hardening
|
||||
image: mtusi-messenger:lab7
|
||||
command: ["python", "-m", "app.worker"]
|
||||
environment:
|
||||
@@ -206,17 +421,25 @@ services:
|
||||
TELEMETRY_ENABLED: ${TELEMETRY_ENABLED:-1}
|
||||
TRACE_SAMPLE_RATIO: ${TRACE_SAMPLE_RATIO:-1}
|
||||
OTEL_SERVICE_NAME: messenger-worker
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /certs/ca.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /certs/worker-client.crt
|
||||
OTEL_EXPORTER_OTLP_CLIENT_KEY: /certs/worker-client.key
|
||||
SERVICE_VERSION: lab7
|
||||
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_ENDPOINT: https://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
BROKER_URL: amqp://messenger:${RABBITMQ_PASSWORD}@rabbitmq:5672/
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
TLS_CERT_FILE: /certs/worker-client.crt
|
||||
TLS_KEY_FILE: /certs/worker-client.key
|
||||
BAO_ADDR: https://openbao:8200
|
||||
BAO_CACERT: /certs/ca.crt
|
||||
BAO_ROLE_ID_FILE: /run/secrets/worker_role_id
|
||||
BAO_SECRET_ID_FILE: /run/secrets/worker_secret_id
|
||||
BAO_SECRET_PATH: secret/data/messenger/worker
|
||||
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2}
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
@@ -224,6 +447,15 @@ services:
|
||||
DB_POOL_MIN: "1"
|
||||
DB_POOL_MAX: "4"
|
||||
MIGRATIONS_ON_START: "0"
|
||||
secrets:
|
||||
- source: worker_role_id
|
||||
target: worker_role_id
|
||||
- source: worker_secret_id
|
||||
target: worker_secret_id
|
||||
volumes:
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
- ./certs/worker-client.crt:/certs/worker-client.crt:ro
|
||||
- ./certs/worker-client.key:/certs/worker-client.key:ro
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
@@ -231,97 +463,130 @@ services:
|
||||
condition: service_healthy
|
||||
rabbitmq:
|
||||
condition: service_healthy
|
||||
rabbit-mtls:
|
||||
condition: service_started
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
openbao:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
mem_limit: 768m
|
||||
cpus: "1.0"
|
||||
stop_grace_period: 20s
|
||||
healthcheck:
|
||||
disable: true
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command:
|
||||
- redis-server
|
||||
- --appendonly
|
||||
- "yes"
|
||||
- --appendfsync
|
||||
- everysec
|
||||
- --save
|
||||
- "60"
|
||||
- "1"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
mem_limit: 128m
|
||||
cpus: "0.50"
|
||||
networks: [data, signals]
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:0.128.0
|
||||
command: ["--config=/etc/otel/collector.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel/collector.yaml:/etc/otel/collector.yaml:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
- ./certs/tempo.crt:/certs/tempo.crt:ro
|
||||
- ./certs/loki.crt:/certs/loki.crt:ro
|
||||
- ./certs/prometheus.crt:/certs/prometheus.crt:ro
|
||||
restart: unless-stopped
|
||||
mem_limit: 320m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [signals, telemetry]
|
||||
|
||||
otel-mtls:
|
||||
image: haproxy:3.1.7-alpine
|
||||
network_mode: service:otel-collector
|
||||
depends_on:
|
||||
otel-collector:
|
||||
condition: service_started
|
||||
volumes:
|
||||
- ./infra/haproxy-otel.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
- ./certs/otel.pem:/certs/otel.pem:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:rw,nosuid,size=16m
|
||||
- /var/run:rw,nosuid,size=8m
|
||||
cap_drop:
|
||||
- ALL
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
restart: unless-stopped
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v2.55.1
|
||||
command:
|
||||
- --config.file=/etc/prometheus/prometheus.yml
|
||||
- --web.config.file=/etc/prometheus/web.yml
|
||||
- --web.enable-remote-write-receiver
|
||||
- --storage.tsdb.path=/prometheus
|
||||
- --storage.tsdb.retention.time=7d
|
||||
volumes:
|
||||
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
|
||||
- ./infra/prometheus/alerts.yml:/etc/prometheus/alerts.yml:ro
|
||||
- ./infra/prometheus/web.yml:/etc/prometheus/web.yml:ro
|
||||
- ./certs/prometheus.crt:/certs/prometheus.crt:ro
|
||||
- ./certs/prometheus.key:/certs/prometheus.key:ro
|
||||
- prometheus_data:/prometheus
|
||||
ports:
|
||||
- "127.0.0.1:9090:9090"
|
||||
restart: unless-stopped
|
||||
mem_limit: 512m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [telemetry]
|
||||
|
||||
loki:
|
||||
image: grafana/loki:3.3.2
|
||||
command: ["-config.file=/etc/loki/loki.yml"]
|
||||
volumes:
|
||||
- ./infra/loki/loki.yml:/etc/loki/loki.yml:ro
|
||||
- ./certs/loki.crt:/certs/loki.crt:ro
|
||||
- ./certs/loki.key:/certs/loki.key:ro
|
||||
- loki_data:/loki
|
||||
restart: unless-stopped
|
||||
mem_limit: 512m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [telemetry]
|
||||
|
||||
tempo:
|
||||
image: grafana/tempo:2.7.1
|
||||
command: ["-config.file=/etc/tempo/tempo.yml"]
|
||||
volumes:
|
||||
- ./infra/tempo/tempo.yml:/etc/tempo/tempo.yml:ro
|
||||
- ./certs/tempo.crt:/certs/tempo.crt:ro
|
||||
- ./certs/tempo.key:/certs/tempo.key:ro
|
||||
- tempo_data:/var/tempo
|
||||
restart: unless-stopped
|
||||
mem_limit: 512m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [telemetry]
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:11.4.0
|
||||
entrypoint: ["/bin/sh", "/entrypoint.sh"]
|
||||
environment:
|
||||
GF_SECURITY_ADMIN_USER: admin
|
||||
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:-lab-dev-only-change-me}
|
||||
GF_AUTH_ANONYMOUS_ENABLED: "true"
|
||||
GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
|
||||
GF_AUTH_ANONYMOUS_ENABLED: "false"
|
||||
GF_USERS_DEFAULT_THEME: light
|
||||
GF_SERVER_PROTOCOL: https
|
||||
GF_SERVER_CERT_FILE: /certs/grafana.crt
|
||||
GF_SERVER_CERT_KEY: /certs/grafana.key
|
||||
SSL_CERT_FILE: /certs/ca.crt
|
||||
ports:
|
||||
- "127.0.0.1:3000:3000"
|
||||
volumes:
|
||||
- ./infra/grafana/entrypoint.sh:/entrypoint.sh:ro
|
||||
- ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
|
||||
- ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro
|
||||
- ./certs/grafana.crt:/certs/grafana.crt:ro
|
||||
- ./certs/grafana.key:/certs/grafana.key:ro
|
||||
- ./certs/ca.crt:/certs/ca.crt:ro
|
||||
- grafana_data:/var/lib/grafana
|
||||
secrets:
|
||||
- source: grafana_password
|
||||
target: grafana_password
|
||||
depends_on:
|
||||
- prometheus
|
||||
- loki
|
||||
@@ -329,6 +594,55 @@ services:
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
cpus: "0.50"
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
networks: [telemetry]
|
||||
|
||||
secrets:
|
||||
db_super_password:
|
||||
file: ./secrets/db_super_password
|
||||
db_api_password:
|
||||
file: ./secrets/db_api_password
|
||||
db_worker_password:
|
||||
file: ./secrets/db_worker_password
|
||||
redis_password:
|
||||
file: ./secrets/redis_password
|
||||
rabbit_api_password:
|
||||
file: ./secrets/rabbit_api_password
|
||||
rabbit_worker_password:
|
||||
file: ./secrets/rabbit_worker_password
|
||||
s3_admin_access:
|
||||
file: ./secrets/s3_admin_access
|
||||
s3_admin_secret:
|
||||
file: ./secrets/s3_admin_secret
|
||||
s3_api_access:
|
||||
file: ./secrets/s3_api_access
|
||||
s3_api_secret:
|
||||
file: ./secrets/s3_api_secret
|
||||
s3_worker_access:
|
||||
file: ./secrets/s3_worker_access
|
||||
s3_worker_secret:
|
||||
file: ./secrets/s3_worker_secret
|
||||
grafana_password:
|
||||
file: ./secrets/grafana_password
|
||||
api_role_id:
|
||||
file: ./secrets/api.role_id
|
||||
api_secret_id:
|
||||
file: ./secrets/api.secret_id
|
||||
worker_role_id:
|
||||
file: ./secrets/worker.role_id
|
||||
worker_secret_id:
|
||||
file: ./secrets/worker.secret_id
|
||||
migrate_role_id:
|
||||
file: ./secrets/migrate.role_id
|
||||
migrate_secret_id:
|
||||
file: ./secrets/migrate.secret_id
|
||||
|
||||
networks:
|
||||
app:
|
||||
data:
|
||||
signals:
|
||||
telemetry:
|
||||
|
||||
volumes:
|
||||
pg_data:
|
||||
@@ -339,3 +653,4 @@ volumes:
|
||||
loki_data:
|
||||
tempo_data:
|
||||
grafana_data:
|
||||
bao_data:
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
if [ -f /certs/ca.crt ]; then
|
||||
cat /certs/ca.crt >> /etc/ssl/certs/ca-certificates.crt
|
||||
fi
|
||||
if [ -f /run/secrets/grafana_password ]; then
|
||||
GF_SECURITY_ADMIN_PASSWORD=$(cat /run/secrets/grafana_password)
|
||||
export GF_SECURITY_ADMIN_PASSWORD
|
||||
fi
|
||||
exec /run.sh "$@"
|
||||
@@ -5,7 +5,7 @@ datasources:
|
||||
uid: prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
url: https://prometheus:9090
|
||||
isDefault: true
|
||||
editable: false
|
||||
jsonData:
|
||||
@@ -14,7 +14,7 @@ datasources:
|
||||
uid: loki
|
||||
type: loki
|
||||
access: proxy
|
||||
url: http://loki:3100
|
||||
url: https://loki:3100
|
||||
editable: false
|
||||
jsonData:
|
||||
derivedFields:
|
||||
@@ -26,7 +26,7 @@ datasources:
|
||||
uid: tempo
|
||||
type: tempo
|
||||
access: proxy
|
||||
url: http://tempo:3200
|
||||
url: https://tempo:3200
|
||||
editable: false
|
||||
jsonData:
|
||||
tracesToLogsV2:
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 200
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
timeout connect 3s
|
||||
timeout client 60s
|
||||
timeout server 60s
|
||||
|
||||
frontend ft_otlp
|
||||
bind *:4318 ssl crt /certs/otel.pem ca-file /certs/ca.crt verify required
|
||||
acl api_id ssl_c_san -m sub spiffe://messenger/api
|
||||
acl worker_id ssl_c_san -m sub spiffe://messenger/worker
|
||||
http-request deny unless api_id or worker_id
|
||||
default_backend be_local
|
||||
|
||||
backend be_local
|
||||
server collector 127.0.0.1:14318
|
||||
@@ -0,0 +1,22 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 200
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode tcp
|
||||
timeout connect 3s
|
||||
timeout client 120s
|
||||
timeout server 120s
|
||||
|
||||
frontend ft_amqp
|
||||
bind *:5671 ssl crt /certs/rabbitmq.pem ca-file /certs/ca.crt verify required
|
||||
tcp-request inspect-delay 5s
|
||||
tcp-request content accept if { req_ssl_hello_type 1 }
|
||||
acl api_id ssl_c_san -m sub spiffe://messenger/api
|
||||
acl worker_id ssl_c_san -m sub spiffe://messenger/worker
|
||||
tcp-request content reject unless api_id or worker_id
|
||||
default_backend be_local
|
||||
|
||||
backend be_local
|
||||
server rabbit 127.0.0.1:5672 check
|
||||
@@ -0,0 +1,18 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 200
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
timeout connect 3s
|
||||
timeout client 120s
|
||||
timeout server 120s
|
||||
timeout http-request 30s
|
||||
|
||||
frontend ft_s3
|
||||
bind *:9000 ssl crt /certs/s3.pem
|
||||
default_backend be_local
|
||||
|
||||
backend be_local
|
||||
server weed 127.0.0.1:19000
|
||||
+7
-4
@@ -22,8 +22,11 @@ resolvers docker
|
||||
hold valid 1s
|
||||
accepted_payload_size 8192
|
||||
|
||||
frontend fe_http
|
||||
bind *:8080
|
||||
frontend fe_https
|
||||
bind *:8443 ssl crt /certs/lb.pem
|
||||
http-request set-header X-Real-IP %[src]
|
||||
http-request del-header X-Forwarded-For
|
||||
option forwardfor
|
||||
default_backend be_api
|
||||
|
||||
backend be_api
|
||||
@@ -31,5 +34,5 @@ backend be_api
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr host api
|
||||
http-check expect status 200
|
||||
default-server inter 1s fall 2 rise 1
|
||||
server-template srv 8 api:8080 check resolvers docker init-addr none resolve-prefer ipv4
|
||||
default-server inter 1s fall 2 rise 1 resolvers docker init-addr none resolve-prefer ipv4
|
||||
server-template srv 8 api:8443 check ssl verify required ca-file /certs/ca.crt crt /certs/lb-client.pem
|
||||
|
||||
@@ -3,6 +3,12 @@ auth_enabled: false
|
||||
server:
|
||||
http_listen_port: 3100
|
||||
grpc_listen_port: 9096
|
||||
http_tls_config:
|
||||
cert_file: /certs/loki.crt
|
||||
key_file: /certs/loki.key
|
||||
grpc_tls_config:
|
||||
cert_file: /certs/loki.crt
|
||||
key_file: /certs/loki.key
|
||||
|
||||
common:
|
||||
instance_addr: 127.0.0.1
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
ui = false
|
||||
disable_mlock = true
|
||||
api_addr = "https://openbao:8200"
|
||||
|
||||
storage "file" {
|
||||
path = "/openbao/file"
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "0.0.0.0:8200"
|
||||
tls_cert_file = "/certs/openbao.crt"
|
||||
tls_key_file = "/certs/openbao.key"
|
||||
}
|
||||
|
||||
# Dev mode is not used. The process starts sealed; the operator unseals it.
|
||||
@@ -0,0 +1,16 @@
|
||||
# API may read only its own KV document and renew its own token.
|
||||
path "secret/data/messenger/api" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/api" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
path "secret/data/messenger/migrate" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/migrate" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
path "secret/data/messenger/worker" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/worker" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
@@ -5,10 +5,8 @@
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
endpoint: 127.0.0.1:14318
|
||||
|
||||
processors:
|
||||
memory_limiter:
|
||||
@@ -22,9 +20,10 @@ processors:
|
||||
|
||||
exporters:
|
||||
otlphttp/tempo:
|
||||
traces_endpoint: http://tempo:4318/v1/traces
|
||||
traces_endpoint: https://tempo:4318/v1/traces
|
||||
tls:
|
||||
insecure: true
|
||||
insecure: false
|
||||
ca_file: /certs/ca.crt
|
||||
timeout: 5s
|
||||
sending_queue:
|
||||
enabled: true
|
||||
@@ -36,9 +35,10 @@ exporters:
|
||||
max_interval: 15s
|
||||
max_elapsed_time: 30s
|
||||
prometheusremotewrite:
|
||||
endpoint: http://prometheus:9090/api/v1/write
|
||||
endpoint: https://prometheus:9090/api/v1/write
|
||||
tls:
|
||||
insecure: true
|
||||
insecure: false
|
||||
ca_file: /certs/ca.crt
|
||||
resource_to_telemetry_conversion:
|
||||
enabled: true
|
||||
remote_write_queue:
|
||||
@@ -51,9 +51,10 @@ exporters:
|
||||
max_interval: 15s
|
||||
max_elapsed_time: 30s
|
||||
otlphttp/loki:
|
||||
logs_endpoint: http://loki:3100/otlp/v1/logs
|
||||
logs_endpoint: https://loki:3100/otlp/v1/logs
|
||||
tls:
|
||||
insecure: true
|
||||
insecure: false
|
||||
ca_file: /certs/ca.crt
|
||||
timeout: 5s
|
||||
sending_queue:
|
||||
enabled: true
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cp /certs/postgres.crt /var/lib/postgresql/server.crt
|
||||
cp /certs/postgres.key /var/lib/postgresql/server.key
|
||||
cp /certs/ca.crt /var/lib/postgresql/ca.crt
|
||||
chown postgres:postgres /var/lib/postgresql/server.crt /var/lib/postgresql/server.key /var/lib/postgresql/ca.crt
|
||||
chmod 644 /var/lib/postgresql/server.crt /var/lib/postgresql/ca.crt
|
||||
chmod 600 /var/lib/postgresql/server.key
|
||||
exec docker-entrypoint.sh postgres \
|
||||
-c ssl=on \
|
||||
-c ssl_cert_file=/var/lib/postgresql/server.crt \
|
||||
-c ssl_key_file=/var/lib/postgresql/server.key \
|
||||
-c ssl_ca_file=/var/lib/postgresql/ca.crt \
|
||||
-c hba_file=/etc/postgres/pg_hba.conf
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
# Create limited roles after migrations. Passwords come from mounted files, not from Git.
|
||||
set -eu
|
||||
api_pass=$(cat /run/secrets/db_api_password)
|
||||
worker_pass=$(cat /run/secrets/db_worker_password)
|
||||
psql -v ON_ERROR_STOP=1 --username messenger --dbname messenger <<SQL
|
||||
DO \$\$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'messenger_api') THEN
|
||||
CREATE ROLE messenger_api LOGIN PASSWORD '${api_pass}';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'messenger_worker') THEN
|
||||
CREATE ROLE messenger_worker LOGIN PASSWORD '${worker_pass}';
|
||||
END IF;
|
||||
END
|
||||
\$\$;
|
||||
ALTER ROLE messenger_api PASSWORD '${api_pass}';
|
||||
ALTER ROLE messenger_worker PASSWORD '${worker_pass}';
|
||||
GRANT CONNECT ON DATABASE messenger TO messenger_api, messenger_worker;
|
||||
GRANT USAGE ON SCHEMA public TO messenger_api, messenger_worker;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON
|
||||
users, chats, chat_members, messages, idempotency_keys,
|
||||
attachments, message_attachments, blob_writes, direct_uploads,
|
||||
attachment_variants, attachment_jobs, attachment_job_events,
|
||||
pipeline_faults, schema_migrations
|
||||
TO messenger_api;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON
|
||||
attachments, message_attachments, blob_writes,
|
||||
attachment_variants, attachment_jobs, attachment_job_events,
|
||||
pipeline_faults
|
||||
TO messenger_worker;
|
||||
SQL
|
||||
echo "database roles granted"
|
||||
@@ -0,0 +1,5 @@
|
||||
local all all trust
|
||||
hostssl all all 0.0.0.0/0 scram-sha-256
|
||||
hostssl all all ::/0 scram-sha-256
|
||||
host all all 0.0.0.0/0 reject
|
||||
host all all ::/0 reject
|
||||
@@ -0,0 +1,3 @@
|
||||
tls_server_config:
|
||||
cert_file: /certs/prometheus.crt
|
||||
key_file: /certs/prometheus.key
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mkdir -p /etc/rabbitmq/certs
|
||||
cp /certs/rabbitmq.crt /etc/rabbitmq/certs/server.crt
|
||||
cp /certs/rabbitmq.key /etc/rabbitmq/certs/server.key
|
||||
cp /certs/ca.crt /etc/rabbitmq/certs/ca.crt
|
||||
chown -R rabbitmq:rabbitmq /etc/rabbitmq/certs
|
||||
chmod 600 /etc/rabbitmq/certs/server.key
|
||||
api_pass=$(cat /run/secrets/rabbit_api_password)
|
||||
worker_pass=$(cat /run/secrets/rabbit_worker_password)
|
||||
cat > /etc/rabbitmq/definitions.json <<EOF
|
||||
{
|
||||
"vhosts": [{"name": "/"}],
|
||||
"users": [
|
||||
{"name": "messenger_api", "password": "${api_pass}", "tags": ""},
|
||||
{"name": "messenger_worker", "password": "${worker_pass}", "tags": ""}
|
||||
],
|
||||
"permissions": [
|
||||
{
|
||||
"user": "messenger_api",
|
||||
"vhost": "/",
|
||||
"configure": ".*",
|
||||
"write": ".*",
|
||||
"read": ".*"
|
||||
},
|
||||
{
|
||||
"user": "messenger_worker",
|
||||
"vhost": "/",
|
||||
"configure": "^(attachment\\\\.jobs.*)$",
|
||||
"write": "^(attachment\\\\.jobs.*)$",
|
||||
"read": "^(attachment\\\\.jobs.*)$"
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
chown rabbitmq:rabbitmq /etc/rabbitmq/definitions.json
|
||||
chmod 640 /etc/rabbitmq/definitions.json
|
||||
cat > /etc/rabbitmq/rabbitmq.conf <<EOF
|
||||
listeners.tcp.1 = 127.0.0.1:5672
|
||||
management.listener.ip = 127.0.0.1
|
||||
management.listener.port = 15672
|
||||
loopback_users.guest = true
|
||||
load_definitions = /etc/rabbitmq/definitions.json
|
||||
EOF
|
||||
exec docker-entrypoint.sh rabbitmq-server
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cp /certs/redis.crt /tmp/redis.crt
|
||||
cp /certs/redis.key /tmp/redis.key
|
||||
cp /certs/ca.crt /tmp/ca.crt
|
||||
chown redis:redis /tmp/redis.crt /tmp/redis.key /tmp/ca.crt
|
||||
chmod 600 /tmp/redis.key
|
||||
pass=$(cat /run/secrets/redis_password)
|
||||
umask 077
|
||||
cat > /tmp/users.acl <<EOF
|
||||
user default off
|
||||
user api on >${pass} ~* &* +@all
|
||||
EOF
|
||||
chown redis:redis /tmp/users.acl
|
||||
exec docker-entrypoint.sh redis-server \
|
||||
--port 0 \
|
||||
--tls-port 6379 \
|
||||
--tls-cert-file /tmp/redis.crt \
|
||||
--tls-key-file /tmp/redis.key \
|
||||
--tls-ca-cert-file /tmp/ca.crt \
|
||||
--tls-auth-clients no \
|
||||
--aclfile /tmp/users.acl \
|
||||
--appendonly yes \
|
||||
--appendfsync everysec \
|
||||
--save 60 1
|
||||
@@ -1,34 +1,48 @@
|
||||
#!/bin/sh
|
||||
# One SeaweedFS process: master, volume, filer, S3. The filer UI is off.
|
||||
# Anonymous identity has no actions. The API user is limited to one bucket.
|
||||
# One SeaweedFS process bound to localhost. HAProxy in this network namespace
|
||||
# is the TLS endpoint other containers use.
|
||||
set -eu
|
||||
: "${S3_BUCKET:?}"
|
||||
: "${S3_ACCESS_KEY_ID:?}"
|
||||
: "${S3_SECRET_ACCESS_KEY:?}"
|
||||
admin_key=$(cat /run/secrets/s3_admin_access)
|
||||
admin_secret=$(cat /run/secrets/s3_admin_secret)
|
||||
api_key=$(cat /run/secrets/s3_api_access)
|
||||
api_secret=$(cat /run/secrets/s3_api_secret)
|
||||
worker_key=$(cat /run/secrets/s3_worker_access)
|
||||
worker_secret=$(cat /run/secrets/s3_worker_secret)
|
||||
|
||||
cat > /tmp/s3.json <<EOF
|
||||
{
|
||||
"identities": [
|
||||
{"name": "anonymous", "actions": []},
|
||||
{
|
||||
"name": "anonymous",
|
||||
"actions": []
|
||||
"name": "s3-admin",
|
||||
"credentials": [{"accessKey": "${admin_key}", "secretKey": "${admin_secret}"}],
|
||||
"actions": ["Admin:${S3_BUCKET}"]
|
||||
},
|
||||
{
|
||||
"name": "messenger-app",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "${S3_ACCESS_KEY_ID}",
|
||||
"secretKey": "${S3_SECRET_ACCESS_KEY}"
|
||||
}
|
||||
],
|
||||
"name": "messenger-api",
|
||||
"credentials": [{"accessKey": "${api_key}", "secretKey": "${api_secret}"}],
|
||||
"actions": [
|
||||
"Read:${S3_BUCKET}",
|
||||
"Read:${S3_BUCKET}/*",
|
||||
"Write:${S3_BUCKET}",
|
||||
"Write:${S3_BUCKET}/*",
|
||||
"List:${S3_BUCKET}",
|
||||
"Tagging:${S3_BUCKET}/*",
|
||||
"Admin:${S3_BUCKET}"
|
||||
"Write:${S3_BUCKET}/staging/*",
|
||||
"Write:${S3_BUCKET}/originals/*",
|
||||
"Tagging:${S3_BUCKET}/*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "messenger-worker",
|
||||
"credentials": [{"accessKey": "${worker_key}", "secretKey": "${worker_secret}"}],
|
||||
"actions": [
|
||||
"Read:${S3_BUCKET}/staging/*",
|
||||
"Read:${S3_BUCKET}/originals/*",
|
||||
"Read:${S3_BUCKET}/variants/*",
|
||||
"List:${S3_BUCKET}",
|
||||
"Write:${S3_BUCKET}/originals/*",
|
||||
"Write:${S3_BUCKET}/variants/*",
|
||||
"Write:${S3_BUCKET}/staging/*",
|
||||
"Tagging:${S3_BUCKET}/*"
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -37,12 +51,12 @@ EOF
|
||||
|
||||
exec weed server \
|
||||
-dir=/data \
|
||||
-ip.bind=0.0.0.0 \
|
||||
-ip.bind=127.0.0.1 \
|
||||
-master.port=9333 \
|
||||
-volume.port=8080 \
|
||||
-filer \
|
||||
-filer.port=8888 \
|
||||
-filer.exposeDirectoryData=false \
|
||||
-s3 \
|
||||
-s3.port=9000 \
|
||||
-s3.port=19000 \
|
||||
-s3.config=/tmp/s3.json
|
||||
|
||||
@@ -2,6 +2,9 @@ stream_over_http_enabled: true
|
||||
|
||||
server:
|
||||
http_listen_port: 3200
|
||||
http_tls_config:
|
||||
cert_file: /certs/tempo.crt
|
||||
key_file: /certs/tempo.key
|
||||
|
||||
distributor:
|
||||
receivers:
|
||||
@@ -9,8 +12,11 @@ distributor:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
tls:
|
||||
cert_file: /certs/tempo.crt
|
||||
key_file: /certs/tempo.key
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
endpoint: 127.0.0.1:4317
|
||||
|
||||
ingester:
|
||||
max_block_duration: 5m
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
python -m app.main &
|
||||
app_pid=$!
|
||||
python -m app.tls_proxy &
|
||||
proxy_pid=$!
|
||||
while kill -0 "$app_pid" 2>/dev/null && kill -0 "$proxy_pid" 2>/dev/null; do
|
||||
sleep 1
|
||||
done
|
||||
kill "$app_pid" "$proxy_pid" 2>/dev/null || true
|
||||
wait || true
|
||||
exit 1
|
||||
@@ -0,0 +1,100 @@
|
||||
$ErrorActionPreference = "Stop"
|
||||
$Root = Split-Path -Parent $PSScriptRoot
|
||||
Set-Location $Root
|
||||
|
||||
function New-Secret([string]$Name) {
|
||||
$path = Join-Path $Root "secrets\$Name"
|
||||
if (-not (Test-Path $path)) {
|
||||
$bytes = New-Object byte[] 24
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
$rng.GetBytes($bytes)
|
||||
$text = -join ($bytes | ForEach-Object { $_.ToString("x2") })
|
||||
[System.IO.File]::WriteAllText($path, $text)
|
||||
}
|
||||
}
|
||||
|
||||
New-Item -ItemType Directory -Force -Path (Join-Path $Root "secrets") | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path (Join-Path $Root "certs") | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path (Join-Path $Root "evidence") | Out-Null
|
||||
|
||||
@(
|
||||
"db_super_password", "db_api_password", "db_worker_password", "redis_password",
|
||||
"rabbit_api_password", "rabbit_worker_password", "grafana_password",
|
||||
"s3_admin_secret", "s3_api_secret", "s3_worker_secret"
|
||||
) | ForEach-Object { New-Secret $_ }
|
||||
|
||||
foreach ($pair in @(
|
||||
@("s3_admin_access", "s3admin"),
|
||||
@("s3_api_access", "s3api"),
|
||||
@("s3_worker_access", "s3worker")
|
||||
)) {
|
||||
$path = Join-Path $Root ("secrets\" + $pair[0])
|
||||
if (-not (Test-Path $path)) {
|
||||
$suffix = ([guid]::NewGuid().ToString("N")).Substring(0, 12)
|
||||
[System.IO.File]::WriteAllText($path, $pair[1] + $suffix)
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($name in @("api", "worker", "migrate")) {
|
||||
foreach ($suffix in @("role_id", "secret_id")) {
|
||||
$path = Join-Path $Root "secrets\$name.$suffix"
|
||||
if (-not (Test-Path $path)) {
|
||||
[System.IO.File]::WriteAllText($path, "pending")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path (Join-Path $Root ".env"))) {
|
||||
Copy-Item (Join-Path $Root ".env.example") (Join-Path $Root ".env")
|
||||
}
|
||||
|
||||
Get-ChildItem -Path (Join-Path $Root "scripts"), (Join-Path $Root "infra") -Recurse -Include *.sh,*.cfg,*.hcl |
|
||||
ForEach-Object {
|
||||
$text = [System.IO.File]::ReadAllText($_.FullName) -replace "`r`n", "`n" -replace "`r", "`n"
|
||||
$utf8 = New-Object System.Text.UTF8Encoding $false
|
||||
[System.IO.File]::WriteAllText($_.FullName, $text, $utf8)
|
||||
}
|
||||
|
||||
Write-Host "creating certificates"
|
||||
docker run --rm -v "${Root}\certs:/work" -v "${Root}\scripts\gen-certs.sh:/gen-certs.sh:ro" alpine:3.21.3 sh /gen-certs.sh
|
||||
if ($LASTEXITCODE -ne 0) { throw "certificate generation failed" }
|
||||
|
||||
Write-Host "building application image"
|
||||
docker compose --env-file .env build
|
||||
if ($LASTEXITCODE -ne 0) { throw "image build failed" }
|
||||
|
||||
Write-Host "starting data services"
|
||||
docker compose --env-file .env up -d postgres redis rabbitmq rabbit-mtls s3 s3-tls openbao otel-collector otel-mtls prometheus loki tempo grafana
|
||||
if ($LASTEXITCODE -ne 0) { throw "data services failed to start" }
|
||||
|
||||
Write-Host "bootstrapping OpenBao"
|
||||
docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap
|
||||
if ($LASTEXITCODE -ne 0) { throw "OpenBao bootstrap failed" }
|
||||
|
||||
Write-Host "applying schema"
|
||||
docker compose --env-file .env up -d migrate
|
||||
if ($LASTEXITCODE -ne 0) { throw "migrate failed to start" }
|
||||
$deadline = (Get-Date).AddMinutes(3)
|
||||
do {
|
||||
Start-Sleep -Seconds 2
|
||||
$state = docker compose --env-file .env ps --format json migrate
|
||||
if ($state -match '"ExitCode":0' -or $state -match '"ExitCode": 0') { break }
|
||||
if ($state -match '"ExitCode":' -and $state -notmatch '"ExitCode":0' -and $state -notmatch '"State":"running"' -and $state -match '"ExitCode":[1-9]') {
|
||||
throw "migrate failed"
|
||||
}
|
||||
} while ((Get-Date) -lt $deadline)
|
||||
|
||||
Write-Host "granting database roles"
|
||||
docker compose --env-file .env exec -T postgres sh /etc/postgres/grant.sh
|
||||
if ($LASTEXITCODE -ne 0) { throw "database grants failed" }
|
||||
|
||||
Write-Host "creating bucket"
|
||||
docker compose --env-file .env up -d s3-init
|
||||
if ($LASTEXITCODE -ne 0) { throw "s3-init failed to start" }
|
||||
|
||||
Write-Host "starting API and worker"
|
||||
docker compose --env-file .env up -d --scale api=2 --no-recreate api worker lb
|
||||
if ($LASTEXITCODE -ne 0) { throw "application services failed to start" }
|
||||
|
||||
Write-Host "stand is starting. Public URL: https://127.0.0.1:8443 CA: certs\ca.crt"
|
||||
Write-Host "OpenBao stays sealed across its own restart. Unseal with: docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap"
|
||||
@@ -0,0 +1,75 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
apk add --no-cache openssl >/dev/null
|
||||
cd /work
|
||||
if [ -f ca.crt ] && [ -f allowed-lb.sha256 ] && [ -f lb.pem ]; then
|
||||
echo "certificates already present"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
openssl req -x509 -newkey rsa:3072 -days 30 -nodes \
|
||||
-keyout ca.key -out ca.crt \
|
||||
-subj "/CN=messenger-lab-ca" \
|
||||
-addext basicConstraints=critical,CA:TRUE \
|
||||
-addext keyUsage=critical,keyCertSign,cRLSign >/dev/null 2>&1
|
||||
|
||||
issue() {
|
||||
name="$1"
|
||||
cn="$2"
|
||||
eku="$3"
|
||||
san="$4"
|
||||
cat > "/tmp/${name}.cnf" <<EOF
|
||||
[req]
|
||||
distinguished_name=dn
|
||||
prompt=no
|
||||
[dn]
|
||||
CN=${cn}
|
||||
[ext]
|
||||
subjectAltName=${san}
|
||||
keyUsage=digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=${eku}
|
||||
basicConstraints=CA:FALSE
|
||||
EOF
|
||||
openssl req -newkey rsa:2048 -nodes -keyout "${name}.key" -out "/tmp/${name}.csr" -subj "/CN=${cn}" >/dev/null 2>&1
|
||||
openssl x509 -req -in "/tmp/${name}.csr" -CA ca.crt -CAkey ca.key -CAcreateserial \
|
||||
-out "${name}.crt" -days 7 -extfile "/tmp/${name}.cnf" -extensions ext >/dev/null 2>&1
|
||||
cat "${name}.crt" "${name}.key" > "${name}.pem"
|
||||
}
|
||||
|
||||
issue lb localhost serverAuth "DNS:localhost,IP:127.0.0.1"
|
||||
issue api api serverAuth "DNS:api,DNS:localhost,IP:127.0.0.1"
|
||||
issue lb-client lb clientAuth "DNS:lb,URI:spiffe://messenger/lb"
|
||||
issue lb-revoked lb clientAuth "DNS:lb,URI:spiffe://messenger/lb"
|
||||
issue stranger stranger clientAuth "URI:spiffe://messenger/stranger"
|
||||
issue api-client api clientAuth "DNS:api-client,URI:spiffe://messenger/api"
|
||||
issue worker-client worker clientAuth "DNS:worker-client,URI:spiffe://messenger/worker"
|
||||
issue postgres postgres serverAuth "DNS:postgres"
|
||||
issue redis redis serverAuth "DNS:redis,IP:127.0.0.1"
|
||||
issue rabbitmq rabbitmq serverAuth "DNS:rabbitmq"
|
||||
issue s3 s3 serverAuth "DNS:s3,DNS:localhost,IP:127.0.0.1"
|
||||
issue otel otel-collector serverAuth "DNS:otel-collector"
|
||||
issue prometheus prometheus serverAuth "DNS:prometheus"
|
||||
issue loki loki serverAuth "DNS:loki"
|
||||
issue tempo tempo serverAuth "DNS:tempo"
|
||||
issue grafana localhost serverAuth "DNS:localhost,IP:127.0.0.1"
|
||||
issue openbao openbao serverAuth "DNS:openbao,IP:127.0.0.1"
|
||||
|
||||
openssl req -x509 -newkey rsa:2048 -days 7 -nodes \
|
||||
-keyout other-ca.key -out other-ca.crt \
|
||||
-subj "/CN=other-ca" \
|
||||
-addext basicConstraints=critical,CA:TRUE \
|
||||
-addext keyUsage=critical,keyCertSign,cRLSign >/dev/null 2>&1
|
||||
openssl req -newkey rsa:2048 -nodes -keyout other-client.key -out /tmp/other-client.csr -subj "/CN=lb" >/dev/null 2>&1
|
||||
cat > /tmp/other-client.cnf <<EOF
|
||||
subjectAltName=URI:spiffe://messenger/lb
|
||||
keyUsage=digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=clientAuth
|
||||
basicConstraints=CA:FALSE
|
||||
EOF
|
||||
openssl x509 -req -in /tmp/other-client.csr -CA other-ca.crt -CAkey other-ca.key -CAcreateserial \
|
||||
-out other-client.crt -days 7 -extfile /tmp/other-client.cnf >/dev/null 2>&1
|
||||
cat other-client.crt other-client.key > other-client.pem
|
||||
|
||||
openssl x509 -in lb-client.crt -outform DER | openssl dgst -sha256 -r | awk '{print $1}' > allowed-lb.sha256
|
||||
find . -name '*.key' -exec chmod 600 {} \;
|
||||
echo "certificates created"
|
||||
+4
-1
@@ -1,10 +1,13 @@
|
||||
#!/bin/sh
|
||||
# Restart API replicas and every store they use. Volumes are kept.
|
||||
# Restart API replicas and the stores they use. Volumes stay. OpenBao is left running
|
||||
# so it does not need another unseal; API logs in again with the AppRole files.
|
||||
set -eu
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
if [ -f .env ]; then
|
||||
docker compose --env-file .env restart postgres redis s3 rabbitmq api worker
|
||||
docker compose --env-file .env restart s3-tls rabbit-mtls
|
||||
else
|
||||
docker compose restart postgres redis s3 rabbitmq api worker
|
||||
docker compose restart s3-tls rabbit-mtls
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user