| author | |
| committer | |
| log | 177bee2d01caa9e04999d5633b55103cbceccc49 |
| tree | 3417decbf8c22cf49c35c74753d442f6fc8e78c2 |
| parent | 1ec773200ff768847f5e58ac0cc1b1e2f355668b |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Keep existing production routes until their service opts in, preserve preview metadata, and exclude generated bridge builds from releases.
Assisted-by: gpt-6.1-sol
Assisted-by: gpt-6-astra10 files changed, 575 insertions(+), 11 deletions(-)
config/Service.pkl+2| ... | ... | @@ -19,6 +19,8 @@ class Http { |
| 19 | 19 | forwardRealIp: Boolean = false |
| 20 | 20 | /// Require HTTPS when browsers send cookies issued by this route. |
| 21 | 21 | secureCookies: Boolean = false |
| 22 | /// Require the native Snow sign-in gateway before Jellyfin handles requests. | |
| 23 | jellyfinAuth: Boolean = false | |
| 22 | 24 | /// Generated secret whose value becomes the private proxy header name. |
| 23 | 25 | identityProofSecret: String? |
| 24 | 26 | /// Additional hosts routed to this container without SSO headers. |
tools/dashboard-stage-test.py+46| ... | ... | @@ -1,5 +1,7 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | from importlib import import_module |
| 3 | from contextlib import redirect_stdout | |
| 4 | import io | |
| 3 | 5 | import json |
| 4 | 6 | import os |
| 5 | 7 | from pathlib import Path |
| ... | ... | @@ -84,6 +86,50 @@ class PreviewBoundary(unittest.TestCase): |
| 84 | 86 | self.assertFalse(any(call.args[:2] == ("zfs", "destroy") for call in commands.call_args_list)) |
| 85 | 87 | |
| 86 | 88 | |
| 89 | class PreviewProof(unittest.TestCase): | |
| 90 | def test_generated_proof_is_private_mounted_and_retained_on_restage(self): | |
| 91 | with tempfile.TemporaryDirectory() as temporary: | |
| 92 | root = Path(temporary) | |
| 93 | state = root / "state" | |
| 94 | site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True} | |
| 95 | jobs = [] | |
| 96 | ||
| 97 | def command(*args, **kwargs): | |
| 98 | output = "" | |
| 99 | if args[0] == "pkl": | |
| 100 | output = json.dumps(site) | |
| 101 | elif args[:4] == ("nomad", "acl", "token", "create"): | |
| 102 | output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"}) | |
| 103 | elif args[:2] == ("podman", "run"): | |
| 104 | output = json.dumps({"username": "preview", "password": "disposable-fixture-password"}) | |
| 105 | elif args[:3] == ("nomad", "job", "run"): | |
| 106 | jobs.append(json.loads(kwargs["input"])) | |
| 107 | return subprocess.CompletedProcess(args, 0, stdout=output) | |
| 108 | ||
| 109 | def run(args, **kwargs): | |
| 110 | return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "") | |
| 111 | ||
| 112 | original_exists = Path.exists | |
| 113 | def exists(path): | |
| 114 | return str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" or original_exists(path) | |
| 115 | ||
| 116 | with patch.object(stage.snowglobe, "STATE", state), patch.object(stage.snowglobe, "REPO", root / "releases/1234567890abcdef"), patch.object(stage.snowglobe, "token"), patch.object(stage.snowglobe, "identity", return_value=os.getuid()), patch.object(stage.snowglobe, "check_staging_pool"), patch.object(stage.snowglobe, "check_http"), patch.object(stage.snowglobe, "command", side_effect=command), patch.object(stage.subprocess, "run", side_effect=run), patch.object(stage.os, "chown") as chown, patch.object(stage.snowglobe.transition, "host_environment", return_value="PATH=/bin"), patch.object(stage.snowglobe.transition, "stop_preview"), patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()): | |
| 117 | stage.stage() | |
| 118 | task = jobs[-1]["TaskGroups"][0]["Tasks"][0] | |
| 119 | directory = root / "srv/staging" / jobs[-1]["ID"] | |
| 120 | proof = directory / "proxy.token" | |
| 121 | value = proof.read_text() | |
| 122 | self.assertRegex(value.strip(), r"^[0-9a-f]{64}$") | |
| 123 | self.assertEqual(proof.stat().st_mode & 0o777, 0o400) | |
| 124 | chown.assert_any_call(proof, os.getuid(), os.getuid()) | |
| 125 | self.assertEqual(task["Env"]["SNOWGLOBE_PROXY_TOKEN_FILE"], "/run/secrets/proxy.token") | |
| 126 | self.assertIn(str(proof) + ":/run/secrets/proxy.token:ro", task["Config"]["volumes"]) | |
| 127 | self.assertNotIn(value.strip(), json.dumps(jobs[-1])) | |
| 128 | stage.stage() | |
| 129 | self.assertEqual(proof.read_text(), value) | |
| 130 | self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"]) | |
| 131 | ||
| 132 | ||
| 87 | 133 | class NativePreview(unittest.TestCase): |
| 88 | 134 | def test_empty_proof_requires_loopback_and_fresh_account_stays_private(self): |
| 89 | 135 | binary = Path(os.environ.get("DASHBOARD_BINARY", "dashboard/target/debug/home-dashboard")).resolve() |
tools/dashboard-stage.py+9-2| ... | ... | @@ -109,7 +109,7 @@ def stage(): |
| 109 | 109 | env = { |
| 110 | 110 | "SNOWGLOBE_DOMAIN": site["domain"], "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + hostname, |
| 111 | 111 | "SNOWGLOBE_FILE_ORIGIN": "https://files-" + hostname, "SNOWGLOBE_AUTH_RP_ID": hostname, |
| 112 | "SNOWGLOBE_AUTH_REQUIRED": "1", "SNOWGLOBE_PROXY_TOKEN_FILE": "", "SNOWGLOBE_DATA_DIR": "/data", | |
| 112 | "SNOWGLOBE_AUTH_REQUIRED": "1", "SNOWGLOBE_PROXY_TOKEN_FILE": "/run/secrets/proxy.token", "SNOWGLOBE_DATA_DIR": "/data", | |
| 113 | 113 | "SNOWGLOBE_PREVIEW": "1", "SNOWGLOBE_METRICS": "follow", |
| 114 | 114 | "SNOWGLOBE_LISTEN_ADDRESS": "127.0.0.1", "PORT": "${NOMAD_PORT_http}", |
| 115 | 115 | "SNOWGLOBE_NOMAD_TOKEN_FILE": "/run/secrets/nomad.token", "NOMAD_ADDR": "http://127.0.0.1:4646", |
| ... | ... | @@ -132,6 +132,11 @@ def stage(): |
| 132 | 132 | fixture = root / "files/preview.txt" |
| 133 | 133 | fixture.write_text("Disposable Snow Globe dashboard preview.\n") |
| 134 | 134 | fixture.chmod(0o644) |
| 135 | proof = root / "proxy.token" | |
| 136 | if not proof.exists(): | |
| 137 | snowglobe.write_private(proof, secrets.token_hex(32) + "\n") | |
| 138 | os.chown(proof, saved["uid"], saved["uid"]) | |
| 139 | proof.chmod(0o400) | |
| 135 | 140 | directory.mkdir(parents=True, exist_ok=True) |
| 136 | 141 | saved.update(release=snowglobe.REPO.name, ready=False) |
| 137 | 142 | snowglobe.write_private(directory / f"{identity}.json", json.dumps(saved)) |
| ... | ... | @@ -148,6 +153,7 @@ def stage(): |
| 148 | 153 | "--user=" + str(saved["uid"]), "--cap-drop=ALL", "--security-opt=no-new-privileges", |
| 149 | 154 | "--entrypoint=/bin/home-dashboard", |
| 150 | 155 | "--volume=" + str(root / "data") + ":/data:rw", |
| 156 | "--volume=" + str(proof) + ":/run/secrets/proxy.token:ro", | |
| 151 | 157 | *["--env=" + key + "=" + value for key, value in env.items()], image, "--preview-account", capture=True) |
| 152 | 158 | snowglobe.write_private(root / "access.json", imported.stdout) |
| 153 | 159 | runtime = "/run/snowglobe-preview/" + identity |
| ... | ... | @@ -172,7 +178,8 @@ def stage(): |
| 172 | 178 | "Tasks": [{"Name": "app", "Driver": "podman", "User": str(saved["uid"]) + ":" + str(saved["uid"]), "Env": env, |
| 173 | 179 | "Config": {"image": image, "network_mode": "host", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges"], |
| 174 | 180 | "volumes": [str(root / "data") + ":/data:rw", str(root / "files") + ":/srv:rw,rslave", runtime + ":/run/snowglobe-host:ro", |
| 175 | str(root / "nomad.token") + ":/run/secrets/nomad.token:ro"]}, | |
| 181 | str(root / "nomad.token") + ":/run/secrets/nomad.token:ro", | |
| 182 | str(proof) + ":/run/secrets/proxy.token:ro"]}, | |
| 176 | 183 | "Resources": {"CPU": 1000, "MemoryMB": 2048}}]}]} |
| 177 | 184 | snowglobe.command("nomad", "job", "run", "-json", "-", input=json.dumps(job)) |
| 178 | 185 | snowglobe.check_http(hostname, "/sign-in", site["tlsInternal"]) |
tools/deploy-test.py+6| ... | ... | @@ -40,6 +40,11 @@ class MainExport(unittest.TestCase): |
| 40 | 40 | subprocess.run(["jj", "new"], cwd=repo, check=True, capture_output=True) |
| 41 | 41 | (repo / "service/fixture").write_text("unfinished") |
| 42 | 42 | (repo / "service/new").write_text("new working file") |
| 43 | generated = repo / "service/jellyfin/bridge/tests/bin/Release/net10.0/fixture.dll" | |
| 44 | generated.parent.mkdir(parents=True) | |
| 45 | digest = release.tree_digest(repo) | |
| 46 | generated.write_bytes(b"compiled fixture") | |
| 47 | self.assertEqual(release.tree_digest(repo), digest) | |
| 43 | 48 | original = subprocess.run |
| 44 | 49 | |
| 45 | 50 | def transport(argv, **kwargs): |
| ... | ... | @@ -66,6 +71,7 @@ class MainExport(unittest.TestCase): |
| 66 | 71 | self.assertEqual(json.loads((main / ".snowglobe-release.json").read_text())["main"], {"commit": commit, "description": "Test main\n"}) |
| 67 | 72 | self.assertEqual((stage / "service/fixture").read_text(), "unfinished") |
| 68 | 73 | self.assertEqual((stage / "service/new").read_text(), "new working file") |
| 74 | self.assertFalse((stage / generated.relative_to(repo)).exists()) | |
| 69 | 75 | self.assertNotIn("main", json.loads((stage / ".snowglobe-release.json").read_text())) |
| 70 | 76 | for exported in (main, stage): |
| 71 | 77 | self.assertEqual((exported / "service/app/service.pkl").read_text(), "service definition") |
tools/deploy.py+2-3| ... | ... | @@ -12,7 +12,7 @@ import sys |
| 12 | 12 | import tempfile |
| 13 | 13 | import time |
| 14 | 14 | |
| 15 | from release import SOURCES, tree_digest | |
| 15 | from release import BUILD_DIRS, SOURCES, tree_digest | |
| 16 | 16 | |
| 17 | 17 | |
| 18 | 18 | REPO = Path(__file__).resolve().parent.parent |
| ... | ... | @@ -92,8 +92,7 @@ def upload(main=False): |
| 92 | 92 | subprocess.run( |
| 93 | 93 | ["rsync", "-a", "--exclude=.DS_Store", "--exclude=__pycache__", "--exclude=*.pyc", |
| 94 | 94 | "--exclude=.identities.lock", "--exclude=identities.pending", "--exclude=._*", |
| 95 | "--exclude=dashboard/node_modules", "--exclude=dashboard/dist", | |
| 96 | "--exclude=dashboard/.cache", "--exclude=dashboard/data", "--exclude=dashboard/target", | |
| 95 | *(f"--exclude={directory}" for directory in BUILD_DIRS), | |
| 97 | 96 | *(str(REPO / path) for path in SOURCES), |
| 98 | 97 | str(snapshot) + "/"], check=True, |
| 99 | 98 | ) |
tools/release.py+3-5| ... | ... | @@ -19,6 +19,8 @@ RELEASES = ROOT / "releases" |
| 19 | 19 | STATE = Path("/var/lib/snowglobe") |
| 20 | 20 | HISTORY = STATE / "deployments.json" |
| 21 | 21 | SOURCES = ("config", "service", "tools", "nixos", "dashboard", "guest", "flake.nix", "flake.lock", "readme.md", "AGENTS.md") |
| 22 | BUILD_DIRS = ("dashboard/node_modules", "dashboard/dist", "dashboard/.cache", "dashboard/data", "dashboard/target", "guest/build", | |
| 23 | "service/jellyfin/bridge/bin", "service/jellyfin/bridge/obj", "service/jellyfin/bridge/tests/bin", "service/jellyfin/bridge/tests/obj") | |
| 22 | 24 | RELEASE_ID = re.compile(r"[0-9a-f]{16}\Z") |
| 23 | 25 | STAGE_ID = re.compile(r"[a-z][a-z0-9-]*\Z") |
| 24 | 26 | |
| ... | ... | @@ -31,11 +33,7 @@ def files(root): |
| 31 | 33 | paths = source.rglob("*") if source.is_dir() else [source] |
| 32 | 34 | for path in sorted(paths): |
| 33 | 35 | relative = path.relative_to(root) |
| 34 | if relative.parts[:2] in ( | |
| 35 | ("dashboard", "node_modules"), ("dashboard", "dist"), | |
| 36 | ("dashboard", ".cache"), ("dashboard", "data"), ("dashboard", "target"), | |
| 37 | ("guest", "build"), | |
| 38 | ): | |
| 36 | if any(relative.is_relative_to(directory) for directory in BUILD_DIRS): | |
| 39 | 37 | continue |
| 40 | 38 | if any(part in {".DS_Store", "__pycache__", ".identities.lock", "identities.pending"} or part.startswith("._") or part.endswith(".pyc") for part in relative.parts): |
| 41 | 39 | continue |
tools/router-jellyfin-test.py created+265| ... | ... | @@ -0,0 +1,265 @@ |
| 1 | #!/usr/bin/env python3 | |
| 2 | import argparse | |
| 3 | from http.client import HTTPConnection | |
| 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | |
| 5 | import json | |
| 6 | import os | |
| 7 | import socket | |
| 8 | import subprocess | |
| 9 | import tempfile | |
| 10 | import threading | |
| 11 | import time | |
| 12 | from pathlib import Path | |
| 13 | import unittest | |
| 14 | import urllib.parse | |
| 15 | from unittest.mock import patch | |
| 16 | ||
| 17 | import router | |
| 18 | ||
| 19 | ||
| 20 | class Backend(BaseHTTPRequestHandler): | |
| 21 | requests = [] | |
| 22 | bodies = [] | |
| 23 | ||
| 24 | def do_GET(self): | |
| 25 | type(self).requests.append((self.command, self.path, dict(self.headers))) | |
| 26 | type(self).bodies.append(self.rfile.read(int(self.headers.get('Content-Length', '0')))) | |
| 27 | if self.headers.get('Upgrade', '').lower() == 'websocket': | |
| 28 | self.send_response(101) | |
| 29 | self.send_header('Connection', 'Upgrade') | |
| 30 | self.send_header('Upgrade', 'websocket') | |
| 31 | self.end_headers() | |
| 32 | return | |
| 33 | self.send_response(206 if self.headers.get('Range') else 200) | |
| 34 | self.send_header('Content-Type', 'application/octet-stream') | |
| 35 | self.send_header('ETag', '"fixture"') | |
| 36 | self.send_header('Content-Length', '5') | |
| 37 | if self.headers.get('Range'): | |
| 38 | self.send_header('Content-Range', 'bytes 0-4/10') | |
| 39 | self.end_headers() | |
| 40 | if self.command != 'HEAD': | |
| 41 | self.wfile.write(b'hello') | |
| 42 | ||
| 43 | do_HEAD = do_GET | |
| 44 | do_POST = do_GET | |
| 45 | ||
| 46 | def log_message(self, *_): | |
| 47 | pass | |
| 48 | ||
| 49 | ||
| 50 | class Gateway(BaseHTTPRequestHandler): | |
| 51 | requests = [] | |
| 52 | ||
| 53 | def do_GET(self): | |
| 54 | size = int(self.headers.get('Content-Length', '0')) | |
| 55 | body = self.rfile.read(size) | |
| 56 | type(self).requests.append((self.command, self.path, dict(self.headers), body)) | |
| 57 | trusted = self.headers.get('Snowglobe-Proxy-Token') == 'a' * 64 | |
| 58 | original = self.headers.get('Snowglobe-Jellyfin-Uri', '') | |
| 59 | if not trusted: | |
| 60 | self.send_error(403) | |
| 61 | elif self.path == '/auth/jellyfin/public': | |
| 62 | self.send_response(200) | |
| 63 | self.send_header('Content-Type', 'application/json') | |
| 64 | self.end_headers() | |
| 65 | self.wfile.write(json.dumps({'method': self.headers.get('Snowglobe-Jellyfin-Method'), 'body': body.decode()}).encode()) | |
| 66 | elif self.headers.get('Authorization') == 'allow' or original.startswith('/Items/' + '1' * 32 + '/Images/Primary'): | |
| 67 | self.send_response(204) | |
| 68 | path, _, query = original.partition('?') | |
| 69 | kept = [part for part in query.split('&') if part and urllib.parse.unquote_plus(part.split('=', 1)[0]).lower() not in {'apikey', 'api_key'}] | |
| 70 | self.send_header('X-Snowglobe-Jellyfin-Uri', path + ('?' + '&'.join(kept) if kept else '')) | |
| 71 | if self.headers.get('Authorization'): | |
| 72 | self.send_header('X-Snowglobe-Jellyfin-Authorization', 'MediaBrowser Token="normalized"') | |
| 73 | self.send_header('Set-Cookie', '__Host-snow-jellyfin=normalized; HttpOnly; Secure; Path=/') | |
| 74 | self.end_headers() | |
| 75 | else: | |
| 76 | self.send_error(401) | |
| 77 | ||
| 78 | do_POST = do_GET | |
| 79 | do_OPTIONS = do_GET | |
| 80 | ||
| 81 | def log_message(self, *_): | |
| 82 | pass | |
| 83 | ||
| 84 | ||
| 85 | class GatewayAssociation(unittest.TestCase): | |
| 86 | def test_only_tagged_preview_uses_its_own_gateway(self): | |
| 87 | with tempfile.TemporaryDirectory(prefix='jellyfin-gateway-') as temporary: | |
| 88 | root = Path(temporary) | |
| 89 | stages = root / 'stages' | |
| 90 | stages.mkdir() | |
| 91 | jellyfin = 'jellyfin-preview-1234abcd' | |
| 92 | dashboard = 'dashboard-preview-1234abcd' | |
| 93 | (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) | |
| 94 | mount = root / 'staging' / dashboard | |
| 95 | mount.mkdir(parents=True) | |
| 96 | proxy_proof = mount / 'proxy.token' | |
| 97 | proxy_proof.write_text('b' * 64) | |
| 98 | proxy_proof.chmod(0o400) | |
| 99 | (stages / (dashboard + '.json')).write_text(json.dumps({'sourceId': 'dashboard', 'mount': str(mount)})) | |
| 100 | proof = root / 'proof' | |
| 101 | proof.write_text('a' * 64) | |
| 102 | services = { | |
| 103 | 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), | |
| 104 | 'copyparty': (1112, ['caddy-host=file.paperclover.net']), | |
| 105 | jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), | |
| 106 | dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), | |
| 107 | } | |
| 108 | ||
| 109 | def nomad(path, _): | |
| 110 | if path == '/v1/services': | |
| 111 | return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in services]}] | |
| 112 | if path.startswith('/v1/service/'): | |
| 113 | name = path.rsplit('/', 1)[1] | |
| 114 | port, tags = services[name] | |
| 115 | return [{'ServiceName': name, 'JobID': name, 'AllocID': name, 'Address': '127.0.0.1', 'Port': port, 'Tags': tags}] | |
| 116 | return {'http': {'Status': 'success'}} | |
| 117 | ||
| 118 | environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '4444', | |
| 119 | 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'SNOWGLOBE_STATE': temporary} | |
| 120 | with patch.dict(os.environ, environment, clear=True), patch.object(router, 'nomad', nomad), patch.object(router, 'ROUTE_DIR', str(root / 'routes')): | |
| 121 | rendered = router.render('fixture') | |
| 122 | legacy = rendered.split('jelly.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 123 | preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 124 | self.assertNotIn('/auth/jellyfin/', legacy) | |
| 125 | self.assertIn('reverse_proxy 127.0.0.1:1111', legacy) | |
| 126 | self.assertIn('reverse_proxy 127.0.0.1:3333', preview) | |
| 127 | self.assertNotIn('reverse_proxy 127.0.0.1:4444', preview) | |
| 128 | self.assertNotIn('a' * 64, preview) | |
| 129 | self.assertIn('b' * 64, preview) | |
| 130 | dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 131 | self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) | |
| 132 | self.assertNotIn('tracing', preview) | |
| 133 | files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 134 | self.assertNotIn('@zig_mirror', files) | |
| 135 | services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) | |
| 136 | enabled = router.render('fixture') | |
| 137 | files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 138 | self.assertIn('reverse_proxy 127.0.0.1:5555', files) | |
| 139 | (stages / (jellyfin + '.json')).unlink() | |
| 140 | rendered = router.render('fixture') | |
| 141 | preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | |
| 142 | self.assertIn('respond 503', preview) | |
| 143 | self.assertNotIn('reverse_proxy', preview) | |
| 144 | ||
| 145 | ||
| 146 | class JellyfinRouter(unittest.TestCase): | |
| 147 | @classmethod | |
| 148 | def setUpClass(cls): | |
| 149 | cls.root = tempfile.TemporaryDirectory(prefix='snowglobe-jellyfin-router-') | |
| 150 | cls.backend = ThreadingHTTPServer(('127.0.0.1', 0), Backend) | |
| 151 | cls.gateway = ThreadingHTTPServer(('127.0.0.1', 0), Gateway) | |
| 152 | for server in (cls.backend, cls.gateway): | |
| 153 | threading.Thread(target=server.serve_forever, daemon=True).start() | |
| 154 | with socket.socket() as listener: | |
| 155 | listener.bind(('127.0.0.1', 0)) | |
| 156 | cls.port = listener.getsockname()[1] | |
| 157 | conf = Path(cls.root.name) / 'Caddyfile' | |
| 158 | route = '\n'.join(router.jellyfin_routes('jellyfin-preview-1234abcd', {f'127.0.0.1:{cls.backend.server_port}'}, f'127.0.0.1:{cls.gateway.server_port}', 'a' * 64)) | |
| 159 | conf.write_text('{\n admin off\n auto_https off\n}\nhttp://127.0.0.1:' + str(cls.port) + ' {\n' + route + '\n}\n') | |
| 160 | validated = subprocess.run([str(CADDY), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) | |
| 161 | if validated.returncode: | |
| 162 | raise RuntimeError(validated.stderr) | |
| 163 | ||
| 164 | cls.log = (Path(cls.root.name) / 'caddy.log').open('w+') | |
| 165 | cls.caddy = subprocess.Popen([str(CADDY), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=cls.log, stderr=cls.log) | |
| 166 | deadline = time.monotonic() + 10 | |
| 167 | while True: | |
| 168 | try: | |
| 169 | cls.request('GET', '/ready') | |
| 170 | break | |
| 171 | except OSError: | |
| 172 | if time.monotonic() > deadline or cls.caddy.poll() is not None: | |
| 173 | cls.log.seek(0) | |
| 174 | raise RuntimeError(cls.log.read()) | |
| 175 | time.sleep(.05) | |
| 176 | ||
| 177 | @classmethod | |
| 178 | def tearDownClass(cls): | |
| 179 | cls.caddy.terminate() | |
| 180 | cls.caddy.wait(timeout=10) | |
| 181 | cls.backend.shutdown() | |
| 182 | cls.gateway.shutdown() | |
| 183 | cls.backend.server_close() | |
| 184 | cls.gateway.server_close() | |
| 185 | cls.log.close() | |
| 186 | cls.root.cleanup() | |
| 187 | ||
| 188 | @classmethod | |
| 189 | def request(cls, method, path, body=None, headers=None): | |
| 190 | connection = HTTPConnection('127.0.0.1', cls.port, timeout=3) | |
| 191 | connection.request(method, path, body=body, headers=headers or {}) | |
| 192 | response = connection.getresponse() | |
| 193 | result = response.status, dict(response.getheaders()), response.read() | |
| 194 | connection.close() | |
| 195 | return result | |
| 196 | ||
| 197 | def setUp(self): | |
| 198 | Backend.requests.clear() | |
| 199 | Gateway.requests.clear() | |
| 200 | ||
| 201 | def test_default_deny_includes_theme_assets_and_fallback(self): | |
| 202 | for path in ['/web/', '/web/extra/cloverfin.css', '/favicon.ico', '/Items/known', '/Videos/x/stream', '/socket']: | |
| 203 | with self.subTest(path=path): | |
| 204 | self.assertEqual(self.request('GET', path, headers={'Snowglobe-Proxy-Token': 'forged', 'Snowglobe-Jellyfin-Service': 'jellyfin'})[0], 401) | |
| 205 | self.assertEqual(Backend.requests, []) | |
| 206 | self.assertTrue(all(r[2]['Snowglobe-Jellyfin-Service'] == 'jellyfin-preview-1234abcd' for r in Gateway.requests)) | |
| 207 | count = len(Gateway.requests) | |
| 208 | for path in ['/Snowglobe/Bridge', '/sNoWgLoBe/Bridge', '/metrics', '/Users/AuthenticateByName', '/Users/' + '1' * 32 + '/Authenticate', '/Users/ForgotPassword/Pin', '/QuickConnect/Authorize']: | |
| 209 | self.assertEqual(self.request('POST', path, body='{}', headers={'Authorization': 'allow'})[0], 404) | |
| 210 | self.assertEqual(len(Gateway.requests), count) | |
| 211 | self.assertEqual(Backend.requests, []) | |
| 212 | ||
| 213 | def test_public_protocol_preserves_method_body_and_uri(self): | |
| 214 | body = '{"DeviceId":"fixture"}' | |
| 215 | status, _, payload = self.request('POST', '/QuickConnect/Initiate?fixture=1', body=body) | |
| 216 | self.assertEqual(status, 200) | |
| 217 | self.assertEqual(json.loads(payload), {'method': 'POST', 'body': body}) | |
| 218 | self.assertEqual(Gateway.requests[-1][2]['Snowglobe-Jellyfin-Uri'], '/QuickConnect/Initiate?fixture=1') | |
| 219 | self.assertEqual(Backend.requests, []) | |
| 220 | self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) | |
| 221 | ||
| 222 | def test_authenticated_ranges_head_websocket_and_normalized_credential(self): | |
| 223 | headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} | |
| 224 | status, response_headers, body = self.request('GET', '/Videos/fixture/stream?ApiKey=old&api_key=old&aPiKeY=old&static=true', headers={**headers, 'Range': 'bytes=0-4'}) | |
| 225 | self.assertEqual((status, body), (206, b'hello')) | |
| 226 | self.assertEqual(response_headers['Content-Range'], 'bytes 0-4/10') | |
| 227 | self.assertEqual({k.lower(): v for k, v in response_headers.items()}['etag'], '"fixture"') | |
| 228 | self.assertIn('HttpOnly', response_headers['Set-Cookie']) | |
| 229 | method, path, upstream = Backend.requests[-1] | |
| 230 | self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) | |
| 231 | self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') | |
| 232 | self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) | |
| 233 | status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) | |
| 234 | self.assertEqual((status, body), (200, b'')) | |
| 235 | self.assertEqual(Backend.requests[-1][0], 'HEAD') | |
| 236 | self.assertEqual(self.request('POST', '/Items/fixture/PlaybackInfo', body='synthetic playback options', headers=headers)[0], 200) | |
| 237 | self.assertEqual(Backend.requests[-1][0], 'POST') | |
| 238 | self.assertEqual(Backend.bodies[-1], b'synthetic playback options') | |
| 239 | self.assertEqual(self.request('GET', '/socket', headers={**headers, 'Connection': 'Upgrade', 'Upgrade': 'websocket'})[0], 101) | |
| 240 | self.assertEqual(Backend.requests[-1][2]['Upgrade'], 'websocket') | |
| 241 | ||
| 242 | def test_narrow_artwork_still_passes_gateway_check(self): | |
| 243 | self.assertEqual(self.request('GET', '/Items/' + '1' * 32 + '/Images/Primary?maxWidth=300')[0], 200) | |
| 244 | self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/check') | |
| 245 | self.assertEqual(len(Backend.requests), 1) | |
| 246 | ||
| 247 | def test_z_upstream_failure_does_not_log_native_credentials(self): | |
| 248 | self.gateway.shutdown() | |
| 249 | self.gateway.server_close() | |
| 250 | secret = 'SYNTHETIC-PAIRING-SECRET' | |
| 251 | self.assertEqual(self.request('GET', '/QuickConnect/Connect?secret=' + secret, | |
| 252 | headers={'X-Emby-Token': 'SYNTHETIC-NATIVE-TOKEN'})[0], 503) | |
| 253 | self.log.flush() | |
| 254 | self.log.seek(0) | |
| 255 | log = self.log.read() | |
| 256 | self.assertNotIn(secret, log) | |
| 257 | self.assertNotIn('SYNTHETIC-NATIVE-TOKEN', log) | |
| 258 | ||
| 259 | ||
| 260 | if __name__ == '__main__': | |
| 261 | parser = argparse.ArgumentParser() | |
| 262 | parser.add_argument('--caddy', type=Path, required=True) | |
| 263 | args, remaining = parser.parse_known_args() | |
| 264 | CADDY = args.caddy | |
| 265 | unittest.main(argv=[__file__, *remaining]) |
tools/router-zig-test.py created+100| ... | ... | @@ -0,0 +1,100 @@ |
| 1 | #!/usr/bin/env python3 | |
| 2 | import argparse | |
| 3 | from http.client import HTTPConnection | |
| 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | |
| 5 | import json | |
| 6 | import os | |
| 7 | from pathlib import Path | |
| 8 | import socket | |
| 9 | import subprocess | |
| 10 | import tempfile | |
| 11 | import threading | |
| 12 | import time | |
| 13 | from unittest.mock import patch | |
| 14 | ||
| 15 | import router | |
| 16 | ||
| 17 | ||
| 18 | class Mirror(BaseHTTPRequestHandler): | |
| 19 | requests = [] | |
| 20 | ||
| 21 | def do_GET(self): | |
| 22 | type(self).requests.append((self.command, self.path, dict(self.headers))) | |
| 23 | self.send_response(206 if self.headers.get('Range') else 200) | |
| 24 | self.send_header('Content-Length', '5') | |
| 25 | self.end_headers() | |
| 26 | if self.command != 'HEAD': | |
| 27 | self.wfile.write(b'hello') | |
| 28 | ||
| 29 | do_HEAD = do_GET | |
| 30 | ||
| 31 | def log_message(self, *_): | |
| 32 | pass | |
| 33 | ||
| 34 | ||
| 35 | def main(): | |
| 36 | parser = argparse.ArgumentParser() | |
| 37 | parser.add_argument('--caddy', type=Path, required=True) | |
| 38 | args = parser.parse_args() | |
| 39 | with tempfile.TemporaryDirectory(prefix='snowglobe-zig-route-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Mirror) as backend: | |
| 40 | root = Path(temporary) | |
| 41 | stages = root / 'stages' | |
| 42 | stages.mkdir() | |
| 43 | paired = 'copyparty-preview-1234abcd' | |
| 44 | mirror = 'zig-mirror-preview-1234abcd' | |
| 45 | (stages / (paired + '.json')).write_text(json.dumps({'sourceId': 'copyparty', 'mirrorStage': mirror})) | |
| 46 | (stages / (mirror + '.json')).write_text(json.dumps({'sourceId': 'zig-mirror'})) | |
| 47 | routes = {(80, 'mirror.localhost'): {'services': {'zig-mirror'}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}, | |
| 48 | (80, 'preview.localhost'): {'services': {mirror}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}} | |
| 49 | with socket.socket() as listener: | |
| 50 | listener.bind(('127.0.0.1', 0)) | |
| 51 | port = listener.getsockname()[1] | |
| 52 | lines = ['{', ' admin off', ' auto_https off', '}'] | |
| 53 | with patch.dict(os.environ, {'SNOWGLOBE_STATE': temporary}): | |
| 54 | for host, service in [('prod.localhost', 'copyparty'), ('paired.localhost', paired), ('unpaired.localhost', 'copyparty-preview-ffffffff')]: | |
| 55 | lines += [f'http://{host}:{port} {{', *router.zig_mirror_routes(service, routes), ' handle {', ' respond 204', ' }', '}'] | |
| 56 | conf = root / 'Caddyfile' | |
| 57 | conf.write_text('\n'.join(lines)) | |
| 58 | validated = subprocess.run([str(args.caddy), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) | |
| 59 | assert validated.returncode == 0, validated.stderr | |
| 60 | threading.Thread(target=backend.serve_forever, daemon=True).start() | |
| 61 | with (root / 'caddy.log').open('w') as log: | |
| 62 | caddy = subprocess.Popen([str(args.caddy), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=log, stderr=log) | |
| 63 | try: | |
| 64 | def request(host, path, method='GET', **headers): | |
| 65 | connection = HTTPConnection('127.0.0.1', port, timeout=3) | |
| 66 | connection.request(method, path, headers={'Host': host + ':' + str(port), **headers}) | |
| 67 | response = connection.getresponse() | |
| 68 | result = response.status, response.read() | |
| 69 | connection.close() | |
| 70 | return result | |
| 71 | deadline = time.monotonic() + 10 | |
| 72 | while True: | |
| 73 | try: | |
| 74 | request('prod.localhost', '/') | |
| 75 | break | |
| 76 | except OSError: | |
| 77 | assert time.monotonic() < deadline and caddy.poll() is None | |
| 78 | time.sleep(.05) | |
| 79 | archive = '/mirrors/zig/zig-x86_64-linux-0.16.0-dev.123+abc123.tar.xz' | |
| 80 | for host in ['prod.localhost', 'paired.localhost']: | |
| 81 | assert request(host, archive + '?download=1', Range='bytes=0-4', Cookie='secret=do-not-forward', Authorization='private') == (206, b'hello') | |
| 82 | method, path, headers = Mirror.requests[-1] | |
| 83 | assert path == archive + '?download=1' | |
| 84 | assert 'Cookie' not in headers and 'Authorization' not in headers | |
| 85 | assert request(host, archive, method='HEAD') == (200, b'') | |
| 86 | for path in ['/mirrors/zig/', '/mirrors/zig/releases/', '/mirrors/zig/dev/', '/mirrors/zig/releases/zig-0.0.0.tar.xz']: | |
| 87 | assert request(host, path) == (204, b'') | |
| 88 | assert request(host, archive, method='POST') == (204, b'') | |
| 89 | count = len(Mirror.requests) | |
| 90 | assert request('unpaired.localhost', archive) == (503, b'') | |
| 91 | assert len(Mirror.requests) == count | |
| 92 | finally: | |
| 93 | caddy.terminate() | |
| 94 | caddy.wait(timeout=10) | |
| 95 | backend.shutdown() | |
| 96 | print('Zig archive routes: production, explicitly paired preview, absent pairing, methods, ranges, query, and nested browse passed') | |
| 97 | ||
| 98 | ||
| 99 | if __name__ == '__main__': | |
| 100 | main() |
tools/router.py+139-1| ... | ... | @@ -99,6 +99,127 @@ def shale_git_routes(upstreams): |
| 99 | 99 | return [*lines, " }", " }"] |
| 100 | 100 | |
| 101 | 101 | |
| 102 | def zig_mirror_routes(service, routes): | |
| 103 | if service == "copyparty": | |
| 104 | mirror = "zig-mirror" | |
| 105 | else: | |
| 106 | state = os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe") | |
| 107 | stages = os.path.join(state, "stages") | |
| 108 | try: | |
| 109 | with open(os.path.join(stages, service + ".json")) as source: | |
| 110 | stage = json.load(source) | |
| 111 | mirror = stage.get("mirrorStage", "") | |
| 112 | if stage.get("sourceId") != "copyparty" or not isinstance(mirror, str) or not re.fullmatch(r"zig-mirror-preview-[0-9a-f]{8}", mirror): | |
| 113 | mirror = None | |
| 114 | if mirror: | |
| 115 | with open(os.path.join(stages, mirror + ".json")) as source: | |
| 116 | if json.load(source).get("sourceId") != "zig-mirror": | |
| 117 | mirror = None | |
| 118 | except FileNotFoundError: | |
| 119 | mirror = None | |
| 120 | upstreams = set().union(*(route["upstreams"] for route in routes.values() if mirror in route["services"])) | |
| 121 | pattern = r"^/mirrors/zig/zig(?:|-bootstrap|-[a-zA-Z0-9_]+-[a-zA-Z0-9_]+)-([0-9]+\.[0-9]+\.[0-9]+(?:-dev\.[0-9]+\+[0-9a-f]+)?)\.(?:tar\.xz|zip)(?:\.minisig)?$" | |
| 122 | lines = [" @zig_mirror {", " method GET HEAD", f" path_regexp zig_mirror {pattern}", " }", | |
| 123 | " handle @zig_mirror {"] | |
| 124 | if upstreams: | |
| 125 | if any(not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream) for upstream in upstreams): | |
| 126 | raise ValueError("Zig mirror requires private loopback upstreams") | |
| 127 | lines += [" request_header -Authorization", " request_header -Cookie", | |
| 128 | *proxy(" ".join(sorted(upstreams)), " ")] | |
| 129 | else: | |
| 130 | lines.append(" respond 503") | |
| 131 | return [*lines, " }"] | |
| 132 | ||
| 133 | ||
| 134 | def dashboard_preview_proof(service): | |
| 135 | if not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", service): | |
| 136 | return None | |
| 137 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") | |
| 138 | try: | |
| 139 | with open(os.path.join(stages, service + ".json")) as source: | |
| 140 | stage = json.load(source) | |
| 141 | mount = stage.get("mount", "") | |
| 142 | if stage.get("sourceId") != "dashboard" or not os.path.isabs(mount) or os.path.basename(mount) != service or os.path.basename(os.path.dirname(mount)) != "staging": | |
| 143 | return None | |
| 144 | path = os.path.join(mount, "proxy.token") | |
| 145 | if os.path.islink(path) or os.stat(path).st_mode & 0o077: | |
| 146 | return None | |
| 147 | with open(path) as source: | |
| 148 | proof = source.read().strip() | |
| 149 | except FileNotFoundError: | |
| 150 | return None | |
| 151 | if not re.fullmatch(r"[0-9a-f]{64}", proof): | |
| 152 | raise ValueError("invalid dashboard preview proxy proof") | |
| 153 | return proof | |
| 154 | ||
| 155 | ||
| 156 | def jellyfin_gateway(service, routes, proof): | |
| 157 | if service == "jellyfin": | |
| 158 | port = int(os.environ["SNOWGLOBE_DASHBOARD_PORT"]) | |
| 159 | if not 1 <= port <= 65535: | |
| 160 | raise ValueError("invalid dashboard port for Jellyfin") | |
| 161 | return f"127.0.0.1:{port}", proof | |
| 162 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") | |
| 163 | try: | |
| 164 | with open(os.path.join(stages, service + ".json")) as source: | |
| 165 | stage = json.load(source) | |
| 166 | gateway = stage.get("gatewayStage", "") | |
| 167 | if stage.get("sourceId") != "jellyfin" or not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway): | |
| 168 | return None, None | |
| 169 | with open(os.path.join(stages, gateway + ".json")) as source: | |
| 170 | if json.load(source).get("sourceId") != "dashboard": | |
| 171 | return None, None | |
| 172 | except FileNotFoundError: | |
| 173 | return None, None | |
| 174 | upstreams = set().union(*(route["upstreams"] for route in routes.values() if gateway in route["services"])) | |
| 175 | if len(upstreams) != 1: | |
| 176 | return None, None | |
| 177 | upstream = next(iter(upstreams)) | |
| 178 | if not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream): | |
| 179 | raise ValueError("Jellyfin preview gateway must bind loopback") | |
| 180 | proof = dashboard_preview_proof(gateway) | |
| 181 | return (upstream, proof) if proof else (None, None) | |
| 182 | ||
| 183 | ||
| 184 | def jellyfin_routes(service, upstreams, gateway, proof): | |
| 185 | if len(upstreams) != 1 or not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", next(iter(upstreams))): | |
| 186 | raise ValueError("Jellyfin requires one private loopback upstream") | |
| 187 | upstream = next(iter(upstreams)) | |
| 188 | trusted = [f" header_up Snowglobe-Proxy-Token {proof}" if proof else " header_up -Snowglobe-Proxy-Token", | |
| 189 | f" header_up Snowglobe-Jellyfin-Service {service}", | |
| 190 | f" header_up Snowglobe-Jellyfin-Upstream {upstream}", | |
| 191 | " header_up Snowglobe-Jellyfin-Uri {http.vars.jellyfin_uri}", | |
| 192 | " header_up Snowglobe-Jellyfin-Method {http.vars.jellyfin_method}", | |
| 193 | " header_up X-Snowglobe-Client-IP {remote_host}"] | |
| 194 | lines = [" route {", " vars jellyfin_uri {uri}", " vars jellyfin_method {method}", | |
| 195 | " request_header -Snowglobe-Jellyfin-*", " request_header -Snowglobe-Proxy-Token", | |
| 196 | " request_header -X-Snowglobe-Jellyfin-*", " request_header -User-Name", " request_header -User-Groups", | |
| 197 | " @jellyfin_private path_regexp jellyfin_private (?i)^/(?:snowglobe(?:/|$)|metrics(?:/|$)|users/(?:authenticatebyname|[0-9a-f-]+/authenticate|forgotpassword(?:/pin)?)(?:/|$)|quickconnect/authorize(?:/|$))", | |
| 198 | " respond @jellyfin_private 404"] | |
| 199 | paths = { | |
| 200 | "GET": "/System/Info/Public /System/Ping /Users/Public /Branding/Configuration /QuickConnect/Enabled /QuickConnect/Connect /auth/jellyfin/start /auth/jellyfin/callback", | |
| 201 | "POST": "/System/Ping /QuickConnect/Initiate /Users/AuthenticateWithQuickConnect /auth/jellyfin/session /auth/jellyfin/logout", | |
| 202 | } | |
| 203 | for method, allowed in paths.items(): | |
| 204 | name = "@jellyfin_public_" + method.lower() | |
| 205 | lines += [f" {name} {{", f" method {method}", f" path {allowed}", " }", | |
| 206 | f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", | |
| 207 | *[" " + line for line in trusted], " }", " }"] | |
| 208 | lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", | |
| 209 | f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", | |
| 210 | *[" " + line for line in trusted], " }", " }", " handle {", | |
| 211 | f" reverse_proxy {gateway} {{", " method GET", " rewrite /auth/jellyfin/check?", | |
| 212 | *[" " + line for line in trusted], " @jellyfin_allowed status 204", " handle_response @jellyfin_allowed {", | |
| 213 | " header +Set-Cookie {rp.header.Set-Cookie}", | |
| 214 | " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", | |
| 215 | " request_header -X-Emby-Authorization", " request_header -Cookie", | |
| 216 | " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", | |
| 217 | " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}", f" reverse_proxy {upstream} {{", | |
| 218 | " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", | |
| 219 | " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] | |
| 220 | return lines | |
| 221 | ||
| 222 | ||
| 102 | 223 | def render(token): |
| 103 | 224 | with open(os.environ["SNOWGLOBE_PROXY_TOKEN_FILE"]) as file: |
| 104 | 225 | dashboard_proof = file.read().strip() |
| ... | ... | @@ -150,7 +271,11 @@ def render(token): |
| 150 | 271 | host = tag.split("=", 1)[1] |
| 151 | 272 | if not HOST.fullmatch(host): |
| 152 | 273 | raise ValueError(f"invalid Caddy host: {host!r}") |
| 153 | route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False}) | |
| 274 | route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False, "jellyfinAuth": False}) | |
| 275 | if "caddy-jellyfin-auth=true" in tags: | |
| 276 | if not (instance["ServiceName"] == "jellyfin" or re.fullmatch(r"jellyfin-preview-[0-9a-f]{8}", instance["ServiceName"])): | |
| 277 | raise ValueError("Jellyfin gateway tag belongs to another service") | |
| 278 | route["jellyfinAuth"] = True | |
| 154 | 279 | if access and route["authRole"] not in (None, access[0]): |
| 155 | 280 | raise ValueError("conflicting route access roles") |
| 156 | 281 | if user_headers and route["userHeader"] not in (None, user_headers[0]): |
| ... | ... | @@ -214,8 +339,18 @@ def render(token): |
| 214 | 339 | if not 1 <= port <= 65535: |
| 215 | 340 | raise ValueError("invalid dashboard port for Shale linking") |
| 216 | 341 | lines += shale_mcp_routes(port) |
| 342 | if route["jellyfinAuth"]: | |
| 343 | gateway, proof = jellyfin_gateway(service, routes, dashboard_proof) | |
| 344 | if gateway: | |
| 345 | lines += jellyfin_routes(service, route["upstreams"], gateway, proof) | |
| 346 | else: | |
| 347 | lines.append(" respond 503") | |
| 348 | lines.append("}") | |
| 349 | continue | |
| 217 | 350 | lines += [" tracing {", f" span {service}", " span_attributes {", |
| 218 | 351 | f" snowglobe.service {service}", " snowglobe.kind edge", " }", " }"] |
| 352 | if (service == "copyparty" and "zig-mirror" in internal_services) or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service): | |
| 353 | lines += zig_mirror_routes(service, routes) | |
| 219 | 354 | manifest = os.path.join(ROUTE_DIR, service + ".json") |
| 220 | 355 | assets = {} |
| 221 | 356 | headers = {} |
| ... | ... | @@ -304,6 +439,9 @@ def render(token): |
| 304 | 439 | f" request_header -{scrub[0]}", " }", " }", |
| 305 | 440 | *proxy(upstreams, " "), " }", "}"] |
| 306 | 441 | else: |
| 442 | preview_proof = dashboard_preview_proof(service) | |
| 443 | if preview_proof: | |
| 444 | lines.append(f" request_header Snowglobe-Proxy-Token {preview_proof}") | |
| 307 | 445 | page_upstream = upstreams |
| 308 | 446 | shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) |
| 309 | 447 | page_headers = [] |
tools/snowglobe.py+3| ... | ... | @@ -348,6 +348,8 @@ def render(data, definitions): |
| 348 | 348 | tags.append("caddy-user-header=" + endpoint["userHeader"]) |
| 349 | 349 | if endpoint["forwardRealIp"]: |
| 350 | 350 | tags.append("caddy-real-ip=true") |
| 351 | if endpoint.get("jellyfinAuth"): | |
| 352 | tags.append("caddy-jellyfin-auth=true") | |
| 351 | 353 | if endpoint.get("secureCookies"): |
| 352 | 354 | tags.append("caddy-secure-cookies=true") |
| 353 | 355 | if endpoint["tlsInternal"]: |
| ... | ... | @@ -1225,6 +1227,7 @@ def main(): |
| 1225 | 1227 | check_http(hostname, http[0]["checkPath"], http[0]["tlsInternal"]) |
| 1226 | 1228 | configure(data) |
| 1227 | 1229 | if REPO.parent == Path("/opt/snowglobe/releases").resolve(): |
| 1230 | metadata = {**json.loads((stage_dir / f"{stage}.json").read_text()), **metadata} | |
| 1228 | 1231 | metadata["ready"] = True |
| 1229 | 1232 | metadata["overrides"] = [assignment.partition("=")[0] for assignment in args.env] |
| 1230 | 1233 | write_private(stage_dir / f"{stage}.json", json.dumps(metadata)) |