@lifeaitools/clauth 2.1.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.clauth-skill/references/keys-guide.md +270 -270
- package/cli/api.js +238 -238
- package/cli/commands/install.js +413 -396
- package/cli/commands/login.test.js +49 -1
- package/cli/commands/ops-install.js +11 -10
- package/cli/commands/ops.test.js +270 -0
- package/cli/commands/scrub.test.js +70 -0
- package/cli/commands/serve.js +2614 -11189
- package/cli/commands/uninstall.js +164 -164
- package/cli/dashboard/dashboard.js +592 -0
- package/cli/dashboard/global.css +65 -0
- package/cli/dashboard/panel-element.js +79 -0
- package/cli/dashboard/panels/build-status-panel.css +12 -0
- package/cli/dashboard/panels/build-status-panel.js +98 -0
- package/cli/dashboard/panels/panel-registry.js +58 -0
- package/cli/dashboard/panels/services-panel.css +95 -0
- package/cli/dashboard/panels/services-panel.js +872 -0
- package/cli/dashboard/panels/surfaces-panel.css +59 -0
- package/cli/dashboard/panels/surfaces-panel.js +474 -0
- package/cli/dashboard/panels/toolbar-panel.css +42 -0
- package/cli/dashboard/panels/toolbar-panel.js +374 -0
- package/cli/dashboard/panels/tunnel-panel.css +61 -0
- package/cli/dashboard/panels/tunnel-panel.js +688 -0
- package/cli/dashboard/panels/webdav-panel.css +38 -0
- package/cli/dashboard/panels/webdav-panel.js +166 -0
- package/cli/fingerprint.test.js +95 -0
- package/cli/http/components/callagent-terminal-component.js +310 -0
- package/cli/http/components/dashboard-component.js +123 -0
- package/cli/http/components/inbox-component.js +41 -0
- package/cli/http/components/mcp-transport-component.js +275 -0
- package/cli/http/components/oauth-component.js +207 -0
- package/cli/http/components/service-crud-component.js +288 -0
- package/cli/http/components/supervisor-component.js +104 -0
- package/cli/http/components/system-component.js +184 -0
- package/cli/http/components/tunnel-component.js +508 -0
- package/cli/http/components/vault-auth-component.js +264 -0
- package/cli/http/components/watchdog-component.js +28 -0
- package/cli/http/request-utils.js +21 -0
- package/cli/http/servers/ops-server.js +335 -0
- package/cli/index.js +5 -91
- package/cli/lib/fs-git.test.js +467 -0
- package/cli/mcp/providers/call-agent-provider.js +30 -0
- package/cli/mcp/providers/clauth-core-provider.js +461 -0
- package/cli/mcp/providers/ops-provider.js +42 -0
- package/cli/mcp/providers/terminal-provider.js +81 -0
- package/cli/mcp/tool-registry.js +48 -0
- package/cli/mcp/tool-schemas.js +822 -0
- package/cli/mcp/tool-schemas.test.js +49 -0
- package/cli/ops/pm2-preflight.js +54 -0
- package/cli/recovery.js +6 -0
- package/cli/recovery.test.js +179 -0
- package/cli/services/agent-pool-instance.js +38 -0
- package/cli/services/claude-binary.js +29 -0
- package/cli/services/delegation-lane-instance.js +35 -0
- package/cli/services/delegation-lane-instance.test.js +37 -0
- package/cli/services/file-io.js +38 -0
- package/cli/services/file-io.test.js +10 -0
- package/cli/services/fs-mount.js +66 -0
- package/cli/services/fs-mount.test.js +14 -0
- package/cli/services/fs-upload-sessions.js +20 -0
- package/cli/services/fs-upload-sessions.test.js +14 -0
- package/cli/services/git-exec.js +37 -0
- package/cli/services/git-exec.test.js +41 -0
- package/cli/services/git-import-guard.js +28 -0
- package/cli/services/git-import-guard.test.js +18 -0
- package/cli/services/service-status.js +191 -0
- package/cli/services/service-status.test.js +39 -0
- package/cli/services/terminal-dispatch.js +135 -0
- package/cli/services/terminal-dispatch.test.js +9 -0
- package/cli/services/terminal-registry.js +39 -0
- package/cli/services/terminal-registry.test.js +31 -0
- package/cli/services/terminal-sessions.js +100 -0
- package/cli/services/terminal-sessions.test.js +26 -0
- package/cli/services/terminal-window.js +106 -0
- package/cli/services/vault-read.js +14 -0
- package/cli/services/vault-read.test.js +35 -0
- package/cli/supervisor-registry.js +116 -0
- package/cli/supervisor-registry.test.js +165 -0
- package/cli/supervisor-ui.test.js +230 -30
- package/cli/webdav-config.js +84 -0
- package/cli/webdav-config.test.js +79 -0
- package/cli/webdav-obscure.js +23 -0
- package/cli/webdav-obscure.test.js +41 -0
- package/cli/webdav-rclone-discovery.js +44 -0
- package/cli/webdav-service.js +30 -118
- package/cli/webdav-service.test.js +107 -0
- package/install.ps1 +102 -102
- package/install.sh +49 -49
- package/package.json +2 -2
- package/scripts/bootstrap.cjs +121 -121
- package/supabase/functions/auth-vault/index.ts +350 -350
- package/supabase/migrations/001_clauth_schema.sql +94 -94
- package/supabase/migrations/002_vault_helpers.sql +90 -90
- package/supabase/migrations/20260317_lockout.sql +26 -26
- package/cli/assets/codevelop/launcher-active.cmd.template +0 -20
- package/cli/assets/codevelop/launcher-static.cmd.template +0 -7
- package/cli/assets/codevelop/windows-terminal.profiles.json +0 -48
- package/cli/commands/codevelop.js +0 -1190
package/cli/api.js
CHANGED
|
@@ -1,238 +1,238 @@
|
|
|
1
|
-
// cli/api.js
|
|
2
|
-
// Thin client that calls the auth-vault Edge Function
|
|
3
|
-
|
|
4
|
-
import { createRequire } from "module";
|
|
5
|
-
const require = createRequire(import.meta.url);
|
|
6
|
-
|
|
7
|
-
import Conf from "conf";
|
|
8
|
-
import { getConfOptions } from "./conf-path.js";
|
|
9
|
-
|
|
10
|
-
const config = new Conf(getConfOptions());
|
|
11
|
-
|
|
12
|
-
// ============================================================
|
|
13
|
-
// Get Edge Function base URL from local config
|
|
14
|
-
// ============================================================
|
|
15
|
-
export function getBaseUrl() {
|
|
16
|
-
const url = config.get("supabase_url") || process.env.CLAUTH_SUPABASE_URL;
|
|
17
|
-
if (!url) throw new Error("Supabase URL not configured. Run: clauth setup");
|
|
18
|
-
return `${url}/functions/v1/auth-vault`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function getAnonKey() {
|
|
22
|
-
const key = config.get("supabase_anon_key") || process.env.CLAUTH_SUPABASE_ANON_KEY;
|
|
23
|
-
if (!key) throw new Error("Supabase anon key not configured. Run: clauth setup");
|
|
24
|
-
return key;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// ============================================================
|
|
28
|
-
// Core POST helper
|
|
29
|
-
// ============================================================
|
|
30
|
-
// Bound every vault round-trip with a wall-clock timeout. Without this, a TCP
|
|
31
|
-
// stall (DNS black-hole, half-open socket — NOT a clean 4xx) hangs the caller
|
|
32
|
-
// forever. Callers that treat persistence as best-effort (e.g. the call_agent
|
|
33
|
-
// scratchpad mirror) rely on this resolving to an error rather than blocking.
|
|
34
|
-
// Override via CLAUTH_VAULT_TIMEOUT_MS; default 8s.
|
|
35
|
-
const VAULT_FETCH_TIMEOUT_MS = (() => {
|
|
36
|
-
const n = Number(process.env.CLAUTH_VAULT_TIMEOUT_MS);
|
|
37
|
-
return Number.isFinite(n) && n > 0 ? n : 8000;
|
|
38
|
-
})();
|
|
39
|
-
|
|
40
|
-
// ============================================================
|
|
41
|
-
// Backend / external-resource error model
|
|
42
|
-
// ============================================================
|
|
43
|
-
// A VaultBackendError means the vault backend (the auth-vault Edge Function or
|
|
44
|
-
// the Postgres database behind it) never rendered an auth verdict — the request
|
|
45
|
-
// timed out, the network failed, the function 5xx'd, the DB was unreachable, or
|
|
46
|
-
// we were rate-limited. These are TRANSIENT and must NEVER be counted as an
|
|
47
|
-
// authentication failure (a wrong password). Treating a database timeout as a
|
|
48
|
-
// bad password is the bug that turned a DB blip into a permanent vault lockout.
|
|
49
|
-
export class VaultBackendError extends Error {
|
|
50
|
-
constructor(kind, message, { status = null, detail = null, cause = null } = {}) {
|
|
51
|
-
super(message);
|
|
52
|
-
this.name = "VaultBackendError";
|
|
53
|
-
this.kind = kind; // timeout | network | server_error | rate_limited | db_error | unknown
|
|
54
|
-
this.isBackend = true; // marker for callers: do not strike
|
|
55
|
-
this.retriable = true; // the verdict was never rendered — safe to retry
|
|
56
|
-
this.status = status; // HTTP status if the server responded
|
|
57
|
-
this.detail = detail; // human-readable backend detail
|
|
58
|
-
if (cause) this.cause = cause;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Server `reason` strings that are genuine AUTH VERDICTS (the backend evaluated
|
|
63
|
-
// the credential and answered). These DO count — they are not backend errors.
|
|
64
|
-
const AUTH_VERDICT_REASONS = [
|
|
65
|
-
"invalid_token", "wrong_password", "invalid_password",
|
|
66
|
-
"machine_locked", "machine_disabled", "machine_not_found",
|
|
67
|
-
];
|
|
68
|
-
|
|
69
|
-
// Classify a server-supplied `reason` string. Returns a VaultBackendError kind
|
|
70
|
-
// when the reason describes a backend/transient condition, or null when the
|
|
71
|
-
// reason is a real auth verdict (or unknown — caller decides).
|
|
72
|
-
export function classifyServerReason(reason) {
|
|
73
|
-
if (!reason || typeof reason !== "string") return null;
|
|
74
|
-
const r = reason.toLowerCase();
|
|
75
|
-
if (AUTH_VERDICT_REASONS.some((v) => r.includes(v))) return null; // genuine verdict
|
|
76
|
-
if (/rate[_\s-]?limit/.test(r)) return "rate_limited";
|
|
77
|
-
if (/(database|\bdb\b|postgres|connection|pool|statement timeout|unavailable|internal|timeout|5\d\d)/.test(r)) return "db_error";
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Classify a thrown error (transport/timeout/network/typed). Returns a kind
|
|
82
|
-
// string when it is a backend/transient failure, or null when it is not.
|
|
83
|
-
export function classifyBackendError(err) {
|
|
84
|
-
if (!err) return null;
|
|
85
|
-
if (err instanceof VaultBackendError) return err.kind;
|
|
86
|
-
const name = err.name || "";
|
|
87
|
-
const msg = (err.message || "").toString();
|
|
88
|
-
if (name === "TimeoutError" || name === "AbortError" || /\babort(ed)?\b|\btim(e|ed)?\s*out\b|timeout/i.test(msg)) return "timeout";
|
|
89
|
-
if (/fetch failed|ECONNREFUSED|ENOTFOUND|ETIMEDOUT|EAI_AGAIN|ECONNRESET|EPIPE|network|socket hang/i.test(msg)) return "network";
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async function post(route, body) {
|
|
94
|
-
const url = `${getBaseUrl()}/${route}`;
|
|
95
|
-
const anonKey = getAnonKey();
|
|
96
|
-
|
|
97
|
-
let res;
|
|
98
|
-
try {
|
|
99
|
-
res = await fetch(url, {
|
|
100
|
-
method: "POST",
|
|
101
|
-
headers: {
|
|
102
|
-
"Content-Type": "application/json",
|
|
103
|
-
"Authorization": `Bearer ${anonKey}`
|
|
104
|
-
},
|
|
105
|
-
body: JSON.stringify(body),
|
|
106
|
-
signal: AbortSignal.timeout(VAULT_FETCH_TIMEOUT_MS)
|
|
107
|
-
});
|
|
108
|
-
} catch (err) {
|
|
109
|
-
// Transport-level failure — the backend never answered. Surface a typed,
|
|
110
|
-
// non-strike error so callers report "backend unreachable", not "bad password".
|
|
111
|
-
const kind = classifyBackendError(err) || "network";
|
|
112
|
-
const why = kind === "timeout"
|
|
113
|
-
? `vault backend did not respond within ${VAULT_FETCH_TIMEOUT_MS}ms`
|
|
114
|
-
: `cannot reach vault backend (${err.message || "network error"})`;
|
|
115
|
-
throw new VaultBackendError(kind, why, { detail: err.message || String(err), cause: err });
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
let data;
|
|
119
|
-
try {
|
|
120
|
-
data = await res.json();
|
|
121
|
-
} catch (err) {
|
|
122
|
-
// 2xx/5xx with a non-JSON or empty body — the function errored without a
|
|
123
|
-
// structured verdict. Transient backend condition, not an auth failure.
|
|
124
|
-
throw new VaultBackendError("server_error", `vault backend returned an unreadable response (HTTP ${res.status})`, { status: res.status, detail: err.message });
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// HTTP 5xx (or any non-ok with no structured error) = backend problem, never a verdict.
|
|
128
|
-
if (res.status >= 500 || (!res.ok && !data.error)) {
|
|
129
|
-
const reasonKind = classifyServerReason(data.reason || data.error) || "server_error";
|
|
130
|
-
throw new VaultBackendError(reasonKind, `vault backend error (HTTP ${res.status})`, { status: res.status, detail: data.error || data.reason || `HTTP ${res.status}` });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// 4xx WITH a structured error: could be a real verdict (invalid_token) or a
|
|
134
|
-
// transient backend signal surfaced as 4xx (rate_limited). Promote the latter
|
|
135
|
-
// to a typed backend error; leave genuine verdicts in `data` for the caller.
|
|
136
|
-
if (data.error) {
|
|
137
|
-
const reasonKind = classifyServerReason(data.reason || data.error);
|
|
138
|
-
if (reasonKind) {
|
|
139
|
-
throw new VaultBackendError(reasonKind, `vault backend ${reasonKind.replace("_", " ")}`, { status: res.status, detail: data.error || data.reason });
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return data;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// ============================================================
|
|
147
|
-
// Auth-bearing calls (require HMAC token)
|
|
148
|
-
// ============================================================
|
|
149
|
-
async function authPost(route, password, machineHash, token, timestamp, extra = {}) {
|
|
150
|
-
return post(route, {
|
|
151
|
-
machine_hash: machineHash,
|
|
152
|
-
token,
|
|
153
|
-
timestamp,
|
|
154
|
-
password,
|
|
155
|
-
...extra
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// ============================================================
|
|
160
|
-
// Exported API surface
|
|
161
|
-
// ============================================================
|
|
162
|
-
|
|
163
|
-
export async function retrieve(password, machineHash, token, timestamp, service) {
|
|
164
|
-
return authPost("retrieve", password, machineHash, token, timestamp, { service });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export async function write(password, machineHash, token, timestamp, service, value) {
|
|
168
|
-
return authPost("write", password, machineHash, token, timestamp, { service, value });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export async function enable(password, machineHash, token, timestamp, service, enabled) {
|
|
172
|
-
return authPost("enable", password, machineHash, token, timestamp, { service, enabled });
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export async function addService(password, machineHash, token, timestamp, name, label, key_type, description, project) {
|
|
176
|
-
const extra = { name, label, key_type, description };
|
|
177
|
-
if (project) extra.project = project;
|
|
178
|
-
return authPost("add", password, machineHash, token, timestamp, extra);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export async function updateService(password, machineHash, token, timestamp, service, updates) {
|
|
182
|
-
return authPost("update", password, machineHash, token, timestamp, { service, ...updates });
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export async function removeService(password, machineHash, token, timestamp, service, confirm) {
|
|
186
|
-
return authPost("remove", password, machineHash, token, timestamp, { service, confirm });
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export async function revoke(password, machineHash, token, timestamp, service, confirm) {
|
|
190
|
-
return authPost("revoke", password, machineHash, token, timestamp, { service, confirm });
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export async function status(password, machineHash, token, timestamp, project) {
|
|
194
|
-
const extra = {};
|
|
195
|
-
if (project) extra.project = project;
|
|
196
|
-
return authPost("status", password, machineHash, token, timestamp, extra);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export async function test(password, machineHash, token, timestamp) {
|
|
200
|
-
return authPost("test", password, machineHash, token, timestamp);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export async function changePassword(password, machineHash, token, timestamp, newSeedHash) {
|
|
204
|
-
return authPost("change-password", password, machineHash, token, timestamp, { new_hmac_seed_hash: newSeedHash });
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export async function createEnrollment(password, machineHash, token, timestamp, label, ttlMinutes, installId) {
|
|
208
|
-
const extra = {};
|
|
209
|
-
if (label) extra.label = label;
|
|
210
|
-
if (ttlMinutes) extra.ttl_minutes = ttlMinutes;
|
|
211
|
-
if (installId) extra.install_id = installId;
|
|
212
|
-
return authPost("create-enrollment", password, machineHash, token, timestamp, extra);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export async function registerMachine(machineHash, seedHash, label, adminToken, extras = {}) {
|
|
216
|
-
return post("register-machine", {
|
|
217
|
-
machine_hash: machineHash,
|
|
218
|
-
hmac_seed_hash: seedHash,
|
|
219
|
-
label,
|
|
220
|
-
admin_token: adminToken,
|
|
221
|
-
...extras
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export async function redeemEnrollment(machineHash, seedHash, label, enrollmentCode) {
|
|
226
|
-
return post("redeem-enrollment", {
|
|
227
|
-
machine_hash: machineHash,
|
|
228
|
-
hmac_seed_hash: seedHash,
|
|
229
|
-
label,
|
|
230
|
-
enrollment_code: enrollmentCode
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export default {
|
|
235
|
-
retrieve, write, enable, addService, updateService, removeService, revoke,
|
|
236
|
-
status, test, createEnrollment, registerMachine, redeemEnrollment, getBaseUrl, getAnonKey,
|
|
237
|
-
VaultBackendError, classifyBackendError, classifyServerReason
|
|
238
|
-
};
|
|
1
|
+
// cli/api.js
|
|
2
|
+
// Thin client that calls the auth-vault Edge Function
|
|
3
|
+
|
|
4
|
+
import { createRequire } from "module";
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
|
|
7
|
+
import Conf from "conf";
|
|
8
|
+
import { getConfOptions } from "./conf-path.js";
|
|
9
|
+
|
|
10
|
+
const config = new Conf(getConfOptions());
|
|
11
|
+
|
|
12
|
+
// ============================================================
|
|
13
|
+
// Get Edge Function base URL from local config
|
|
14
|
+
// ============================================================
|
|
15
|
+
export function getBaseUrl() {
|
|
16
|
+
const url = config.get("supabase_url") || process.env.CLAUTH_SUPABASE_URL;
|
|
17
|
+
if (!url) throw new Error("Supabase URL not configured. Run: clauth setup");
|
|
18
|
+
return `${url}/functions/v1/auth-vault`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getAnonKey() {
|
|
22
|
+
const key = config.get("supabase_anon_key") || process.env.CLAUTH_SUPABASE_ANON_KEY;
|
|
23
|
+
if (!key) throw new Error("Supabase anon key not configured. Run: clauth setup");
|
|
24
|
+
return key;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ============================================================
|
|
28
|
+
// Core POST helper
|
|
29
|
+
// ============================================================
|
|
30
|
+
// Bound every vault round-trip with a wall-clock timeout. Without this, a TCP
|
|
31
|
+
// stall (DNS black-hole, half-open socket — NOT a clean 4xx) hangs the caller
|
|
32
|
+
// forever. Callers that treat persistence as best-effort (e.g. the call_agent
|
|
33
|
+
// scratchpad mirror) rely on this resolving to an error rather than blocking.
|
|
34
|
+
// Override via CLAUTH_VAULT_TIMEOUT_MS; default 8s.
|
|
35
|
+
const VAULT_FETCH_TIMEOUT_MS = (() => {
|
|
36
|
+
const n = Number(process.env.CLAUTH_VAULT_TIMEOUT_MS);
|
|
37
|
+
return Number.isFinite(n) && n > 0 ? n : 8000;
|
|
38
|
+
})();
|
|
39
|
+
|
|
40
|
+
// ============================================================
|
|
41
|
+
// Backend / external-resource error model
|
|
42
|
+
// ============================================================
|
|
43
|
+
// A VaultBackendError means the vault backend (the auth-vault Edge Function or
|
|
44
|
+
// the Postgres database behind it) never rendered an auth verdict — the request
|
|
45
|
+
// timed out, the network failed, the function 5xx'd, the DB was unreachable, or
|
|
46
|
+
// we were rate-limited. These are TRANSIENT and must NEVER be counted as an
|
|
47
|
+
// authentication failure (a wrong password). Treating a database timeout as a
|
|
48
|
+
// bad password is the bug that turned a DB blip into a permanent vault lockout.
|
|
49
|
+
export class VaultBackendError extends Error {
|
|
50
|
+
constructor(kind, message, { status = null, detail = null, cause = null } = {}) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = "VaultBackendError";
|
|
53
|
+
this.kind = kind; // timeout | network | server_error | rate_limited | db_error | unknown
|
|
54
|
+
this.isBackend = true; // marker for callers: do not strike
|
|
55
|
+
this.retriable = true; // the verdict was never rendered — safe to retry
|
|
56
|
+
this.status = status; // HTTP status if the server responded
|
|
57
|
+
this.detail = detail; // human-readable backend detail
|
|
58
|
+
if (cause) this.cause = cause;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Server `reason` strings that are genuine AUTH VERDICTS (the backend evaluated
|
|
63
|
+
// the credential and answered). These DO count — they are not backend errors.
|
|
64
|
+
const AUTH_VERDICT_REASONS = [
|
|
65
|
+
"invalid_token", "wrong_password", "invalid_password",
|
|
66
|
+
"machine_locked", "machine_disabled", "machine_not_found",
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
// Classify a server-supplied `reason` string. Returns a VaultBackendError kind
|
|
70
|
+
// when the reason describes a backend/transient condition, or null when the
|
|
71
|
+
// reason is a real auth verdict (or unknown — caller decides).
|
|
72
|
+
export function classifyServerReason(reason) {
|
|
73
|
+
if (!reason || typeof reason !== "string") return null;
|
|
74
|
+
const r = reason.toLowerCase();
|
|
75
|
+
if (AUTH_VERDICT_REASONS.some((v) => r.includes(v))) return null; // genuine verdict
|
|
76
|
+
if (/rate[_\s-]?limit/.test(r)) return "rate_limited";
|
|
77
|
+
if (/(database|\bdb\b|postgres|connection|pool|statement timeout|unavailable|internal|timeout|5\d\d)/.test(r)) return "db_error";
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Classify a thrown error (transport/timeout/network/typed). Returns a kind
|
|
82
|
+
// string when it is a backend/transient failure, or null when it is not.
|
|
83
|
+
export function classifyBackendError(err) {
|
|
84
|
+
if (!err) return null;
|
|
85
|
+
if (err instanceof VaultBackendError) return err.kind;
|
|
86
|
+
const name = err.name || "";
|
|
87
|
+
const msg = (err.message || "").toString();
|
|
88
|
+
if (name === "TimeoutError" || name === "AbortError" || /\babort(ed)?\b|\btim(e|ed)?\s*out\b|timeout/i.test(msg)) return "timeout";
|
|
89
|
+
if (/fetch failed|ECONNREFUSED|ENOTFOUND|ETIMEDOUT|EAI_AGAIN|ECONNRESET|EPIPE|network|socket hang/i.test(msg)) return "network";
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function post(route, body) {
|
|
94
|
+
const url = `${getBaseUrl()}/${route}`;
|
|
95
|
+
const anonKey = getAnonKey();
|
|
96
|
+
|
|
97
|
+
let res;
|
|
98
|
+
try {
|
|
99
|
+
res = await fetch(url, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
"Authorization": `Bearer ${anonKey}`
|
|
104
|
+
},
|
|
105
|
+
body: JSON.stringify(body),
|
|
106
|
+
signal: AbortSignal.timeout(VAULT_FETCH_TIMEOUT_MS)
|
|
107
|
+
});
|
|
108
|
+
} catch (err) {
|
|
109
|
+
// Transport-level failure — the backend never answered. Surface a typed,
|
|
110
|
+
// non-strike error so callers report "backend unreachable", not "bad password".
|
|
111
|
+
const kind = classifyBackendError(err) || "network";
|
|
112
|
+
const why = kind === "timeout"
|
|
113
|
+
? `vault backend did not respond within ${VAULT_FETCH_TIMEOUT_MS}ms`
|
|
114
|
+
: `cannot reach vault backend (${err.message || "network error"})`;
|
|
115
|
+
throw new VaultBackendError(kind, why, { detail: err.message || String(err), cause: err });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let data;
|
|
119
|
+
try {
|
|
120
|
+
data = await res.json();
|
|
121
|
+
} catch (err) {
|
|
122
|
+
// 2xx/5xx with a non-JSON or empty body — the function errored without a
|
|
123
|
+
// structured verdict. Transient backend condition, not an auth failure.
|
|
124
|
+
throw new VaultBackendError("server_error", `vault backend returned an unreadable response (HTTP ${res.status})`, { status: res.status, detail: err.message });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// HTTP 5xx (or any non-ok with no structured error) = backend problem, never a verdict.
|
|
128
|
+
if (res.status >= 500 || (!res.ok && !data.error)) {
|
|
129
|
+
const reasonKind = classifyServerReason(data.reason || data.error) || "server_error";
|
|
130
|
+
throw new VaultBackendError(reasonKind, `vault backend error (HTTP ${res.status})`, { status: res.status, detail: data.error || data.reason || `HTTP ${res.status}` });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 4xx WITH a structured error: could be a real verdict (invalid_token) or a
|
|
134
|
+
// transient backend signal surfaced as 4xx (rate_limited). Promote the latter
|
|
135
|
+
// to a typed backend error; leave genuine verdicts in `data` for the caller.
|
|
136
|
+
if (data.error) {
|
|
137
|
+
const reasonKind = classifyServerReason(data.reason || data.error);
|
|
138
|
+
if (reasonKind) {
|
|
139
|
+
throw new VaultBackendError(reasonKind, `vault backend ${reasonKind.replace("_", " ")}`, { status: res.status, detail: data.error || data.reason });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return data;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ============================================================
|
|
147
|
+
// Auth-bearing calls (require HMAC token)
|
|
148
|
+
// ============================================================
|
|
149
|
+
async function authPost(route, password, machineHash, token, timestamp, extra = {}) {
|
|
150
|
+
return post(route, {
|
|
151
|
+
machine_hash: machineHash,
|
|
152
|
+
token,
|
|
153
|
+
timestamp,
|
|
154
|
+
password,
|
|
155
|
+
...extra
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ============================================================
|
|
160
|
+
// Exported API surface
|
|
161
|
+
// ============================================================
|
|
162
|
+
|
|
163
|
+
export async function retrieve(password, machineHash, token, timestamp, service) {
|
|
164
|
+
return authPost("retrieve", password, machineHash, token, timestamp, { service });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function write(password, machineHash, token, timestamp, service, value) {
|
|
168
|
+
return authPost("write", password, machineHash, token, timestamp, { service, value });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function enable(password, machineHash, token, timestamp, service, enabled) {
|
|
172
|
+
return authPost("enable", password, machineHash, token, timestamp, { service, enabled });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function addService(password, machineHash, token, timestamp, name, label, key_type, description, project) {
|
|
176
|
+
const extra = { name, label, key_type, description };
|
|
177
|
+
if (project) extra.project = project;
|
|
178
|
+
return authPost("add", password, machineHash, token, timestamp, extra);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function updateService(password, machineHash, token, timestamp, service, updates) {
|
|
182
|
+
return authPost("update", password, machineHash, token, timestamp, { service, ...updates });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function removeService(password, machineHash, token, timestamp, service, confirm) {
|
|
186
|
+
return authPost("remove", password, machineHash, token, timestamp, { service, confirm });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function revoke(password, machineHash, token, timestamp, service, confirm) {
|
|
190
|
+
return authPost("revoke", password, machineHash, token, timestamp, { service, confirm });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export async function status(password, machineHash, token, timestamp, project) {
|
|
194
|
+
const extra = {};
|
|
195
|
+
if (project) extra.project = project;
|
|
196
|
+
return authPost("status", password, machineHash, token, timestamp, extra);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function test(password, machineHash, token, timestamp) {
|
|
200
|
+
return authPost("test", password, machineHash, token, timestamp);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function changePassword(password, machineHash, token, timestamp, newSeedHash) {
|
|
204
|
+
return authPost("change-password", password, machineHash, token, timestamp, { new_hmac_seed_hash: newSeedHash });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function createEnrollment(password, machineHash, token, timestamp, label, ttlMinutes, installId) {
|
|
208
|
+
const extra = {};
|
|
209
|
+
if (label) extra.label = label;
|
|
210
|
+
if (ttlMinutes) extra.ttl_minutes = ttlMinutes;
|
|
211
|
+
if (installId) extra.install_id = installId;
|
|
212
|
+
return authPost("create-enrollment", password, machineHash, token, timestamp, extra);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function registerMachine(machineHash, seedHash, label, adminToken, extras = {}) {
|
|
216
|
+
return post("register-machine", {
|
|
217
|
+
machine_hash: machineHash,
|
|
218
|
+
hmac_seed_hash: seedHash,
|
|
219
|
+
label,
|
|
220
|
+
admin_token: adminToken,
|
|
221
|
+
...extras
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export async function redeemEnrollment(machineHash, seedHash, label, enrollmentCode) {
|
|
226
|
+
return post("redeem-enrollment", {
|
|
227
|
+
machine_hash: machineHash,
|
|
228
|
+
hmac_seed_hash: seedHash,
|
|
229
|
+
label,
|
|
230
|
+
enrollment_code: enrollmentCode
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export default {
|
|
235
|
+
retrieve, write, enable, addService, updateService, removeService, revoke,
|
|
236
|
+
status, test, createEnrollment, registerMachine, redeemEnrollment, getBaseUrl, getAnonKey,
|
|
237
|
+
VaultBackendError, classifyBackendError, classifyServerReason
|
|
238
|
+
};
|