diff --git a/config/Service.pkl b/config/Service.pkl index 170580f8e5f3999353a6c4a64cf4790b74766fc1..6ad587ab962aae5286a468a0dca91f5ffe43a75f 100644 --- a/config/Service.pkl +++ b/config/Service.pkl @@ -84,6 +84,7 @@ class Container { args: Listing = new {} capAdd: Listing = new {} readOnlyRoot: Boolean = false + killTimeout: String? devices: Listing = new {} extraHosts: Listing = new {} hostNetwork: Boolean = false diff --git a/service/cliproxy/Dockerfile b/service/cliproxy/Dockerfile deleted file mode 100644 index 59e5b21ffd7f7fc173f80fb279d642dfce011c25..0000000000000000000000000000000000000000 --- a/service/cliproxy/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM docker.io/eceasy/cli-proxy-api@sha256:c08d02fbd94927708721afd059bff9ff36a04d9cedb4d69f2c2671aba8239031 AS proxy -FROM docker.io/library/nginx@sha256:17ae566734b63632e543c907ba74757e0c1a25d812ab9f10a07a6bed98dd199c -COPY --from=proxy /CLIProxyAPI/CLIProxyAPI /CLIProxyAPI/CLIProxyAPI diff --git a/service/cliproxy/build-source.json b/service/cliproxy/build-source.json new file mode 100644 index 0000000000000000000000000000000000000000..d4b71993ed4ec21b7fa7da19f0581eeb13ee7891 --- /dev/null +++ b/service/cliproxy/build-source.json @@ -0,0 +1,30 @@ +{ + "sources": [ + { + "source": "../cliproxyapi", + "revision": "91b6ce009bc93dbefd319e7a5b84812f333363ba", + "dockerfile": "Dockerfile.snowglobe", + "include": [ + "go.mod", + "go.sum", + "cmd", + "internal", + "sdk" + ] + }, + { + "source": "../cliproxy-panel", + "revision": "c355dc7b07797c42e8cb7e191e3b176b5442da37", + "destination": "management", + "include": [ + "package.json", + "bun.lock", + "index.html", + "tsconfig.json", + "tsconfig.node.json", + "vite.config.ts", + "src" + ] + } + ] +} diff --git a/service/cliproxy/nginx.conf b/service/cliproxy/nginx.conf deleted file mode 100644 index 630178a8748f3e625ad4bd6f3a1a22ec50c21bd9..0000000000000000000000000000000000000000 --- a/service/cliproxy/nginx.conf +++ /dev/null @@ -1,113 +0,0 @@ -worker_processes 1; -pid /tmp/nginx.pid; -error_log /dev/stderr crit; -events { worker_connections 512; } -http { - access_log off; - server_tokens off; - absolute_redirect off; - map_hash_bucket_size 128; - server_names_hash_bucket_size 128; - client_body_temp_path /tmp/client; - proxy_temp_path /tmp/proxy; - fastcgi_temp_path /tmp/fastcgi; - uwsgi_temp_path /tmp/uwsgi; - scgi_temp_path /tmp/scgi; - map $http_upgrade $connection_upgrade { default upgrade; '' close; } - map "$request_method:$uri" $client_route { - default 0; - ~^GET:/v1/models(?:/[^/]+)?$ 1; - ~^POST:/v1/(?:chat/completions|completions|messages(?:/count_tokens)?|responses(?:/compact)?|alpha/search)$ 1; - "GET:/v1/responses" 1; - ~^POST:/backend-api/codex/(?:responses(?:/compact)?|alpha/search)$ 1; - "GET:/backend-api/codex/responses" 1; - } - map "$request_method:$uri" $browser_oauth_callback { - default 0; - ~^(?:GET|HEAD):/(?:v0/management/oauth-callback|v8/management/oauth/callback|(?:anthropic|codex|antigravity|devin)/callback|callback)/?$ 1; - } - map "$request_method:$http_origin" $admin_origin { - default 0; - "GET:" 1; - "HEAD:" 1; - "GET:https://ADMIN_HOST" 1; - "HEAD:https://ADMIN_HOST" 1; - "POST:https://ADMIN_HOST" 1; - "PUT:https://ADMIN_HOST" 1; - "PATCH:https://ADMIN_HOST" 1; - "DELETE:https://ADMIN_HOST" 1; - } - {{ with nomadVar "NOMAD_VARIABLE" }} - map $http_authorization $bearer_key { default 0; "Bearer {{ .api_key.Value }}" 1; } - map $http_x_api_key $claude_key { default 0; "{{ .api_key.Value }}" 1; } - map $client_key $upstream_authorization { default ""; 1 "Bearer {{ .api_key.Value }}"; } - map "$request_method:$uri" $admin_authorization { - default ""; - ~^[A-Z]+:/v(?:0|8)/management/ "Bearer {{ .management_key.Value }}"; - "GET:/v1/models" "Bearer {{ .api_key.Value }}"; - } - {{ end }} - map "$bearer_key:$claude_key" $client_key { default 1; "0:0" 0; } - upstream cliproxy_backend { - server 127.0.0.1:8317; - } - server { - listen 8080 default_server; - server_name _; - location = /healthz { proxy_pass http://cliproxy_backend; } - location / { return 404; } - } - server { - listen 8080; - server_name ADMIN_HOST; - client_max_body_size 32m; - location = / { return 302 /management.html; } - location = /sign-out { return 303 /snow.oauth2/sign_out?rd=https%3A%2F%2FSSO_HOST%2Fsign-in; } - location / { - if ($browser_oauth_callback = 1) { return 404; } - if ($arg_code != "") { return 404; } - if ($admin_origin = 0) { return 403; } - proxy_pass http://cliproxy_backend; - proxy_http_version 1.1; - proxy_set_header Host $host; - proxy_set_header Authorization $admin_authorization; - proxy_set_header X-Management-Key ""; - proxy_set_header Cookie ""; - proxy_hide_header Access-Control-Allow-Origin; - add_header Cache-Control no-store always; - proxy_buffering off; - proxy_read_timeout 3600s; - } - } - server { - listen 8080; - server_name API_HOST; - client_max_body_size 32m; - location = /healthz { proxy_pass http://cliproxy_backend; } - location = / { return 404; } - location / { - if ($client_route = 0) { return 404; } - if ($client_key = 0) { return 401; } - if ($arg_key != "") { return 400; } - if ($arg_auth_token != "") { return 400; } - proxy_pass http://cliproxy_backend$uri$is_args$args; - proxy_http_version 1.1; - proxy_set_header Host $host; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade; - proxy_set_header Cookie ""; - proxy_set_header Authorization $upstream_authorization; - proxy_set_header X-Api-Key ""; - proxy_set_header X-Goog-Api-Key ""; - proxy_set_header X-Management-Key ""; - proxy_set_header X-Forwarded-For ""; - proxy_set_header X-Forwarded-Host ""; - proxy_set_header X-Forwarded-Proto ""; - proxy_buffering off; - proxy_request_buffering off; - proxy_cache off; - proxy_read_timeout 3600s; - proxy_send_timeout 3600s; - } - } -} diff --git a/service/cliproxy/panel-test.mjs b/service/cliproxy/panel-test.mjs deleted file mode 100644 index 377eba99234d0348fea1a233ed54cce5461d4922..0000000000000000000000000000000000000000 --- a/service/cliproxy/panel-test.mjs +++ /dev/null @@ -1,110 +0,0 @@ -import assert from 'node:assert/strict'; -import { readFileSync } from 'node:fs'; -import { request } from 'node:http'; -import vm from 'node:vm'; - -const [panelPath, origin, host] = process.argv.slice(2); -const panel = readFileSync(panelPath, 'utf8'); -function between(start, end) { - const offset = panel.indexOf(start); - assert(offset >= 0, 'Pinned native code entry exists'); - const limit = panel.indexOf(end, offset); - assert(limit > offset, 'Pinned native code exit exists'); - return panel.slice(offset, limit); -} -const nativeStore = between('var yc=e=>', 'function Tc()'); -const nativeStorage = 'var ' + between('_l=`enc::v1::`', 'function Al(e,t)'); -const nativeAuth = 'var Pm=null,' + between('Fm=wc()(', '));typeof window') + '));'; -const nativeRoute = between('function tse({children:e})', 'function nse()'); - -for (const [remembered, rejected] of [[false, false], [true, false], [true, true]]) { - const storage = new Map(); - const requests = []; - const effects = []; - const location = { origin, host: new URL(origin).host, assign: value => requests.push(value) }; - const localStorage = { - getItem: key => storage.get(key) ?? null, - setItem: (key, value) => storage.set(key, String(value)), - removeItem: key => storage.delete(key), - }; - let connection; - let revision = 0; - const context = vm.createContext({ - window: { location, localStorage }, localStorage, - navigator: { userAgent: 'synthetic-native-auth-test' }, - TextEncoder, TextDecoder, Uint8Array, btoa, atob, console, - y: { - useSyncExternalStore: (_, snapshot) => snapshot(), - useCallback: callback => callback, - useDebugValue() {}, - useState: value => [value, () => {}], - useEffect: callback => effects.push(callback), - }, - dr: () => ({ pathname: '/' }), - H: { jsx: (type, props) => ({ type, props }) }, - ti: 'redirect', tO: 'loading', - Bc: 'cli-proxy-auth', - tp: value => value.replace(/\/+$/, ''), - G: { - setConfig: value => { connection = value; revision++; }, - getConnectionRevision: () => revision, - }, - Vp: { getState: () => ({ - clearCache() {}, - async fetchConfig() { - assert(connection.apiBase === origin, 'Restore uses the current origin'); - assert(connection.managementKey === '', 'Browser sends no management secret'); - requests.push(connection.apiBase); - return new Promise((resolve, reject) => { - const pending = request(connection.apiBase + '/v8/management/config', { - headers: { Host: host, ...(rejected ? { 'X-Synthetic-Reject': 'yes' } : {}) }, - }, response => { - let body = ''; - response.setEncoding('utf8'); - response.on('data', chunk => { body += chunk; }); - response.on('end', () => { - if (response.statusCode !== 200) reject(Object.assign(new Error('Synthetic backend rejection'), { status: response.statusCode })); - else resolve(JSON.parse(body)); - }); - }); - pending.on('error', reject); - pending.end(); - }); - }, - }) }, - sm: { getState: () => ({ clearCache() {} }) }, - jm: { getState: () => ({ clearQuotaCache() {} }) }, - }); - vm.runInContext(nativeStore + nativeStorage, context); - if (remembered) { - vm.runInContext('kl.setItem(Bc,{state:{apiBase:"https://obsolete.example",managementKey:"synthetic-old-password",rememberPassword:true},version:0});kl.setItem("managementKey","synthetic-old-password");localStorage.setItem("isLoggedIn","true")', context); - } - vm.runInContext(nativeAuth + nativeRoute, context); - const auth = context.Fm; - assert(!auth.getState().isAuthenticated, 'Stored credentials do not establish authentication'); - assert(auth.getState().managementKey === '', 'Remembered password never hydrates into the native store'); - assert(auth.getState().apiBase === '', 'Remembered connection never hydrates into the native store'); - const route = vm.runInContext('tse({children:"protected"})', context); - for (const effect of effects) effect(); - assert(route.type === 'redirect' && route.props.to === '/login', 'Protected route enters the SSO restore flow'); - assert(requests.length === 0, 'Protected route never revalidates a remembered password'); - const pending = auth.getState().restoreSession(); - assert(!auth.getState().isAuthenticated, 'Authentication waits for the real config response'); - assert(localStorage.getItem('managementKey') === null, 'Legacy password is removed before the request'); - assert(localStorage.getItem('cli-proxy-auth') === null, 'Persisted password is removed before the request'); - assert(await pending === !rejected, 'Restore follows the backend result'); - assert(await auth.getState().restoreSession() === !rejected, 'Concurrent restore shares the validated result'); - assert(requests.length === 1, 'Restore validates once'); - assert(auth.getState().isAuthenticated === !rejected, 'Only the successful backend response authenticates'); - assert(auth.getState().connectionStatus === (rejected ? 'error' : 'connected'), 'Native connection status follows the backend'); - assert(auth.getState().managementKey === '', 'Native state retains no password'); - assert(!auth.getState().rememberPassword, 'Native password persistence is disabled'); - assert(localStorage.getItem('isLoggedIn') === null, 'No persisted authenticated flag is needed'); - assert(localStorage.getItem('cli-proxy-auth') === null, 'Native auth state remains transient'); - if (!rejected) assert(await auth.getState().checkAuth(), 'Native revalidation accepts SSO without a browser key'); - const logout = panel.match(/onClick:(\(\)=>window\.location\.assign\([^)]*\)),title:e\(`header\.logout`\)/); - assert(logout, 'Native logout uses the SSO exit'); - vm.runInContext('(' + logout[1] + ')()', context); - assert(requests.at(-1) === '/sign-out', 'Logout reaches the fixed server redirect'); -} -console.log('Pinned native auth store: fresh, remembered, rejected, revalidation, and logout passed'); diff --git a/service/cliproxy/prepare.py b/service/cliproxy/prepare.py deleted file mode 100644 index 4133bddacfbd825a9b826c85586250f2645f306a..0000000000000000000000000000000000000000 --- a/service/cliproxy/prepare.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python3 -import hashlib -import json -import os -from pathlib import Path -import re -import stat -import sys -import tempfile -import urllib.request - - -PANEL_URL = "https://github.com/router-for-me/Cli-Proxy-API-Management-Center/releases/download/v1.26.5/management.html" -PANEL_SHA256 = "57558cd023475674634a114824b014d3e5ac161f6f3791e3d86b6adab962a700" -PANEL_SIZE = 3312180 -PANEL_SSO_SHA256 = "fdb185df6395bb6652af7171d7193be9a568d027d66d5cd0106b797494f5b4f0" - - -def prepare(data): - root = Path(data["hostRoot"]) / "data" - static = root / "static" - panel = static / "management.html" - if root.is_symlink() or static.is_symlink() or panel.is_symlink(): - raise ValueError("CLIProxyAPI panel paths must not be symbolic links") - static.mkdir(parents=True, exist_ok=True) - if panel.exists(): - metadata = panel.stat() - if not stat.S_ISREG(metadata.st_mode): - raise ValueError("CLIProxyAPI panel must be a regular file") - if metadata.st_size <= PANEL_SIZE and hashlib.sha256(panel.read_bytes()).hexdigest() == PANEL_SSO_SHA256: - panel.chmod(0o444) - return - with urllib.request.urlopen(PANEL_URL, timeout=60) as response: - contents = response.read(PANEL_SIZE + 1) - if len(contents) != PANEL_SIZE or hashlib.sha256(contents).hexdigest() != PANEL_SHA256: - raise ValueError("CLIProxyAPI management panel failed checksum verification") - contents, count = re.subn( - rb"restoreSession:\(\)=>Pm\|\|\(Pm=\(async\(\)=>\{.*?\}\)\(\),Pm\),login:async", - b'restoreSession:()=>Pm||(Pm=(async()=>{localStorage.removeItem(`managementKey`);localStorage.removeItem(`isLoggedIn`);localStorage.removeItem(`cli-proxy-auth`);try{await t().login({apiBase:window.location.origin,managementKey:``,rememberPassword:!1});return!0}catch{return!1}})(),Pm),login:async', - contents, - ) - if count != 1: - raise ValueError("CLIProxyAPI session restoration seam changed") - for before, after in [ - (b'Fm=wc()(Xc(', b'Fm=wc()('), - (b'}),{name:Bc,storage:Jc(()=>({getItem:e=>{let t=kl.getItem(e);return t?JSON.stringify(t):null},setItem:(e,t)=>{kl.setItem(e,JSON.parse(t))},removeItem:e=>{kl.removeItem(e)}})),partialize:e=>({apiBase:e.apiBase,...e.rememberPassword?{managementKey:e.managementKey}:{},rememberPassword:e.rememberPassword,serverVersion:e.serverVersion,serverBuildDate:e.serverBuildDate})}));', - b'}));'), - (b'checkAuth:async()=>{let{managementKey:n,apiBase:r}=t();if(!n||!r)return!1;', - b'checkAuth:async()=>{let{managementKey:n,apiBase:r}=t();if(!r)return!1;'), - (b'onClick:r,title:e(`header.logout`)', - b'onClick:()=>window.location.assign(`/sign-out`),title:e(`header.logout`)'), - ]: - if contents.count(before) != 1: - raise ValueError("CLIProxyAPI management panel authentication seam changed") - contents = contents.replace(before, after) - if hashlib.sha256(contents).hexdigest() != PANEL_SSO_SHA256: - raise ValueError("CLIProxyAPI SSO panel failed checksum verification") - with tempfile.NamedTemporaryFile(dir=static, prefix=".panel-", delete=False) as file: - pending = Path(file.name) - try: - file.write(contents) - os.fchmod(file.fileno(), 0o444) - os.fchown(file.fileno(), data["uid"], data["uid"]) - file.flush() - os.fsync(file.fileno()) - pending.replace(panel) - finally: - pending.unlink(missing_ok=True) - os.chown(static, data["uid"], data["uid"]) - static.chmod(0o755) - - -if __name__ == "__main__": - prepare(json.load(sys.stdin)) diff --git a/service/cliproxy/service.pkl b/service/cliproxy/service.pkl index 11680c0ad14ab567dcc815b37339c474e775637f..dd9f57abd7f5ab29cd391d48bf3bffce22ddb206 100644 --- a/service/cliproxy/service.pkl +++ b/service/cliproxy/service.pkl @@ -1,13 +1,11 @@ amends "../../config/Service.pkl" import "../../config/site.pkl" as site -import "../../config/OpenID.pkl" as sso name = "CLIProxyAPI" tagline = "Codex and Claude API access" access = "infra-admin" stageIsolation = "fresh" -prepare = "prepare.py" secrets { ["api_key"] {} @@ -15,13 +13,14 @@ secrets { } container { - build = "." - entrypoint = "/bin/bash" + build = "build" + killTimeout = "30s" + entrypoint = "/bin/sh" args { "/snowglobe/start.sh" } readOnlyRoot = true tmpfs { "/tmp:rw,nosuid,nodev,noexec,size=64m" } http { - containerPort = 8080 + containerPort = 8317 subdomain = "cpa" authRole = "infra-admin" checkPath = "/healthz" @@ -29,13 +28,13 @@ container { } volumes { ["/data"] {} - ["/data/static"] { readOnly = true } ["/snowglobe/start.sh"] { config = "start.sh" } } env { ["HOME"] = "/data" ["WRITABLE_PATH"] = "/data" - ["MANAGEMENT_STATIC_PATH"] = "/data/static" + ["MANAGEMENT_STATIC_PATH"] = "/CLIProxyAPI/static" + ["CLIPROXY_SHUTDOWN_DRAIN"] = "25s" ["TZ"] = "America/Los_Angeles" } secretFiles { @@ -43,19 +42,16 @@ container { {{ with nomadVar "nomad/jobs/\(module.id)" }} { "config-version": 8, - "server": {"host": "127.0.0.1", "port": 8317, "commercial-mode": true, "max-request-body-bytes": 33554432, "discovery": {"enabled": false}}, + "server": {"host": "0.0.0.0", "port": 8317, "commercial-mode": true, "max-request-body-bytes": 33554432, "discovery": {"enabled": false}}, "access": {"api-keys": [{{ .api_key.Value | toJSON }}]}, "management": {"allow-remote": true, "secret-key": {{ .management_key.Value | toJSON }}, "disable-auto-update-panel": true}, "oauth": {"auth-dir": "/data/auth"}, + "routing": {"strategy": "reset-soonest", "session-affinity": true, "session-affinity-reclaim": true, "session-affinity-ttl": "1h", "cooldown": {"save-cooldown-status": true}}, + "client": {"codex": {"codex-models-only": true}}, "plugins": {"enabled": false, "dir": "/data/plugins"}, - "observability": {"logs": {"debug": false, "logging-to-file": false, "request-log": false}, "usage": {"usage-statistics-enabled": false}, "pprof": {"enable": false}} + "observability": {"history": {"enabled": true}, "logs": {"debug": false, "logging-to-file": false, "request-log": false}, "usage": {"usage-statistics-enabled": false}, "pprof": {"enable": false}} } {{ end }} """ - ["/run/secrets/cliproxy-nginx.conf"] = read("nginx.conf").text - .replaceAll("NOMAD_VARIABLE", "nomad/jobs/\(module.id)") - .replaceAll("ADMIN_HOST", module.container.http.hostname!!) - .replaceAll("API_HOST", module.container.http.plainHostnames[0]) - .replaceAll("SSO_HOST", sso.hostname) } } diff --git a/service/cliproxy/start.sh b/service/cliproxy/start.sh index fa0afc605a0e6265129a1c98e14c455b7b57cb70..18857c32dd49350c8be1a6fe6ed6fed1dd8d2664 100644 --- a/service/cliproxy/start.sh +++ b/service/cliproxy/start.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -eu umask 077 @@ -18,12 +18,8 @@ test -s /data/config.yaml chmod 600 /data/config.yaml mkdir -p /data/auth /data/plugins chmod 700 /data /data/auth /data/plugins -/CLIProxyAPI/CLIProxyAPI --config /data/config.yaml & -app=$! -/usr/sbin/nginx -c /run/secrets/cliproxy-nginx.conf -g 'daemon off;' & -gateway=$! -trap 'kill "$app" "$gateway" 2>/dev/null || :; wait "$app" "$gateway" 2>/dev/null || :' EXIT -trap 'exit 143' TERM -trap 'exit 130' INT -wait -n "$app" "$gateway" -exit 1 +if [ ! -e /data/.snowglobe-fork-v1 ]; then + /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml --fork-migrate + touch /data/.snowglobe-fork-v1 +fi +exec /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml diff --git a/service/shale/service.pkl b/service/shale/service.pkl index dbdb537322551e9b1bb15efa2e5b011b3815b3ac..0fdfe0fb269db7badba13f01f645b683b91d68f4 100644 --- a/service/shale/service.pkl +++ b/service/shale/service.pkl @@ -8,10 +8,7 @@ requirements { new sso.Client { clientId = module.id name = module.name - redirectUris { - "https://\(module.container.http.hostname)/-/callback" - "http://git1.localhost/-/callback" - } + redirectUris { "https://\(module.container.http.hostname)/-/callback" } usernameAliases { ["snow"] = "clover" } allowGuests = true usernameRequired = true diff --git a/tools/cliproxy-test.py b/tools/cliproxy-test.py index 3218829d5b59dfda5967be0f73801052f8fad120..a0870e2ad152d01031602e5d99fe1465897ed99c 100644 --- a/tools/cliproxy-test.py +++ b/tools/cliproxy-test.py @@ -5,8 +5,6 @@ from contextlib import contextmanager import hashlib import http.client from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer -import importlib.util -import io import json import os from pathlib import Path @@ -20,14 +18,11 @@ import unittest from unittest.mock import patch import snowglobe +import router SERVICE = Path(__file__).resolve().parents[1] / "service/cliproxy" -spec = importlib.util.spec_from_file_location("cliproxy_prepare", SERVICE / "prepare.py") -prepare = importlib.util.module_from_spec(spec) -spec.loader.exec_module(prepare) -NGINX = None -PANEL = None +CADDY = None API_KEY = "a1" * 32 MANAGEMENT_KEY = "b2" * 32 ADMIN_HOST = "cliproxy-preview-12345678.paperclover.net" @@ -66,18 +61,17 @@ class ServiceTests(unittest.TestCase): rendered = snowglobe.render(self.data, {"cliproxy": self.data}) self.assertEqual(rendered.count('user = "3150:3150"'), 1) self.assertEqual(rendered.count('readonly_rootfs = true'), 1) - self.assertEqual(rendered.count('perms = "0400"'), 2) + self.assertEqual(rendered.count('perms = "0400"'), 1) self.assertIn('secrets/cliproxy-seed.json:/run/secrets/cliproxy-seed.json:ro', rendered) - self.assertIn('secrets/cliproxy-nginx.conf:/run/secrets/cliproxy-nginx.conf:ro', rendered) - self.assertIn('server 127.0.0.1:8317;', rendered) - self.assertIn('/data/static:/data/static:ro', rendered) + self.assertIn('kill_timeout = "30s"', rendered) + self.assertIn('/CLIProxyAPI/static', rendered) self.assertNotIn("NOMAD_SERVICE", rendered) self.assertNotIn("NOMAD_VARIABLE", rendered) self.assertNotIn("/srv/clover", rendered) self.assertNotIn("host_port", rendered) self.assertNotIn('env = true', rendered) - def test_bootstrap_preserves_native_config_and_provider_credentials(self): + def test_bootstrap_preserves_config_after_the_first_migration(self): with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) data = root / "data" @@ -85,13 +79,10 @@ class ServiceTests(unittest.TestCase): seed = root / "seed.json" seed.write_text(json.dumps({"access": {"api-keys": [API_KEY]}})) binary = root / "binary" - binary.write_text('#!/usr/bin/env python3\nimport pathlib,signal,sys\nassert sys.argv[1] == "--config" and pathlib.Path(sys.argv[2]).stat().st_size\nsignal.pause()\n') + binary.write_text('#!/usr/bin/env python3\nimport pathlib,sys\nassert sys.argv[1] == "--config" and pathlib.Path(sys.argv[2]).stat().st_size\n') binary.chmod(0o700) - gateway = root / "gateway" - gateway.write_text('#!/bin/sh\nexit 0\n') - gateway.chmod(0o700) - script = (SERVICE / "start.sh").read_text().replace('/run/secrets/cliproxy-seed.json', str(seed)).replace('/CLIProxyAPI/CLIProxyAPI', str(binary)).replace('/usr/sbin/nginx', str(gateway)).replace('/data', str(data)) - self.assertEqual(subprocess.run(["bash", "-ec", script], timeout=5).returncode, 1) + script = (SERVICE / "start.sh").read_text().replace('/run/secrets/cliproxy-seed.json', str(seed)).replace('/CLIProxyAPI/CLIProxyAPI', str(binary)).replace('/data', str(data)) + self.assertEqual(subprocess.run(["sh", "-ec", script], timeout=5).returncode, 0) config = data / "config.yaml" self.assertEqual(json.loads(config.read_text())["access"]["api-keys"], [API_KEY]) self.assertEqual(config.stat().st_mode & 0o777, 0o600) @@ -100,345 +91,233 @@ class ServiceTests(unittest.TestCase): credential = data / "auth/fixture.json" credential.write_text('{"synthetic":true}') seed.write_text('different seed') - self.assertEqual(subprocess.run(["bash", "-ec", script], timeout=5).returncode, 1) + self.assertEqual(subprocess.run(["sh", "-ec", script], timeout=5).returncode, 0) self.assertIn('provider-setting: retained', config.read_text()) self.assertEqual(credential.read_text(), '{"synthetic":true}') config.unlink() config.symlink_to(seed) - result = subprocess.run(["bash", "-ec", script], capture_output=True) + result = subprocess.run(["sh", "-ec", script], capture_output=True) self.assertNotEqual(result.returncode, 0) self.assertEqual(seed.read_text(), 'different seed') - def test_panel_pin_rejects_bad_download_without_replacing_existing_file(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - static = root / "data/static" - static.mkdir(parents=True) - panel = static / "management.html" - panel.write_bytes(b"previous panel") - with patch.object(prepare.urllib.request, "urlopen", return_value=io.BytesIO(b"bad panel")): - with self.assertRaisesRegex(ValueError, "checksum"): - prepare.prepare({"hostRoot": str(root), "uid": os.getuid()}) - self.assertEqual(panel.read_bytes(), b"previous panel") - panel.unlink() - panel.symlink_to(root / "outside") - with self.assertRaisesRegex(ValueError, "symbolic"): - prepare.prepare({"hostRoot": str(root), "uid": os.getuid()}) - def test_panel_verified_existing_asset_never_contacts_release_feed(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary) - static = root / "data/static" - static.mkdir(parents=True) - panel = static / "management.html" - contents = b"pinned synthetic panel" - panel.write_bytes(contents) - with patch.object(prepare, "PANEL_SSO_SHA256", hashlib.sha256(contents).hexdigest()), patch.object(prepare.urllib.request, "urlopen", side_effect=AssertionError("unexpected download")): - prepare.prepare({"hostRoot": str(root), "uid": os.getuid()}) - self.assertEqual(panel.read_bytes(), contents) - -@unittest.skipUnless(NGINX, "pass --nginx to test the real gateway binary") +@unittest.skipUnless(CADDY, "pass --caddy to test the real router") class GatewayTests(unittest.TestCase): def setUp(self): - self.temporary = tempfile.TemporaryDirectory(prefix="cliproxy-gateway-") - self.addCleanup(self.temporary.cleanup) - root = Path(self.temporary.name) + temporary = tempfile.TemporaryDirectory(prefix='cliproxy-caddy-') + self.addCleanup(temporary.cleanup) + root = Path(temporary.name) self.observed = [] - self.secret_markers = [API_KEY, MANAGEMENT_KEY] - observed = self.observed + self.auth_checks = [] + self.websocket_started = threading.Event() + self.websocket_release = threading.Event() + observed, auth_checks = self.observed, self.auth_checks + websocket_started, websocket_release = self.websocket_started, self.websocket_release class Backend(BaseHTTPRequestHandler): - protocol_version = "HTTP/1.1" + protocol_version = 'HTTP/1.1' def do_GET(self): - self.respond() - - def do_POST(self): - self.respond() - - do_PUT = do_POST - do_PATCH = do_POST - do_DELETE = do_POST - - def respond(self): - observed.append((self.command, self.path, dict(self.headers), self.rfile.read(int(self.headers.get('Content-Length', '0'))))) - if self.path == '/v8/management/config' and (self.headers.get('Authorization') != 'Bearer ' + MANAGEMENT_KEY or self.headers.get('X-Synthetic-Reject')): - self.send_response(401) - self.send_header('Content-Length', '0') - self.end_headers() - return - if self.headers.get("Upgrade", "").lower() == "websocket": + observed.append((self.command, self.path, dict(self.headers))) + management = self.path.startswith(('/v0/management/', '/v8/management/')) + if management: + valid = self.headers.get('Authorization') == 'Bearer ' + MANAGEMENT_KEY or self.headers.get('X-Management-Key') == MANAGEMENT_KEY + elif self.path.startswith('/v1/'): + valid = self.headers.get('Authorization') == 'Bearer ' + API_KEY or self.headers.get('X-Api-Key') == API_KEY + else: + valid = True + if self.headers.get('Upgrade', '').lower() == 'websocket' and valid: accept = base64.b64encode(hashlib.sha1((self.headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').encode()).digest()).decode() self.send_response(101) - self.send_header("Upgrade", "websocket") - self.send_header("Connection", "Upgrade") - self.send_header("Sec-WebSocket-Accept", accept) - self.end_headers() - header = self.rfile.read(2) - mask = self.rfile.read(4) - body = self.rfile.read(2) + self.send_header('Upgrade', 'websocket'); self.send_header('Connection', 'Upgrade') + self.send_header('Sec-WebSocket-Accept', accept); self.end_headers() + header, mask, body = self.rfile.read(2), self.rfile.read(4), self.rfile.read(2) if header != b'\x81\x82' or bytes(value ^ mask[index % 4] for index, value in enumerate(body)) != b'hi': raise AssertionError('incorrect client WebSocket frame') - self.wfile.write(b'\x81\x02hi') - self.wfile.flush() + self.wfile.write(b'\x81\x02hi'); self.wfile.flush() + if self.headers.get('X-Synthetic-Hold'): + websocket_started.set() + if not websocket_release.wait(5): + raise AssertionError('WebSocket release was not signaled') + self.wfile.write(b'\x81\x02ok'); self.wfile.flush() self.close_connection = True return - if self.headers.get("X-Synthetic-Stream"): - self.send_response(200) - self.send_header("Content-Type", "text/event-stream") - self.send_header("Connection", "close") - self.end_headers() - self.wfile.write(b'data: first\n\n') - self.wfile.flush() - time.sleep(.5) - self.wfile.write(b'data: last\n\n') - self.close_connection = True - return - body = json.dumps({"method": self.command, "path": self.path}).encode() - self.send_response(200) + self.send_response(200 if valid else 401) + self.send_header('Content-Type', 'application/json') self.send_header('Access-Control-Allow-Origin', '*') - self.send_header("Content-Length", str(len(body))) + if self.headers.get('X-Synthetic-Stream') and valid: + self.send_header('Connection', 'close'); self.end_headers() + self.wfile.write(b'data: first\n\n'); self.wfile.flush() + time.sleep(.5) + self.wfile.write(b'data: last\n\n'); self.close_connection = True + else: + body = b'{}' + self.send_header('Content-Length', str(len(body))); self.end_headers(); self.wfile.write(body) + + do_POST = do_GET + do_PUT = do_GET + + def log_message(self, *args): + pass + + class Auth(BaseHTTPRequestHandler): + def do_GET(self): + auth_checks.append(dict(self.headers)) + cookie = self.headers.get('Cookie', '') + self.send_response(202 if cookie else 401) + if cookie: + self.send_header('X-Auth-Request-Groups', 'role:infra-admin' if cookie == 'fixture=admin' else 'role:media') self.end_headers() - self.wfile.write(body) def log_message(self, *args): pass - backend = ThreadingHTTPServer(("127.0.0.1", 0), Backend) - threading.Thread(target=backend.serve_forever, daemon=True).start() - self.addCleanup(backend.server_close) - self.addCleanup(backend.shutdown) + for handler, attribute in [(Backend, 'backend'), (Auth, 'auth')]: + server = ThreadingHTTPServer(('127.0.0.1', 0), handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + self.addCleanup(server.server_close); self.addCleanup(server.shutdown) + setattr(self, attribute, server) with socket.socket() as reservation: - reservation.bind(("127.0.0.1", 0)) - self.port = reservation.getsockname()[1] - config = (SERVICE / "nginx.conf").read_text() - config = config.replace('server 127.0.0.1:8317;', 'server 127.0.0.1:' + str(backend.server_port) + ';') - config = re.sub(r'{{ with nomadVar .*?}}|{{ end }}', '', config) - config = config.replace('{{ .api_key.Value }}', API_KEY) - config = config.replace('{{ .management_key.Value }}', MANAGEMENT_KEY) - config = config.replace('/tmp/', str(root) + '/').replace('listen 8080', f'listen 127.0.0.1:{self.port}') - config = config.replace('ADMIN_HOST', ADMIN_HOST).replace('API_HOST', API_HOST) - config = config.replace('SSO_HOST', 'snowglobe.paperclover.net') - self.assertNotIn('{{', config) - path = root / "nginx.conf" - path.write_text(config) - log = (root / "stderr").open('w+b') - self.addCleanup(log.close) - process = subprocess.Popen([NGINX, '-c', str(path), '-g', 'daemon off;'], stdout=log, stderr=log) + reservation.bind(('127.0.0.1', 0)); self.port = reservation.getsockname()[1] + proof = root / 'proof'; proof.write_text('f' * 64) + (root / 'cliproxy.json').write_text(json.dumps({'cliproxy': {'api_key': API_KEY, 'management_key': MANAGEMENT_KEY}})) + def nomad(path, token): + if path == '/v1/services': + return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in ('cliproxy', 'forward-auth')]}] + if path.startswith('/v1/service/'): + service = path.rsplit('/', 1)[1] + return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1', + 'Port': self.backend.server_port if service == 'cliproxy' else self.auth.server_port, + 'Tags': ['caddy-host=' + ADMIN_HOST, 'caddy-plain-host=' + API_HOST, 'caddy-auth-role=infra-admin'] if service == 'cliproxy' else []}] + return {'http': {'Status': 'success'}} + environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '28001', + 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof)} + with patch.dict(os.environ, environment), patch.object(router, 'ROUTE_DIR', str(root)), patch.object(router, 'nomad', nomad): + rendered = router.render('fixture') + with socket.socket() as admin: + admin.bind(('127.0.0.1', 0)); self.admin_port = admin.getsockname()[1] + config = '{\n admin 127.0.0.1:' + str(self.admin_port) + '\n auto_https off\n}\n' + for host in (ADMIN_HOST, API_HOST): + block = rendered.split(host + ' {', 1)[1].split('\n}\n', 1)[0] + config += f'http://{host}:{self.port} {{' + block + '\n}\n' + path = root / 'Caddyfile'; path.write_text(config) + self.config_path = path + self.log = root / 'caddy.log' + log = self.log.open('wb'); self.addCleanup(log.close) + process = subprocess.Popen([CADDY, 'run', '--config', str(path), '--adapter', 'caddyfile'], stdout=log, stderr=log) def stop(): - process.terminate() - process.wait(timeout=5) - log.seek(0) - contents = log.read() - for marker in self.secret_markers: - self.assertNotIn(marker.encode(), contents) + process.terminate(); process.wait(timeout=5) self.addCleanup(stop) for _ in range(100): if process.poll() is not None: - log.seek(0) - self.fail('nginx failed: ' + log.read().decode()) + self.fail('Caddy failed: ' + self.log.read_text()) try: - connection = socket.create_connection(('127.0.0.1', self.port), .1) - connection.close() - break + with socket.create_connection(('127.0.0.1', self.port), timeout=.1): + break except OSError: time.sleep(.02) else: - self.fail('nginx did not listen') + self.fail('Caddy did not listen') @contextmanager - def request(self, method, path, headers=None, body=None): + def request(self, method, path, headers=None): connection = http.client.HTTPConnection('127.0.0.1', self.port, timeout=3) - connection.request(method, path, body=body, headers={'Host': API_HOST, **(headers or {})}) + connection.request(method, path, headers={'Host': ADMIN_HOST, **(headers or {})}) response = connection.getresponse() try: yield response finally: - response.close() - connection.close() + response.close(); connection.close() - def test_management_callback_and_path_confusion_never_reach_backend(self): - for path in ['/', '/management.html', '/v8/management/config', '/v0/management/auth-files', '/v0/resource/plugins/x', '/codex/callback?code=synthetic', '/anthropic/callback', '/healthz/../v8/management/config', '/v1/models/../../v8/management/config', '/v1/models/%2e%2e/%2e%2e/v8/management/config', '/v1/models/%2f..%2f..%2fv8%2fmanagement%2fconfig', '/v1/responses/../management']: - with self.subTest(path=path), self.request('GET', path, {'Authorization': 'Bearer ' + API_KEY}) as response: - self.assertIn(response.status, [400, 404]) - response.read() - self.assertEqual(self.observed, []) + def test_native_keys_bypass_sso_on_both_hosts(self): + for host in (ADMIN_HOST, API_HOST): + for headers in ({'Authorization': 'Bearer ' + MANAGEMENT_KEY}, {'X-Management-Key': MANAGEMENT_KEY}): + with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response: + self.assertEqual(response.status, 200); response.read() + for headers in ({'Authorization': 'Bearer ' + API_KEY}, {'X-Api-Key': API_KEY}): + with self.request('GET', '/v1/models', {'Host': host, **headers}) as response: + self.assertEqual(response.status, 200); response.read() + self.assertEqual(self.auth_checks, []) + self.assertTrue(all('Cookie' not in headers for _, _, headers in self.observed)) - def test_admin_hostname_has_native_panel_and_unknown_hostname_is_denied(self): - with self.request('GET', '/', {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 302) - self.assertEqual(response.getheader('Location'), '/management.html') - response.read() - with self.request('GET', '/management.html', {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 200) - response.read() - count = len(self.observed) - with self.request('GET', '/management.html', {'Host': 'other.example.test'}) as response: - self.assertEqual(response.status, 404) - response.read() - self.assertEqual(len(self.observed), count) + def test_wrong_or_absent_keys_cannot_gain_management_access(self): + for host in (ADMIN_HOST, API_HOST): + for headers in ({}, {'Authorization': 'Bearer invalid'}, {'Authorization': 'Bearer ' + API_KEY}): + with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response: + self.assertEqual(response.status, 401); response.read() - def test_sso_management_key_is_injected_only_on_admin_management_paths(self): - for path in ['/v0/management/auth-files', '/v8/management/config', '/v8/management/config.yaml']: - with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer stale-browser-key', 'X-Management-Key': 'stale-browser-key', 'Cookie': 'synthetic-session'}) as response: + def test_dashboard_requires_sso_even_with_management_key(self): + for path in ('/', '/management.html'): + with self.request('GET', path, {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response: + self.assertEqual(response.status, 302) + self.assertTrue(response.getheader('Location').startswith('/snow.oauth2/sign_in')); response.read() + with self.request('GET', path, {'Host': API_HOST}) as response: + self.assertEqual(response.status, 404); response.read() + with self.request('GET', '/management.html', {'Cookie': 'fixture=member'}) as response: + self.assertEqual(response.status, 403); response.read() + with self.request('GET', '/management.html', {'Cookie': 'fixture=admin'}) as response: + self.assertEqual(response.status, 200); response.read() + + def test_sso_panel_injects_private_credentials_and_guards_origins(self): + for path in ('/v8/management/config', '/v1/models'): + with self.request('GET', path, {'Cookie': 'fixture=admin'}) as response: self.assertEqual(response.status, 200) self.assertEqual(response.getheader('Cache-Control'), 'no-store') - self.assertIsNone(response.getheader('Access-Control-Allow-Origin')) - self.assertNotIn(MANAGEMENT_KEY.encode(), response.read()) - headers = self.observed[-1][2] - self.assertEqual(headers['Authorization'], 'Bearer ' + MANAGEMENT_KEY) - self.assertNotIn('X-Management-Key', headers) - self.assertNotIn('Cookie', headers) - for path in ['/management.html', '/v8/management-other/config']: - with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response: - self.assertEqual(response.status, 200) - response.read() - self.assertNotIn('Authorization', self.observed[-1][2]) - count = len(self.observed) - for host in [API_HOST, 'other.example.test']: - with self.request('GET', '/v8/management/config', {'Host': host, 'Authorization': 'Bearer ' + API_KEY}) as response: - self.assertEqual(response.status, 404) - response.read() - self.assertEqual(len(self.observed), count) - - def test_admin_model_list_uses_client_key_only_for_exact_read(self): - for path in ['/v1/models', '/v1/models?fixture=yes']: - with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY, 'Cookie': 'synthetic-session'}) as response: - self.assertEqual(response.status, 200) - self.assertNotIn(API_KEY.encode(), response.read()) - self.assertEqual(self.observed[-1][2]['Authorization'], 'Bearer ' + API_KEY) - self.assertNotIn('Cookie', self.observed[-1][2]) - for method, path in [('GET', '/v1/models/'), ('GET', '/v1/models/nearby'), ('POST', '/v1/models'), ('POST', '/v1/responses'), ('POST', '/v1/chat/completions'), ('GET', '/v1/responses')]: - with self.subTest(method=method, path=path), self.request(method, path, {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response: - self.assertEqual(response.status, 200) - response.read() - self.assertNotIn('Authorization', self.observed[-1][2]) - count = len(self.observed) - with self.request('GET', '/v1/models') as response: - self.assertEqual(response.status, 401) - response.read() - self.assertEqual(len(self.observed), count) - - def test_management_mutations_require_exact_admin_origin(self): - for method in ['POST', 'PUT', 'PATCH', 'DELETE']: - for origin in ['', 'null', 'https://other.example.test', 'https://' + ADMIN_HOST.replace('.', '-'), 'http://' + ADMIN_HOST, 'https://' + API_HOST]: - with self.subTest(method=method, origin=origin), self.request(method, '/v8/management/config', {'Host': ADMIN_HOST, **({'Origin': origin} if origin else {})}, body='{}') as response: - self.assertEqual(response.status, 403) - response.read() - self.assertEqual(self.observed, []) - for method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']: - with self.request(method, '/v8/management/config', {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST}, body='{}' if method != 'GET' else None) as response: - self.assertEqual(response.status, 200) - response.read() - with self.request('GET', '/v8/management/config', {'Host': ADMIN_HOST, 'Origin': 'https://other.example.test'}) as response: - self.assertEqual(response.status, 403) - response.read() - - def test_logout_uses_fixed_sso_destination(self): - with self.request('GET', '/sign-out?rd=https://other.example.test', {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 303) - self.assertEqual(response.getheader('Location'), '/snow.oauth2/sign_out?rd=https%3A%2F%2Fsnowglobe.paperclover.net%2Fsign-in') - response.read() - self.assertEqual(self.observed, []) - - def test_pinned_native_panel_auth_flow_through_gateway(self): - if not PANEL: - self.skipTest('pass --panel with the pinned upstream management.html') - root = Path(self.temporary.name) - with patch.object(prepare.urllib.request, 'urlopen', return_value=io.BytesIO(PANEL.read_bytes())), patch.object(prepare.os, 'fchown'), patch.object(prepare.os, 'chown'): - prepare.prepare({'hostRoot': str(root), 'uid': os.getuid()}) - panel = root / 'data/static/management.html' - with patch.object(prepare.urllib.request, 'urlopen', side_effect=AssertionError('unexpected download')): - prepare.prepare({'hostRoot': str(root), 'uid': os.getuid()}) - subprocess.run(['node', str(SERVICE / 'panel-test.mjs'), str(panel), 'http://127.0.0.1:' + str(self.port), ADMIN_HOST], check=True, timeout=15) - - def test_browser_oauth_callback_credentials_never_reach_upstream_or_logs(self): - code = 'synthetic-oauth-code-must-not-be-logged' - state = 'synthetic-oauth-state-must-not-be-logged' - self.secret_markers.extend([code, state]) - paths = ['/v0/management/oauth-callback', '/v8/management/oauth/callback', '/anthropic/callback', '/codex/callback', '/antigravity/callback', '/devin/callback', '/callback'] - for path in paths: - for method in ['GET', 'HEAD']: - for suffix in ['', '/']: - with self.subTest(path=path, method=method, suffix=suffix), self.request(method, path + suffix + '?code=' + code + '&state=' + state, {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 404) - response.read() - with self.request('GET', path + '?state=' + state, {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 404) - response.read() - with self.request('GET', '/future-provider/callback?code=' + code, {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 404) - response.read() - self.assertEqual(self.observed, []) - payload = json.dumps({'provider': 'codex', 'redirect_url': 'http://localhost:1455/auth/callback?code=' + code + '&state=' + state}).encode() - for path in paths[:2]: - with self.request('POST', path, {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST, 'Content-Type': 'application/json'}, body=payload) as response: - self.assertEqual(response.status, 200) - response.read() - self.assertEqual(self.observed[-1][3], payload) - self.assertNotIn(code, self.observed[-1][1]) - self.assertNotIn(state, self.observed[-1][1]) - with self.request('GET', '/v8/management/oauth/status?state=' + state, {'Host': ADMIN_HOST}) as response: - self.assertEqual(response.status, 200) - response.read() - - def test_api_key_is_required_independently_of_upstream_auth(self): - for headers in [{}, {'Authorization': 'Bearer ' + MANAGEMENT_KEY}, {'Authorization': 'Bearer wrong'}, {'X-Api-Key': 'wrong'}]: - with self.request('GET', '/v1/models', headers) as response: - self.assertEqual(response.status, 401) - response.read() - self.assertEqual(self.observed, []) - for headers in [{'Authorization': 'Bearer ' + API_KEY}, {'X-Api-Key': API_KEY}]: - with self.request('GET', '/v1/models', headers) as response: - self.assertEqual(response.status, 200) - response.read() - self.assertEqual(self.observed[-1][2]['Authorization'], 'Bearer ' + API_KEY) - self.assertNotIn('X-Api-Key', self.observed[-1][2]) - for query in ['key=other', 'auth_token=other']: - with self.request('GET', '/v1/models?' + query, {'Authorization': 'Bearer ' + API_KEY}) as response: - self.assertEqual(response.status, 400) - response.read() - - def test_confirmed_codex_claude_methods_preserve_query_and_strip_admin_cookies(self): - for method, path in [('GET', '/v1/models'), ('POST', '/v1/chat/completions'), ('POST', '/v1/responses'), ('POST', '/v1/responses/compact'), ('POST', '/v1/messages'), ('POST', '/v1/messages/count_tokens'), ('POST', '/backend-api/codex/responses')]: - with self.subTest(path=path), self.request(method, path + '?fixture=yes', {'Authorization': 'Bearer ' + API_KEY, 'Cookie': 'snowglobe_session=synthetic', 'X-Management-Key': MANAGEMENT_KEY}) as response: - self.assertEqual(response.status, 200) - self.assertEqual(json.loads(response.read())['path'], path + '?fixture=yes') + self.assertIsNone(response.getheader('Access-Control-Allow-Origin')); response.read() self.assertNotIn('Cookie', self.observed[-1][2]) - self.assertNotIn('X-Management-Key', self.observed[-1][2]) - for method, path in [('DELETE', '/v1/models'), ('GET', '/v1/messages'), ('POST', '/v1/models'), ('OPTIONS', '/v1/responses')]: - with self.request(method, path, {'Authorization': 'Bearer ' + API_KEY}) as response: - self.assertEqual(response.status, 404) - response.read() - - def test_stream_is_delivered_before_upstream_finishes(self): - started = time.monotonic() - with self.request('POST', '/v1/responses', {'Authorization': 'Bearer ' + API_KEY, 'X-Synthetic-Stream': 'yes'}) as response: - self.assertEqual(response.status, 200) - self.assertEqual(response.readline(), b'data: first\n') - self.assertLess(time.monotonic() - started, .4) - self.assertIn(b'data: last', response.read()) - - def test_websocket_upgrade_reaches_responses_route(self): + for origin in ('', 'https://evil.example', 'null'): + with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', **({'Origin': origin} if origin else {})}) as response: + self.assertEqual(response.status, 403); response.read() + with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', 'Origin': 'https://' + ADMIN_HOST}) as response: + self.assertEqual(response.status, 200); response.read() + with self.request('POST', '/v8/management/config', {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response: + self.assertEqual(response.status, 200); response.read() + + def test_native_websocket_upgrade(self): key = base64.b64encode(b'synthetic-16-key').decode() with socket.create_connection(('127.0.0.1', self.port), timeout=3) as connection: - connection.sendall(('GET /v1/responses HTTP/1.1\r\nHost: ' + API_HOST + '\r\nAuthorization: Bearer ' + API_KEY + '\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ' + key + '\r\n\r\n').encode()) + connection.sendall(('GET /v1/responses HTTP/1.1\r\nHost: ' + ADMIN_HOST + '\r\nAuthorization: Bearer ' + API_KEY + '\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ' + key + '\r\n\r\n').encode()) headers = b'' while not headers.endswith(b'\r\n\r\n'): headers += connection.recv(1) self.assertIn(b' 101 ', headers.split(b'\r\n')[0]) - self.assertIn(b'upgrade: websocket', headers.lower()) connection.sendall(b'\x81\x82\x01\x02\x03\x04ik') self.assertEqual(connection.recv(4), b'\x81\x02hi') + self.assertEqual(self.auth_checks, []) + + def test_websocket_survives_caddy_configuration_reload(self): + for host in (ADMIN_HOST, API_HOST): + self.websocket_started.clear(); self.websocket_release.clear() + key = base64.b64encode(b'synthetic-16-key').decode() + with socket.create_connection(('127.0.0.1', self.port), timeout=3) as connection: + connection.sendall(('GET /v1/responses HTTP/1.1\r\nHost: ' + host + '\r\nAuthorization: Bearer ' + API_KEY + '\r\nX-Synthetic-Hold: yes\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ' + key + '\r\n\r\n').encode()) + headers = b'' + while not headers.endswith(b'\r\n\r\n'): + headers += connection.recv(1) + self.assertIn(b' 101 ', headers) + connection.sendall(b'\x81\x82\x01\x02\x03\x04ik') + self.assertEqual(connection.recv(4), b'\x81\x02hi') + self.assertTrue(self.websocket_started.wait(1)) + subprocess.run([CADDY, 'reload', '--address', f'127.0.0.1:{self.admin_port}', '--config', str(self.config_path), '--adapter', 'caddyfile', '--force'], check=True, capture_output=True) + self.websocket_release.set() + self.assertEqual(connection.recv(4), b'\x81\x02ok') + + def test_native_response_streams(self): + started = time.monotonic() + with self.request('POST', '/v1/responses', {'Authorization': 'Bearer ' + API_KEY, 'X-Synthetic-Stream': 'yes'}) as response: + self.assertEqual(response.status, 200) + self.assertEqual(response.readline(), b'data: first\n') + self.assertLess(time.monotonic() - started, .4) + self.assertIn(b'data: last', response.read()) + log = self.log.read_text() + self.assertNotIn(API_KEY, log); self.assertNotIn(MANAGEMENT_KEY, log) if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('--nginx', type=Path) - parser.add_argument('--panel', type=Path) + parser.add_argument('--caddy', type=Path) options, remaining = parser.parse_known_args() - NGINX = str(options.nginx.resolve()) if options.nginx else None - PANEL = options.panel.resolve() if options.panel else None - GatewayTests.__unittest_skip__ = not NGINX + CADDY = str(options.caddy.resolve()) if options.caddy else None + GatewayTests.__unittest_skip__ = not CADDY unittest.main(argv=[__file__, *remaining]) diff --git a/tools/cliproxy.md b/tools/cliproxy.md index c676e410b4461c09d4abfbfdc385e8ea36c403d5..ca392793cf2c6465b4511379a2cd81163f64ae8c 100644 --- a/tools/cliproxy.md +++ b/tools/cliproxy.md @@ -1,71 +1,114 @@ # CLIProxyAPI -`cpa.paperclover.net` is the management panel, protected by Snow's -`infra-admin` role. Snow sign-in opens it automatically; nginx supplies the -independent management key to its management endpoints on the server. -`cpa-api.paperclover.net` is the client endpoint: use -`https://cpa-api.paperclover.net/v1` for OpenAI-compatible clients or -`https://cpa-api.paperclover.net` for Claude-compatible clients. - -Nomad variable `nomad/jobs/cliproxy` owns `api_key` and `management_key`. -Keep values in restricted private files or a password manager; do not paste them -into chat, command arguments, or tracked configuration. The management key never -needs to enter the browser. Opening the panel clears any previously remembered -native management password and validates access through the native config API. -Its Logout button clears the shared forward-auth cookie and opens Snow sign-in. -API clients accept the separate client key through `Authorization: Bearer …` -or `X-Api-Key`. The gateway independently enforces this one infra-managed key, -even if the native panel removes its own API keys. Query-string keys are rejected. - -## Connect providers - -Open the management panel and sign in through Snow. -Its OAuth section supports **Codex** and **Claude**. Start the -chosen provider's login and complete consent in the provider's own browser page. -For remote OAuth flows that finish on an unreachable `localhost` URL, copy that -entire callback URL into the panel's callback form. Treat the callback URL as a -temporary credential. No callback ports or provider callbacks are exposed on -the client hostname. Browser GET/HEAD callback routes are blocked on the admin -hostname too: CLIProxyAPI logs URL queries, so callback credentials must be -submitted through the management POST form. API-key provider credentials can also be configured in -the native panel; their persistent home is the private service dataset. - -`/data/config.yaml` is seeded once from a read-only Nomad template. Native -configuration edits, hashed management key, OAuth accounts, and refreshed tokens -remain under `/srv/prod/cliproxy/data`; restarts and normal releases retain them. -Changing a generated Nomad key does not overwrite an existing native config: -coordinate key rotation in both Nomad and the native panel. Fresh staging uses -separate storage and generated keys and must use synthetic providers only. - -## Boundary and updates - -Only the confirmed Codex/Claude client routes are public: model listing, chat, -completions, messages/counting, Responses/compact/WebSocket, and Codex search -and direct aliases. Management, plugins, OAuth callbacks, unknown paths, and -unapproved methods return 404 on the API hostname. The app listens only on -container loopback; nginx publishes the sole port. Both processes exit together -if either fails. The host-side route requires Snow for management; nginx's -admin-host routing does not replace that authentication boundary. -Management writes require the exact admin origin; cross-origin reads with an -Origin header are rejected, and upstream CORS headers are suppressed. The -management key is added only to `/v0/management/` and `/v8/management/` paths. -The panel's `GET /v1/models` uses the client key server-side; other client routes -and methods receive no injected key on the admin hostname. - -The service pins CLIProxyAPI v8.0.32 and the management panel v1.26.5. Panel -downloads are size- and SHA-256-checked during preparation; runtime auto-update -is disabled. Preparation patches session restoration, removes auth-store -persistence, permits empty-key revalidation, and redirects Logout; the resulting -asset has its own pinned checksum. Native login still establishes authentication only after its config -request succeeds. Request-body logging is omitted by `commercial-mode: true` and -`request-log: false`. Plugins, discovery, usage aggregation, and profiling start -disabled. The read-only image has only `/data` and a private `/tmp` writable; -the pinned `/data/static` subdirectory is mounted read-only separately. - -Run `python3 tools/cliproxy-test.py --nginx /path/to/nginx --panel /path/to/upstream-management.html` -with the pinned upstream v1.26.5 asset for bootstrap, rendering, pinned-asset, -actual HTTP/streaming/WebSocket boundaries, and the native auth-store flow. -The test uses temporary synthetic credentials and a loopback fixture upstream. -Stage with the normal deployment command, verify anonymous management is routed -to Snow, valid/invalid client-key behavior, provider persistence on restart, -and logs containing no fixture keys or prompts before production promotion. +`https://cpa.paperclover.net` serves the Snow Globe CPA fork’s inference and management +APIs. Caddy protects the dashboard with Snow's `infra-admin` role. The +`cpa-api.paperclover.net` alias serves the same native APIs, with dashboard +paths disabled. + +CPA validates its two independent keys: + +- The client `api_key` authenticates inference and model listing through + `Authorization: Bearer …` or the provider-compatible key headers. +- The `management_key` authenticates `/v8/management/*` through + `Authorization: Bearer …` or `X-Management-Key`. + +Nomad variable `nomad/jobs/cliproxy` owns both keys. Keep their values in +restricted files or a password manager, never in chat, arguments, or tracked +configuration. An inference key does not grant management access. A management +key can modify configuration and use stored provider credentials. + +For OpenAI-compatible clients, use `https://cpa.paperclover.net/v1`; for +Claude-compatible clients, use `https://cpa.paperclover.net`. Management callers +use `https://cpa.paperclover.net/v8/management` with the management key. Native +requests carrying a key bypass SSO; CPA rejects missing or invalid credentials. +The dashboard still requires SSO even when a management key is supplied. + +## Dashboard + +Snow sign-in opens the panel automatically. Caddy supplies the management key +only after an `infra-admin` SSO check, and the client key for the panel's model +list. Cookie-authenticated writes require the exact dashboard Origin; reads +reject a foreign Origin. Keys stay on the server, and cookies are removed before +proxying to CPA. Native requests with explicit key headers retain CPA's own +authentication and do not receive injected credentials. + +Opening the panel clears remembered native passwords and validates access +through the config API. Logout clears the shared forward-auth cookie and opens +Snow sign-in. The source panel implements this SSO adaptation; the image contains +one immutable panel build. + +## Providers and persistence + +Use the panel's OAuth section to connect providers. For a remote flow ending at +an unreachable localhost URL, submit the entire callback URL through the panel's +callback form. Treat it as a temporary credential. API-key providers can also be +configured in the native panel. + +`/data/config.yaml` is seeded once from a private Nomad template. Configuration, +OAuth accounts, and refreshed tokens persist in `/srv/prod/cliproxy/data`. +The first fork start atomically migrates the native configuration, preserving +provider credentials and unrelated settings. It selects reset-soonest routing, +affinity with priority reclaim, retained history, Codex-only model discovery, +and saved cooldowns; panel auto-updates and request logging stay disabled. +`/data/.snowglobe-fork-v1` makes this a one-time migration, so later operator +changes survive restarts. The container publishes one +CPA port through Nomad's loopback binding; Caddy is the only public entry point. +There is no nginx container or second CPA listener. + +Changing a Nomad key does not overwrite existing native configuration. Rotate it +in CPA and Nomad together; Caddy's private route manifest is refreshed during +provisioning. Fresh previews have separate storage and keys and must use +synthetic providers only. Shared forward-auth does not register arbitrary preview +callback URLs; a fresh preview cannot complete interactive sign-in through it. + +## Fork sources and history + +[Backend](https://shale.paperclover.net/cliproxyapi/) and +[panel](https://shale.paperclover.net/cliproxy-panel/) fork CPA v8.0.32 and panel +v1.26.5. `service/cliproxy/build-source.json` pins the exact source revisions; +the deployment exporter builds both into one image using +`Dockerfile.snowglobe`. Upstream panel auto-update cannot replace the baked file. +Cursor's private agent protocol is excluded. + +`observability.history.enabled` enables retained monthly JSON usage and quota +files under `/data/history`, beside the auth directory. They contain token +counts, provider/model, hashed sessions and hashed account identities, never +prompts. Quota is sampled each minute; idle subscription usage is polled every +ten minutes. Usage-plan calls share a two-minute cache, with up to thirty minutes +of stale successful data after a 429. The sample retains its original observation +time so stale data cannot masquerade as fresh quota. + +`GET /v8/management/history/{usage,quota,accounts}` reads history; `since` accepts +an RFC3339 timestamp. Accounts are masked with a unique suffix; `reveal=true` +reveals email addresses to an authenticated management caller. `POST` to usage or +quota accepts up to 10,000 bounded rows; stable row IDs make repeated imports +idempotent. `tools/export-history.py` in the backend exports only transcript +paths explicitly supplied by the operator, requires a cutoff, and never exports +message contents. Do not point it at personal transcripts without authorization. + +API-equivalent estimates use models.dev, then LiteLLM, then explicit +`observability.history.prices` overrides (USD per million tokens: `input`, +`output`, `cache-read`, `cache-write`). Catalogs are cached for a day; overrides +apply immediately to new records and imports. Missing catalog cache rates fall back to +input rates; explicit zero rates remain zero. Estimates are neither subscription +bills nor guaranteed provider charges. Existing retained rows keep the price +observed when they were recorded. Restart after enabling history on an instance +that started with it disabled. + +## Deployment and checks + +Stage with `python3 tools/deploy.py stage cliproxy`. A preview retains its own +configuration/history across updates and never promotes that data to production. +Production uses `python3 tools/deploy.py prod` from a described, conflict-free +main. The fork closes its listener before draining active requests for up to +25 seconds, flushes usage, and exits within Nomad's 30-second kill timeout. +Caddy retries new connections briefly and preserves existing WebSockets across +route reloads. A single writer still has a short replacement window; a turn +longer than the drain budget can be interrupted. + +Run `python3 tools/cliproxy-test.py --caddy /path/to/caddy` for native key +separation, SSO roles, dashboard protection, Origin checks, streams, WebSockets +including Caddy reloads, and configuration preservation. It uses disposable +HTTP fixtures, synthetic keys, and a separate Caddy process. +`python3 tools/deploy-test.py` covers composed source exports. The backend uses +`go test ./...` and `go build ./cmd/server`; the panel uses `bun run verify`. diff --git a/tools/deploy-test.py b/tools/deploy-test.py index a7809f6eed9c8f7a8804019d68a2fe93d3a86994..b569b48499e2b599badf06ac9e6be3ab1c5430be 100644 --- a/tools/deploy-test.py +++ b/tools/deploy-test.py @@ -154,6 +154,23 @@ class BuildSourceExport(unittest.TestCase): self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") self.assertFalse((context / "src/app.dockerfile").exists()) + def test_composed_sources_export_pinned_files_under_distinct_destinations(self): + self.manifest.write_text(json.dumps({"sources": [self.spec, { + "source": "../source", "revision": self.commit, + "destination": "panel", "include": ["run.sh", "lib"], + }]})) + context = self.remote / "releases" / deploy.upload() / "service/app/build" + self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n") + self.assertEqual((context / "panel/lib/committed").read_text(), "committed library") + self.assertFalse((context / "panel/lib/new").exists()) + for target in ("../escape", "/absolute", "."): + self.manifest.write_text(json.dumps({"sources": [self.spec, { + "source": "../source", "revision": self.commit, + "destination": target, "include": ["run.sh"], + }]})) + with self.assertRaisesRegex(ValueError, "invalid build source destination"): + deploy.upload() + def test_unpinned_source_uses_working_files(self): del self.spec["revision"] self.manifest.write_text(json.dumps(self.spec)) diff --git a/tools/deploy.py b/tools/deploy.py index 05c1dde02626f00ca24e0a1ad27344c1256f875a..c7d5c92b49daf0f40c3334770aa8069cc032f43e 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -88,48 +88,59 @@ def upload(main=False): ) for manifest in snapshot.glob("service/*/build-source.json"): spec = json.loads(manifest.read_text()) - source = (REPO / spec["source"]).resolve(strict=True) - if not source.is_dir() or not source.is_relative_to(REPO.parent): - raise ValueError(f"invalid build source: {manifest.parent.name}") context = manifest.parent / "build" if context.exists(): raise ValueError(f"build context already exists: {manifest.parent.name}") context.mkdir() - paths = [*spec["include"], spec["dockerfile"]] - for name in paths: - relative = Path(name) - if relative.is_absolute() or ".." in relative.parts or not relative.parts: - raise ValueError(f"invalid build source path: {name}") - if "revision" in spec: - commit = spec["revision"] - if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit): - raise ValueError(f"invalid build source revision: {manifest.parent.name}") - export_revision(source, commit, paths, context) + for index, source_spec in enumerate(spec.get("sources", [spec])): + source = (REPO / source_spec["source"]).resolve(strict=True) + if not source.is_dir() or not source.is_relative_to(REPO.parent): + raise ValueError(f"invalid build source: {manifest.parent.name}") + target = Path(source_spec.get("destination", ".")) + if target.is_absolute() or ".." in target.parts or (index and not target.parts): + raise ValueError(f"invalid build source destination: {target}") + source_context = context / target + if source_context != context: + source_context.mkdir(parents=True, exist_ok=False) + dockerfile_name = source_spec.get("dockerfile") + paths = [*source_spec["include"], *([dockerfile_name] if dockerfile_name else [])] for name in paths: - if not (context / name).exists(): - raise ValueError(f"missing build source path at revision: {name}") - dockerfile = context / spec["dockerfile"] - if not dockerfile.is_file(): - raise ValueError(f"invalid build source Dockerfile: {spec['dockerfile']}") - if dockerfile != context / "Dockerfile": - if (context / "Dockerfile").exists(): - raise ValueError("build source already includes Dockerfile") - if any(dockerfile.is_relative_to(context / name) for name in spec["include"]): - shutil.copy2(dockerfile, context / "Dockerfile") + relative = Path(name) + if relative.is_absolute() or ".." in relative.parts or not relative.parts: + raise ValueError(f"invalid build source path: {name}") + if "revision" in source_spec: + commit = source_spec["revision"] + if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit): + raise ValueError(f"invalid build source revision: {manifest.parent.name}") + export_revision(source, commit, paths, source_context) + for name in paths: + if not (source_context / name).exists(): + raise ValueError(f"missing build source path at revision: {name}") + if dockerfile_name: + dockerfile = source_context / dockerfile_name + if not dockerfile.is_file(): + raise ValueError(f"invalid build source Dockerfile: {dockerfile_name}") + if dockerfile != source_context / "Dockerfile": + if (source_context / "Dockerfile").exists(): + raise ValueError("build source already includes Dockerfile") + if any(dockerfile.is_relative_to(source_context / name) for name in source_spec["include"]): + shutil.copy2(dockerfile, source_context / "Dockerfile") + else: + dockerfile.rename(source_context / "Dockerfile") + continue + for name in paths: + relative = Path(name) + origin = source / relative + if not origin.resolve(strict=True).is_relative_to(source): + raise ValueError(f"build source escapes context: {name}") + destination = source_context / ("Dockerfile" if name == dockerfile_name else name) + destination.parent.mkdir(parents=True, exist_ok=True) + if origin.is_dir(): + shutil.copytree(origin, destination, symlinks=True) else: - dockerfile.rename(context / "Dockerfile") - continue - for name in paths: - relative = Path(name) - origin = source / relative - if not origin.resolve(strict=True).is_relative_to(source): - raise ValueError(f"build source escapes context: {name}") - destination = context / ("Dockerfile" if name == spec["dockerfile"] else name) - destination.parent.mkdir(parents=True, exist_ok=True) - if origin.is_dir(): - shutil.copytree(origin, destination, symlinks=True) - else: - shutil.copy2(origin, destination) + shutil.copy2(origin, destination) + if not (context / "Dockerfile").is_file(): + raise ValueError(f"missing build source Dockerfile: {manifest.parent.name}") digest = tree_digest(snapshot) release = digest[:16] remote_release = REMOTE / "releases" / release diff --git a/tools/router.py b/tools/router.py index 113adcfb37a50a2b7b3277ac696a21a991be5306..c884ff71dd3591c9f32d68e84d10e0ba4333fda9 100644 --- a/tools/router.py +++ b/tools/router.py @@ -40,6 +40,55 @@ def proxy(upstreams, indent, uncompressed=False, host=None, keep_streams=False): return [*lines, f"{indent}}}"] +def cliproxy_routes(host, upstreams, auth, keys, role): + native = [" request_header -Cookie", *proxy(upstreams, " ", keep_streams=True)] + if not role: + return [" @dashboard path / /management.html /sign-out /snow.oauth2/*", + " respond @dashboard 404", " request_header -Cookie", *proxy(upstreams, " ", keep_streams=True)] + if any(not re.fullmatch(r"[0-9a-f]{64}", keys.get(name, "")) for name in ("api_key", "management_key")): + raise ValueError("missing CLIProxyAPI gateway credentials") + return [ + " route {", + " handle /snow.oauth2/* {", *proxy(auth, " "), " }", + " @credentials {", + " not path / /management.html /sign-out /snow.oauth2/*", + ' expression `{http.request.header.Authorization} != "" || {http.request.header.X-Management-Key} != "" || {http.request.header.X-Api-Key} != "" || {http.request.header.X-Goog-Api-Key} != ""`', + " }", + " handle @credentials {", *native, " }", + " @session path / /management.html /sign-out /v0/management/* /v8/management/* /v1/models", + " handle @session {", + f" reverse_proxy {auth} {{", + " method GET", " rewrite /snow.oauth2/auth", + " header_up X-Forwarded-Method {method}", " header_up X-Forwarded-Uri {uri}", + " @unauthorized status 401", + " handle_response @unauthorized {", + " @dashboard path / /management.html /sign-out", + " route {", + " redir @dashboard /snow.oauth2/sign_in?rd={scheme}://{host}{uri}", + " respond 401", " }", " }", + f" @allowed header X-Auth-Request-Groups *role:{role}*", + " handle_response @allowed {", + " method {method}", " rewrite {uri}", + " @foreign_origin {", " header Origin *", f" not header Origin https://{host}", " }", + " @unsafe_origin {", " method POST PUT PATCH DELETE", f" not header Origin https://{host}", " }", + " route {", + " respond @foreign_origin 403", " respond @unsafe_origin 403", + " redir / /management.html 302", + f" redir /sign-out /snow.oauth2/sign_out?rd=https%3A%2F%2Fsnowglobe.{os.environ['SNOWGLOBE_DOMAIN']}%2Fsign-in 303", + " @management path /v0/management/* /v8/management/*", + f' request_header @management Authorization "Bearer {keys["management_key"]}"', + " @models path /v1/models", + f' request_header @models Authorization "Bearer {keys["api_key"]}"', + " request_header -Cookie", + f" reverse_proxy {upstreams} {{", + " lb_try_duration 5s", " stream_close_delay 8h", + " header_down -Access-Control-Allow-Origin", ' header_down Cache-Control "no-store"', + " }", " }", " }", + " handle_response {", " respond 403", " }", " }", " }", + " handle {", *native, " }", " }", + ] + + def shale_mcp_routes(port): return [" @shale_mcp_link path_regexp shale_mcp_link ^/-/snowglobe-mcp/([A-Za-z0-9_-]{43})$", " handle @shale_mcp_link {", " rewrite * /oauth/shale/link/{re.shale_mcp_link.1}", @@ -288,6 +337,12 @@ def render(token): service = next(iter(route["services"])) if not re.fullmatch(r"[a-z][a-z0-9-]*", service): raise ValueError(f"invalid service name: {service}") + if service == "cliproxy" or re.fullmatch(r"cliproxy-preview-[0-9a-f]{8}", service): + with open(os.path.join(ROUTE_DIR, service + ".json")) as source: + keys = json.load(source).get("cliproxy", {}) + lines += [*cliproxy_routes(host, " ".join(sorted(route["upstreams"])), + " ".join(sorted(auth_upstreams)), keys, role), "}"] + continue if service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service): # Keep the Origin guard before every static/proxy handle; # otherwise Caddy sorts those handles ahead of respond. diff --git a/tools/snowglobe.py b/tools/snowglobe.py index 154b757764ae43f5a0469438c95f8ca9a496eec2..fdd3132fb74edadeed2492c5a70849d36d81dc6f 100644 --- a/tools/snowglobe.py +++ b/tools/snowglobe.py @@ -373,6 +373,8 @@ def render(data, definitions): ] for task_name, task in data["containers"].items(): lines += [f" task {q(task_name)} {{", ' driver = "podman"'] + if task.get("killTimeout"): + lines.append(f" kill_timeout = {q(task['killTimeout'])}") if task["lifecycle"] != "main": lines += [' lifecycle {', ' hook = "prestart"', f" sidecar = {str(task['lifecycle'] == 'prestartSidecar').lower()}", ' }'] @@ -619,6 +621,8 @@ def provision(data): write_private(route_dir / f"{data['id']}.json", json.dumps({ "files": static, "dirs": directories, "identity": identity, "identityProof": proof, "headHtml": head_html, "metricsPaths": metrics_paths, + **({"cliproxy": {name: values[name] for name in ("api_key", "management_key")}} + if data["sourceId"] == "cliproxy" else {}), }))