Skip to content

Commit d924d0c

Browse files
committed
Merge branch 'auth-procedures' of github.com:DiamondLightSource/python-murfey into auth-procedures
2 parents b42fa4e + ff03dcb commit d924d0c

File tree

7 files changed

+47
-107
lines changed

7 files changed

+47
-107
lines changed

src/murfey/server/api/bootstrap.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ def _sanitise_str(input: str) -> str:
7070
return input_clean
7171

7272

73+
def resolve_netloc(request: Request) -> str:
74+
"""
75+
Helper function to construct the correct netloc (hostname[:port]) to use based on
76+
the request received. It will prioritise parsing the request headers for the host,
77+
port, protocol and using them to construct the netloc before defaulting to parsing
78+
the FastAPI Request object to do so.
79+
"""
80+
81+
# Prefer headers added by reverse proxies
82+
host = request.headers.get("X-Forwarded-Host", request.url.hostname)
83+
port = request.headers.get(
84+
"X-Forwarded-Port", str(request.url.port) if request.url.port else None
85+
)
86+
proto = request.headers.get("X-Forwarded-Proto", request.url.scheme)
87+
88+
# Default ports shouldn't be included; if no ports are found, return just the host
89+
if (
90+
(proto == "http" and port == "80")
91+
or (proto == "https" and port == "443")
92+
or not port
93+
):
94+
return host
95+
96+
return f"{host}:{port}"
97+
98+
7399
"""
74100
=======================================================================================
75101
VERSION-RELATED API ENDPOINTS
@@ -126,15 +152,10 @@ def get_bootstrap_instructions(request: Request):
126152
machine with no internet access.
127153
"""
128154

129-
# Constructs the netloc (hostname + port) and proxy path depending on if the
130-
# request was forwarded via proxy
131-
netloc = (
132-
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
133-
if request.headers.get("X-Forwarded-Host")
134-
and request.headers.get("X-Forwarded-Port")
135-
else request.url.netloc
136-
)
137-
# Additional bit in URL path after the netloc caused by the proxy reroute
155+
# Check if this is a forwarded request from somewhere else and construct netloc
156+
netloc = resolve_netloc(request)
157+
158+
# Find path to 'bootstrap' router using current URL path
138159
proxy_path = request.url.path.removesuffix(f"{bootstrap.prefix}/")
139160

