From 66953506c0a7cb004ba5560501ba5d45bb5c8851 Mon Sep 17 00:00:00 2001 From: Eu Pin Tien Date: Wed, 28 May 2025 17:14:10 +0100 Subject: [PATCH 1/5] Added function to construct netloc by parsing request headers before defaulting to parsing the Request object's properties --- src/murfey/server/api/bootstrap.py | 60 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/murfey/server/api/bootstrap.py b/src/murfey/server/api/bootstrap.py index 879bbfb11..a88d0c243 100644 --- a/src/murfey/server/api/bootstrap.py +++ b/src/murfey/server/api/bootstrap.py @@ -70,6 +70,32 @@ def _sanitise_str(input: str) -> str: return input_clean +def resolve_netloc(request: Request) -> str: + """ + Helper function to construct the correct netloc (hostname[:port]) to use based on + the request received. It will prioritise parsing the request headers for the host, + port, protocol and using them to construct the netloc before defaulting to parsing + the FastAPI Request object to do so. + """ + + # Prefer headers added by reverse proxies + host = request.headers.get("X-Forwarded-Host", request.url.hostname) + port = request.headers.get( + "X-Forwarded-Port", str(request.url.port) if request.url.port else None + ) + proto = request.headers.get("X-Forwarded-Proto", request.url.scheme) + + # Default ports shouldn't be included; if no ports are found, return just the host + if ( + (proto == "http" and port == "80") + or (proto == "https" and port == "443") + or not port + ): + return host + + return f"{host}:{port}" + + """ ======================================================================================= VERSION-RELATED API ENDPOINTS @@ -126,15 +152,10 @@ def get_bootstrap_instructions(request: Request): machine with no internet access. """ - # Constructs the netloc (hostname + port) and proxy path depending on if the - # request was forwarded via proxy - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) - # Additional bit in URL path after the netloc caused by the proxy reroute + # Check if this is a forwarded request from somewhere else and construct netloc + netloc = resolve_netloc(request) + + # Find path to 'bootstrap' router using current URL path proxy_path = request.url.path.removesuffix(f"{bootstrap.prefix}/") return respond_with_template( @@ -362,12 +383,7 @@ def get_pacman_mirrors(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/config/pacman.d.zip") @@ -641,12 +657,7 @@ def get_cargo_config(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/cargo/config.toml") @@ -723,12 +734,7 @@ def get_index_config(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/index/config.json") From 0b76e25a2aee5bfeac568daed339ff70ae62c8f2 Mon Sep 17 00:00:00 2001 From: Eu Pin Tien Date: Fri, 30 May 2025 15:39:59 +0100 Subject: [PATCH 2/5] Removed templates and endpoints that are no longer needed --- src/murfey/server/api/session_info.py | 24 ++------------------ src/murfey/templates/home.html | 8 ------- src/murfey/templates/visit.html | 32 --------------------------- 3 files changed, 2 insertions(+), 62 deletions(-) delete mode 100644 src/murfey/templates/home.html delete mode 100644 src/murfey/templates/visit.html diff --git a/src/murfey/server/api/session_info.py b/src/murfey/server/api/session_info.py index 50e38ba2b..a844cb822 100644 --- a/src/murfey/server/api/session_info.py +++ b/src/murfey/server/api/session_info.py @@ -4,13 +4,12 @@ from typing import Dict, List, Optional from fastapi import APIRouter, Depends, Request -from fastapi.responses import FileResponse, HTMLResponse +from fastapi.responses import FileResponse from pydantic import BaseModel from sqlalchemy import func from sqlmodel import select from werkzeug.utils import secure_filename -import murfey import murfey.server.api.websocket as ws from murfey.server import _transport_object from murfey.server.api import templates @@ -32,12 +31,7 @@ from murfey.server.ispyb import get_all_ongoing_visits from murfey.server.murfey_db import murfey_db from murfey.util import sanitise -from murfey.util.config import ( - MachineConfig, - get_hostname, - get_machine_config, - get_microscope, -) +from murfey.util.config import MachineConfig, get_machine_config from murfey.util.db import ( ClientEnvironment, DataCollection, @@ -64,20 +58,6 @@ ) -# This will be the homepage for a given microscope. -@router.get("/", response_class=HTMLResponse) -async def root(request: Request): - return templates.TemplateResponse( - request=request, - name="home.html", - context={ - "hostname": get_hostname(), - "microscope": get_microscope(), - "version": murfey.__version__, - }, - ) - - @router.get("/health/") def health_check(db=ispyb_db): conn = db.connection() diff --git a/src/murfey/templates/home.html b/src/murfey/templates/home.html deleted file mode 100644 index 47b9604a9..000000000 --- a/src/murfey/templates/home.html +++ /dev/null @@ -1,8 +0,0 @@ -{% extends "base.html" %} {% block title %}MURFEY{% endblock %} {% block content -%} -

