diff --git a/gateway/conf.py b/gateway/conf.py index c52ba8c..240a036 100644 --- a/gateway/conf.py +++ b/gateway/conf.py @@ -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() diff --git a/gateway/core.py b/gateway/core.py index 59b555f..98a657f 100644 --- a/gateway/core.py +++ b/gateway/core.py @@ -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 @@ -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( @@ -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( diff --git a/gateway/routers/hub.py b/gateway/routers/hub.py index 515ec13..5aa3f81 100644 --- a/gateway/routers/hub.py +++ b/gateway/routers/hub.py @@ -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" } @@ -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" } @@ -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", } @@ -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 diff --git a/gateway/routers/k8s.py b/gateway/routers/k8s.py index 673e8fa..16a082a 100644 --- a/gateway/routers/k8s.py +++ b/gateway/routers/k8s.py @@ -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"}}, ) diff --git a/gateway/routers/results.py b/gateway/routers/results.py index 147dd6a..0331a8a 100644 --- a/gateway/routers/results.py +++ b/gateway/routers/results.py @@ -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"}}, )