forked from mtusi2026/mtusi2026lab7
104 lines
4.0 KiB
Python
104 lines
4.0 KiB
Python
"""Explicit configuration; credentials are read from untracked secret files."""
|
|
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
from messenger.secrets_provider import load_service_secrets
|
|
|
|
|
|
def secret(name: str, default: str, optional=False) -> str:
|
|
path = Path(os.getenv(name, default))
|
|
return path.read_text().strip() if path.exists() or not optional else ""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
database_url: str = field(repr=False)
|
|
cache_url: str = field(repr=False)
|
|
session_ttl: int = 3600
|
|
pool_size: int = 8
|
|
idempotency_ttl: int = 86400
|
|
read_autocommit: bool = True
|
|
|
|
s3_endpoint: str = "http://127.0.0.1:59006"
|
|
s3_region: str = "us-east-1"
|
|
s3_bucket: str = "messenger-attachments"
|
|
s3_access_key: str = field(default="", repr=False)
|
|
s3_secret_key: str = field(default="", repr=False)
|
|
max_upload_bytes: int = 10485760
|
|
max_image_pixels: int = 40000000
|
|
upload_slots: int = 1
|
|
|
|
broker_url: str = field(default="redis://broker:6379/0", repr=False)
|
|
worker_concurrency: int = 2
|
|
job_max_attempts: int = 5
|
|
job_timeout: int = 20
|
|
staging_retention: int = 86400
|
|
fault_injection: bool = False
|
|
|
|
@property
|
|
def lease_seconds(self):
|
|
return self.job_timeout + 3
|
|
|
|
@classmethod
|
|
def from_env(cls):
|
|
ttl = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
|
size = int(os.getenv("DATABASE_POOL_SIZE", "8"))
|
|
idempotency_ttl = int(os.getenv("IDEMPOTENCY_TTL_SECONDS", "86400"))
|
|
if idempotency_ttl < 86400:
|
|
raise ValueError("Idempotency retention must be at least 24 hours")
|
|
if not 1 <= ttl <= 2_592_000 or not 1 <= size <= 32:
|
|
raise ValueError("Invalid session lifetime or connection pool size")
|
|
maximum = int(os.getenv("MAX_UPLOAD_BYTES", "10485760"))
|
|
pixels = int(os.getenv("MAX_IMAGE_PIXELS", "40000000"))
|
|
if not 1 <= maximum <= 10485760 or not 1 <= pixels <= 40000000:
|
|
raise ValueError("Invalid upload limits")
|
|
concurrency = int(os.getenv("WORKER_CONCURRENCY", "2"))
|
|
attempts = int(os.getenv("JOB_MAX_ATTEMPTS", "5"))
|
|
timeout = int(os.getenv("JOB_TIMEOUT_SECONDS", "20"))
|
|
retention = int(os.getenv("STAGING_RETENTION_SECONDS", "86400"))
|
|
if not 1 <= concurrency <= 4 or not 1 <= attempts <= 10 or not 2 <= timeout <= 120:
|
|
raise ValueError("Invalid worker limits")
|
|
if retention < 300:
|
|
raise ValueError("Staging retention must be at least 300 seconds")
|
|
role = os.getenv("BAO_ROLE")
|
|
scoped = load_service_secrets(role) if role else {}
|
|
|
|
def credential(key, name, path, *, optional=False):
|
|
if role:
|
|
return scoped[key]
|
|
return secret(name, path, optional)
|
|
|
|
return cls(
|
|
credential("database_url", "DATABASE_URL_FILE", "/run/secrets/database_url"),
|
|
credential("cache_url", "CACHE_URL_FILE", "/run/secrets/cache_url"),
|
|
ttl,
|
|
size,
|
|
idempotency_ttl,
|
|
os.getenv("DATABASE_READ_AUTOCOMMIT", "1") == "1",
|
|
os.getenv("S3_ENDPOINT", "http://s3:9000"),
|
|
os.getenv("S3_REGION", "us-east-1"),
|
|
os.getenv("S3_BUCKET", "messenger-attachments"),
|
|
credential(
|
|
"s3_access_key",
|
|
"S3_ACCESS_KEY_ID_FILE",
|
|
"/run/secrets/s3_access_key_id",
|
|
optional=True,
|
|
),
|
|
credential(
|
|
"s3_secret_key",
|
|
"S3_SECRET_ACCESS_KEY_FILE",
|
|
"/run/secrets/s3_secret_access_key",
|
|
optional=True,
|
|
),
|
|
maximum,
|
|
pixels,
|
|
broker_url=scoped["broker_url"] if role else "rediss://broker:6380/0",
|
|
worker_concurrency=concurrency,
|
|
job_max_attempts=attempts,
|
|
job_timeout=timeout,
|
|
staging_retention=retention,
|
|
fault_injection=os.getenv("ENABLE_FAULT_INJECTION", "0") == "1",
|
|
)
|