authoravatarclover caruso<git@paperclover.net>2026-10-28 22:14:54-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-12 16:08:55-08:00
log8b83b426422ffbcea026852a96e781de7616fd8f
tree73195b1e9ff501242040847f315b6cb9206bd699
parent6ea9559d597852c84d29e625b048170ec1826660
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Deploy the Snow Globe CLIProxyAPI and panel forks

Serve native inference and management through Caddy with separate keys and an SSO-protected panel. Build pinned Clover-owned forks with reset-soonest routing, priority reclaim, truthful limit responses, permanent quota/usage history and forecasts. Preserve provider configuration through a one-time migration. Drain active requests and flush usage within Nomad’s shutdown budget; retain WebSockets across Caddy reloads. Verified full Go tests/build, 1,643 panel tests/lint/build, deployment export and Caddy tests, mobile/desktop UX reviews, and staged synthetic inference, streaming, import idempotence, pricing and restart persistence. Assisted-by: gpt-6.1-sol

15 files changed, 461 insertions(+), 732 deletions(-)

config/Service.pkl+1
......@@ -84,6 +84,7 @@ class Container {
8484 args: Listing<String> = new {}
8585 capAdd: Listing<String> = new {}
8686 readOnlyRoot: Boolean = false
87 killTimeout: String?
8788 devices: Listing<String> = new {}
8889 extraHosts: Listing<String> = new {}
8990 hostNetwork: Boolean = false
service/cliproxy/Dockerfile deleted-3
......@@ -1,3 +0,0 @@
1FROM docker.io/eceasy/cli-proxy-api@sha256:c08d02fbd94927708721afd059bff9ff36a04d9cedb4d69f2c2671aba8239031 AS proxy
2FROM docker.io/library/nginx@sha256:17ae566734b63632e543c907ba74757e0c1a25d812ab9f10a07a6bed98dd199c
3COPY --from=proxy /CLIProxyAPI/CLIProxyAPI /CLIProxyAPI/CLIProxyAPI
service/cliproxy/build-source.json created+30
......@@ -0,0 +1,30 @@
1{
2 "sources": [
3 {
4 "source": "../cliproxyapi",
5 "revision": "91b6ce009bc93dbefd319e7a5b84812f333363ba",
6 "dockerfile": "Dockerfile.snowglobe",
7 "include": [
8 "go.mod",
9 "go.sum",
10 "cmd",
11 "internal",
12 "sdk"
13 ]
14 },
15 {
16 "source": "../cliproxy-panel",
17 "revision": "c355dc7b07797c42e8cb7e191e3b176b5442da37",
18 "destination": "management",
19 "include": [
20 "package.json",
21 "bun.lock",
22 "index.html",
23 "tsconfig.json",
24 "tsconfig.node.json",
25 "vite.config.ts",
26 "src"
27 ]
28 }
29 ]
30}
service/cliproxy/nginx.conf deleted-113
......@@ -1,113 +0,0 @@
1worker_processes 1;
2pid /tmp/nginx.pid;
3error_log /dev/stderr crit;
4events { worker_connections 512; }
5http {
6 access_log off;
7 server_tokens off;
8 absolute_redirect off;
9 map_hash_bucket_size 128;
10 server_names_hash_bucket_size 128;
11 client_body_temp_path /tmp/client;
12 proxy_temp_path /tmp/proxy;
13 fastcgi_temp_path /tmp/fastcgi;
14 uwsgi_temp_path /tmp/uwsgi;
15 scgi_temp_path /tmp/scgi;
16 map $http_upgrade $connection_upgrade { default upgrade; '' close; }
17 map "$request_method:$uri" $client_route {
18 default 0;
19 ~^GET:/v1/models(?:/[^/]+)?$ 1;
20 ~^POST:/v1/(?:chat/completions|completions|messages(?:/count_tokens)?|responses(?:/compact)?|alpha/search)$ 1;
21 "GET:/v1/responses" 1;
22 ~^POST:/backend-api/codex/(?:responses(?:/compact)?|alpha/search)$ 1;
23 "GET:/backend-api/codex/responses" 1;
24 }
25 map "$request_method:$uri" $browser_oauth_callback {
26 default 0;
27 ~^(?:GET|HEAD):/(?:v0/management/oauth-callback|v8/management/oauth/callback|(?:anthropic|codex|antigravity|devin)/callback|callback)/?$ 1;
28 }
29 map "$request_method:$http_origin" $admin_origin {
30 default 0;
31 "GET:" 1;
32 "HEAD:" 1;
33 "GET:https://ADMIN_HOST" 1;
34 "HEAD:https://ADMIN_HOST" 1;
35 "POST:https://ADMIN_HOST" 1;
36 "PUT:https://ADMIN_HOST" 1;
37 "PATCH:https://ADMIN_HOST" 1;
38 "DELETE:https://ADMIN_HOST" 1;
39 }
40 {{ with nomadVar "NOMAD_VARIABLE" }}
41 map $http_authorization $bearer_key { default 0; "Bearer {{ .api_key.Value }}" 1; }
42 map $http_x_api_key $claude_key { default 0; "{{ .api_key.Value }}" 1; }
43 map $client_key $upstream_authorization { default ""; 1 "Bearer {{ .api_key.Value }}"; }
44 map "$request_method:$uri" $admin_authorization {
45 default "";
46 ~^[A-Z]+:/v(?:0|8)/management/ "Bearer {{ .management_key.Value }}";
47 "GET:/v1/models" "Bearer {{ .api_key.Value }}";
48 }
49 {{ end }}
50 map "$bearer_key:$claude_key" $client_key { default 1; "0:0" 0; }
51 upstream cliproxy_backend {
52 server 127.0.0.1:8317;
53 }
54 server {
55 listen 8080 default_server;
56 server_name _;
57 location = /healthz { proxy_pass http://cliproxy_backend; }
58 location / { return 404; }
59 }
60 server {
61 listen 8080;
62 server_name ADMIN_HOST;
63 client_max_body_size 32m;
64 location = / { return 302 /management.html; }
65 location = /sign-out { return 303 /snow.oauth2/sign_out?rd=https%3A%2F%2FSSO_HOST%2Fsign-in; }
66 location / {
67 if ($browser_oauth_callback = 1) { return 404; }
68 if ($arg_code != "") { return 404; }
69 if ($admin_origin = 0) { return 403; }
70 proxy_pass http://cliproxy_backend;
71 proxy_http_version 1.1;
72 proxy_set_header Host $host;
73 proxy_set_header Authorization $admin_authorization;
74 proxy_set_header X-Management-Key "";
75 proxy_set_header Cookie "";
76 proxy_hide_header Access-Control-Allow-Origin;
77 add_header Cache-Control no-store always;
78 proxy_buffering off;
79 proxy_read_timeout 3600s;
80 }
81 }
82 server {
83 listen 8080;
84 server_name API_HOST;
85 client_max_body_size 32m;
86 location = /healthz { proxy_pass http://cliproxy_backend; }
87 location = / { return 404; }
88 location / {
89 if ($client_route = 0) { return 404; }
90 if ($client_key = 0) { return 401; }
91 if ($arg_key != "") { return 400; }
92 if ($arg_auth_token != "") { return 400; }
93 proxy_pass http://cliproxy_backend$uri$is_args$args;
94 proxy_http_version 1.1;
95 proxy_set_header Host $host;
96 proxy_set_header Upgrade $http_upgrade;
97 proxy_set_header Connection $connection_upgrade;
98 proxy_set_header Cookie "";
99 proxy_set_header Authorization $upstream_authorization;
100 proxy_set_header X-Api-Key "";
101 proxy_set_header X-Goog-Api-Key "";
102 proxy_set_header X-Management-Key "";
103 proxy_set_header X-Forwarded-For "";
104 proxy_set_header X-Forwarded-Host "";
105 proxy_set_header X-Forwarded-Proto "";
106 proxy_buffering off;
107 proxy_request_buffering off;
108 proxy_cache off;
109 proxy_read_timeout 3600s;
110 proxy_send_timeout 3600s;
111 }
112 }
113}
service/cliproxy/panel-test.mjs deleted-110
......@@ -1,110 +0,0 @@
1import assert from 'node:assert/strict';
2import { readFileSync } from 'node:fs';
3import { request } from 'node:http';
4import vm from 'node:vm';
5
6const [panelPath, origin, host] = process.argv.slice(2);
7const panel = readFileSync(panelPath, 'utf8');
8function between(start, end) {
9 const offset = panel.indexOf(start);
10 assert(offset >= 0, 'Pinned native code entry exists');
11 const limit = panel.indexOf(end, offset);
12 assert(limit > offset, 'Pinned native code exit exists');
13 return panel.slice(offset, limit);
14}
15const nativeStore = between('var yc=e=>', 'function Tc()');
16const nativeStorage = 'var ' + between('_l=`enc::v1::`', 'function Al(e,t)');
17const nativeAuth = 'var Pm=null,' + between('Fm=wc()(', '));typeof window') + '));';
18const nativeRoute = between('function tse({children:e})', 'function nse()');
19
20for (const [remembered, rejected] of [[false, false], [true, false], [true, true]]) {
21 const storage = new Map();
22 const requests = [];
23 const effects = [];
24 const location = { origin, host: new URL(origin).host, assign: value => requests.push(value) };
25 const localStorage = {
26 getItem: key => storage.get(key) ?? null,
27 setItem: (key, value) => storage.set(key, String(value)),
28 removeItem: key => storage.delete(key),
29 };
30 let connection;
31 let revision = 0;
32 const context = vm.createContext({
33 window: { location, localStorage }, localStorage,
34 navigator: { userAgent: 'synthetic-native-auth-test' },
35 TextEncoder, TextDecoder, Uint8Array, btoa, atob, console,
36 y: {
37 useSyncExternalStore: (_, snapshot) => snapshot(),
38 useCallback: callback => callback,
39 useDebugValue() {},
40 useState: value => [value, () => {}],
41 useEffect: callback => effects.push(callback),
42 },
43 dr: () => ({ pathname: '/' }),
44 H: { jsx: (type, props) => ({ type, props }) },
45 ti: 'redirect', tO: 'loading',
46 Bc: 'cli-proxy-auth',
47 tp: value => value.replace(/\/+$/, ''),
48 G: {
49 setConfig: value => { connection = value; revision++; },
50 getConnectionRevision: () => revision,
51 },
52 Vp: { getState: () => ({
53 clearCache() {},
54 async fetchConfig() {
55 assert(connection.apiBase === origin, 'Restore uses the current origin');
56 assert(connection.managementKey === '', 'Browser sends no management secret');
57 requests.push(connection.apiBase);
58 return new Promise((resolve, reject) => {
59 const pending = request(connection.apiBase + '/v8/management/config', {
60 headers: { Host: host, ...(rejected ? { 'X-Synthetic-Reject': 'yes' } : {}) },
61 }, response => {
62 let body = '';
63 response.setEncoding('utf8');
64 response.on('data', chunk => { body += chunk; });
65 response.on('end', () => {
66 if (response.statusCode !== 200) reject(Object.assign(new Error('Synthetic backend rejection'), { status: response.statusCode }));
67 else resolve(JSON.parse(body));
68 });
69 });
70 pending.on('error', reject);
71 pending.end();
72 });
73 },
74 }) },
75 sm: { getState: () => ({ clearCache() {} }) },
76 jm: { getState: () => ({ clearQuotaCache() {} }) },
77 });
78 vm.runInContext(nativeStore + nativeStorage, context);
79 if (remembered) {
80 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);
81 }
82 vm.runInContext(nativeAuth + nativeRoute, context);
83 const auth = context.Fm;
84 assert(!auth.getState().isAuthenticated, 'Stored credentials do not establish authentication');
85 assert(auth.getState().managementKey === '', 'Remembered password never hydrates into the native store');
86 assert(auth.getState().apiBase === '', 'Remembered connection never hydrates into the native store');
87 const route = vm.runInContext('tse({children:"protected"})', context);
88 for (const effect of effects) effect();
89 assert(route.type === 'redirect' && route.props.to === '/login', 'Protected route enters the SSO restore flow');
90 assert(requests.length === 0, 'Protected route never revalidates a remembered password');
91 const pending = auth.getState().restoreSession();
92 assert(!auth.getState().isAuthenticated, 'Authentication waits for the real config response');
93 assert(localStorage.getItem('managementKey') === null, 'Legacy password is removed before the request');
94 assert(localStorage.getItem('cli-proxy-auth') === null, 'Persisted password is removed before the request');
95 assert(await pending === !rejected, 'Restore follows the backend result');
96 assert(await auth.getState().restoreSession() === !rejected, 'Concurrent restore shares the validated result');
97 assert(requests.length === 1, 'Restore validates once');
98 assert(auth.getState().isAuthenticated === !rejected, 'Only the successful backend response authenticates');
99 assert(auth.getState().connectionStatus === (rejected ? 'error' : 'connected'), 'Native connection status follows the backend');
100 assert(auth.getState().managementKey === '', 'Native state retains no password');
101 assert(!auth.getState().rememberPassword, 'Native password persistence is disabled');
102 assert(localStorage.getItem('isLoggedIn') === null, 'No persisted authenticated flag is needed');
103 assert(localStorage.getItem('cli-proxy-auth') === null, 'Native auth state remains transient');
104 if (!rejected) assert(await auth.getState().checkAuth(), 'Native revalidation accepts SSO without a browser key');
105 const logout = panel.match(/onClick:(\(\)=>window\.location\.assign\([^)]*\)),title:e\(`header\.logout`\)/);
106 assert(logout, 'Native logout uses the SSO exit');
107 vm.runInContext('(' + logout[1] + ')()', context);
108 assert(requests.at(-1) === '/sign-out', 'Logout reaches the fixed server redirect');
109}
110console.log('Pinned native auth store: fresh, remembered, rejected, revalidation, and logout passed');
service/cliproxy/prepare.py deleted-74
......@@ -1,74 +0,0 @@
1#!/usr/bin/env python3
2import hashlib
3import json
4import os
5from pathlib import Path
6import re
7import stat
8import sys
9import tempfile
10import urllib.request
11
12
13PANEL_URL = "https://github.com/router-for-me/Cli-Proxy-API-Management-Center/releases/download/v1.26.5/management.html"
14PANEL_SHA256 = "57558cd023475674634a114824b014d3e5ac161f6f3791e3d86b6adab962a700"
15PANEL_SIZE = 3312180
16PANEL_SSO_SHA256 = "fdb185df6395bb6652af7171d7193be9a568d027d66d5cd0106b797494f5b4f0"
17
18
19def prepare(data):
20 root = Path(data["hostRoot"]) / "data"
21 static = root / "static"
22 panel = static / "management.html"
23 if root.is_symlink() or static.is_symlink() or panel.is_symlink():
24 raise ValueError("CLIProxyAPI panel paths must not be symbolic links")
25 static.mkdir(parents=True, exist_ok=True)
26 if panel.exists():
27 metadata = panel.stat()
28 if not stat.S_ISREG(metadata.st_mode):
29 raise ValueError("CLIProxyAPI panel must be a regular file")
30 if metadata.st_size <= PANEL_SIZE and hashlib.sha256(panel.read_bytes()).hexdigest() == PANEL_SSO_SHA256:
31 panel.chmod(0o444)
32 return
33 with urllib.request.urlopen(PANEL_URL, timeout=60) as response:
34 contents = response.read(PANEL_SIZE + 1)
35 if len(contents) != PANEL_SIZE or hashlib.sha256(contents).hexdigest() != PANEL_SHA256:
36 raise ValueError("CLIProxyAPI management panel failed checksum verification")
37 contents, count = re.subn(
38 rb"restoreSession:\(\)=>Pm\|\|\(Pm=\(async\(\)=>\{.*?\}\)\(\),Pm\),login:async",
39 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',
40 contents,
41 )
42 if count != 1:
43 raise ValueError("CLIProxyAPI session restoration seam changed")
44 for before, after in [
45 (b'Fm=wc()(Xc(', b'Fm=wc()('),
46 (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})}));',
47 b'}));'),
48 (b'checkAuth:async()=>{let{managementKey:n,apiBase:r}=t();if(!n||!r)return!1;',
49 b'checkAuth:async()=>{let{managementKey:n,apiBase:r}=t();if(!r)return!1;'),
50 (b'onClick:r,title:e(`header.logout`)',
51 b'onClick:()=>window.location.assign(`/sign-out`),title:e(`header.logout`)'),
52 ]:
53 if contents.count(before) != 1:
54 raise ValueError("CLIProxyAPI management panel authentication seam changed")
55 contents = contents.replace(before, after)
56 if hashlib.sha256(contents).hexdigest() != PANEL_SSO_SHA256:
57 raise ValueError("CLIProxyAPI SSO panel failed checksum verification")
58 with tempfile.NamedTemporaryFile(dir=static, prefix=".panel-", delete=False) as file:
59 pending = Path(file.name)
60 try:
61 file.write(contents)
62 os.fchmod(file.fileno(), 0o444)
63 os.fchown(file.fileno(), data["uid"], data["uid"])
64 file.flush()
65 os.fsync(file.fileno())
66 pending.replace(panel)
67 finally:
68 pending.unlink(missing_ok=True)
69 os.chown(static, data["uid"], data["uid"])
70 static.chmod(0o755)
71
72
73if __name__ == "__main__":
74 prepare(json.load(sys.stdin))
service/cliproxy/service.pkl+10-14
......@@ -1,13 +1,11 @@
11amends "../../config/Service.pkl"
22
33import "../../config/site.pkl" as site
4import "../../config/OpenID.pkl" as sso
54
65name = "CLIProxyAPI"
76tagline = "Codex and Claude API access"
87access = "infra-admin"
98stageIsolation = "fresh"
10prepare = "prepare.py"
119
1210secrets {
1311 ["api_key"] {}
......@@ -15,13 +13,14 @@ secrets {
1513}
1614
1715container {
18 build = "."
19 entrypoint = "/bin/bash"
16 build = "build"
17 killTimeout = "30s"
18 entrypoint = "/bin/sh"
2019 args { "/snowglobe/start.sh" }
2120 readOnlyRoot = true
2221 tmpfs { "/tmp:rw,nosuid,nodev,noexec,size=64m" }
2322 http {
24 containerPort = 8080
23 containerPort = 8317
2524 subdomain = "cpa"
2625 authRole = "infra-admin"
2726 checkPath = "/healthz"
......@@ -29,13 +28,13 @@ container {
2928 }
3029 volumes {
3130 ["/data"] {}
32 ["/data/static"] { readOnly = true }
3331 ["/snowglobe/start.sh"] { config = "start.sh" }
3432 }
3533 env {
3634 ["HOME"] = "/data"
3735 ["WRITABLE_PATH"] = "/data"
38 ["MANAGEMENT_STATIC_PATH"] = "/data/static"
36 ["MANAGEMENT_STATIC_PATH"] = "/CLIProxyAPI/static"
37 ["CLIPROXY_SHUTDOWN_DRAIN"] = "25s"
3938 ["TZ"] = "America/Los_Angeles"
4039 }
4140 secretFiles {
......@@ -43,19 +42,16 @@ container {
4342 {{ with nomadVar "nomad/jobs/\(module.id)" }}
4443 {
4544 "config-version": 8,
46 "server": {"host": "127.0.0.1", "port": 8317, "commercial-mode": true, "max-request-body-bytes": 33554432, "discovery": {"enabled": false}},
45 "server": {"host": "0.0.0.0", "port": 8317, "commercial-mode": true, "max-request-body-bytes": 33554432, "discovery": {"enabled": false}},
4746 "access": {"api-keys": [{{ .api_key.Value | toJSON }}]},
4847 "management": {"allow-remote": true, "secret-key": {{ .management_key.Value | toJSON }}, "disable-auto-update-panel": true},
4948 "oauth": {"auth-dir": "/data/auth"},
49 "routing": {"strategy": "reset-soonest", "session-affinity": true, "session-affinity-reclaim": true, "session-affinity-ttl": "1h", "cooldown": {"save-cooldown-status": true}},
50 "client": {"codex": {"codex-models-only": true}},
5051 "plugins": {"enabled": false, "dir": "/data/plugins"},
51 "observability": {"logs": {"debug": false, "logging-to-file": false, "request-log": false}, "usage": {"usage-statistics-enabled": false}, "pprof": {"enable": false}}
52 "observability": {"history": {"enabled": true}, "logs": {"debug": false, "logging-to-file": false, "request-log": false}, "usage": {"usage-statistics-enabled": false}, "pprof": {"enable": false}}
5253 }
5354 {{ end }}
5455 """
55 ["/run/secrets/cliproxy-nginx.conf"] = read("nginx.conf").text
56 .replaceAll("NOMAD_VARIABLE", "nomad/jobs/\(module.id)")
57 .replaceAll("ADMIN_HOST", module.container.http.hostname!!)
58 .replaceAll("API_HOST", module.container.http.plainHostnames[0])
59 .replaceAll("SSO_HOST", sso.hostname)
6056 }
6157}
service/cliproxy/start.sh+6-10
......@@ -1,4 +1,4 @@
1#!/bin/bash
1#!/bin/sh
22set -eu
33umask 077
44
......@@ -18,12 +18,8 @@ test -s /data/config.yaml
1818chmod 600 /data/config.yaml
1919mkdir -p /data/auth /data/plugins
2020chmod 700 /data /data/auth /data/plugins
21/CLIProxyAPI/CLIProxyAPI --config /data/config.yaml &
22app=$!
23/usr/sbin/nginx -c /run/secrets/cliproxy-nginx.conf -g 'daemon off;' &
24gateway=$!
25trap 'kill "$app" "$gateway" 2>/dev/null || :; wait "$app" "$gateway" 2>/dev/null || :' EXIT
26trap 'exit 143' TERM
27trap 'exit 130' INT
28wait -n "$app" "$gateway"
29exit 1
21if [ ! -e /data/.snowglobe-fork-v1 ]; then
22 /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml --fork-migrate
23 touch /data/.snowglobe-fork-v1
24fi
25exec /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml
service/shale/service.pkl+1-4
......@@ -8,10 +8,7 @@ requirements {
88 new sso.Client {
99 clientId = module.id
1010 name = module.name
11 redirectUris {
12 "https://\(module.container.http.hostname)/-/callback"
13 "http://git1.localhost/-/callback"
14 }
11 redirectUris { "https://\(module.container.http.hostname)/-/callback" }
1512 usernameAliases { ["snow"] = "clover" }
1613 allowGuests = true
1714 usernameRequired = true
tools/cliproxy-test.py+178-299
......@@ -5,8 +5,6 @@ from contextlib import contextmanager
55import hashlib
66import http.client
77from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
8import importlib.util
9import io
108import json
119import os
1210from pathlib import Path
......@@ -20,14 +18,11 @@ import unittest
2018from unittest.mock import patch
2119
2220import snowglobe
21import router
2322
2423
2524SERVICE = Path(__file__).resolve().parents[1] / "service/cliproxy"
26spec = importlib.util.spec_from_file_location("cliproxy_prepare", SERVICE / "prepare.py")
27prepare = importlib.util.module_from_spec(spec)
28spec.loader.exec_module(prepare)
29NGINX = None
30PANEL = None
25CADDY = None
3126API_KEY = "a1" * 32
3227MANAGEMENT_KEY = "b2" * 32
3328ADMIN_HOST = "cliproxy-preview-12345678.paperclover.net"
......@@ -66,18 +61,17 @@ class ServiceTests(unittest.TestCase):
6661 rendered = snowglobe.render(self.data, {"cliproxy": self.data})
6762 self.assertEqual(rendered.count('user = "3150:3150"'), 1)
6863 self.assertEqual(rendered.count('readonly_rootfs = true'), 1)
69 self.assertEqual(rendered.count('perms = "0400"'), 2)
64 self.assertEqual(rendered.count('perms = "0400"'), 1)
7065 self.assertIn('secrets/cliproxy-seed.json:/run/secrets/cliproxy-seed.json:ro', rendered)
71 self.assertIn('secrets/cliproxy-nginx.conf:/run/secrets/cliproxy-nginx.conf:ro', rendered)
72 self.assertIn('server 127.0.0.1:8317;', rendered)
73 self.assertIn('/data/static:/data/static:ro', rendered)
66 self.assertIn('kill_timeout = "30s"', rendered)
67 self.assertIn('/CLIProxyAPI/static', rendered)
7468 self.assertNotIn("NOMAD_SERVICE", rendered)
7569 self.assertNotIn("NOMAD_VARIABLE", rendered)
7670 self.assertNotIn("/srv/clover", rendered)
7771 self.assertNotIn("host_port", rendered)
7872 self.assertNotIn('env = true', rendered)
7973
80 def test_bootstrap_preserves_native_config_and_provider_credentials(self):
74 def test_bootstrap_preserves_config_after_the_first_migration(self):
8175 with tempfile.TemporaryDirectory() as temporary:
8276 root = Path(temporary)
8377 data = root / "data"
......@@ -85,13 +79,10 @@ class ServiceTests(unittest.TestCase):
8579 seed = root / "seed.json"
8680 seed.write_text(json.dumps({"access": {"api-keys": [API_KEY]}}))
8781 binary = root / "binary"
88 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')
82 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')
8983 binary.chmod(0o700)
90 gateway = root / "gateway"
91 gateway.write_text('#!/bin/sh\nexit 0\n')
92 gateway.chmod(0o700)
93 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))
94 self.assertEqual(subprocess.run(["bash", "-ec", script], timeout=5).returncode, 1)
84 script = (SERVICE / "start.sh").read_text().replace('/run/secrets/cliproxy-seed.json', str(seed)).replace('/CLIProxyAPI/CLIProxyAPI', str(binary)).replace('/data', str(data))
85 self.assertEqual(subprocess.run(["sh", "-ec", script], timeout=5).returncode, 0)
9586 config = data / "config.yaml"
9687 self.assertEqual(json.loads(config.read_text())["access"]["api-keys"], [API_KEY])
9788 self.assertEqual(config.stat().st_mode & 0o777, 0o600)
......@@ -100,345 +91,233 @@ class ServiceTests(unittest.TestCase):
10091 credential = data / "auth/fixture.json"
10192 credential.write_text('{"synthetic":true}')
10293 seed.write_text('different seed')
103 self.assertEqual(subprocess.run(["bash", "-ec", script], timeout=5).returncode, 1)
94 self.assertEqual(subprocess.run(["sh", "-ec", script], timeout=5).returncode, 0)
10495 self.assertIn('provider-setting: retained', config.read_text())
10596 self.assertEqual(credential.read_text(), '{"synthetic":true}')
10697 config.unlink()
10798 config.symlink_to(seed)
108 result = subprocess.run(["bash", "-ec", script], capture_output=True)
99 result = subprocess.run(["sh", "-ec", script], capture_output=True)
109100 self.assertNotEqual(result.returncode, 0)
110101 self.assertEqual(seed.read_text(), 'different seed')
111102
112 def test_panel_pin_rejects_bad_download_without_replacing_existing_file(self):
113 with tempfile.TemporaryDirectory() as temporary:
114 root = Path(temporary)
115 static = root / "data/static"
116 static.mkdir(parents=True)
117 panel = static / "management.html"
118 panel.write_bytes(b"previous panel")
119 with patch.object(prepare.urllib.request, "urlopen", return_value=io.BytesIO(b"bad panel")):
120 with self.assertRaisesRegex(ValueError, "checksum"):
121 prepare.prepare({"hostRoot": str(root), "uid": os.getuid()})
122 self.assertEqual(panel.read_bytes(), b"previous panel")
123 panel.unlink()
124 panel.symlink_to(root / "outside")
125 with self.assertRaisesRegex(ValueError, "symbolic"):
126 prepare.prepare({"hostRoot": str(root), "uid": os.getuid()})
127103
128 def test_panel_verified_existing_asset_never_contacts_release_feed(self):
129 with tempfile.TemporaryDirectory() as temporary:
130 root = Path(temporary)
131 static = root / "data/static"
132 static.mkdir(parents=True)
133 panel = static / "management.html"
134 contents = b"pinned synthetic panel"
135 panel.write_bytes(contents)
136 with patch.object(prepare, "PANEL_SSO_SHA256", hashlib.sha256(contents).hexdigest()), patch.object(prepare.urllib.request, "urlopen", side_effect=AssertionError("unexpected download")):
137 prepare.prepare({"hostRoot": str(root), "uid": os.getuid()})
138 self.assertEqual(panel.read_bytes(), contents)
139104
140
141@unittest.skipUnless(NGINX, "pass --nginx to test the real gateway binary")
105@unittest.skipUnless(CADDY, "pass --caddy to test the real router")
142106class GatewayTests(unittest.TestCase):
143107 def setUp(self):
144 self.temporary = tempfile.TemporaryDirectory(prefix="cliproxy-gateway-")
145 self.addCleanup(self.temporary.cleanup)
146 root = Path(self.temporary.name)
108 temporary = tempfile.TemporaryDirectory(prefix='cliproxy-caddy-')
109 self.addCleanup(temporary.cleanup)
110 root = Path(temporary.name)
147111 self.observed = []
148 self.secret_markers = [API_KEY, MANAGEMENT_KEY]
149 observed = self.observed
112 self.auth_checks = []
113 self.websocket_started = threading.Event()
114 self.websocket_release = threading.Event()
115 observed, auth_checks = self.observed, self.auth_checks
116 websocket_started, websocket_release = self.websocket_started, self.websocket_release
150117
151118 class Backend(BaseHTTPRequestHandler):
152 protocol_version = "HTTP/1.1"
119 protocol_version = 'HTTP/1.1'
153120
154121 def do_GET(self):
155 self.respond()
156
157 def do_POST(self):
158 self.respond()
159
160 do_PUT = do_POST
161 do_PATCH = do_POST
162 do_DELETE = do_POST
163
164 def respond(self):
165 observed.append((self.command, self.path, dict(self.headers), self.rfile.read(int(self.headers.get('Content-Length', '0')))))
166 if self.path == '/v8/management/config' and (self.headers.get('Authorization') != 'Bearer ' + MANAGEMENT_KEY or self.headers.get('X-Synthetic-Reject')):
167 self.send_response(401)
168 self.send_header('Content-Length', '0')
169 self.end_headers()
170 return
171 if self.headers.get("Upgrade", "").lower() == "websocket":
122 observed.append((self.command, self.path, dict(self.headers)))
123 management = self.path.startswith(('/v0/management/', '/v8/management/'))
124 if management:
125 valid = self.headers.get('Authorization') == 'Bearer ' + MANAGEMENT_KEY or self.headers.get('X-Management-Key') == MANAGEMENT_KEY
126 elif self.path.startswith('/v1/'):
127 valid = self.headers.get('Authorization') == 'Bearer ' + API_KEY or self.headers.get('X-Api-Key') == API_KEY
128 else:
129 valid = True
130 if self.headers.get('Upgrade', '').lower() == 'websocket' and valid:
172131 accept = base64.b64encode(hashlib.sha1((self.headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').encode()).digest()).decode()
173132 self.send_response(101)
174 self.send_header("Upgrade", "websocket")
175 self.send_header("Connection", "Upgrade")
176 self.send_header("Sec-WebSocket-Accept", accept)
177 self.end_headers()
178 header = self.rfile.read(2)
179 mask = self.rfile.read(4)
180 body = self.rfile.read(2)
133 self.send_header('Upgrade', 'websocket'); self.send_header('Connection', 'Upgrade')
134 self.send_header('Sec-WebSocket-Accept', accept); self.end_headers()
135 header, mask, body = self.rfile.read(2), self.rfile.read(4), self.rfile.read(2)
181136 if header != b'\x81\x82' or bytes(value ^ mask[index % 4] for index, value in enumerate(body)) != b'hi':
182137 raise AssertionError('incorrect client WebSocket frame')
183 self.wfile.write(b'\x81\x02hi')
184 self.wfile.flush()
138 self.wfile.write(b'\x81\x02hi'); self.wfile.flush()
139 if self.headers.get('X-Synthetic-Hold'):
140 websocket_started.set()
141 if not websocket_release.wait(5):
142 raise AssertionError('WebSocket release was not signaled')
143 self.wfile.write(b'\x81\x02ok'); self.wfile.flush()
185144 self.close_connection = True
186145 return
187 if self.headers.get("X-Synthetic-Stream"):
188 self.send_response(200)
189 self.send_header("Content-Type", "text/event-stream")
190 self.send_header("Connection", "close")
191 self.end_headers()
192 self.wfile.write(b'data: first\n\n')
193 self.wfile.flush()
194 time.sleep(.5)
195 self.wfile.write(b'data: last\n\n')
196 self.close_connection = True
197 return
198 body = json.dumps({"method": self.command, "path": self.path}).encode()
199 self.send_response(200)
146 self.send_response(200 if valid else 401)
147 self.send_header('Content-Type', 'application/json')
200148 self.send_header('Access-Control-Allow-Origin', '*')
201 self.send_header("Content-Length", str(len(body)))
149 if self.headers.get('X-Synthetic-Stream') and valid:
150 self.send_header('Connection', 'close'); self.end_headers()
151 self.wfile.write(b'data: first\n\n'); self.wfile.flush()
152 time.sleep(.5)
153 self.wfile.write(b'data: last\n\n'); self.close_connection = True
154 else:
155 body = b'{}'
156 self.send_header('Content-Length', str(len(body))); self.end_headers(); self.wfile.write(body)
157
158 do_POST = do_GET
159 do_PUT = do_GET
160
161 def log_message(self, *args):
162 pass
163
164 class Auth(BaseHTTPRequestHandler):
165 def do_GET(self):
166 auth_checks.append(dict(self.headers))
167 cookie = self.headers.get('Cookie', '')
168 self.send_response(202 if cookie else 401)
169 if cookie:
170 self.send_header('X-Auth-Request-Groups', 'role:infra-admin' if cookie == 'fixture=admin' else 'role:media')
202171 self.end_headers()
203 self.wfile.write(body)
204172
205173 def log_message(self, *args):
206174 pass
207175
208 backend = ThreadingHTTPServer(("127.0.0.1", 0), Backend)
209 threading.Thread(target=backend.serve_forever, daemon=True).start()
210 self.addCleanup(backend.server_close)
211 self.addCleanup(backend.shutdown)
176 for handler, attribute in [(Backend, 'backend'), (Auth, 'auth')]:
177 server = ThreadingHTTPServer(('127.0.0.1', 0), handler)
178 threading.Thread(target=server.serve_forever, daemon=True).start()
179 self.addCleanup(server.server_close); self.addCleanup(server.shutdown)
180 setattr(self, attribute, server)
212181 with socket.socket() as reservation:
213 reservation.bind(("127.0.0.1", 0))
214 self.port = reservation.getsockname()[1]
215 config = (SERVICE / "nginx.conf").read_text()
216 config = config.replace('server 127.0.0.1:8317;', 'server 127.0.0.1:' + str(backend.server_port) + ';')
217 config = re.sub(r'{{ with nomadVar .*?}}|{{ end }}', '', config)
218 config = config.replace('{{ .api_key.Value }}', API_KEY)
219 config = config.replace('{{ .management_key.Value }}', MANAGEMENT_KEY)
220 config = config.replace('/tmp/', str(root) + '/').replace('listen 8080', f'listen 127.0.0.1:{self.port}')
221 config = config.replace('ADMIN_HOST', ADMIN_HOST).replace('API_HOST', API_HOST)
222 config = config.replace('SSO_HOST', 'snowglobe.paperclover.net')
223 self.assertNotIn('{{', config)
224 path = root / "nginx.conf"
225 path.write_text(config)
226 log = (root / "stderr").open('w+b')
227 self.addCleanup(log.close)
228 process = subprocess.Popen([NGINX, '-c', str(path), '-g', 'daemon off;'], stdout=log, stderr=log)
182 reservation.bind(('127.0.0.1', 0)); self.port = reservation.getsockname()[1]
183 proof = root / 'proof'; proof.write_text('f' * 64)
184 (root / 'cliproxy.json').write_text(json.dumps({'cliproxy': {'api_key': API_KEY, 'management_key': MANAGEMENT_KEY}}))
185 def nomad(path, token):
186 if path == '/v1/services':
187 return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in ('cliproxy', 'forward-auth')]}]
188 if path.startswith('/v1/service/'):
189 service = path.rsplit('/', 1)[1]
190 return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1',
191 'Port': self.backend.server_port if service == 'cliproxy' else self.auth.server_port,
192 'Tags': ['caddy-host=' + ADMIN_HOST, 'caddy-plain-host=' + API_HOST, 'caddy-auth-role=infra-admin'] if service == 'cliproxy' else []}]
193 return {'http': {'Status': 'success'}}
194 environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '28001',
195 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof)}
196 with patch.dict(os.environ, environment), patch.object(router, 'ROUTE_DIR', str(root)), patch.object(router, 'nomad', nomad):
197 rendered = router.render('fixture')
198 with socket.socket() as admin:
199 admin.bind(('127.0.0.1', 0)); self.admin_port = admin.getsockname()[1]
200 config = '{\n admin 127.0.0.1:' + str(self.admin_port) + '\n auto_https off\n}\n'
201 for host in (ADMIN_HOST, API_HOST):
202 block = rendered.split(host + ' {', 1)[1].split('\n}\n', 1)[0]
203 config += f'http://{host}:{self.port} {{' + block + '\n}\n'
204 path = root / 'Caddyfile'; path.write_text(config)
205 self.config_path = path
206 self.log = root / 'caddy.log'
207 log = self.log.open('wb'); self.addCleanup(log.close)
208 process = subprocess.Popen([CADDY, 'run', '--config', str(path), '--adapter', 'caddyfile'], stdout=log, stderr=log)
229209 def stop():
230 process.terminate()
231 process.wait(timeout=5)
232 log.seek(0)
233 contents = log.read()
234 for marker in self.secret_markers:
235 self.assertNotIn(marker.encode(), contents)
210 process.terminate(); process.wait(timeout=5)
236211 self.addCleanup(stop)
237212 for _ in range(100):
238213 if process.poll() is not None:
239 log.seek(0)
240 self.fail('nginx failed: ' + log.read().decode())
214 self.fail('Caddy failed: ' + self.log.read_text())
241215 try:
242 connection = socket.create_connection(('127.0.0.1', self.port), .1)
243 connection.close()
244 break
216 with socket.create_connection(('127.0.0.1', self.port), timeout=.1):
217 break
245218 except OSError:
246219 time.sleep(.02)
247220 else:
248 self.fail('nginx did not listen')
221 self.fail('Caddy did not listen')
249222
250223 @contextmanager
251 def request(self, method, path, headers=None, body=None):
224 def request(self, method, path, headers=None):
252225 connection = http.client.HTTPConnection('127.0.0.1', self.port, timeout=3)
253 connection.request(method, path, body=body, headers={'Host': API_HOST, **(headers or {})})
226 connection.request(method, path, headers={'Host': ADMIN_HOST, **(headers or {})})
254227 response = connection.getresponse()
255228 try:
256229 yield response
257230 finally:
258 response.close()
259 connection.close()
260
261 def test_management_callback_and_path_confusion_never_reach_backend(self):
262 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']:
263 with self.subTest(path=path), self.request('GET', path, {'Authorization': 'Bearer ' + API_KEY}) as response:
264 self.assertIn(response.status, [400, 404])
265 response.read()
266 self.assertEqual(self.observed, [])
267
268 def test_admin_hostname_has_native_panel_and_unknown_hostname_is_denied(self):
269 with self.request('GET', '/', {'Host': ADMIN_HOST}) as response:
270 self.assertEqual(response.status, 302)
271 self.assertEqual(response.getheader('Location'), '/management.html')
272 response.read()
273 with self.request('GET', '/management.html', {'Host': ADMIN_HOST}) as response:
274 self.assertEqual(response.status, 200)
275 response.read()
276 count = len(self.observed)
277 with self.request('GET', '/management.html', {'Host': 'other.example.test'}) as response:
278 self.assertEqual(response.status, 404)
279 response.read()
280 self.assertEqual(len(self.observed), count)
281
282 def test_sso_management_key_is_injected_only_on_admin_management_paths(self):
283 for path in ['/v0/management/auth-files', '/v8/management/config', '/v8/management/config.yaml']:
284 with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer stale-browser-key', 'X-Management-Key': 'stale-browser-key', 'Cookie': 'synthetic-session'}) as response:
231 response.close(); connection.close()
232
233 def test_native_keys_bypass_sso_on_both_hosts(self):
234 for host in (ADMIN_HOST, API_HOST):
235 for headers in ({'Authorization': 'Bearer ' + MANAGEMENT_KEY}, {'X-Management-Key': MANAGEMENT_KEY}):
236 with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response:
237 self.assertEqual(response.status, 200); response.read()
238 for headers in ({'Authorization': 'Bearer ' + API_KEY}, {'X-Api-Key': API_KEY}):
239 with self.request('GET', '/v1/models', {'Host': host, **headers}) as response:
240 self.assertEqual(response.status, 200); response.read()
241 self.assertEqual(self.auth_checks, [])
242 self.assertTrue(all('Cookie' not in headers for _, _, headers in self.observed))
243
244 def test_wrong_or_absent_keys_cannot_gain_management_access(self):
245 for host in (ADMIN_HOST, API_HOST):
246 for headers in ({}, {'Authorization': 'Bearer invalid'}, {'Authorization': 'Bearer ' + API_KEY}):
247 with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response:
248 self.assertEqual(response.status, 401); response.read()
249
250 def test_dashboard_requires_sso_even_with_management_key(self):
251 for path in ('/', '/management.html'):
252 with self.request('GET', path, {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
253 self.assertEqual(response.status, 302)
254 self.assertTrue(response.getheader('Location').startswith('/snow.oauth2/sign_in')); response.read()
255 with self.request('GET', path, {'Host': API_HOST}) as response:
256 self.assertEqual(response.status, 404); response.read()
257 with self.request('GET', '/management.html', {'Cookie': 'fixture=member'}) as response:
258 self.assertEqual(response.status, 403); response.read()
259 with self.request('GET', '/management.html', {'Cookie': 'fixture=admin'}) as response:
260 self.assertEqual(response.status, 200); response.read()
261
262 def test_sso_panel_injects_private_credentials_and_guards_origins(self):
263 for path in ('/v8/management/config', '/v1/models'):
264 with self.request('GET', path, {'Cookie': 'fixture=admin'}) as response:
285265 self.assertEqual(response.status, 200)
286266 self.assertEqual(response.getheader('Cache-Control'), 'no-store')
287 self.assertIsNone(response.getheader('Access-Control-Allow-Origin'))
288 self.assertNotIn(MANAGEMENT_KEY.encode(), response.read())
289 headers = self.observed[-1][2]
290 self.assertEqual(headers['Authorization'], 'Bearer ' + MANAGEMENT_KEY)
291 self.assertNotIn('X-Management-Key', headers)
292 self.assertNotIn('Cookie', headers)
293 for path in ['/management.html', '/v8/management-other/config']:
294 with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
295 self.assertEqual(response.status, 200)
296 response.read()
297 self.assertNotIn('Authorization', self.observed[-1][2])
298 count = len(self.observed)
299 for host in [API_HOST, 'other.example.test']:
300 with self.request('GET', '/v8/management/config', {'Host': host, 'Authorization': 'Bearer ' + API_KEY}) as response:
301 self.assertEqual(response.status, 404)
302 response.read()
303 self.assertEqual(len(self.observed), count)
304
305 def test_admin_model_list_uses_client_key_only_for_exact_read(self):
306 for path in ['/v1/models', '/v1/models?fixture=yes']:
307 with self.request('GET', path, {'Host': ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY, 'Cookie': 'synthetic-session'}) as response:
308 self.assertEqual(response.status, 200)
309 self.assertNotIn(API_KEY.encode(), response.read())
310 self.assertEqual(self.observed[-1][2]['Authorization'], 'Bearer ' + API_KEY)
267 self.assertIsNone(response.getheader('Access-Control-Allow-Origin')); response.read()
311268 self.assertNotIn('Cookie', self.observed[-1][2])
312 for method, path in [('GET', '/v1/models/'), ('GET', '/v1/models/nearby'), ('POST', '/v1/models'), ('POST', '/v1/responses'), ('POST', '/v1/chat/completions'), ('GET', '/v1/responses')]:
313 with self.subTest(method=method, path=path), self.request(method, path, {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST, 'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
314 self.assertEqual(response.status, 200)
315 response.read()
316 self.assertNotIn('Authorization', self.observed[-1][2])
317 count = len(self.observed)
318 with self.request('GET', '/v1/models') as response:
319 self.assertEqual(response.status, 401)
320 response.read()
321 self.assertEqual(len(self.observed), count)
322
323 def test_management_mutations_require_exact_admin_origin(self):
324 for method in ['POST', 'PUT', 'PATCH', 'DELETE']:
325 for origin in ['', 'null', 'https://other.example.test', 'https://' + ADMIN_HOST.replace('.', '-'), 'http://' + ADMIN_HOST, 'https://' + API_HOST]:
326 with self.subTest(method=method, origin=origin), self.request(method, '/v8/management/config', {'Host': ADMIN_HOST, **({'Origin': origin} if origin else {})}, body='{}') as response:
327 self.assertEqual(response.status, 403)
328 response.read()
329 self.assertEqual(self.observed, [])
330 for method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']:
331 with self.request(method, '/v8/management/config', {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST}, body='{}' if method != 'GET' else None) as response:
332 self.assertEqual(response.status, 200)
333 response.read()
334 with self.request('GET', '/v8/management/config', {'Host': ADMIN_HOST, 'Origin': 'https://other.example.test'}) as response:
335 self.assertEqual(response.status, 403)
336 response.read()
337
338 def test_logout_uses_fixed_sso_destination(self):
339 with self.request('GET', '/sign-out?rd=https://other.example.test', {'Host': ADMIN_HOST}) as response:
340 self.assertEqual(response.status, 303)
341 self.assertEqual(response.getheader('Location'), '/snow.oauth2/sign_out?rd=https%3A%2F%2Fsnowglobe.paperclover.net%2Fsign-in')
342 response.read()
343 self.assertEqual(self.observed, [])
344
345 def test_pinned_native_panel_auth_flow_through_gateway(self):
346 if not PANEL:
347 self.skipTest('pass --panel with the pinned upstream management.html')
348 root = Path(self.temporary.name)
349 with patch.object(prepare.urllib.request, 'urlopen', return_value=io.BytesIO(PANEL.read_bytes())), patch.object(prepare.os, 'fchown'), patch.object(prepare.os, 'chown'):
350 prepare.prepare({'hostRoot': str(root), 'uid': os.getuid()})
351 panel = root / 'data/static/management.html'
352 with patch.object(prepare.urllib.request, 'urlopen', side_effect=AssertionError('unexpected download')):
353 prepare.prepare({'hostRoot': str(root), 'uid': os.getuid()})
354 subprocess.run(['node', str(SERVICE / 'panel-test.mjs'), str(panel), 'http://127.0.0.1:' + str(self.port), ADMIN_HOST], check=True, timeout=15)
355
356 def test_browser_oauth_callback_credentials_never_reach_upstream_or_logs(self):
357 code = 'synthetic-oauth-code-must-not-be-logged'
358 state = 'synthetic-oauth-state-must-not-be-logged'
359 self.secret_markers.extend([code, state])
360 paths = ['/v0/management/oauth-callback', '/v8/management/oauth/callback', '/anthropic/callback', '/codex/callback', '/antigravity/callback', '/devin/callback', '/callback']
361 for path in paths:
362 for method in ['GET', 'HEAD']:
363 for suffix in ['', '/']:
364 with self.subTest(path=path, method=method, suffix=suffix), self.request(method, path + suffix + '?code=' + code + '&state=' + state, {'Host': ADMIN_HOST}) as response:
365 self.assertEqual(response.status, 404)
366 response.read()
367 with self.request('GET', path + '?state=' + state, {'Host': ADMIN_HOST}) as response:
368 self.assertEqual(response.status, 404)
369 response.read()
370 with self.request('GET', '/future-provider/callback?code=' + code, {'Host': ADMIN_HOST}) as response:
371 self.assertEqual(response.status, 404)
372 response.read()
373 self.assertEqual(self.observed, [])
374 payload = json.dumps({'provider': 'codex', 'redirect_url': 'http://localhost:1455/auth/callback?code=' + code + '&state=' + state}).encode()
375 for path in paths[:2]:
376 with self.request('POST', path, {'Host': ADMIN_HOST, 'Origin': 'https://' + ADMIN_HOST, 'Content-Type': 'application/json'}, body=payload) as response:
377 self.assertEqual(response.status, 200)
378 response.read()
379 self.assertEqual(self.observed[-1][3], payload)
380 self.assertNotIn(code, self.observed[-1][1])
381 self.assertNotIn(state, self.observed[-1][1])
382 with self.request('GET', '/v8/management/oauth/status?state=' + state, {'Host': ADMIN_HOST}) as response:
383 self.assertEqual(response.status, 200)
384 response.read()
385
386 def test_api_key_is_required_independently_of_upstream_auth(self):
387 for headers in [{}, {'Authorization': 'Bearer ' + MANAGEMENT_KEY}, {'Authorization': 'Bearer wrong'}, {'X-Api-Key': 'wrong'}]:
388 with self.request('GET', '/v1/models', headers) as response:
389 self.assertEqual(response.status, 401)
390 response.read()
391 self.assertEqual(self.observed, [])
392 for headers in [{'Authorization': 'Bearer ' + API_KEY}, {'X-Api-Key': API_KEY}]:
393 with self.request('GET', '/v1/models', headers) as response:
394 self.assertEqual(response.status, 200)
395 response.read()
396 self.assertEqual(self.observed[-1][2]['Authorization'], 'Bearer ' + API_KEY)
397 self.assertNotIn('X-Api-Key', self.observed[-1][2])
398 for query in ['key=other', 'auth_token=other']:
399 with self.request('GET', '/v1/models?' + query, {'Authorization': 'Bearer ' + API_KEY}) as response:
400 self.assertEqual(response.status, 400)
401 response.read()
402
403 def test_confirmed_codex_claude_methods_preserve_query_and_strip_admin_cookies(self):
404 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')]:
405 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:
406 self.assertEqual(response.status, 200)
407 self.assertEqual(json.loads(response.read())['path'], path + '?fixture=yes')
408 self.assertNotIn('Cookie', self.observed[-1][2])
409 self.assertNotIn('X-Management-Key', self.observed[-1][2])
410 for method, path in [('DELETE', '/v1/models'), ('GET', '/v1/messages'), ('POST', '/v1/models'), ('OPTIONS', '/v1/responses')]:
411 with self.request(method, path, {'Authorization': 'Bearer ' + API_KEY}) as response:
412 self.assertEqual(response.status, 404)
413 response.read()
414
415 def test_stream_is_delivered_before_upstream_finishes(self):
416 started = time.monotonic()
417 with self.request('POST', '/v1/responses', {'Authorization': 'Bearer ' + API_KEY, 'X-Synthetic-Stream': 'yes'}) as response:
418 self.assertEqual(response.status, 200)
419 self.assertEqual(response.readline(), b'data: first\n')
420 self.assertLess(time.monotonic() - started, .4)
421 self.assertIn(b'data: last', response.read())
422
423 def test_websocket_upgrade_reaches_responses_route(self):
269 for origin in ('', 'https://evil.example', 'null'):
270 with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', **({'Origin': origin} if origin else {})}) as response:
271 self.assertEqual(response.status, 403); response.read()
272 with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', 'Origin': 'https://' + ADMIN_HOST}) as response:
273 self.assertEqual(response.status, 200); response.read()
274 with self.request('POST', '/v8/management/config', {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
275 self.assertEqual(response.status, 200); response.read()
276
277 def test_native_websocket_upgrade(self):
424278 key = base64.b64encode(b'synthetic-16-key').decode()
425279 with socket.create_connection(('127.0.0.1', self.port), timeout=3) as connection:
426 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())
280 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())
427281 headers = b''
428282 while not headers.endswith(b'\r\n\r\n'):
429283 headers += connection.recv(1)
430284 self.assertIn(b' 101 ', headers.split(b'\r\n')[0])
431 self.assertIn(b'upgrade: websocket', headers.lower())
432285 connection.sendall(b'\x81\x82\x01\x02\x03\x04ik')
433286 self.assertEqual(connection.recv(4), b'\x81\x02hi')
287 self.assertEqual(self.auth_checks, [])
288
289 def test_websocket_survives_caddy_configuration_reload(self):
290 for host in (ADMIN_HOST, API_HOST):
291 self.websocket_started.clear(); self.websocket_release.clear()
292 key = base64.b64encode(b'synthetic-16-key').decode()
293 with socket.create_connection(('127.0.0.1', self.port), timeout=3) as connection:
294 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())
295 headers = b''
296 while not headers.endswith(b'\r\n\r\n'):
297 headers += connection.recv(1)
298 self.assertIn(b' 101 ', headers)
299 connection.sendall(b'\x81\x82\x01\x02\x03\x04ik')
300 self.assertEqual(connection.recv(4), b'\x81\x02hi')
301 self.assertTrue(self.websocket_started.wait(1))
302 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)
303 self.websocket_release.set()
304 self.assertEqual(connection.recv(4), b'\x81\x02ok')
305
306 def test_native_response_streams(self):
307 started = time.monotonic()
308 with self.request('POST', '/v1/responses', {'Authorization': 'Bearer ' + API_KEY, 'X-Synthetic-Stream': 'yes'}) as response:
309 self.assertEqual(response.status, 200)
310 self.assertEqual(response.readline(), b'data: first\n')
311 self.assertLess(time.monotonic() - started, .4)
312 self.assertIn(b'data: last', response.read())
313 log = self.log.read_text()
314 self.assertNotIn(API_KEY, log); self.assertNotIn(MANAGEMENT_KEY, log)
434315
435316
436317if __name__ == '__main__':
437318 parser = argparse.ArgumentParser()
438 parser.add_argument('--nginx', type=Path)
439 parser.add_argument('--panel', type=Path)
319 parser.add_argument('--caddy', type=Path)
440320 options, remaining = parser.parse_known_args()
441 NGINX = str(options.nginx.resolve()) if options.nginx else None
442 PANEL = options.panel.resolve() if options.panel else None
443 GatewayTests.__unittest_skip__ = not NGINX
321 CADDY = str(options.caddy.resolve()) if options.caddy else None
322 GatewayTests.__unittest_skip__ = not CADDY
444323 unittest.main(argv=[__file__, *remaining])
tools/cliproxy.md+112-69
......@@ -1,71 +1,114 @@
11# CLIProxyAPI
22
3`cpa.paperclover.net` is the management panel, protected by Snow's
4`infra-admin` role. Snow sign-in opens it automatically; nginx supplies the
5independent management key to its management endpoints on the server.
6`cpa-api.paperclover.net` is the client endpoint: use
7`https://cpa-api.paperclover.net/v1` for OpenAI-compatible clients or
8`https://cpa-api.paperclover.net` for Claude-compatible clients.
9
10Nomad variable `nomad/jobs/cliproxy` owns `api_key` and `management_key`.
11Keep values in restricted private files or a password manager; do not paste them
12into chat, command arguments, or tracked configuration. The management key never
13needs to enter the browser. Opening the panel clears any previously remembered
14native management password and validates access through the native config API.
15Its Logout button clears the shared forward-auth cookie and opens Snow sign-in.
16API clients accept the separate client key through `Authorization: Bearer …`
17or `X-Api-Key`. The gateway independently enforces this one infra-managed key,
18even if the native panel removes its own API keys. Query-string keys are rejected.
19
20## Connect providers
21
22Open the management panel and sign in through Snow.
23Its OAuth section supports **Codex** and **Claude**. Start the
24chosen provider's login and complete consent in the provider's own browser page.
25For remote OAuth flows that finish on an unreachable `localhost` URL, copy that
26entire callback URL into the panel's callback form. Treat the callback URL as a
27temporary credential. No callback ports or provider callbacks are exposed on
28the client hostname. Browser GET/HEAD callback routes are blocked on the admin
29hostname too: CLIProxyAPI logs URL queries, so callback credentials must be
30submitted through the management POST form. API-key provider credentials can also be configured in
31the native panel; their persistent home is the private service dataset.
32
33`/data/config.yaml` is seeded once from a read-only Nomad template. Native
34configuration edits, hashed management key, OAuth accounts, and refreshed tokens
35remain under `/srv/prod/cliproxy/data`; restarts and normal releases retain them.
36Changing a generated Nomad key does not overwrite an existing native config:
37coordinate key rotation in both Nomad and the native panel. Fresh staging uses
38separate storage and generated keys and must use synthetic providers only.
39
40## Boundary and updates
41
42Only the confirmed Codex/Claude client routes are public: model listing, chat,
43completions, messages/counting, Responses/compact/WebSocket, and Codex search
44and direct aliases. Management, plugins, OAuth callbacks, unknown paths, and
45unapproved methods return 404 on the API hostname. The app listens only on
46container loopback; nginx publishes the sole port. Both processes exit together
47if either fails. The host-side route requires Snow for management; nginx's
48admin-host routing does not replace that authentication boundary.
49Management writes require the exact admin origin; cross-origin reads with an
50Origin header are rejected, and upstream CORS headers are suppressed. The
51management key is added only to `/v0/management/` and `/v8/management/` paths.
52The panel's `GET /v1/models` uses the client key server-side; other client routes
53and methods receive no injected key on the admin hostname.
54
55The service pins CLIProxyAPI v8.0.32 and the management panel v1.26.5. Panel
56downloads are size- and SHA-256-checked during preparation; runtime auto-update
57is disabled. Preparation patches session restoration, removes auth-store
58persistence, permits empty-key revalidation, and redirects Logout; the resulting
59asset has its own pinned checksum. Native login still establishes authentication only after its config
60request succeeds. Request-body logging is omitted by `commercial-mode: true` and
61`request-log: false`. Plugins, discovery, usage aggregation, and profiling start
62disabled. The read-only image has only `/data` and a private `/tmp` writable;
63the pinned `/data/static` subdirectory is mounted read-only separately.
64
65Run `python3 tools/cliproxy-test.py --nginx /path/to/nginx --panel /path/to/upstream-management.html`
66with the pinned upstream v1.26.5 asset for bootstrap, rendering, pinned-asset,
67actual HTTP/streaming/WebSocket boundaries, and the native auth-store flow.
68The test uses temporary synthetic credentials and a loopback fixture upstream.
69Stage with the normal deployment command, verify anonymous management is routed
70to Snow, valid/invalid client-key behavior, provider persistence on restart,
71and logs containing no fixture keys or prompts before production promotion.
3`https://cpa.paperclover.net` serves the Snow Globe CPA fork’s inference and management
4APIs. Caddy protects the dashboard with Snow's `infra-admin` role. The
5`cpa-api.paperclover.net` alias serves the same native APIs, with dashboard
6paths disabled.
7
8CPA validates its two independent keys:
9
10- The client `api_key` authenticates inference and model listing through
11 `Authorization: Bearer …` or the provider-compatible key headers.
12- The `management_key` authenticates `/v8/management/*` through
13 `Authorization: Bearer …` or `X-Management-Key`.
14
15Nomad variable `nomad/jobs/cliproxy` owns both keys. Keep their values in
16restricted files or a password manager, never in chat, arguments, or tracked
17configuration. An inference key does not grant management access. A management
18key can modify configuration and use stored provider credentials.
19
20For OpenAI-compatible clients, use `https://cpa.paperclover.net/v1`; for
21Claude-compatible clients, use `https://cpa.paperclover.net`. Management callers
22use `https://cpa.paperclover.net/v8/management` with the management key. Native
23requests carrying a key bypass SSO; CPA rejects missing or invalid credentials.
24The dashboard still requires SSO even when a management key is supplied.
25
26## Dashboard
27
28Snow sign-in opens the panel automatically. Caddy supplies the management key
29only after an `infra-admin` SSO check, and the client key for the panel's model
30list. Cookie-authenticated writes require the exact dashboard Origin; reads
31reject a foreign Origin. Keys stay on the server, and cookies are removed before
32proxying to CPA. Native requests with explicit key headers retain CPA's own
33authentication and do not receive injected credentials.
34
35Opening the panel clears remembered native passwords and validates access
36through the config API. Logout clears the shared forward-auth cookie and opens
37Snow sign-in. The source panel implements this SSO adaptation; the image contains
38one immutable panel build.
39
40## Providers and persistence
41
42Use the panel's OAuth section to connect providers. For a remote flow ending at
43an unreachable localhost URL, submit the entire callback URL through the panel's
44callback form. Treat it as a temporary credential. API-key providers can also be
45configured in the native panel.
46
47`/data/config.yaml` is seeded once from a private Nomad template. Configuration,
48OAuth accounts, and refreshed tokens persist in `/srv/prod/cliproxy/data`.
49The first fork start atomically migrates the native configuration, preserving
50provider credentials and unrelated settings. It selects reset-soonest routing,
51affinity with priority reclaim, retained history, Codex-only model discovery,
52and saved cooldowns; panel auto-updates and request logging stay disabled.
53`/data/.snowglobe-fork-v1` makes this a one-time migration, so later operator
54changes survive restarts. The container publishes one
55CPA port through Nomad's loopback binding; Caddy is the only public entry point.
56There is no nginx container or second CPA listener.
57
58Changing a Nomad key does not overwrite existing native configuration. Rotate it
59in CPA and Nomad together; Caddy's private route manifest is refreshed during
60provisioning. Fresh previews have separate storage and keys and must use
61synthetic providers only. Shared forward-auth does not register arbitrary preview
62callback URLs; a fresh preview cannot complete interactive sign-in through it.
63
64## Fork sources and history
65
66[Backend](https://shale.paperclover.net/cliproxyapi/) and
67[panel](https://shale.paperclover.net/cliproxy-panel/) fork CPA v8.0.32 and panel
68v1.26.5. `service/cliproxy/build-source.json` pins the exact source revisions;
69the deployment exporter builds both into one image using
70`Dockerfile.snowglobe`. Upstream panel auto-update cannot replace the baked file.
71Cursor's private agent protocol is excluded.
72
73`observability.history.enabled` enables retained monthly JSON usage and quota
74files under `/data/history`, beside the auth directory. They contain token
75counts, provider/model, hashed sessions and hashed account identities, never
76prompts. Quota is sampled each minute; idle subscription usage is polled every
77ten minutes. Usage-plan calls share a two-minute cache, with up to thirty minutes
78of stale successful data after a 429. The sample retains its original observation
79time so stale data cannot masquerade as fresh quota.
80
81`GET /v8/management/history/{usage,quota,accounts}` reads history; `since` accepts
82an RFC3339 timestamp. Accounts are masked with a unique suffix; `reveal=true`
83reveals email addresses to an authenticated management caller. `POST` to usage or
84quota accepts up to 10,000 bounded rows; stable row IDs make repeated imports
85idempotent. `tools/export-history.py` in the backend exports only transcript
86paths explicitly supplied by the operator, requires a cutoff, and never exports
87message contents. Do not point it at personal transcripts without authorization.
88
89API-equivalent estimates use models.dev, then LiteLLM, then explicit
90`observability.history.prices` overrides (USD per million tokens: `input`,
91`output`, `cache-read`, `cache-write`). Catalogs are cached for a day; overrides
92apply immediately to new records and imports. Missing catalog cache rates fall back to
93input rates; explicit zero rates remain zero. Estimates are neither subscription
94bills nor guaranteed provider charges. Existing retained rows keep the price
95observed when they were recorded. Restart after enabling history on an instance
96that started with it disabled.
97
98## Deployment and checks
99
100Stage with `python3 tools/deploy.py stage cliproxy`. A preview retains its own
101configuration/history across updates and never promotes that data to production.
102Production uses `python3 tools/deploy.py prod` from a described, conflict-free
103main. The fork closes its listener before draining active requests for up to
10425 seconds, flushes usage, and exits within Nomad's 30-second kill timeout.
105Caddy retries new connections briefly and preserves existing WebSockets across
106route reloads. A single writer still has a short replacement window; a turn
107longer than the drain budget can be interrupted.
108
109Run `python3 tools/cliproxy-test.py --caddy /path/to/caddy` for native key
110separation, SSO roles, dashboard protection, Origin checks, streams, WebSockets
111including Caddy reloads, and configuration preservation. It uses disposable
112HTTP fixtures, synthetic keys, and a separate Caddy process.
113`python3 tools/deploy-test.py` covers composed source exports. The backend uses
114`go test ./...` and `go build ./cmd/server`; the panel uses `bun run verify`.
tools/deploy-test.py+17
......@@ -154,6 +154,23 @@ class BuildSourceExport(unittest.TestCase):
154154 self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n")
155155 self.assertFalse((context / "src/app.dockerfile").exists())
156156
157 def test_composed_sources_export_pinned_files_under_distinct_destinations(self):
158 self.manifest.write_text(json.dumps({"sources": [self.spec, {
159 "source": "../source", "revision": self.commit,
160 "destination": "panel", "include": ["run.sh", "lib"],
161 }]}))
162 context = self.remote / "releases" / deploy.upload() / "service/app/build"
163 self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n")
164 self.assertEqual((context / "panel/lib/committed").read_text(), "committed library")
165 self.assertFalse((context / "panel/lib/new").exists())
166 for target in ("../escape", "/absolute", "."):
167 self.manifest.write_text(json.dumps({"sources": [self.spec, {
168 "source": "../source", "revision": self.commit,
169 "destination": target, "include": ["run.sh"],
170 }]}))
171 with self.assertRaisesRegex(ValueError, "invalid build source destination"):
172 deploy.upload()
173
157174 def test_unpinned_source_uses_working_files(self):
158175 del self.spec["revision"]
159176 self.manifest.write_text(json.dumps(self.spec))
tools/deploy.py+47-36
......@@ -88,48 +88,59 @@ def upload(main=False):
8888 )
8989 for manifest in snapshot.glob("service/*/build-source.json"):
9090 spec = json.loads(manifest.read_text())
91 source = (REPO / spec["source"]).resolve(strict=True)
92 if not source.is_dir() or not source.is_relative_to(REPO.parent):
93 raise ValueError(f"invalid build source: {manifest.parent.name}")
9491 context = manifest.parent / "build"
9592 if context.exists():
9693 raise ValueError(f"build context already exists: {manifest.parent.name}")
9794 context.mkdir()
98 paths = [*spec["include"], spec["dockerfile"]]
99 for name in paths:
100 relative = Path(name)
101 if relative.is_absolute() or ".." in relative.parts or not relative.parts:
102 raise ValueError(f"invalid build source path: {name}")
103 if "revision" in spec:
104 commit = spec["revision"]
105 if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit):
106 raise ValueError(f"invalid build source revision: {manifest.parent.name}")
107 export_revision(source, commit, paths, context)
95 for index, source_spec in enumerate(spec.get("sources", [spec])):
96 source = (REPO / source_spec["source"]).resolve(strict=True)
97 if not source.is_dir() or not source.is_relative_to(REPO.parent):
98 raise ValueError(f"invalid build source: {manifest.parent.name}")
99 target = Path(source_spec.get("destination", "."))
100 if target.is_absolute() or ".." in target.parts or (index and not target.parts):
101 raise ValueError(f"invalid build source destination: {target}")
102 source_context = context / target
103 if source_context != context:
104 source_context.mkdir(parents=True, exist_ok=False)
105 dockerfile_name = source_spec.get("dockerfile")
106 paths = [*source_spec["include"], *([dockerfile_name] if dockerfile_name else [])]
108107 for name in paths:
109 if not (context / name).exists():
110 raise ValueError(f"missing build source path at revision: {name}")
111 dockerfile = context / spec["dockerfile"]
112 if not dockerfile.is_file():
113 raise ValueError(f"invalid build source Dockerfile: {spec['dockerfile']}")
114 if dockerfile != context / "Dockerfile":
115 if (context / "Dockerfile").exists():
116 raise ValueError("build source already includes Dockerfile")
117 if any(dockerfile.is_relative_to(context / name) for name in spec["include"]):
118 shutil.copy2(dockerfile, context / "Dockerfile")
108 relative = Path(name)
109 if relative.is_absolute() or ".." in relative.parts or not relative.parts:
110 raise ValueError(f"invalid build source path: {name}")
111 if "revision" in source_spec:
112 commit = source_spec["revision"]
113 if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit):
114 raise ValueError(f"invalid build source revision: {manifest.parent.name}")
115 export_revision(source, commit, paths, source_context)
116 for name in paths:
117 if not (source_context / name).exists():
118 raise ValueError(f"missing build source path at revision: {name}")
119 if dockerfile_name:
120 dockerfile = source_context / dockerfile_name
121 if not dockerfile.is_file():
122 raise ValueError(f"invalid build source Dockerfile: {dockerfile_name}")
123 if dockerfile != source_context / "Dockerfile":
124 if (source_context / "Dockerfile").exists():
125 raise ValueError("build source already includes Dockerfile")
126 if any(dockerfile.is_relative_to(source_context / name) for name in source_spec["include"]):
127 shutil.copy2(dockerfile, source_context / "Dockerfile")
128 else:
129 dockerfile.rename(source_context / "Dockerfile")
130 continue
131 for name in paths:
132 relative = Path(name)
133 origin = source / relative
134 if not origin.resolve(strict=True).is_relative_to(source):
135 raise ValueError(f"build source escapes context: {name}")
136 destination = source_context / ("Dockerfile" if name == dockerfile_name else name)
137 destination.parent.mkdir(parents=True, exist_ok=True)
138 if origin.is_dir():
139 shutil.copytree(origin, destination, symlinks=True)
119140 else:
120 dockerfile.rename(context / "Dockerfile")
121 continue
122 for name in paths:
123 relative = Path(name)
124 origin = source / relative
125 if not origin.resolve(strict=True).is_relative_to(source):
126 raise ValueError(f"build source escapes context: {name}")
127 destination = context / ("Dockerfile" if name == spec["dockerfile"] else name)
128 destination.parent.mkdir(parents=True, exist_ok=True)
129 if origin.is_dir():
130 shutil.copytree(origin, destination, symlinks=True)
131 else:
132 shutil.copy2(origin, destination)
141 shutil.copy2(origin, destination)
142 if not (context / "Dockerfile").is_file():
143 raise ValueError(f"missing build source Dockerfile: {manifest.parent.name}")
133144 digest = tree_digest(snapshot)
134145 release = digest[:16]
135146 remote_release = REMOTE / "releases" / release
tools/router.py+55
......@@ -40,6 +40,55 @@ def proxy(upstreams, indent, uncompressed=False, host=None, keep_streams=False):
4040 return [*lines, f"{indent}}}"]
4141
4242
43def cliproxy_routes(host, upstreams, auth, keys, role):
44 native = [" request_header -Cookie", *proxy(upstreams, " ", keep_streams=True)]
45 if not role:
46 return [" @dashboard path / /management.html /sign-out /snow.oauth2/*",
47 " respond @dashboard 404", " request_header -Cookie", *proxy(upstreams, " ", keep_streams=True)]
48 if any(not re.fullmatch(r"[0-9a-f]{64}", keys.get(name, "")) for name in ("api_key", "management_key")):
49 raise ValueError("missing CLIProxyAPI gateway credentials")
50 return [
51 " route {",
52 " handle /snow.oauth2/* {", *proxy(auth, " "), " }",
53 " @credentials {",
54 " not path / /management.html /sign-out /snow.oauth2/*",
55 ' expression `{http.request.header.Authorization} != "" || {http.request.header.X-Management-Key} != "" || {http.request.header.X-Api-Key} != "" || {http.request.header.X-Goog-Api-Key} != ""`',
56 " }",
57 " handle @credentials {", *native, " }",
58 " @session path / /management.html /sign-out /v0/management/* /v8/management/* /v1/models",
59 " handle @session {",
60 f" reverse_proxy {auth} {{",
61 " method GET", " rewrite /snow.oauth2/auth",
62 " header_up X-Forwarded-Method {method}", " header_up X-Forwarded-Uri {uri}",
63 " @unauthorized status 401",
64 " handle_response @unauthorized {",
65 " @dashboard path / /management.html /sign-out",
66 " route {",
67 " redir @dashboard /snow.oauth2/sign_in?rd={scheme}://{host}{uri}",
68 " respond 401", " }", " }",
69 f" @allowed header X-Auth-Request-Groups *role:{role}*",
70 " handle_response @allowed {",
71 " method {method}", " rewrite {uri}",
72 " @foreign_origin {", " header Origin *", f" not header Origin https://{host}", " }",
73 " @unsafe_origin {", " method POST PUT PATCH DELETE", f" not header Origin https://{host}", " }",
74 " route {",
75 " respond @foreign_origin 403", " respond @unsafe_origin 403",
76 " redir / /management.html 302",
77 f" redir /sign-out /snow.oauth2/sign_out?rd=https%3A%2F%2Fsnowglobe.{os.environ['SNOWGLOBE_DOMAIN']}%2Fsign-in 303",
78 " @management path /v0/management/* /v8/management/*",
79 f' request_header @management Authorization "Bearer {keys["management_key"]}"',
80 " @models path /v1/models",
81 f' request_header @models Authorization "Bearer {keys["api_key"]}"',
82 " request_header -Cookie",
83 f" reverse_proxy {upstreams} {{",
84 " lb_try_duration 5s", " stream_close_delay 8h",
85 " header_down -Access-Control-Allow-Origin", ' header_down Cache-Control "no-store"',
86 " }", " }", " }",
87 " handle_response {", " respond 403", " }", " }", " }",
88 " handle {", *native, " }", " }",
89 ]
90
91
4392def shale_mcp_routes(port):
4493 return [" @shale_mcp_link path_regexp shale_mcp_link ^/-/snowglobe-mcp/([A-Za-z0-9_-]{43})$",
4594 " handle @shale_mcp_link {", " rewrite * /oauth/shale/link/{re.shale_mcp_link.1}",
......@@ -288,6 +337,12 @@ def render(token):
288337 service = next(iter(route["services"]))
289338 if not re.fullmatch(r"[a-z][a-z0-9-]*", service):
290339 raise ValueError(f"invalid service name: {service}")
340 if service == "cliproxy" or re.fullmatch(r"cliproxy-preview-[0-9a-f]{8}", service):
341 with open(os.path.join(ROUTE_DIR, service + ".json")) as source:
342 keys = json.load(source).get("cliproxy", {})
343 lines += [*cliproxy_routes(host, " ".join(sorted(route["upstreams"])),
344 " ".join(sorted(auth_upstreams)), keys, role), "}"]
345 continue
291346 if service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):
292347 # Keep the Origin guard before every static/proxy handle;
293348 # otherwise Caddy sorts those handles ahead of respond.
tools/snowglobe.py+4
......@@ -373,6 +373,8 @@ def render(data, definitions):
373373 ]
374374 for task_name, task in data["containers"].items():
375375 lines += [f" task {q(task_name)} {{", ' driver = "podman"']
376 if task.get("killTimeout"):
377 lines.append(f" kill_timeout = {q(task['killTimeout'])}")
376378 if task["lifecycle"] != "main":
377379 lines += [' lifecycle {', ' hook = "prestart"',
378380 f" sidecar = {str(task['lifecycle'] == 'prestartSidecar').lower()}", ' }']
......@@ -619,6 +621,8 @@ def provision(data):
619621 write_private(route_dir / f"{data['id']}.json", json.dumps({
620622 "files": static, "dirs": directories, "identity": identity,
621623 "identityProof": proof, "headHtml": head_html, "metricsPaths": metrics_paths,
624 **({"cliproxy": {name: values[name] for name in ("api_key", "management_key")}}
625 if data["sourceId"] == "cliproxy" else {}),
622626 }))
623627
624628