Skip to content

Commit

Permalink
refactor: change pkg name from gateway to hub-adapter build_image
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Apr 5, 2024
1 parent 3dafa42 commit 8d6ecbe
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 140 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ FROM python:3.11-alpine
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"

COPY ./logging.json .
COPY ./gateway/ ./gateway/
COPY hub_adapter/ ./hub_adapter/

# API server port
EXPOSE 5000

ENTRYPOINT ["python", "-m", "gateway.cli", "serve"]
ENTRYPOINT ["python", "-m", "hub_adapter.cli", "serve"]

HEALTHCHECK CMD curl --fail http://localhost:5000/health || exit 1
HEALTHCHECK CMD curl --fail http://localhost:5000/healthz || exit 1
File renamed without changes.
20 changes: 10 additions & 10 deletions gateway/auth.py → hub_adapter/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
from starlette.datastructures import MutableHeaders
from starlette.requests import Request

from gateway.conf import gateway_settings
from gateway.models.conf import AuthConfiguration, Token
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.models.conf import AuthConfiguration, Token

logger = logging.getLogger(__name__)

IDP_ISSUER_URL = gateway_settings.IDP_URL.rstrip("/") + "/" + "/".join(["realms", gateway_settings.IDP_REALM])
IDP_ISSUER_URL = hub_adapter_settings.IDP_URL.rstrip("/") + "/" + "/".join(["realms", hub_adapter_settings.IDP_REALM])