140161
return respond_with_template(
@@ -362,12 +383,7 @@ def get_pacman_mirrors(request: Request):
362383
"""
363384

364385
# Check if this is a forwarded request from somewhere else and construct netloc
365-
netloc = (
366-
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
367-
if request.headers.get("X-Forwarded-Host")
368-
and request.headers.get("X-Forwarded-Port")
369-
else request.url.netloc
370-
)
386+
netloc = resolve_netloc(request)
371387

372388
# Find path to Rust router using current URL Path
373389
path_to_router = request.url.path.removesuffix("/config/pacman.d.zip")
@@ -641,12 +657,7 @@ def get_cargo_config(request: Request):
641657
"""
642658

643659
# Check if this is a forwarded request from somewhere else and construct netloc
644-
netloc = (
645-
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
646-
if request.headers.get("X-Forwarded-Host")
647-
and request.headers.get("X-Forwarded-Port")
648-
else request.url.netloc
649-
)
660+
netloc = resolve_netloc(request)
650661

651662
# Find path to Rust router using current URL Path
652663
path_to_router = request.url.path.removesuffix("/cargo/config.toml")
@@ -723,12 +734,7 @@ def get_index_config(request: Request):
723734
"""
724735

725736
# Check if this is a forwarded request from somewhere else and construct netloc
726-
netloc = (
727-
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
728-
if request.headers.get("X-Forwarded-Host")
729-
and request.headers.get("X-Forwarded-Port")
730-
else request.url.netloc
731-
)
737+
netloc = resolve_netloc(request)
732738

733739
# Find path to Rust router using current URL Path
734740
path_to_router = request.url.path.removesuffix("/index/config.json")

src/murfey/server/api/session_info.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
from typing import Dict, List, Optional
55

66
from fastapi import APIRouter, Depends, Request
7-
from fastapi.responses import FileResponse, HTMLResponse
7+
from fastapi.responses import FileResponse
88
from pydantic import BaseModel
99
from sqlmodel import select
1010
from werkzeug.utils import secure_filename
1111

12-
import murfey
1312
import murfey.server.api.websocket as ws
1413
from murfey.server import _transport_object
1514
from murfey.server.api import templates
@@ -32,12 +31,7 @@
3231
from murfey.server.ispyb import get_all_ongoing_visits
3332
from murfey.server.murfey_db import murfey_db
3433
from murfey.util import sanitise
35-
from murfey.util.config import (
36-
MachineConfig,
37-
get_hostname,
38-
get_machine_config,
39-
get_microscope,
40-
)
34+
from murfey.util.config import MachineConfig, get_machine_config
4135
from murfey.util.db import (
4236
ClientEnvironment,
4337
DataCollection,
@@ -64,20 +58,6 @@
6458
)
6559

6660

67-
# This will be the homepage for a given microscope.
68-
@router.get("/", response_class=HTMLResponse)
69-
async def root(request: Request):
70-
return templates.TemplateResponse(
71-
request=request,
72-
name="home.html",
73-
context={
74-
"hostname": get_hostname(),
75-
"microscope": get_microscope(),
76-
"version": murfey.__version__,
77-
},
78-
)
79-
80-
8161
@router.get("/health/")
8262
def health_check(db=ispyb_db):
8363
conn = db.connection()

src/murfey/templates/activevisits.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<html>
22
<head>
3-
<title>{{ microscope }}</title>
4-
<link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet" />
3+
<title>Visits {{ "({})".format(microscope) if microscope else "" }}</title>
4+
<link href="{{ proxy_path }}/static/styles.css" rel="stylesheet" />
55
<link
6-
href="{{ url_for('images', path='/icon_268.png') }}"
6+
href="{{ proxy_path }}/images/icon_268.png"
77
rel="icon"
88
type="image/png"
99
/>

src/murfey/templates/base.html

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
<html>
22
<head>
33
<title>Murfey - {% block title %}{% endblock %}</title>
4+
<link href="{{ proxy_path }}/static/styles.css" rel="stylesheet" />
45
<link
5-
href="{{ proxy_path }}{{ url_for('static', path='/styles.css') }}"
6-
rel="stylesheet"
7-
/>
8-
<link
9-
href="{{ proxy_path }}{{ url_for('images', path='/icon_268.png') }}"
6+
href="{{ proxy_path }}/images/icon_268.png"
107
rel="icon"
118
type="image/png"
129
/>
1310
</head>
1411
<body>
1512
<div class="topnav">
1613
<a href="{{ proxy_path }}/docs">Home</a>
17-
<a href="{{ proxy_path }}/bootstrap">Installation Instructions</a>
18-
<a href="{{ proxy_path }}/pypi/fastapi">FastAPI (PyPI)</a>
14+
<a href="{{ proxy_path }}/bootstrap/">Installation Instructions</a>
15+
<a href="{{ proxy_path }}/pypi/fastapi/">FastAPI (PyPI)</a>
1916
<p style="text-align: right; margin: 8px 10px 0 auto">
20-
<img
21-
src="{{ proxy_path }}{{ url_for('images', path='/diamond.png') }}"
22-
style="height: 25px"
23-
/>
17+
<img src="{{ proxy_path }}/images/diamond.png" style="height: 25px" />
2418
</p>
2519
</div>
2620

@@ -31,7 +25,8 @@
3125
For help please contact your local contact or the EM Data Analysis team.
3226
</p>
3327
<p style="font-size: 60%">
34-
Murfey v{{ version }} running on {{ hostname }} ({{ microscope }})
28+
Murfey v{{ version }} running on {{ hostname }} {{
29+
"({})".format(microscope) if microscope else "" }}
3530
</p>
3631
</div>
3732
</body>

src/murfey/templates/home.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/murfey/templates/visit.html

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/server/test_main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ def login(test_user):
3535
def test_read_main(mock_check, test_user):
3636
token = login(test_user)
3737
response = client.get(
38-
"/session_info/",
39-
headers={"Authorization": f"Bearer {token}"},
38+
"/session_info/connections",
39+
headers={"Authorization": f"Bearer {token}"}
4040
)
4141
assert mock_check.called_once()
4242
assert response.status_code == 200
43-
assert "<title>" in response.text.lower()
4443

4544

4645
def test_pypi_proxy():

0 commit comments

Comments
 (0)