Skip to content

Commit

Permalink
refactor(auth): add hub params and refactor node IDP variables
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 8, 2024
1 parent 206dae6 commit 02449df
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
7 changes: 6 additions & 1 deletion gateway/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ class Settings(BaseModel):
# Service URLs
RESULTS_SERVICE_URL: str = os.getenv("RESULTS_SERVICE_URL", "http://localhost:8000")
PODORC_SERVICE_URL: str = os.getenv("PODORC_SERVICE_URL")
HUB_SERVICE_URL: str = os.getenv("HUB_SERVICE_URL")

# UI ID and secret
UI_CLIENT_ID: str = os.getenv("UI_CLIENT_ID", "test-client")
UI_CLIENT_SECRET: str = os.getenv("UI_CLIENT_SECRET", "someSecret")

# Hub
HUB_AUTH_SERVICE_URL: str = os.getenv("HUB_AUTH_SERVICE_URL", "https://auth.privateaim.net")
HUB_SERVICE_URL: str = os.getenv("HUB_AUTH_SERVICE_URL", "https://api.privateaim.net")
HUB_USERNAME: str = os.getenv("HUB_USERNAME", "admin")
HUB_PASSWORD: str = os.getenv("HUB_PASSWORD", "start123")


gateway_settings = Settings()
57 changes: 30 additions & 27 deletions gateway/core.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import functools
import os
import tempfile
from typing import Sequence

import async_timeout
from aiohttp import JsonPayload, ClientSession, hdrs
import httpx
from aiohttp import JsonPayload, hdrs
from aiohttp.client_exceptions import ClientConnectorError, ContentTypeError
from fastapi import HTTPException, params, status
from fastapi.datastructures import Headers
from fastapi.requests import Request
from fastapi.responses import StreamingResponse, JSONResponse
from starlette.background import BackgroundTask
from starlette.responses import Response, FileResponse
from starlette.responses import Response

from gateway.conf import gateway_settings
from gateway.models import GatewayFormData
from gateway.utils import unzip_form_params, unzip_body_object, create_request_data

Expand Down Expand Up @@ -46,26 +42,32 @@ async def make_request(
if not data: # Always package data else error
data = {}

with async_timeout.timeout(gateway_settings.GATEWAY_TIMEOUT):
async with ClientSession(headers=headers) as session:
async with session.request(url=url, method=method, data=data) as resp:
if resp.headers[hdrs.CONTENT_TYPE] == 'application/json':
resp_data = await resp.json()
return resp_data, resp.status

elif resp.headers[hdrs.CONTENT_TYPE] == 'application/octet-stream':
with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as temp_file:
async for chunk, _ in resp.content.iter_chunks(): # iterates over chunks received from microsvc
temp_file.write(chunk)

def cleanup():
os.remove(temp_file.name)

return FileResponse(
temp_file.name,
background=BackgroundTask(cleanup),
headers=resp.headers
), resp.status
async with httpx.AsyncClient(headers=headers) as client:
r = await client.request(url=url, method=method, data=data)
resp_data = r.json()
return resp_data, r.status_code

# with async_timeout.timeout(gateway_settings.GATEWAY_TIMEOUT):
# async with ClientSession(headers=headers) as session:
# async with session.request(url=url, method=method, data=data) as resp:
#
# if hdrs.CONTENT_TYPE not in resp.headers or resp.headers[hdrs.CONTENT_TYPE] == 'application/json':
# resp_data = await resp.json()
# return resp_data, resp.status
#
# elif resp.headers[hdrs.CONTENT_TYPE] == 'application/octet-stream':
# with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as temp_file:
# async for chunk, _ in resp.content.iter_chunks(): # iterates over chunks received from microsvc
# temp_file.write(chunk)
#
# def cleanup():
# os.remove(temp_file.name)
#
# return FileResponse(
# temp_file.name,
# background=BackgroundTask(cleanup),
# headers=resp.headers
# ), resp.status


def route(
Expand Down Expand Up @@ -146,6 +148,7 @@ async def inner(request: Request, response: Response, **kwargs):
request_headers = dict(request.headers)
request_headers.pop("content-length", None) # Let aiohttp configure content-length
request_headers.pop("content-type", None) # Let aiohttp configure content-type
request_headers.pop("host", None)

# Prepare body and form data
request_body = await unzip_body_object(
Expand Down
9 changes: 5 additions & 4 deletions gateway/routers/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ async def get_images():
"""Return list of images for the frontend."""
# TODO: replace with data from https://api.privateaim.net/projects
# TODO: add project specific call / filter?

dummy_data = {
"pullImages": [
{
"id": "59081687-3dfe-46cf-afb5-07c562a002af",
"train_class_id": "choochoo",
"repo_tag": "Awesome tag",
"repo_tag": "0.5.23-pull",
"job_id": "49e79b47-686b-4fb8-9259-fd0035b0b7f6",
"status": "pulled"
}
Expand All @@ -30,7 +31,7 @@ async def get_images():
{
"id": "4a941577-46ce-4220-8ca0-181cf45abe29",
"train_class_id": "choochoo",
"repo_tag": "Awesome tag",
"repo_tag": "latest",
"job_id": "5efabb71-ba5d-4d00-9ed4-f27eb6a52e8f",
"status": "waiting_to_push"
}
Expand All @@ -53,7 +54,7 @@ async def get_containers():
"image": "4a941577-46ce-4220-8ca0-181cf45abe29",
"state": "Running",
"status": "Active",
"next_tag": "Aachen",
"next_tag": "Köln",
"repo": "/data",
"train_class_id": "choochoo",
}
Expand All @@ -71,7 +72,7 @@ async def get_vault_status():
"authenticated": True,
"config": {
"stationID": "4c0e4a1a-795b",
"stationName": "Aachen Central",
"stationName": "Test FLAME Node Central",
}
}
return dummy_data
4 changes: 2 additions & 2 deletions gateway/routers/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import kubernetes.client
from fastapi import APIRouter, Path, Security

from gateway.auth import oauth2_scheme
from gateway.auth import realm_oauth2_scheme
from gateway.conf import gateway_settings

k8s_router = APIRouter(
dependencies=[Security(oauth2_scheme)],
dependencies=[Security(realm_oauth2_scheme)],
tags=["PodOrc"],
responses={404: {"description": "Not found"}},
)
Expand Down
4 changes: 2 additions & 2 deletions gateway/routers/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import oauth2_scheme
from gateway.auth import realm_oauth2_scheme
from gateway.conf import gateway_settings
from gateway.core import route

results_router = APIRouter(
dependencies=[Security(oauth2_scheme)],
dependencies=[Security(realm_oauth2_scheme)],
tags=["Results"],
responses={404: {"description": "Not found"}},
)
Expand Down

0 comments on commit 02449df

Please sign in to comment.