ProdLaunch
This commit is contained in:
+32
-1
@@ -24,14 +24,45 @@ settings = get_settings()
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def _validate_production_config() -> None:
|
||||
"""
|
||||
Verifica che le variabili critiche di sicurezza siano state
|
||||
sostituite rispetto ai valori di default insicuri.
|
||||
Blocca il boot se APP_ENV=production e i valori non sono stati cambiati.
|
||||
"""
|
||||
insecure_defaults = {
|
||||
"SECRET_KEY": (settings.secret_key, "change-me-in-production"),
|
||||
"ENCRYPTION_KEY": (settings.encryption_key, "0" * 64),
|
||||
}
|
||||
errors: list[str] = []
|
||||
for var, (current, default) in insecure_defaults.items():
|
||||
if current == default:
|
||||
errors.append(f"{var} non e' stato impostato (usa ancora il valore di default)")
|
||||
|
||||
if not settings.admin_secret_key:
|
||||
errors.append(
|
||||
"ADMIN_SECRET_KEY e' vuota: gli endpoint /api/v1/tenants non sono protetti"
|
||||
)
|
||||
|
||||
if errors:
|
||||
for err in errors:
|
||||
logger.warning(f"[CONFIG] {err}")
|
||||
if settings.is_production:
|
||||
raise RuntimeError(
|
||||
"Configurazione insicura rilevata in ambiente production. "
|
||||
"Correggere le variabili e riavviare: " + "; ".join(errors)
|
||||
)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
"""Gestione ciclo di vita dell'applicazione."""
|
||||
import asyncio
|
||||
|
||||
setup_logging()
|
||||
_validate_production_config()
|
||||
logger.info(
|
||||
"🚀 PEChub Backend avviato",
|
||||
"PEChub Backend avviato",
|
||||
extra={"env": settings.app_env, "debug": settings.app_debug},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user