{{ microscope }}

-

- A transporter for data from Diamond eBIC microscope and detector machines onto - the Diamond network. -

-{% endblock %} diff --git a/src/murfey/templates/visit.html b/src/murfey/templates/visit.html deleted file mode 100644 index 097279dc6..000000000 --- a/src/murfey/templates/visit.html +++ /dev/null @@ -1,32 +0,0 @@ - - - {{ visit[0]['Visit name'] }} - - - - - - - -
-

{{ visit[0]['Visit name'] }}

-
- -
-

Time remaining: {{ visit[0]['Time remaining'] }}

-

Start time: {{ visit[0]['Start date'] }}

-

End time: {{ visit[0]['End date'] }}

-
- - - - From 7e4ddfb31df9f357a6e2e0842fd4f9dbd649e89a Mon Sep 17 00:00:00 2001 From: Eu Pin Tien Date: Fri, 30 May 2025 15:41:39 +0100 Subject: [PATCH 3/5] Replaced 'url_for()' function with calculated 'proxy_path' value instead; allows correct URL generation when server is behind NGINX server and accessed via Ingress or LoadBalancer, and when server is standalone --- src/murfey/templates/activevisits.html | 6 +++--- src/murfey/templates/base.html | 19 +++++++------------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/murfey/templates/activevisits.html b/src/murfey/templates/activevisits.html index 279f67c10..77ec4d05c 100644 --- a/src/murfey/templates/activevisits.html +++ b/src/murfey/templates/activevisits.html @@ -1,9 +1,9 @@ - {{ microscope }} - + Visits {{ "({})".format(microscope) if microscope else "" }} + diff --git a/src/murfey/templates/base.html b/src/murfey/templates/base.html index ce6fa129e..598c9218d 100644 --- a/src/murfey/templates/base.html +++ b/src/murfey/templates/base.html @@ -1,12 +1,9 @@ Murfey - {% block title %}{% endblock %} + - @@ -14,13 +11,10 @@ @@ -31,7 +25,8 @@ For help please contact your local contact or the EM Data Analysis team.

- Murfey v{{ version }} running on {{ hostname }} ({{ microscope }}) + Murfey v{{ version }} running on {{ hostname }} {{ + "({})".format(microscope) if microscope else "" }}

From 9204baeff1c9704ba632a68d69e083c9bd16f3e6 Mon Sep 17 00:00:00 2001 From: Eu Pin Tien Date: Fri, 30 May 2025 15:52:51 +0100 Subject: [PATCH 4/5] Test a different 'session_info' endpoint after removing 'root()' --- tests/server/test_main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/server/test_main.py b/tests/server/test_main.py index 24aab534b..77613069f 100644 --- a/tests/server/test_main.py +++ b/tests/server/test_main.py @@ -30,10 +30,11 @@ def login(test_user): @patch("murfey.server.api.auth.check_user", return_value=True) def test_read_main(mock_check, test_user): token = login(test_user) - response = client.get("/session_info", headers={"Authorization": f"Bearer {token}"}) + response = client.get( + "/session_info/health", headers={"Authorization": f"Bearer {token}"} + ) assert mock_check.called_once() assert response.status_code == 200 - assert "" in response.text.lower() def test_pypi_proxy(): From 56b3a94acee0e60aba436d3ef853ce60a2c01063 Mon Sep 17 00:00:00 2001 From: Eu Pin Tien <eu-pin.tien@diamond.ac.uk> Date: Fri, 30 May 2025 15:59:29 +0100 Subject: [PATCH 5/5] Try testing '/session_info/connections' instead --- tests/server/test_main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/server/test_main.py b/tests/server/test_main.py index 77613069f..2018885d3 100644 --- a/tests/server/test_main.py +++ b/tests/server/test_main.py @@ -31,7 +31,7 @@ def login(test_user): def test_read_main(mock_check, test_user): token = login(test_user) response = client.get( - "/session_info/health", headers={"Authorization": f"Bearer {token}"} + "/session_info/connections", headers={"Authorization": f"Bearer {token}"} ) assert mock_check.called_once() assert response.status_code == 200