Skip to content

Commit

Permalink
Fix: Solved CORS issues on PAYG creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nesitor authored and hoh committed Apr 16, 2024
1 parent 57695a1 commit 8bbd65b
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions src/aleph/vm/orchestrator/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from secrets import token_urlsafe
from typing import Callable

import aiohttp_cors
from aiohttp import web
from aiohttp_cors import ResourceOptions, setup

from aleph.vm.conf import settings
from aleph.vm.pool import VmPool
Expand Down Expand Up @@ -88,44 +88,62 @@ async def http_not_found(request: web.Request):


app = web.Application(middlewares=[server_version_middleware])
cors = aiohttp_cors.setup(app)

app.add_routes(
[
# /about APIs return information about the VM Orchestrator
web.get("/about/login", about_login),
web.get("/about/executions/list", list_executions),
web.get("/about/executions/details", about_executions),
web.get("/about/executions/records", about_execution_records),
web.get("/about/usage/system", about_system_usage),
web.get("/about/config", about_config),
# /control APIs are used to control the VMs and access their logs
web.post("/control/allocations", update_allocations),
web.post("/control/allocation/notify", notify_allocation),
web.get("/control/machine/{ref}/logs", stream_logs),
web.post("/control/machine/{ref}/expire", operate_expire),
web.post("/control/machine/{ref}/stop", operate_stop),
web.post("/control/machine/{ref}/erase", operate_erase),
web.post("/control/machine/{ref}/reboot", operate_reboot),
# /status APIs are used to check that the VM Orchestrator is running properly
web.get("/status/check/fastapi", status_check_fastapi),
web.get("/status/check/fastapi/legacy", status_check_fastapi_legacy),
web.get("/status/check/host", status_check_host),
web.get("/status/check/version", status_check_version),
web.get("/status/check/ipv6", status_check_ipv6),
web.get("/status/config", status_public_config),
# Raise an HTTP Error 404 if attempting to access an unknown URL within these paths.
web.get("/about/{suffix:.*}", http_not_found),
web.get("/control/{suffix:.*}", http_not_found),
web.get("/status/{suffix:.*}", http_not_found),
# /static is used to serve static files
web.static("/static", Path(__file__).parent / "views/static"),
# /vm is used to launch VMs on-demand
web.route("*", "/vm/{ref}{suffix:.*}", run_code_from_path),
web.route("*", "/{suffix:.*}", run_code_from_hostname),
]
cors = setup(
app,
defaults={
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
},
)

# Routes that need CORS enabled
cors_routes = [
# /about APIs return information about the VM Orchestrator
web.get("/about/login", about_login),
web.get("/about/executions/list", list_executions),
web.get("/about/executions/details", about_executions),
web.get("/about/executions/records", about_execution_records),
web.get("/about/usage/system", about_system_usage),
web.get("/about/config", about_config),
# /control APIs are used to control the VMs and access their logs
web.post("/control/allocation/notify", notify_allocation),
web.get("/control/machine/{ref}/logs", stream_logs),
web.post("/control/machine/{ref}/expire", operate_expire),
web.post("/control/machine/{ref}/stop", operate_stop),
web.post("/control/machine/{ref}/erase", operate_erase),
web.post("/control/machine/{ref}/reboot", operate_reboot),
# /status APIs are used to check that the VM Orchestrator is running properly
web.get("/status/check/fastapi", status_check_fastapi),
web.get("/status/check/fastapi/legacy", status_check_fastapi_legacy),
web.get("/status/check/host", status_check_host),
web.get("/status/check/version", status_check_version),
web.get("/status/check/ipv6", status_check_ipv6),
web.get("/status/config", status_public_config),
]
routes = app.add_routes(cors_routes)
for route in routes:
cors.add(route)


# Routes that don't need CORS enabled
other_routes = [
# /control APIs are used to control the VMs and access their logs
web.post("/control/allocations", update_allocations),
# Raise an HTTP Error 404 if attempting to access an unknown URL within these paths.
web.get("/about/{suffix:.*}", http_not_found),
web.get("/control/{suffix:.*}", http_not_found),
web.get("/status/{suffix:.*}", http_not_found),
# /static is used to serve static files
web.static("/static", Path(__file__).parent / "views/static"),
# /vm is used to launch VMs on-demand
web.route("*", "/vm/{ref}{suffix:.*}", run_code_from_path),
web.route("*", "/{suffix:.*}", run_code_from_hostname),
]
app.add_routes(other_routes)


async def stop_all_vms(app: web.Application):
pool: VmPool = app["vm_pool"]
Expand Down

0 comments on commit 8bbd65b

Please sign in to comment.