[Feature] Support multiple api keys in server (#18548)

Signed-off-by: Yan Pashkovsky <yanp.bugz@gmail.com>
This commit is contained in:
Yan Pashkovsky
2025-07-30 15:03:23 +01:00
committed by GitHub
parent da3e0bd6e5
commit bf668b5bf5
3 changed files with 30 additions and 29 deletions

View File

@@ -1239,9 +1239,9 @@ class AuthenticationMiddleware:
2. The request path doesn't start with /v1 (e.g. /health).
"""
def __init__(self, app: ASGIApp, api_token: str) -> None:
def __init__(self, app: ASGIApp, tokens: list[str]) -> None:
self.app = app
self.api_token = api_token
self.api_tokens = {f"Bearer {token}" for token in tokens}
def __call__(self, scope: Scope, receive: Receive,
send: Send) -> Awaitable[None]:
@@ -1255,7 +1255,7 @@ class AuthenticationMiddleware:
headers = Headers(scope=scope)
# Type narrow to satisfy mypy.
if url_path.startswith("/v1") and headers.get(
"Authorization") != f"Bearer {self.api_token}":
"Authorization") not in self.api_tokens:
response = JSONResponse(content={"error": "Unauthorized"},
status_code=401)
return response(scope, receive, send)
@@ -1303,7 +1303,7 @@ class ScalingMiddleware:
"""
Middleware that checks if the model is currently scaling and
returns a 503 Service Unavailable response if it is.
This middleware applies to all HTTP requests and prevents
processing when the model is in a scaling state.
"""
@@ -1512,8 +1512,8 @@ def build_app(args: Namespace) -> FastAPI:
status_code=HTTPStatus.BAD_REQUEST)
# Ensure --api-key option from CLI takes precedence over VLLM_API_KEY
if token := args.api_key or envs.VLLM_API_KEY:
app.add_middleware(AuthenticationMiddleware, api_token=token)
if tokens := [key for key in (args.api_key or [envs.VLLM_API_KEY]) if key]:
app.add_middleware(AuthenticationMiddleware, tokens=tokens)
if args.enable_request_id_headers:
app.add_middleware(XRequestIdMiddleware)