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 {...@@ -84,6 +84,7 @@ class Container {
84 args: Listing<String> = new {}84 args: Listing<String> = new {}
85 capAdd: Listing<String> = new {}85 capAdd: Listing<String> = new {}
86 readOnlyRoot: Boolean = false86 readOnlyRoot: Boolean = false
87 killTimeout: String?
87 devices: Listing<String> = new {}88 devices: Listing<String> = new {}
88 extraHosts: Listing<String> = new {}89 extraHosts: Listing<String> = new {}
89 hostNetwork: Boolean = false90 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 @@...@@ -1,13 +1,11 @@
1amends "../../config/Service.pkl"1amends "../../config/Service.pkl"
22
3import "../../config/site.pkl" as site3import "../../config/site.pkl" as site
4import "../../config/OpenID.pkl" as sso
54
6name = "CLIProxyAPI"5name = "CLIProxyAPI"
7tagline = "Codex and Claude API access"6tagline = "Codex and Claude API access"
8access = "infra-admin"7access = "infra-admin"
9stageIsolation = "fresh"8stageIsolation = "fresh"
10prepare = "prepare.py"
119
12secrets {10secrets {
13 ["api_key"] {}11 ["api_key"] {}
...@@ -15,13 +13,14 @@ secrets {...@@ -15,13 +13,14 @@ secrets {
15}13}
1614
17container {15container {
18 build = "."16 build = "build"
19 entrypoint = "/bin/bash"17 killTimeout = "30s"
18 entrypoint = "/bin/sh"
20 args { "/snowglobe/start.sh" }19 args { "/snowglobe/start.sh" }
21 readOnlyRoot = true20 readOnlyRoot = true
22 tmpfs { "/tmp:rw,nosuid,nodev,noexec,size=64m" }21 tmpfs { "/tmp:rw,nosuid,nodev,noexec,size=64m" }
23 http {22 http {
24 containerPort = 808023 containerPort = 8317
25 subdomain = "cpa"24 subdomain = "cpa"
26 authRole = "infra-admin"25 authRole = "infra-admin"
27 checkPath = "/healthz"26 checkPath = "/healthz"
...@@ -29,13 +28,13 @@ container {...@@ -29,13 +28,13 @@ container {
29 }28 }
30 volumes {29 volumes {
31 ["/data"] {}30 ["/data"] {}
32 ["/data/static"] { readOnly = true }
33 ["/snowglobe/start.sh"] { config = "start.sh" }31 ["/snowglobe/start.sh"] { config = "start.sh" }
34 }32 }
35 env {33 env {
36 ["HOME"] = "/data"34 ["HOME"] = "/data"
37 ["WRITABLE_PATH"] = "/data"35 ["WRITABLE_PATH"] = "/data"
38 ["MANAGEMENT_STATIC_PATH"] = "/data/static"36 ["MANAGEMENT_STATIC_PATH"] = "/CLIProxyAPI/static"
37 ["CLIPROXY_SHUTDOWN_DRAIN"] = "25s"
39 ["TZ"] = "America/Los_Angeles"38 ["TZ"] = "America/Los_Angeles"
40 }39 }
41 secretFiles {40 secretFiles {
...@@ -43,19 +42,16 @@ container {...@@ -43,19 +42,16 @@ container {
43 {{ with nomadVar "nomad/jobs/\(module.id)" }}42 {{ with nomadVar "nomad/jobs/\(module.id)" }}
44 {43 {
45 "config-version": 8,44 "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}},
47 "access": {"api-keys": [{{ .api_key.Value | toJSON }}]},46 "access": {"api-keys": [{{ .api_key.Value | toJSON }}]},
48 "management": {"allow-remote": true, "secret-key": {{ .management_key.Value | toJSON }}, "disable-auto-update-panel": true},47 "management": {"allow-remote": true, "secret-key": {{ .management_key.Value | toJSON }}, "disable-auto-update-panel": true},
49 "oauth": {"auth-dir": "/data/auth"},48 "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}},
50 "plugins": {"enabled": false, "dir": "/data/plugins"},51 "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}}
52 }53 }
53 {{ end }}54 {{ end }}
54 """55 """
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)
60 }56 }
61}57}
service/cliproxy/start.sh+6-10
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1#!/bin/bash1#!/bin/sh
2set -eu2set -eu
3umask 0773umask 077
44
...@@ -18,12 +18,8 @@ test -s /data/config.yaml...@@ -18,12 +18,8 @@ test -s /data/config.yaml
18chmod 600 /data/config.yaml18chmod 600 /data/config.yaml
19mkdir -p /data/auth /data/plugins19mkdir -p /data/auth /data/plugins
20chmod 700 /data /data/auth /data/plugins20chmod 700 /data /data/auth /data/plugins
21/CLIProxyAPI/CLIProxyAPI --config /data/config.yaml &21if [ ! -e /data/.snowglobe-fork-v1 ]; then
22app=$!22 /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml --fork-migrate
23/usr/sbin/nginx -c /run/secrets/cliproxy-nginx.conf -g 'daemon off;' &23 touch /data/.snowglobe-fork-v1
24gateway=$!24fi
25trap 'kill "$app" "$gateway" 2>/dev/null || :; wait "$app" "$gateway" 2>/dev/null || :' EXIT25exec /CLIProxyAPI/CLIProxyAPI --config /data/config.yaml
26trap 'exit 143' TERM
27trap 'exit 130' INT
28wait -n "$app" "$gateway"
29exit 1
service/shale/service.pkl+1-4
...@@ -8,10 +8,7 @@ requirements {...@@ -8,10 +8,7 @@ requirements {
8 new sso.Client {8 new sso.Client {
9 clientId = module.id9 clientId = module.id
10 name = module.name10 name = module.name
11 redirectUris {11 redirectUris { "https://\(module.container.http.hostname)/-/callback" }
12 "https://\(module.container.http.hostname)/-/callback"
13 "http://git1.localhost/-/callback"
14 }
15 usernameAliases { ["snow"] = "clover" }12 usernameAliases { ["snow"] = "clover" }
16 allowGuests = true13 allowGuests = true
17 usernameRequired = true14 usernameRequired = true
tools/cliproxy-test.py+178-299
...@@ -5,8 +5,6 @@ from contextlib import contextmanager...@@ -5,8 +5,6 @@ from contextlib import contextmanager
5import hashlib5import hashlib
6import http.client6import http.client
7from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer7from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
8import importlib.util
9import io
10import json8import json
11import os9import os
12from pathlib import Path10from pathlib import Path
...@@ -20,14 +18,11 @@ import unittest...@@ -20,14 +18,11 @@ import unittest
20from unittest.mock import patch18from unittest.mock import patch
2119
22import snowglobe20import snowglobe
21import router
2322
2423
25SERVICE = Path(__file__).resolve().parents[1] / "service/cliproxy"24SERVICE = Path(__file__).resolve().parents[1] / "service/cliproxy"
26spec = importlib.util.spec_from_file_location("cliproxy_prepare", SERVICE / "prepare.py")25CADDY = None
27prepare = importlib.util.module_from_spec(spec)
28spec.loader.exec_module(prepare)
29NGINX = None
30PANEL = None
31API_KEY = "a1" * 3226API_KEY = "a1" * 32
32MANAGEMENT_KEY = "b2" * 3227MANAGEMENT_KEY = "b2" * 32
33ADMIN_HOST = "cliproxy-preview-12345678.paperclover.net"28ADMIN_HOST = "cliproxy-preview-12345678.paperclover.net"
...@@ -66,18 +61,17 @@ class ServiceTests(unittest.TestCase):...@@ -66,18 +61,17 @@ class ServiceTests(unittest.TestCase):
66 rendered = snowglobe.render(self.data, {"cliproxy": self.data})61 rendered = snowglobe.render(self.data, {"cliproxy": self.data})
67 self.assertEqual(rendered.count('user = "3150:3150"'), 1)62 self.assertEqual(rendered.count('user = "3150:3150"'), 1)
68 self.assertEqual(rendered.count('readonly_rootfs = true'), 1)63 self.assertEqual(rendered.count('readonly_rootfs = true'), 1)
69 self.assertEqual(rendered.count('perms = "0400"'), 2)64 self.assertEqual(rendered.count('perms = "0400"'), 1)
70 self.assertIn('secrets/cliproxy-seed.json:/run/secrets/cliproxy-seed.json:ro', rendered)65 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)66 self.assertIn('kill_timeout = "30s"', rendered)
72 self.assertIn('server 127.0.0.1:8317;', rendered)67 self.assertIn('/CLIProxyAPI/static', rendered)
73 self.assertIn('/data/static:/data/static:ro', rendered)
74 self.assertNotIn("NOMAD_SERVICE", rendered)68 self.assertNotIn("NOMAD_SERVICE", rendered)
75 self.assertNotIn("NOMAD_VARIABLE", rendered)69 self.assertNotIn("NOMAD_VARIABLE", rendered)
76 self.assertNotIn("/srv/clover", rendered)70 self.assertNotIn("/srv/clover", rendered)
77 self.assertNotIn("host_port", rendered)71 self.assertNotIn("host_port", rendered)
78 self.assertNotIn('env = true', rendered)72 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):
81 with tempfile.TemporaryDirectory() as temporary:75 with tempfile.TemporaryDirectory() as temporary:
82 root = Path(temporary)76 root = Path(temporary)
83 data = root / "data"77 data = root / "data"
...@@ -85,13 +79,10 @@ class ServiceTests(unittest.TestCase):...@@ -85,13 +79,10 @@ class ServiceTests(unittest.TestCase):
85 seed = root / "seed.json"79 seed = root / "seed.json"
86 seed.write_text(json.dumps({"access": {"api-keys": [API_KEY]}}))80 seed.write_text(json.dumps({"access": {"api-keys": [API_KEY]}}))
87 binary = root / "binary"81 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')
89 binary.chmod(0o700)83 binary.chmod(0o700)
90 gateway = root / "gateway"84 script = (SERVICE / "start.sh").read_text().replace('/run/secrets/cliproxy-seed.json', str(seed)).replace('/CLIProxyAPI/CLIProxyAPI', str(binary)).replace('/data', str(data))
91 gateway.write_text('#!/bin/sh\nexit 0\n')85 self.assertEqual(subprocess.run(["sh", "-ec", script], timeout=5).returncode, 0)
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)
95 config = data / "config.yaml"86 config = data / "config.yaml"
96 self.assertEqual(json.loads(config.read_text())["access"]["api-keys"], [API_KEY])87 self.assertEqual(json.loads(config.read_text())["access"]["api-keys"], [API_KEY])
97 self.assertEqual(config.stat().st_mode & 0o777, 0o600)88 self.assertEqual(config.stat().st_mode & 0o777, 0o600)
...@@ -100,345 +91,233 @@ class ServiceTests(unittest.TestCase):...@@ -100,345 +91,233 @@ class ServiceTests(unittest.TestCase):
100 credential = data / "auth/fixture.json"91 credential = data / "auth/fixture.json"
101 credential.write_text('{"synthetic":true}')92 credential.write_text('{"synthetic":true}')
102 seed.write_text('different seed')93 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)
104 self.assertIn('provider-setting: retained', config.read_text())95 self.assertIn('provider-setting: retained', config.read_text())
105 self.assertEqual(credential.read_text(), '{"synthetic":true}')96 self.assertEqual(credential.read_text(), '{"synthetic":true}')
106 config.unlink()97 config.unlink()
107 config.symlink_to(seed)98 config.symlink_to(seed)
108 result = subprocess.run(["bash", "-ec", script], capture_output=True)99 result = subprocess.run(["sh", "-ec", script], capture_output=True)
109 self.assertNotEqual(result.returncode, 0)100 self.assertNotEqual(result.returncode, 0)
110 self.assertEqual(seed.read_text(), 'different seed')101 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
140105@unittest.skipUnless(CADDY, "pass --caddy to test the real router")
141@unittest.skipUnless(NGINX, "pass --nginx to test the real gateway binary")
142class GatewayTests(unittest.TestCase):106class GatewayTests(unittest.TestCase):
143 def setUp(self):107 def setUp(self):
144 self.temporary = tempfile.TemporaryDirectory(prefix="cliproxy-gateway-")108 temporary = tempfile.TemporaryDirectory(prefix='cliproxy-caddy-')
145 self.addCleanup(self.temporary.cleanup)109 self.addCleanup(temporary.cleanup)
146 root = Path(self.temporary.name)110 root = Path(temporary.name)
147 self.observed = []111 self.observed = []
148 self.secret_markers = [API_KEY, MANAGEMENT_KEY]112 self.auth_checks = []
149 observed = self.observed113 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
151 class Backend(BaseHTTPRequestHandler):118 class Backend(BaseHTTPRequestHandler):
152 protocol_version = "HTTP/1.1"119 protocol_version = 'HTTP/1.1'
153120
154 def do_GET(self):121 def do_GET(self):
155 self.respond()122 observed.append((self.command, self.path, dict(self.headers)))
156123 management = self.path.startswith(('/v0/management/', '/v8/management/'))
157 def do_POST(self):124 if management:
158 self.respond()125 valid = self.headers.get('Authorization') == 'Bearer ' + MANAGEMENT_KEY or self.headers.get('X-Management-Key') == MANAGEMENT_KEY
159126 elif self.path.startswith('/v1/'):
160 do_PUT = do_POST127 valid = self.headers.get('Authorization') == 'Bearer ' + API_KEY or self.headers.get('X-Api-Key') == API_KEY
161 do_PATCH = do_POST128 else:
162 do_DELETE = do_POST129 valid = True
163130 if self.headers.get('Upgrade', '').lower() == 'websocket' and valid:
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":
172 accept = base64.b64encode(hashlib.sha1((self.headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').encode()).digest()).decode()131 accept = base64.b64encode(hashlib.sha1((self.headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').encode()).digest()).decode()
173 self.send_response(101)132 self.send_response(101)
174 self.send_header("Upgrade", "websocket")133 self.send_header('Upgrade', 'websocket'); self.send_header('Connection', 'Upgrade')
175 self.send_header("Connection", "Upgrade")134 self.send_header('Sec-WebSocket-Accept', accept); self.end_headers()
176 self.send_header("Sec-WebSocket-Accept", accept)135 header, mask, body = self.rfile.read(2), self.rfile.read(4), self.rfile.read(2)
177 self.end_headers()
178 header = self.rfile.read(2)
179 mask = self.rfile.read(4)
180 body = self.rfile.read(2)
181 if header != b'\x81\x82' or bytes(value ^ mask[index % 4] for index, value in enumerate(body)) != b'hi':136 if header != b'\x81\x82' or bytes(value ^ mask[index % 4] for index, value in enumerate(body)) != b'hi':
182 raise AssertionError('incorrect client WebSocket frame')137 raise AssertionError('incorrect client WebSocket frame')
183 self.wfile.write(b'\x81\x02hi')138 self.wfile.write(b'\x81\x02hi'); self.wfile.flush()
184 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()
185 self.close_connection = True144 self.close_connection = True
186 return145 return
187 if self.headers.get("X-Synthetic-Stream"):146 self.send_response(200 if valid else 401)
188 self.send_response(200)147 self.send_header('Content-Type', 'application/json')
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)
200 self.send_header('Access-Control-Allow-Origin', '*')148 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')
202 self.end_headers()171 self.end_headers()
203 self.wfile.write(body)
204172
205 def log_message(self, *args):173 def log_message(self, *args):
206 pass174 pass
207175
208 backend = ThreadingHTTPServer(("127.0.0.1", 0), Backend)176 for handler, attribute in [(Backend, 'backend'), (Auth, 'auth')]:
209 threading.Thread(target=backend.serve_forever, daemon=True).start()177 server = ThreadingHTTPServer(('127.0.0.1', 0), handler)
210 self.addCleanup(backend.server_close)178 threading.Thread(target=server.serve_forever, daemon=True).start()
211 self.addCleanup(backend.shutdown)179 self.addCleanup(server.server_close); self.addCleanup(server.shutdown)
180 setattr(self, attribute, server)
212 with socket.socket() as reservation:181 with socket.socket() as reservation:
213 reservation.bind(("127.0.0.1", 0))182 reservation.bind(('127.0.0.1', 0)); self.port = reservation.getsockname()[1]
214 self.port = reservation.getsockname()[1]183 proof = root / 'proof'; proof.write_text('f' * 64)
215 config = (SERVICE / "nginx.conf").read_text()184 (root / 'cliproxy.json').write_text(json.dumps({'cliproxy': {'api_key': API_KEY, 'management_key': MANAGEMENT_KEY}}))
216 config = config.replace('server 127.0.0.1:8317;', 'server 127.0.0.1:' + str(backend.server_port) + ';')185 def nomad(path, token):
217 config = re.sub(r'{{ with nomadVar .*?}}|{{ end }}', '', config)186 if path == '/v1/services':
218 config = config.replace('{{ .api_key.Value }}', API_KEY)187 return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in ('cliproxy', 'forward-auth')]}]
219 config = config.replace('{{ .management_key.Value }}', MANAGEMENT_KEY)188 if path.startswith('/v1/service/'):
220 config = config.replace('/tmp/', str(root) + '/').replace('listen 8080', f'listen 127.0.0.1:{self.port}')189 service = path.rsplit('/', 1)[1]
221 config = config.replace('ADMIN_HOST', ADMIN_HOST).replace('API_HOST', API_HOST)190 return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1',
222 config = config.replace('SSO_HOST', 'snowglobe.paperclover.net')191 'Port': self.backend.server_port if service == 'cliproxy' else self.auth.server_port,
223 self.assertNotIn('{{', config)192 'Tags': ['caddy-host=' + ADMIN_HOST, 'caddy-plain-host=' + API_HOST, 'caddy-auth-role=infra-admin'] if service == 'cliproxy' else []}]
224 path = root / "nginx.conf"193 return {'http': {'Status': 'success'}}
225 path.write_text(config)194 environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '28001',
226 log = (root / "stderr").open('w+b')195 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof)}
227 self.addCleanup(log.close)196 with patch.dict(os.environ, environment), patch.object(router, 'ROUTE_DIR', str(root)), patch.object(router, 'nomad', nomad):
228 process = subprocess.Popen([NGINX, '-c', str(path), '-g', 'daemon off;'], stdout=log, stderr=log)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)
229 def stop():209 def stop():
230 process.terminate()210 process.terminate(); process.wait(timeout=5)
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)
236 self.addCleanup(stop)211 self.addCleanup(stop)
237 for _ in range(100):212 for _ in range(100):
238 if process.poll() is not None:213 if process.poll() is not None:
239 log.seek(0)214 self.fail('Caddy failed: ' + self.log.read_text())
240 self.fail('nginx failed: ' + log.read().decode())
241 try:215 try:
242 connection = socket.create_connection(('127.0.0.1', self.port), .1)216 with socket.create_connection(('127.0.0.1', self.port), timeout=.1):
243 connection.close()217 break
244 break
245 except OSError:218 except OSError:
246 time.sleep(.02)219 time.sleep(.02)
247 else:220 else:
248 self.fail('nginx did not listen')221 self.fail('Caddy did not listen')
249222
250 @contextmanager223 @contextmanager
251 def request(self, method, path, headers=None, body=None):224 def request(self, method, path, headers=None):
252 connection = http.client.HTTPConnection('127.0.0.1', self.port, timeout=3)225 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 {})})
254 response = connection.getresponse()227 response = connection.getresponse()
255 try:228 try:
256 yield response229 yield response
257 finally:230 finally:
258 response.close()231 response.close(); connection.close()
259 connection.close()232
260233 def test_native_keys_bypass_sso_on_both_hosts(self):
261 def test_management_callback_and_path_confusion_never_reach_backend(self):234 for host in (ADMIN_HOST, API_HOST):
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']:235 for headers in ({'Authorization': 'Bearer ' + MANAGEMENT_KEY}, {'X-Management-Key': MANAGEMENT_KEY}):
263 with self.subTest(path=path), self.request('GET', path, {'Authorization': 'Bearer ' + API_KEY}) as response:236 with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response:
264 self.assertIn(response.status, [400, 404])237 self.assertEqual(response.status, 200); response.read()
265 response.read()238 for headers in ({'Authorization': 'Bearer ' + API_KEY}, {'X-Api-Key': API_KEY}):
266 self.assertEqual(self.observed, [])239 with self.request('GET', '/v1/models', {'Host': host, **headers}) as response:
267240 self.assertEqual(response.status, 200); response.read()
268 def test_admin_hostname_has_native_panel_and_unknown_hostname_is_denied(self):241 self.assertEqual(self.auth_checks, [])
269 with self.request('GET', '/', {'Host': ADMIN_HOST}) as response:242 self.assertTrue(all('Cookie' not in headers for _, _, headers in self.observed))
270 self.assertEqual(response.status, 302)243
271 self.assertEqual(response.getheader('Location'), '/management.html')244 def test_wrong_or_absent_keys_cannot_gain_management_access(self):
272 response.read()245 for host in (ADMIN_HOST, API_HOST):
273 with self.request('GET', '/management.html', {'Host': ADMIN_HOST}) as response:246 for headers in ({}, {'Authorization': 'Bearer invalid'}, {'Authorization': 'Bearer ' + API_KEY}):
274 self.assertEqual(response.status, 200)247 with self.request('GET', '/v8/management/config', {'Host': host, **headers}) as response:
275 response.read()248 self.assertEqual(response.status, 401); response.read()
276 count = len(self.observed)249
277 with self.request('GET', '/management.html', {'Host': 'other.example.test'}) as response:250 def test_dashboard_requires_sso_even_with_management_key(self):
278 self.assertEqual(response.status, 404)251 for path in ('/', '/management.html'):
279 response.read()252 with self.request('GET', path, {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
280 self.assertEqual(len(self.observed), count)253 self.assertEqual(response.status, 302)
281254 self.assertTrue(response.getheader('Location').startswith('/snow.oauth2/sign_in')); response.read()
282 def test_sso_management_key_is_injected_only_on_admin_management_paths(self):255 with self.request('GET', path, {'Host': API_HOST}) as response:
283 for path in ['/v0/management/auth-files', '/v8/management/config', '/v8/management/config.yaml']:256 self.assertEqual(response.status, 404); response.read()
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: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:
285 self.assertEqual(response.status, 200)265 self.assertEqual(response.status, 200)
286 self.assertEqual(response.getheader('Cache-Control'), 'no-store')266 self.assertEqual(response.getheader('Cache-Control'), 'no-store')
287 self.assertIsNone(response.getheader('Access-Control-Allow-Origin'))267 self.assertIsNone(response.getheader('Access-Control-Allow-Origin')); response.read()
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)
311 self.assertNotIn('Cookie', self.observed[-1][2])268 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')]:269 for origin in ('', 'https://evil.example', 'null'):
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:270 with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', **({'Origin': origin} if origin else {})}) as response:
314 self.assertEqual(response.status, 200)271 self.assertEqual(response.status, 403); response.read()
315 response.read()272 with self.request('POST', '/v8/management/config', {'Cookie': 'fixture=admin', 'Origin': 'https://' + ADMIN_HOST}) as response:
316 self.assertNotIn('Authorization', self.observed[-1][2])273 self.assertEqual(response.status, 200); response.read()
317 count = len(self.observed)274 with self.request('POST', '/v8/management/config', {'Authorization': 'Bearer ' + MANAGEMENT_KEY}) as response:
318 with self.request('GET', '/v1/models') as response:275 self.assertEqual(response.status, 200); response.read()
319 self.assertEqual(response.status, 401)276
320 response.read()277 def test_native_websocket_upgrade(self):
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):
424 key = base64.b64encode(b'synthetic-16-key').decode()278 key = base64.b64encode(b'synthetic-16-key').decode()
425 with socket.create_connection(('127.0.0.1', self.port), timeout=3) as connection:279 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())
427 headers = b''281 headers = b''
428 while not headers.endswith(b'\r\n\r\n'):282 while not headers.endswith(b'\r\n\r\n'):
429 headers += connection.recv(1)283 headers += connection.recv(1)
430 self.assertIn(b' 101 ', headers.split(b'\r\n')[0])284 self.assertIn(b' 101 ', headers.split(b'\r\n')[0])
431 self.assertIn(b'upgrade: websocket', headers.lower())
432 connection.sendall(b'\x81\x82\x01\x02\x03\x04ik')285 connection.sendall(b'\x81\x82\x01\x02\x03\x04ik')
433 self.assertEqual(connection.recv(4), b'\x81\x02hi')286 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
436if __name__ == '__main__':317if __name__ == '__main__':
437 parser = argparse.ArgumentParser()318 parser = argparse.ArgumentParser()
438 parser.add_argument('--nginx', type=Path)319 parser.add_argument('--caddy', type=Path)
439 parser.add_argument('--panel', type=Path)
440 options, remaining = parser.parse_known_args()320 options, remaining = parser.parse_known_args()
441 NGINX = str(options.nginx.resolve()) if options.nginx else None321 CADDY = str(options.caddy.resolve()) if options.caddy else None
442 PANEL = options.panel.resolve() if options.panel else None322 GatewayTests.__unittest_skip__ = not CADDY
443 GatewayTests.__unittest_skip__ = not NGINX
444 unittest.main(argv=[__file__, *remaining])323 unittest.main(argv=[__file__, *remaining])
tools/cliproxy.md+112-69
...@@ -1,71 +1,114 @@...@@ -1,71 +1,114 @@
1# CLIProxyAPI1# CLIProxyAPI
22
3`cpa.paperclover.net` is the management panel, protected by Snow's3`https://cpa.paperclover.net` serves the Snow Globe CPA fork’s inference and management
4`infra-admin` role. Snow sign-in opens it automatically; nginx supplies the4APIs. Caddy protects the dashboard with Snow's `infra-admin` role. The
5independent management key to its management endpoints on the server.5`cpa-api.paperclover.net` alias serves the same native APIs, with dashboard
6`cpa-api.paperclover.net` is the client endpoint: use6paths disabled.
7`https://cpa-api.paperclover.net/v1` for OpenAI-compatible clients or7
8`https://cpa-api.paperclover.net` for Claude-compatible clients.8CPA validates its two independent keys:
99
10Nomad variable `nomad/jobs/cliproxy` owns `api_key` and `management_key`.10- The client `api_key` authenticates inference and model listing through
11Keep values in restricted private files or a password manager; do not paste them11 `Authorization: Bearer …` or the provider-compatible key headers.
12into chat, command arguments, or tracked configuration. The management key never12- The `management_key` authenticates `/v8/management/*` through
13needs to enter the browser. Opening the panel clears any previously remembered13 `Authorization: Bearer …` or `X-Management-Key`.
14native management password and validates access through the native config API.14
15Its Logout button clears the shared forward-auth cookie and opens Snow sign-in.15Nomad variable `nomad/jobs/cliproxy` owns both keys. Keep their values in
16API clients accept the separate client key through `Authorization: Bearer …`16restricted files or a password manager, never in chat, arguments, or tracked
17or `X-Api-Key`. The gateway independently enforces this one infra-managed key,17configuration. An inference key does not grant management access. A management
18even if the native panel removes its own API keys. Query-string keys are rejected.18key can modify configuration and use stored provider credentials.
1919
20## Connect providers20For OpenAI-compatible clients, use `https://cpa.paperclover.net/v1`; for
2121Claude-compatible clients, use `https://cpa.paperclover.net`. Management callers
22Open the management panel and sign in through Snow.22use `https://cpa.paperclover.net/v8/management` with the management key. Native
23Its OAuth section supports **Codex** and **Claude**. Start the23requests carrying a key bypass SSO; CPA rejects missing or invalid credentials.
24chosen provider's login and complete consent in the provider's own browser page.24The dashboard still requires SSO even when a management key is supplied.
25For remote OAuth flows that finish on an unreachable `localhost` URL, copy that25
26entire callback URL into the panel's callback form. Treat the callback URL as a26## Dashboard
27temporary credential. No callback ports or provider callbacks are exposed on27
28the client hostname. Browser GET/HEAD callback routes are blocked on the admin28Snow sign-in opens the panel automatically. Caddy supplies the management key
29hostname too: CLIProxyAPI logs URL queries, so callback credentials must be29only after an `infra-admin` SSO check, and the client key for the panel's model
30submitted through the management POST form. API-key provider credentials can also be configured in30list. Cookie-authenticated writes require the exact dashboard Origin; reads
31the native panel; their persistent home is the private service dataset.31reject a foreign Origin. Keys stay on the server, and cookies are removed before
3232proxying to CPA. Native requests with explicit key headers retain CPA's own
33`/data/config.yaml` is seeded once from a read-only Nomad template. Native33authentication and do not receive injected credentials.
34configuration edits, hashed management key, OAuth accounts, and refreshed tokens34
35remain under `/srv/prod/cliproxy/data`; restarts and normal releases retain them.35Opening the panel clears remembered native passwords and validates access
36Changing a generated Nomad key does not overwrite an existing native config:36through the config API. Logout clears the shared forward-auth cookie and opens
37coordinate key rotation in both Nomad and the native panel. Fresh staging uses37Snow sign-in. The source panel implements this SSO adaptation; the image contains
38separate storage and generated keys and must use synthetic providers only.38one immutable panel build.
3939
40## Boundary and updates40## Providers and persistence
4141
42Only the confirmed Codex/Claude client routes are public: model listing, chat,42Use the panel's OAuth section to connect providers. For a remote flow ending at
43completions, messages/counting, Responses/compact/WebSocket, and Codex search43an unreachable localhost URL, submit the entire callback URL through the panel's
44and direct aliases. Management, plugins, OAuth callbacks, unknown paths, and44callback form. Treat it as a temporary credential. API-key providers can also be
45unapproved methods return 404 on the API hostname. The app listens only on45configured in the native panel.
46container loopback; nginx publishes the sole port. Both processes exit together46
47if either fails. The host-side route requires Snow for management; nginx's47`/data/config.yaml` is seeded once from a private Nomad template. Configuration,
48admin-host routing does not replace that authentication boundary.48OAuth accounts, and refreshed tokens persist in `/srv/prod/cliproxy/data`.
49Management writes require the exact admin origin; cross-origin reads with an49The first fork start atomically migrates the native configuration, preserving
50Origin header are rejected, and upstream CORS headers are suppressed. The50provider credentials and unrelated settings. It selects reset-soonest routing,
51management key is added only to `/v0/management/` and `/v8/management/` paths.51affinity with priority reclaim, retained history, Codex-only model discovery,
52The panel's `GET /v1/models` uses the client key server-side; other client routes52and saved cooldowns; panel auto-updates and request logging stay disabled.
53and methods receive no injected key on the admin hostname.53`/data/.snowglobe-fork-v1` makes this a one-time migration, so later operator
5454changes survive restarts. The container publishes one
55The service pins CLIProxyAPI v8.0.32 and the management panel v1.26.5. Panel55CPA port through Nomad's loopback binding; Caddy is the only public entry point.
56downloads are size- and SHA-256-checked during preparation; runtime auto-update56There is no nginx container or second CPA listener.
57is disabled. Preparation patches session restoration, removes auth-store57
58persistence, permits empty-key revalidation, and redirects Logout; the resulting58Changing a Nomad key does not overwrite existing native configuration. Rotate it
59asset has its own pinned checksum. Native login still establishes authentication only after its config59in CPA and Nomad together; Caddy's private route manifest is refreshed during
60request succeeds. Request-body logging is omitted by `commercial-mode: true` and60provisioning. Fresh previews have separate storage and keys and must use
61`request-log: false`. Plugins, discovery, usage aggregation, and profiling start61synthetic providers only. Shared forward-auth does not register arbitrary preview
62disabled. The read-only image has only `/data` and a private `/tmp` writable;62callback URLs; a fresh preview cannot complete interactive sign-in through it.
63the pinned `/data/static` subdirectory is mounted read-only separately.63
6464## Fork sources and history
65Run `python3 tools/cliproxy-test.py --nginx /path/to/nginx --panel /path/to/upstream-management.html`65
66with the pinned upstream v1.26.5 asset for bootstrap, rendering, pinned-asset,66[Backend](https://shale.paperclover.net/cliproxyapi/) and
67actual HTTP/streaming/WebSocket boundaries, and the native auth-store flow.67[panel](https://shale.paperclover.net/cliproxy-panel/) fork CPA v8.0.32 and panel
68The test uses temporary synthetic credentials and a loopback fixture upstream.68v1.26.5. `service/cliproxy/build-source.json` pins the exact source revisions;
69Stage with the normal deployment command, verify anonymous management is routed69the deployment exporter builds both into one image using
70to Snow, valid/invalid client-key behavior, provider persistence on restart,70`Dockerfile.snowglobe`. Upstream panel auto-update cannot replace the baked file.
71and logs containing no fixture keys or prompts before production promotion.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):...@@ -154,6 +154,23 @@ class BuildSourceExport(unittest.TestCase):
154 self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n")154 self.assertEqual((context / "Dockerfile").read_text(), "FROM committed\n")
155 self.assertFalse((context / "src/app.dockerfile").exists())155 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
157 def test_unpinned_source_uses_working_files(self):174 def test_unpinned_source_uses_working_files(self):
158 del self.spec["revision"]175 del self.spec["revision"]
159 self.manifest.write_text(json.dumps(self.spec))176 self.manifest.write_text(json.dumps(self.spec))
tools/deploy.py+47-36
...@@ -88,48 +88,59 @@ def upload(main=False):...@@ -88,48 +88,59 @@ def upload(main=False):
88 )88 )
89 for manifest in snapshot.glob("service/*/build-source.json"):89 for manifest in snapshot.glob("service/*/build-source.json"):
90 spec = json.loads(manifest.read_text())90 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}")
94 context = manifest.parent / "build"91 context = manifest.parent / "build"
95 if context.exists():92 if context.exists():
96 raise ValueError(f"build context already exists: {manifest.parent.name}")93 raise ValueError(f"build context already exists: {manifest.parent.name}")
97 context.mkdir()94 context.mkdir()
98 paths = [*spec["include"], spec["dockerfile"]]95 for index, source_spec in enumerate(spec.get("sources", [spec])):
99 for name in paths:96 source = (REPO / source_spec["source"]).resolve(strict=True)
100 relative = Path(name)97 if not source.is_dir() or not source.is_relative_to(REPO.parent):
101 if relative.is_absolute() or ".." in relative.parts or not relative.parts:98 raise ValueError(f"invalid build source: {manifest.parent.name}")
102 raise ValueError(f"invalid build source path: {name}")99 target = Path(source_spec.get("destination", "."))
103 if "revision" in spec:100 if target.is_absolute() or ".." in target.parts or (index and not target.parts):
104 commit = spec["revision"]101 raise ValueError(f"invalid build source destination: {target}")
105 if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit):102 source_context = context / target
106 raise ValueError(f"invalid build source revision: {manifest.parent.name}")103 if source_context != context:
107 export_revision(source, commit, paths, 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 [])]
108 for name in paths:107 for name in paths:
109 if not (context / name).exists():108 relative = Path(name)
110 raise ValueError(f"missing build source path at revision: {name}")109 if relative.is_absolute() or ".." in relative.parts or not relative.parts:
111 dockerfile = context / spec["dockerfile"]110 raise ValueError(f"invalid build source path: {name}")
112 if not dockerfile.is_file():111 if "revision" in source_spec:
113 raise ValueError(f"invalid build source Dockerfile: {spec['dockerfile']}")112 commit = source_spec["revision"]
114 if dockerfile != context / "Dockerfile":113 if not isinstance(commit, str) or not re.fullmatch(r"[0-9a-f]{40}", commit):
115 if (context / "Dockerfile").exists():114 raise ValueError(f"invalid build source revision: {manifest.parent.name}")
116 raise ValueError("build source already includes Dockerfile")115 export_revision(source, commit, paths, source_context)
117 if any(dockerfile.is_relative_to(context / name) for name in spec["include"]):116 for name in paths:
118 shutil.copy2(dockerfile, context / "Dockerfile")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)
119 else:140 else:
120 dockerfile.rename(context / "Dockerfile")141 shutil.copy2(origin, destination)
121 continue142 if not (context / "Dockerfile").is_file():
122 for name in paths:143 raise ValueError(f"missing build source Dockerfile: {manifest.parent.name}")
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)
133 digest = tree_digest(snapshot)144 digest = tree_digest(snapshot)
134 release = digest[:16]145 release = digest[:16]
135 remote_release = REMOTE / "releases" / release146 remote_release = REMOTE / "releases" / release
tools/router.py+55
...@@ -40,6 +40,55 @@ def proxy(upstreams, indent, uncompressed=False, host=None, keep_streams=False):...@@ -40,6 +40,55 @@ def proxy(upstreams, indent, uncompressed=False, host=None, keep_streams=False):
40 return [*lines, f"{indent}}}"]40 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
43def shale_mcp_routes(port):92def shale_mcp_routes(port):
44 return [" @shale_mcp_link path_regexp shale_mcp_link ^/-/snowglobe-mcp/([A-Za-z0-9_-]{43})$",93 return [" @shale_mcp_link path_regexp shale_mcp_link ^/-/snowglobe-mcp/([A-Za-z0-9_-]{43})$",
45 " handle @shale_mcp_link {", " rewrite * /oauth/shale/link/{re.shale_mcp_link.1}",94 " handle @shale_mcp_link {", " rewrite * /oauth/shale/link/{re.shale_mcp_link.1}",
...@@ -288,6 +337,12 @@ def render(token):...@@ -288,6 +337,12 @@ def render(token):
288 service = next(iter(route["services"]))337 service = next(iter(route["services"]))
289 if not re.fullmatch(r"[a-z][a-z0-9-]*", service):338 if not re.fullmatch(r"[a-z][a-z0-9-]*", service):
290 raise ValueError(f"invalid service name: {service}")339 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
291 if service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):346 if service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):
292 # Keep the Origin guard before every static/proxy handle;347 # Keep the Origin guard before every static/proxy handle;
293 # otherwise Caddy sorts those handles ahead of respond.348 # otherwise Caddy sorts those handles ahead of respond.
tools/snowglobe.py+4
...@@ -373,6 +373,8 @@ def render(data, definitions):...@@ -373,6 +373,8 @@ def render(data, definitions):
373 ]373 ]
374 for task_name, task in data["containers"].items():374 for task_name, task in data["containers"].items():
375 lines += [f" task {q(task_name)} {{", ' driver = "podman"']375 lines += [f" task {q(task_name)} {{", ' driver = "podman"']
376 if task.get("killTimeout"):
377 lines.append(f" kill_timeout = {q(task['killTimeout'])}")
376 if task["lifecycle"] != "main":378 if task["lifecycle"] != "main":
377 lines += [' lifecycle {', ' hook = "prestart"',379 lines += [' lifecycle {', ' hook = "prestart"',
378 f" sidecar = {str(task['lifecycle'] == 'prestartSidecar').lower()}", ' }']380 f" sidecar = {str(task['lifecycle'] == 'prestartSidecar').lower()}", ' }']
...@@ -619,6 +621,8 @@ def provision(data):...@@ -619,6 +621,8 @@ def provision(data):
619 write_private(route_dir / f"{data['id']}.json", json.dumps({621 write_private(route_dir / f"{data['id']}.json", json.dumps({
620 "files": static, "dirs": directories, "identity": identity,622 "files": static, "dirs": directories, "identity": identity,
621 "identityProof": proof, "headHtml": head_html, "metricsPaths": metrics_paths,623 "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 {}),
622 }))626 }))
623627
624628