Skip to content

Commit

Permalink
Removed github auth requirements in development mode
Browse files Browse the repository at this point in the history
Removed github auth requirements in development mode

Removed github auth requirements in development mode

Signed-off-by: AyishikD <[email protected]>

Removed github auth requirements in development mode

Signed-off-by: AyishikD <[email protected]>

Removed github auth requirements in development mode

Signed-off-by: AyishikD <[email protected]>

Removed github auth requirements in development mode

Signed-off-by: AyishikD <[email protected]>
  • Loading branch information
AyishikD committed Sep 10, 2024
1 parent 4660f3c commit 00c2c25
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/teuthology_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

load_dotenv()

DEPLOYMENT = os.getenv("DEPLOYMENT")
DEPLOYMENT = os.getenv("DEPLOYMENT", "development")
SESSION_SECRET_KEY = os.getenv("SESSION_SECRET_KEY")
PULPITO_URL = os.getenv("PULPITO_URL")
PADDLES_URL = os.getenv("PADDLES_URL")
Expand All @@ -26,6 +26,7 @@ def read_root(request: Request):
return {"root": "success", "session": request.session.get("user", None)}



if DEPLOYMENT == "development":
app.add_middleware(
CORSMiddleware,
Expand All @@ -35,7 +36,12 @@ def read_root(request: Request):
allow_headers=["*"],
)

app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET_KEY)

if SESSION_SECRET_KEY:
app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET_KEY)
else:
log.warning("SESSION_SECRET_KEY is not set. Sessions are disabled.")

app.include_router(suite.router)
app.include_router(kill.router)
app.include_router(login.router)
Expand Down
16 changes: 12 additions & 4 deletions src/teuthology_api/routes/suite.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os

from fastapi import APIRouter, HTTPException, Depends, Request

from teuthology_api.services.suite import run
from teuthology_api.services.helpers import get_token, get_username
from teuthology_api.schemas.suite import SuiteArgs
Expand All @@ -14,6 +14,7 @@
responses={404: {"description": "Not found"}},
)

DEPLOYMENT = os.getenv("DEPLOYMENT", "development")

@router.post("/", status_code=200)
def create_run(
Expand All @@ -22,6 +23,13 @@ def create_run(
access_token: str = Depends(get_token),
logs: bool = False,
):
args = args.model_dump(by_alias=True)
args["--user"] = get_username(request)
return run(args, logs, access_token)
try:
args = args.model_dump(by_alias=True)
args["--user"] = get_username(request)
return run(args, logs, access_token)
except HTTPException as exc:
log.error(f"HTTP exception occurred: {exc.detail}")
raise
except Exception as exc:
log.error(f"Unexpected error occurred: {repr(exc)}")
raise HTTPException(status_code=500, detail="An unexpected error occurred.")
19 changes: 12 additions & 7 deletions src/teuthology_api/services/helpers.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
from multiprocessing import Process
import logging
import os
import uuid
from pathlib import Path

from fastapi import HTTPException, Request
from dotenv import load_dotenv

import teuthology
import requests # Note: import requests after teuthology
import requests
from requests.exceptions import HTTPError

load_dotenv()

PADDLES_URL = os.getenv("PADDLES_URL")
ARCHIVE_DIR = os.getenv("ARCHIVE_DIR")
DEPLOYMENT = os.getenv("DEPLOYMENT", "development")

log = logging.getLogger(__name__)


def logs_run(func, args):
"""
Run the command function in a seperate process (to isolate logs),
Run the command function in a separate process (to isolate logs),
and return logs printed during the execution of the function.
"""
_id = str(uuid.uuid4())
Expand Down Expand Up @@ -70,8 +68,11 @@ def get_run_details(run_name: str):

def get_username(request: Request):
"""
Get username from request.session
Get username from request.session, bypass authentication if in development.
"""
if DEPLOYMENT == "development":
return "dev_user"

username = request.session.get("user", {}).get("username")
if username:
return username
Expand All @@ -85,8 +86,11 @@ def get_username(request: Request):

def get_token(request: Request):
"""
Get access token from request.session
Get access token from request.session, bypass authentication if in development.
"""
if DEPLOYMENT == "development":
return {"access_token": "dev_token", "token_type": "bearer"}

token = request.session.get("user", {}).get("access_token")
if token:
return {"access_token": token, "token_type": "bearer"}
Expand All @@ -96,3 +100,4 @@ def get_token(request: Request):
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)

4 changes: 2 additions & 2 deletions src/teuthology_api/services/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run(args, send_logs: bool, access_token: str, request: Request):
else:
log.error("teuthology-kill is missing --run")
raise HTTPException(status_code=400, detail="--run is a required argument")
# TODO if user has admin priviledge, then they can kill any run/job.
# TODO if user has admin privilege, then they can kill any run/job.
if run_owner.lower() != username.lower():
log.error(
"%s doesn't have permission to kill a job scheduled by: %s",
Expand Down Expand Up @@ -60,4 +60,4 @@ def run(args, send_logs: bool, access_token: str, request: Request):
return {"kill": "success"}
except Exception as exc:
log.error("teuthology-kill command failed with the error: %s", repr(exc))
raise HTTPException(status_code=500, detail=repr(exc)) from exc
raise HTTPException(status_code=500, detail=repr(exc)) from exc
2 changes: 1 addition & 1 deletion src/teuthology_api/services/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ def make_run_name(run_dic):
run_dic["flavor"],
worker,
]
).replace("/", ":")
).replace("/", ":")

0 comments on commit 00c2c25

Please sign in to comment.