Add remote autoscaler daemon endpoint support
All checks were successful
buildbot/nix-eval Build done.
buildbot/nix-build Build done.
buildbot/nix-effects Build done.

This commit is contained in:
Abel Luck 2026-03-05 15:47:57 +01:00
parent 95021a4253
commit 679b5c8d07
11 changed files with 291 additions and 22 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations
import hmac
import logging
import uuid
from collections.abc import Callable
@ -118,6 +119,8 @@ def create_app(
app.state.runtime = runtime
app.state.haproxy = haproxy
auth_token = config.server.auth_token.strip()
@app.middleware("http")
async def request_id_middleware(request: Request, call_next: Callable) -> Response:
request.state.request_id = str(uuid.uuid4())
@ -125,6 +128,25 @@ def create_app(
response.headers["x-request-id"] = request.state.request_id
return response
@app.middleware("http")
async def auth_middleware(request: Request, call_next: Callable) -> Response:
path = request.url.path
if auth_token != "" and (path.startswith("/v1/") or path == "/metrics"):
expected = f"Bearer {auth_token}"
provided = request.headers.get("authorization", "")
if not hmac.compare_digest(provided, expected):
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
payload = ErrorResponse(
error=ErrorDetail(
code="unauthorized",
message="Missing or invalid bearer token",
retryable=False,
),
request_id=request_id,
)
return JSONResponse(status_code=401, content=payload.model_dump(mode="json"))
return await call_next(request)
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
detail = exc.detail