# IDP i.e. Keycloak
realm_idp_settings = AuthConfiguration(
server_url=gateway_settings.IDP_URL,
realm=gateway_settings.IDP_REALM,
client_id=gateway_settings.API_CLIENT_ID,
client_secret=gateway_settings.API_CLIENT_SECRET,
server_url=hub_adapter_settings.IDP_URL,
realm=hub_adapter_settings.IDP_REALM,
client_id=hub_adapter_settings.API_CLIENT_ID,
client_secret=hub_adapter_settings.API_CLIENT_SECRET,
authorization_url=IDP_ISSUER_URL + "/protocol/openid-connect/auth",
token_url=IDP_ISSUER_URL + "/protocol/openid-connect/token",
issuer_url=IDP_ISSUER_URL,
Expand Down Expand Up @@ -52,7 +52,7 @@ async def get_idp_public_key() -> str:

async def get_hub_public_key() -> dict:
"""Get the central hub service public key."""
hub_jwks_ep = gateway_settings.HUB_AUTH_SERVICE_URL.rstrip("/") + "/jwks"
hub_jwks_ep = hub_adapter_settings.HUB_AUTH_SERVICE_URL.rstrip("/") + "/jwks"
return httpx.get(hub_jwks_ep).json()


Expand Down Expand Up @@ -100,9 +100,9 @@ async def verify_idp_token(token: str = Security(idp_oauth2_scheme)) -> dict:

async def get_hub_token() -> dict:
"""Automated method for getting a token from the central Hub service."""
hub_user, hub_pwd = gateway_settings.HUB_USERNAME, gateway_settings.HUB_PASSWORD
hub_user, hub_pwd = hub_adapter_settings.HUB_USERNAME, hub_adapter_settings.HUB_PASSWORD
payload = {"username": hub_user, "password": hub_pwd}
token_route = gateway_settings.HUB_AUTH_SERVICE_URL.rstrip("/") + "/token"
token_route = hub_adapter_settings.HUB_AUTH_SERVICE_URL.rstrip("/") + "/token"
resp = httpx.post(token_route, data=payload)

if not resp.status_code == httpx.codes.OK:
Expand Down
2 changes: 1 addition & 1 deletion gateway/cli.py → hub_adapter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():
@click.option("-r", "--reload", is_flag=True, default=False, help="Enable reload")
def serve(host, port, reload):
"""Start the API RESTful server."""
uvicorn.run("gateway.server:app", host=host, port=int(port), reload=reload)
uvicorn.run("hub_adapter.server:app", host=host, port=int(port), reload=reload)


if __name__ == "__main__":
Expand Down
36 changes: 32 additions & 4 deletions gateway/conf.py → hub_adapter/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Adapter API Settings."""
import logging
import logging.handlers as handlers
import os
import sys
from pathlib import Path

from pydantic import BaseModel

Expand All @@ -11,9 +15,6 @@ class Settings(BaseModel):
ACCESS_TOKEN_DEFAULT_EXPIRE_MINUTES: int = 360
GATEWAY_TIMEOUT: int = 59

# K8s
K8S_API_KEY: str = os.getenv("K8S_API_KEY")

# IDP Settings
IDP_URL: str = os.getenv("IDP_URL", "http://localhost:8080")
IDP_REALM: str = os.getenv("IDP_REALM", "flame")
Expand All @@ -34,4 +35,31 @@ class Settings(BaseModel):
HUB_PASSWORD: str = os.getenv("HUB_PASSWORD")


gateway_settings = Settings()
hub_adapter_settings = Settings()

# Logging
root_dir = Path(__file__).parent.resolve()
log_dir = root_dir.joinpath("logs")
log_dir.mkdir(parents=True, exist_ok=True)

main_logger = logging.getLogger("hub_adapter")

# Log Handler
logHandler = handlers.RotatingFileHandler(
filename=log_dir.joinpath("node_hub_api_adapter.log"),
mode="a",
maxBytes=4098 * 10, # 4MB file max
backupCount=5,
)
logh_format = logging.Formatter("%(levelname)s - %(module)s:L%(lineno)d - %(asctime)s - %(message)s")
logHandler.setFormatter(logh_format)
logHandler.setLevel(logging.DEBUG)

# Console Handler
streamHandler = logging.StreamHandler(stream=sys.stderr)
stream_format = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s")
streamHandler.setFormatter(stream_format)
streamHandler.setLevel(logging.WARNING)

main_logger.addHandler(logHandler)
main_logger.addHandler(streamHandler)
File renamed without changes.
6 changes: 3 additions & 3 deletions gateway/core.py → hub_adapter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from httpx import ConnectError, DecodingError
from starlette.responses import Response, FileResponse

from gateway import post_processing
from gateway.constants import CONTENT_TYPE
from gateway.utils import unzip_form_params, unzip_body_object, create_request_data, unzip_query_params, \
from hub_adapter import post_processing
from hub_adapter.constants import CONTENT_TYPE
from hub_adapter.utils import unzip_form_params, unzip_body_object, create_request_data, unzip_query_params, \
unzip_file_params

logger = logging.getLogger(__name__)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions gateway/routers/health.py → hub_adapter/routers/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from fastapi import APIRouter
from starlette import status

from gateway.conf import gateway_settings
from gateway.models.health import HealthCheck, DownstreamHealthCheck
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.models.health import HealthCheck, DownstreamHealthCheck

health_router = APIRouter(
tags=["Health"],
Expand Down Expand Up @@ -44,9 +44,9 @@ def get_health() -> HealthCheck:
def get_health_downstream_services() -> DownstreamHealthCheck:
"""Return the health of the downstream microservices."""
health_eps = {
"po": gateway_settings.PODORC_SERVICE_URL.rstrip("/") + "/healthz",
"results": gateway_settings.RESULTS_SERVICE_URL.rstrip("/") + "/healthz",
"hub": gateway_settings.HUB_SERVICE_URL,
"kong": gateway_settings.KONG_ADMIN_SERVICE_URL,
"po": hub_adapter_settings.PODORC_SERVICE_URL.rstrip("/") + "/healthz",
"results": hub_adapter_settings.RESULTS_SERVICE_URL.rstrip("/") + "/healthz",
"hub": hub_adapter_settings.HUB_SERVICE_URL,
"kong": hub_adapter_settings.KONG_ADMIN_SERVICE_URL,
}
return health_eps
25 changes: 13 additions & 12 deletions gateway/routers/hub.py → hub_adapter/routers/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import add_hub_jwt, verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route
from gateway.models.hub import Project, AllProjects, ApprovalStatus, AnalysisOrProjectNode, ListAnalysisOrProjectNodes, \
from hub_adapter.auth import add_hub_jwt, verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.core import route
from hub_adapter.models.hub import Project, AllProjects, ApprovalStatus, AnalysisOrProjectNode, \
ListAnalysisOrProjectNodes, \
AnalysisNode
from gateway.models.podorc import ImageDataResponse, ContainerResponse
from hub_adapter.models.podorc import ImageDataResponse, ContainerResponse

hub_router = APIRouter(
dependencies=[Security(verify_idp_token), Depends(add_hub_jwt), Security(idp_oauth2_scheme_pass),
Expand Down Expand Up @@ -69,7 +70,7 @@ async def get_vault_status():
request_method=hub_router.get,
path="/projects",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=AllProjects,
query_params=["filter_id", "filter_realm_id", "filter_user_id", "include"],
)
Expand All @@ -95,7 +96,7 @@ async def list_all_projects(
request_method=hub_router.get,
path="/projects/{project_id}",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=Project,
)
async def list_specific_project(
Expand All @@ -111,7 +112,7 @@ async def list_specific_project(
request_method=hub_router.get,
path="/project-nodes",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=ListAnalysisOrProjectNodes,
query_params=["filter_id", "filter_project_id", "filter_project_realm_id",
"filter_node_id", "filter_node_realm_id"],
Expand Down Expand Up @@ -158,7 +159,7 @@ async def list_projects_and_nodes(
request_method=hub_router.post,
path="/project-nodes/{project_id}",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=AnalysisOrProjectNode,
body_params=["approval_status"],
)
Expand All @@ -178,7 +179,7 @@ async def accept_reject_project_node(
request_method=hub_router.get,
path="/analysis-nodes",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
# response_model=ListAnalysisNodes,
response_model=ContainerResponse,
query_params=["filter_id", "filter_project_id", "filter_project_realm_id",
Expand Down Expand Up @@ -246,7 +247,7 @@ async def list_analyses_of_nodes(
request_method=hub_router.get,
path="/analysis-nodes/{analysis_id}",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=AnalysisNode,
)
async def list_specific_analysis(
Expand All @@ -262,7 +263,7 @@ async def list_specific_analysis(
request_method=hub_router.post,
path="/analysis-nodes/{analysis_id}",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.HUB_SERVICE_URL,
service_url=hub_adapter_settings.HUB_SERVICE_URL,
response_model=AnalysisNode,
body_params=["approval_status"],
)
Expand Down
8 changes: 4 additions & 4 deletions gateway/routers/kong.py → hub_adapter/routers/kong.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from kong_admin_client.rest import ApiException
from starlette import status

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.models.kong import ServiceRequest, HttpMethodCode, ProtocolCode, LinkDataStoreProject, \
from hub_adapter.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.models.kong import ServiceRequest, HttpMethodCode, ProtocolCode, LinkDataStoreProject, \
Disconnect, LinkProjectAnalysis

kong_router = APIRouter(
Expand All @@ -22,7 +22,7 @@
)

logger = logging.getLogger(__name__)
kong_admin_url = gateway_settings.KONG_ADMIN_SERVICE_URL
kong_admin_url = hub_adapter_settings.KONG_ADMIN_SERVICE_URL


@kong_router.get("/datastore", response_model=ListRoute200Response, status_code=status.HTTP_200_OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from fastapi import APIRouter

from gateway.conf import gateway_settings
from gateway.models.conf import KeycloakConfig
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.models.conf import KeycloakConfig

metadata_router = APIRouter(
# dependencies=[Security(oauth2_scheme)],
Expand All @@ -16,8 +16,8 @@
async def get_keycloak_config():
"""Return keycloak metadata for the frontend."""
return {
"realm": gateway_settings.IDP_REALM,
"url": gateway_settings.IDP_URL,
"realm": hub_adapter_settings.IDP_REALM,
"url": hub_adapter_settings.IDP_URL,
"clientId": "node-ui-app",
}

Expand Down
18 changes: 9 additions & 9 deletions gateway/routers/podorc.py → hub_adapter/routers/podorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route
from hub_adapter.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.core import route

po_router = APIRouter(
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass), Security(httpbearer)],
Expand All @@ -24,7 +24,7 @@
request_method=po_router.post,
path="/po",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
body_params=["analysis_id", "project_id"],
)
async def create_analysis(
Expand All @@ -41,7 +41,7 @@ async def create_analysis(
request_method=po_router.get,
path="/po/{analysis_id}/logs",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
)
async def get_analysis_logs(
request: Request,
Expand All @@ -56,7 +56,7 @@ async def get_analysis_logs(
request_method=po_router.get,
path="/po/{analysis_id}/status",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
)
async def get_analysis_status(
request: Request,
Expand All @@ -71,7 +71,7 @@ async def get_analysis_status(
request_method=po_router.get,
path="/po/{analysis_id}/pods",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
)
async def get_analysis_pods(
request: Request,
Expand All @@ -86,7 +86,7 @@ async def get_analysis_pods(
request_method=po_router.put,
path="/po/{analysis_id}/stop",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
)
async def stop_analysis(
request: Request,
Expand All @@ -101,7 +101,7 @@ async def stop_analysis(
request_method=po_router.delete,
path="/po/{analysis_id}/delete",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
service_url=hub_adapter_settings.PODORC_SERVICE_URL,
)
async def delete_analysis(
request: Request,
Expand Down
14 changes: 7 additions & 7 deletions gateway/routers/results.py → hub_adapter/routers/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route
from gateway.models.results import ResultsUploadResponse
from hub_adapter.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from hub_adapter.conf import hub_adapter_settings
from hub_adapter.core import route
from hub_adapter.models.results import ResultsUploadResponse

results_router = APIRouter(
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass), Security(httpbearer)],
Expand All @@ -22,7 +22,7 @@
request_method=results_router.get,
path="/scratch/{object_id}",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.RESULTS_SERVICE_URL,
service_url=hub_adapter_settings.RESULTS_SERVICE_URL,
response_model=None,
file_response=True,
)
Expand All @@ -38,7 +38,7 @@ async def read_from_scratch(
request_method=results_router.put,
path="/scratch",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.RESULTS_SERVICE_URL,
service_url=hub_adapter_settings.RESULTS_SERVICE_URL,
response_model=ResultsUploadResponse,
file_params=["file"],
)
Expand All @@ -54,7 +54,7 @@ async def upload_to_scratch(
request_method=results_router.put,
path="/upload",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.RESULTS_SERVICE_URL,
service_url=hub_adapter_settings.RESULTS_SERVICE_URL,
response_model=ResultsUploadResponse,
file_params=["file"],
)
Expand Down
Loading

0 comments on commit 8d6ecbe

Please sign in to comment.