Skip to content

Commit

Permalink
runner+worker: Add bearer auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yondonfu committed Feb 13, 2024
1 parent df80da8 commit 34d5555
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 32 deletions.
13 changes: 12 additions & 1 deletion runner/app/routes/image_to_image.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from fastapi import Depends, APIRouter, UploadFile, File, Form
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from app.pipelines import ImageToImagePipeline
from app.dependencies import get_pipeline
from app.routes.util import image_to_data_url, ImageResponse, HTTPError, http_error
import PIL
from typing import Annotated
import logging
import random
from typing import List
import os

router = APIRouter()

Expand Down Expand Up @@ -35,7 +36,17 @@ async def image_to_image(
seed: Annotated[int, Form()] = None,
num_images_per_prompt: Annotated[int, Form()] = 1,
pipeline: ImageToImagePipeline = Depends(get_pipeline),
token: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False)),
):
auth_token = os.environ.get("AUTH_TOKEN")
if auth_token:
if not token or token.credentials != auth_token:
return JSONResponse(
status_code=401,
headers={"WWW-Authenticate": "Bearer"},
content=http_error("Invalid bearer token"),
)

if model_id != "" and model_id != pipeline.model_id:
return JSONResponse(
status_code=400,
Expand Down
24 changes: 17 additions & 7 deletions runner/app/routes/image_to_video.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from fastapi import Depends, APIRouter, UploadFile, File, Form
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from app.pipelines import ImageToVideoPipeline
from app.dependencies import get_pipeline
from app.routes.util import image_to_data_url, VideoResponse, HTTPError
from app.routes.util import image_to_data_url, VideoResponse, HTTPError, http_error
import PIL
from typing import Annotated
import logging
import random
import os

router = APIRouter()

Expand Down Expand Up @@ -34,15 +36,23 @@ async def image_to_video(
noise_aug_strength: Annotated[float, Form()] = 0.02,
seed: Annotated[int, Form()] = None,
pipeline: ImageToVideoPipeline = Depends(get_pipeline),
token: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False)),
):
auth_token = os.environ.get("AUTH_TOKEN")
if auth_token:
if not token or token.credentials != auth_token:
return JSONResponse(
status_code=401,
headers={"WWW-Authenticate": "Bearer"},
content=http_error("Invalid bearer token"),
)

if model_id != "" and model_id != pipeline.model_id:
return JSONResponse(
status_code=400,
content={
"detail": {
"msg": f"pipeline configured with {pipeline.model_id} but called with {model_id}"
}
},
content=http_error(
f"pipeline configured with {pipeline.model_id} but called with {model_id}"
),
)

if seed is None:
Expand All @@ -62,7 +72,7 @@ async def image_to_video(
logger.error(f"ImageToVideoPipeline error: {e}")
logger.exception(e)
return JSONResponse(
status_code=500, content={"detail": {"msg": "ImageToVideoPipeline error"}}
status_code=500, content=http_error("ImageToVideoPipeline error")
)

output_frames = []
Expand Down
16 changes: 14 additions & 2 deletions runner/app/routes/text_to_image.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from pydantic import BaseModel
from fastapi import Depends, APIRouter
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from app.pipelines import TextToImagePipeline
from app.dependencies import get_pipeline
from app.routes.util import image_to_data_url, ImageResponse, HTTPError, http_error
import logging
from typing import List
import random
import os

router = APIRouter()

Expand All @@ -32,8 +33,19 @@ class TextToImageParams(BaseModel):
@router.post("/text-to-image", response_model=ImageResponse, responses=responses)
@router.post("/text-to-image/", response_model=ImageResponse, include_in_schema=False)
async def text_to_image(
params: TextToImageParams, pipeline: TextToImagePipeline = Depends(get_pipeline)
params: TextToImageParams,
pipeline: TextToImagePipeline = Depends(get_pipeline),
token: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False)),
):
auth_token = os.environ.get("AUTH_TOKEN")
if auth_token:
if not token or token.credentials != auth_token:
return JSONResponse(
status_code=401,
headers={"WWW-Authenticate": "Bearer"},
content=http_error("Invalid bearer token"),
)

if params.model_id != "" and params.model_id != pipeline.model_id:
return JSONResponse(
status_code=400,
Expand Down
27 changes: 24 additions & 3 deletions runner/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@
}
}
}
}
},
"security": [
{
"HTTPBearer": []
}
]
}
},
"/image-to-image": {
Expand Down Expand Up @@ -137,7 +142,12 @@
}
}
}
}
},
"security": [
{
"HTTPBearer": []
}
]
}
},
"/image-to-video": {
Expand Down Expand Up @@ -195,7 +205,12 @@
}
}
}
}
},
"security": [
{
"HTTPBearer": []
}
]
}
}
},
Expand Down Expand Up @@ -477,6 +492,12 @@
],
"title": "VideoResponse"
}
},
"securitySchemes": {
"HTTPBearer": {
"type": "http",
"scheme": "bearer"
}
}
}
}
48 changes: 29 additions & 19 deletions worker/runner.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34d5555

Please sign in to comment.