@openparachute/agent 0.2.2 → 0.2.3-rc.11
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/.parachute/module.json +3 -3
- package/package.json +4 -1
- package/src/agent-defs.ts +9 -0
- package/src/auth.ts +182 -14
- package/src/backends/programmatic.ts +35 -2
- package/src/backends/registry.ts +159 -40
- package/src/backends/types.ts +44 -0
- package/src/daemon.ts +317 -12
- package/src/def-vault-triggers.ts +317 -0
- package/src/preflight.ts +139 -0
- package/src/spawn-agent.ts +16 -0
- package/src/step-up.ts +316 -0
- package/src/terminal-ui.ts +73 -0
- package/src/transports/http-ui.ts +10 -8
- package/src/transports/vault.ts +48 -27
- package/src/ui-kit.ts +6 -3
- package/src/ui-ticket.ts +121 -0
- package/web/ui/dist/assets/index-Dhr5Kl_d.css +1 -0
- package/web/ui/dist/assets/index-Di5MmFZR.js +60 -0
- package/web/ui/dist/index.html +2 -2
- package/src/_parked/interactive-spawn.test.ts +0 -324
- package/src/_parked/interactive-spawn.ts +0 -701
- package/src/agent-defs.test.ts +0 -1504
- package/src/agent-mcp-config.test.ts +0 -115
- package/src/agents.test.ts +0 -360
- package/src/auth.test.ts +0 -46
- package/src/backends/attached-queue.test.ts +0 -376
- package/src/backends/programmatic.test.ts +0 -1715
- package/src/backends/registry.test.ts +0 -1494
- package/src/backends/stream-json.test.ts +0 -570
- package/src/channel-backend-wiring.test.ts +0 -237
- package/src/credentials.test.ts +0 -274
- package/src/cron.test.ts +0 -342
- package/src/daemon-agent-def-api.test.ts +0 -166
- package/src/daemon-agent-defs-api.test.ts +0 -953
- package/src/daemon-agent-env-api.test.ts +0 -338
- package/src/daemon-attached-queue-store.test.ts +0 -65
- package/src/daemon-config-api.test.ts +0 -962
- package/src/daemon-jobs-api.test.ts +0 -271
- package/src/daemon-vault-chat.test.ts +0 -250
- package/src/daemon.test.ts +0 -746
- package/src/def-vaults.test.ts +0 -136
- package/src/delivery-state.test.ts +0 -110
- package/src/effective-env.test.ts +0 -114
- package/src/grants.test.ts +0 -638
- package/src/hub-jwt.test.ts +0 -161
- package/src/jobs.test.ts +0 -245
- package/src/mcp-http.test.ts +0 -265
- package/src/mint-token.test.ts +0 -152
- package/src/module-manifest.test.ts +0 -158
- package/src/programmatic-wiring.test.ts +0 -838
- package/src/registry.test.ts +0 -227
- package/src/resolve-port.test.ts +0 -64
- package/src/routing.test.ts +0 -184
- package/src/runner.test.ts +0 -506
- package/src/sandbox/config.test.ts +0 -150
- package/src/sandbox/egress.test.ts +0 -113
- package/src/sandbox/live-seatbelt.test.ts +0 -277
- package/src/sandbox/mounts.test.ts +0 -154
- package/src/sandbox/sandbox.test.ts +0 -168
- package/src/services-manifest.test.ts +0 -106
- package/src/spa-serve.test.ts +0 -116
- package/src/spawn-agent-cli.test.ts +0 -172
- package/src/spawn-agent.test.ts +0 -1218
- package/src/spawn-deps.test.ts +0 -54
- package/src/terminal-assets.test.ts +0 -50
- package/src/terminal.test.ts +0 -530
- package/src/transports/http-ui.test.ts +0 -455
- package/src/transports/telegram.test.ts +0 -174
- package/src/transports/vault.test.ts +0 -2011
- package/src/ui-kit.test.ts +0 -178
- package/web/ui/dist/assets/index-C-iWdFFV.css +0 -1
- package/web/ui/dist/assets/index-VFETBk0a.js +0 -60
- package/web/ui/tsconfig.json +0 -21
|
@@ -1,962 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PR 2 of the frictionless-channel-setup arc — config-management API + webhook
|
|
3
|
-
* JWT auth + the prescribed trigger template.
|
|
4
|
-
*
|
|
5
|
-
* These cover the THREE new surfaces, all on the real daemon fetch handler:
|
|
6
|
-
*
|
|
7
|
-
* A. Webhook JWT auth on POST /api/vault/inbound — a hub JWT (aud:agent,
|
|
8
|
-
* scope agent:send) is accepted and routes the note; the DEPRECATED
|
|
9
|
-
* ?secret= fallback still works; neither → 401; an insufficient scope → 401.
|
|
10
|
-
* A legacy aud:channel / channel:send token is ALSO accepted (dual-accept).
|
|
11
|
-
* B. Config-management API (agent:admin) — POST writes channels.json (600
|
|
12
|
-
* perms) + hot-adds the channel live (a subsequent inbound routes without a
|
|
13
|
-
* restart); GET never leaks token/secret; DELETE removes from file + stops
|
|
14
|
-
* routing; other scopes → 403.
|
|
15
|
-
* C. AGENT_VAULT_TRIGGER_TEMPLATE is exposed via /.parachute/config.
|
|
16
|
-
*
|
|
17
|
-
* The hub JWT validator is stubbed (sentinel tokens → fixed scope sets) so the
|
|
18
|
-
* accept paths run without a live hub/JWKS. The no-token reject still hits the
|
|
19
|
-
* real short-circuit. This mirrors the http-ui Layer-2 test's approach.
|
|
20
|
-
*/
|
|
21
|
-
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
|
|
22
|
-
import { mkdtempSync, rmSync, existsSync, readFileSync, statSync } from "fs";
|
|
23
|
-
import { join } from "path";
|
|
24
|
-
import { tmpdir } from "os";
|
|
25
|
-
// Re-export the REAL error class + shape helper from scope-guard in the mock
|
|
26
|
-
// below, so this `mock.module` (which Bun applies process-wide) doesn't break
|
|
27
|
-
// hub-jwt.test.ts's assertions on the genuine HubJwtError(code, message) shape.
|
|
28
|
-
import { HubJwtError, looksLikeJwt } from "@openparachute/scope-guard";
|
|
29
|
-
|
|
30
|
-
const SEND_TOKEN = "test-send-token"; // agent:send (the trigger token)
|
|
31
|
-
const ADMIN_TOKEN = "test-admin-token"; // agent:admin (config-mgmt)
|
|
32
|
-
const READ_TOKEN = "test-read-token"; // agent:read only (insufficient)
|
|
33
|
-
// A pre-rename token: aud "channel" + the legacy channel:send scope. It must
|
|
34
|
-
// STILL authorize the agent:send-gated webhook via requireScope's dual-accept.
|
|
35
|
-
const LEGACY_SEND_TOKEN = "test-legacy-send-token";
|
|
36
|
-
mock.module("./hub-jwt.ts", () => ({
|
|
37
|
-
// NEW canonical audience + the deprecated alias kept so nothing breaks.
|
|
38
|
-
AGENT_AUDIENCE: "agent",
|
|
39
|
-
CHANNEL_AUDIENCE: "channel",
|
|
40
|
-
async validateHubJwt(token: string) {
|
|
41
|
-
// NEW-style tokens: aud "agent", agent:* scopes.
|
|
42
|
-
const base = { sub: "test", aud: "agent", jti: undefined, clientId: undefined, vaultScope: undefined };
|
|
43
|
-
if (token === SEND_TOKEN) return { ...base, scopes: ["agent:send"] };
|
|
44
|
-
if (token === ADMIN_TOKEN) return { ...base, scopes: ["agent:admin"] };
|
|
45
|
-
if (token === READ_TOKEN) return { ...base, scopes: ["agent:read"] };
|
|
46
|
-
// LEGACY token: aud "channel" + channel:send — the dual-accept path.
|
|
47
|
-
if (token === LEGACY_SEND_TOKEN) {
|
|
48
|
-
return { sub: "test", aud: "channel", jti: undefined, clientId: undefined, vaultScope: undefined, scopes: ["channel:send"] };
|
|
49
|
-
}
|
|
50
|
-
throw new HubJwtError("issuer", "invalid token");
|
|
51
|
-
},
|
|
52
|
-
HubJwtError,
|
|
53
|
-
looksLikeJwt,
|
|
54
|
-
resetJwksCache() {},
|
|
55
|
-
resetRevocationCache() {},
|
|
56
|
-
}));
|
|
57
|
-
|
|
58
|
-
import { createFetchHandler } from "./daemon.ts";
|
|
59
|
-
import { ClientRegistry } from "./routing.ts";
|
|
60
|
-
import { VaultTransport, AGENT_VAULT_TRIGGER_TEMPLATE } from "./transports/vault.ts";
|
|
61
|
-
import { channelsFilePath } from "./registry.ts";
|
|
62
|
-
import type { Channel } from "./registry.ts";
|
|
63
|
-
import {
|
|
64
|
-
credentialsFilePath,
|
|
65
|
-
resolveClaudeCredential,
|
|
66
|
-
resolveChannelEnv,
|
|
67
|
-
describeChannelEnv,
|
|
68
|
-
} from "./credentials.ts";
|
|
69
|
-
import type { TransportContext, InboundMessage } from "./transport.ts";
|
|
70
|
-
|
|
71
|
-
const sendAuth = { authorization: "Bearer " + SEND_TOKEN } as const;
|
|
72
|
-
const adminAuth = { authorization: "Bearer " + ADMIN_TOKEN } as const;
|
|
73
|
-
const readAuth = { authorization: "Bearer " + READ_TOKEN } as const;
|
|
74
|
-
const legacySendAuth = { authorization: "Bearer " + LEGACY_SEND_TOKEN } as const;
|
|
75
|
-
|
|
76
|
-
let stateDir: string;
|
|
77
|
-
|
|
78
|
-
beforeEach(() => {
|
|
79
|
-
// Sandbox channels.json under a throwaway state dir — the config API resolves
|
|
80
|
-
// STATE_DIR from PARACHUTE_AGENT_STATE_DIR (read once at daemon module load),
|
|
81
|
-
// so set it BEFORE importing... but the module is already imported. Instead the
|
|
82
|
-
// daemon's STATE_DIR is captured at module init; we set the env to a temp dir
|
|
83
|
-
// and the tests that touch the file assert under that path. Re-resolve per test.
|
|
84
|
-
stateDir = mkdtempSync(join(tmpdir(), "agent-cfg-"));
|
|
85
|
-
process.env.PARACHUTE_AGENT_STATE_DIR = stateDir;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
afterEach(() => {
|
|
89
|
-
try {
|
|
90
|
-
rmSync(stateDir, { recursive: true, force: true });
|
|
91
|
-
} catch {}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// ---------------------------------------------------------------------------
|
|
95
|
-
// A vault channel + a recording ctx, wired through the real fetch handler.
|
|
96
|
-
// ---------------------------------------------------------------------------
|
|
97
|
-
// `secret`: a string → that shared secret; `null` → a JWT-only channel with NO
|
|
98
|
-
// webhookSecret configured; omitted → defaults to "s3cret".
|
|
99
|
-
function buildServer(initial: Array<{ name: string; secret?: string | null }> = [{ name: "eng", secret: "s3cret" }]) {
|
|
100
|
-
const registry = new ClientRegistry();
|
|
101
|
-
const channels = new Map<string, Channel>();
|
|
102
|
-
const emitted: InboundMessage[] = [];
|
|
103
|
-
for (const { name, secret } of initial) {
|
|
104
|
-
const transport = new VaultTransport({
|
|
105
|
-
vault: "default",
|
|
106
|
-
vaultUrl: "http://127.0.0.1:1940",
|
|
107
|
-
token: "x",
|
|
108
|
-
// null → omit (JWT-only channel); undefined → default "s3cret".
|
|
109
|
-
...(secret === null ? {} : { webhookSecret: secret ?? "s3cret" }),
|
|
110
|
-
});
|
|
111
|
-
const ctx: TransportContext = {
|
|
112
|
-
channel: name,
|
|
113
|
-
emit(msg) {
|
|
114
|
-
emitted.push(msg);
|
|
115
|
-
},
|
|
116
|
-
emitPermissionVerdict() {},
|
|
117
|
-
};
|
|
118
|
-
void transport.start(ctx);
|
|
119
|
-
channels.set(name, { name, transport, entry: { name, transport: "vault", config: { vault: "default" } } });
|
|
120
|
-
}
|
|
121
|
-
const srv = Bun.serve({ port: 0, hostname: "127.0.0.1", idleTimeout: 0, fetch: createFetchHandler(channels, registry) });
|
|
122
|
-
return { srv, base: `http://127.0.0.1:${srv.port}`, emitted, channels };
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function inboundBody(noteId: string, channel = "eng") {
|
|
126
|
-
return JSON.stringify({
|
|
127
|
-
trigger: "channel-inbound",
|
|
128
|
-
event: "created",
|
|
129
|
-
note: {
|
|
130
|
-
id: noteId,
|
|
131
|
-
path: `channel/${channel}/${noteId}`,
|
|
132
|
-
content: "wake up session",
|
|
133
|
-
tags: ["#agent/message", "#agent/message/inbound"],
|
|
134
|
-
metadata: { channel, direction: "inbound", sender: "aaron" },
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// ===========================================================================
|
|
140
|
-
// A. Webhook JWT auth on POST /api/vault/inbound
|
|
141
|
-
// ===========================================================================
|
|
142
|
-
describe("A — webhook hub-JWT auth (agent:send), secret fallback retained", () => {
|
|
143
|
-
test("valid agent:send JWT → 200 + routes the note (emits)", async () => {
|
|
144
|
-
const { srv, base, emitted } = buildServer();
|
|
145
|
-
try {
|
|
146
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
147
|
-
method: "POST",
|
|
148
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
149
|
-
body: inboundBody("jwt-1"),
|
|
150
|
-
});
|
|
151
|
-
expect(res.status).toBe(200);
|
|
152
|
-
expect(await res.json()).toEqual({ ok: true });
|
|
153
|
-
expect(emitted).toHaveLength(1);
|
|
154
|
-
expect(emitted[0]!.channel).toBe("eng");
|
|
155
|
-
expect(emitted[0]!.meta.note_id).toBe("jwt-1");
|
|
156
|
-
} finally {
|
|
157
|
-
srv.stop(true);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
test("DUAL-ACCEPT: a LEGACY channel:send JWT (aud:channel) STILL authorizes the agent:send-gated webhook → 200 + emits", async () => {
|
|
162
|
-
// requireScope dual-accepts a pre-rename `channel:<verb>` scope for the
|
|
163
|
-
// matching `agent:<verb>` gate, and validateHubJwt accepts aud "channel" in
|
|
164
|
-
// addition to "agent". So a token minted before the channel→agent rename
|
|
165
|
-
// (aud "channel", scope "channel:send") must keep routing inbound notes
|
|
166
|
-
// until it's re-minted — no flag-day for live triggers.
|
|
167
|
-
const { srv, base, emitted } = buildServer();
|
|
168
|
-
try {
|
|
169
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
170
|
-
method: "POST",
|
|
171
|
-
headers: { "content-type": "application/json", ...legacySendAuth },
|
|
172
|
-
body: inboundBody("legacy-1"),
|
|
173
|
-
});
|
|
174
|
-
expect(res.status).toBe(200);
|
|
175
|
-
expect(await res.json()).toEqual({ ok: true });
|
|
176
|
-
expect(emitted).toHaveLength(1);
|
|
177
|
-
expect(emitted[0]!.channel).toBe("eng");
|
|
178
|
-
expect(emitted[0]!.meta.note_id).toBe("legacy-1");
|
|
179
|
-
} finally {
|
|
180
|
-
srv.stop(true);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test("an agent:read-only JWT → 401 (uniform on the webhook — no scope/channel probing), no emit", async () => {
|
|
185
|
-
// The webhook collapses insufficient-scope to a uniform 401 (unlike the
|
|
186
|
-
// operator-facing config API, which returns 403). This tailnet-reachable
|
|
187
|
-
// endpoint stays opaque to scope/channel enumeration.
|
|
188
|
-
const { srv, base, emitted } = buildServer();
|
|
189
|
-
try {
|
|
190
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
191
|
-
method: "POST",
|
|
192
|
-
headers: { "content-type": "application/json", ...readAuth },
|
|
193
|
-
body: inboundBody("jwt-ro"),
|
|
194
|
-
});
|
|
195
|
-
expect(res.status).toBe(401);
|
|
196
|
-
expect(emitted).toHaveLength(0);
|
|
197
|
-
} finally {
|
|
198
|
-
srv.stop(true);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
test("a bad/garbage Bearer → 401, no emit", async () => {
|
|
203
|
-
const { srv, base, emitted } = buildServer();
|
|
204
|
-
try {
|
|
205
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
206
|
-
method: "POST",
|
|
207
|
-
headers: { "content-type": "application/json", authorization: "Bearer nope" },
|
|
208
|
-
body: inboundBody("jwt-bad"),
|
|
209
|
-
});
|
|
210
|
-
expect(res.status).toBe(401);
|
|
211
|
-
expect(emitted).toHaveLength(0);
|
|
212
|
-
} finally {
|
|
213
|
-
srv.stop(true);
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
test("no Bearer AND no secret → 401, no emit", async () => {
|
|
218
|
-
const { srv, base, emitted } = buildServer();
|
|
219
|
-
try {
|
|
220
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
221
|
-
method: "POST",
|
|
222
|
-
headers: { "content-type": "application/json" },
|
|
223
|
-
body: inboundBody("none"),
|
|
224
|
-
});
|
|
225
|
-
expect(res.status).toBe(401);
|
|
226
|
-
expect(emitted).toHaveLength(0);
|
|
227
|
-
} finally {
|
|
228
|
-
srv.stop(true);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
test("M1 — whitespace-only `Authorization: Bearer ` + a VALID ?secret= → 401 (no fallthrough to the secret path)", async () => {
|
|
233
|
-
// An Authorization header present-but-empty must take the JWT path and fail
|
|
234
|
-
// hard — it must NOT fall through to the deprecated ?secret= path even though
|
|
235
|
-
// the secret is correct. Branching on header PRESENCE (not token truthiness)
|
|
236
|
-
// is what closes this auth-confusion.
|
|
237
|
-
const { srv, base, emitted } = buildServer([{ name: "eng", secret: "s3cret" }]);
|
|
238
|
-
try {
|
|
239
|
-
const res = await fetch(`${base}/api/vault/inbound?secret=s3cret`, {
|
|
240
|
-
method: "POST",
|
|
241
|
-
headers: { "content-type": "application/json", authorization: "Bearer " },
|
|
242
|
-
body: inboundBody("ws-1"),
|
|
243
|
-
});
|
|
244
|
-
expect(res.status).toBe(401);
|
|
245
|
-
expect(emitted).toHaveLength(0);
|
|
246
|
-
} finally {
|
|
247
|
-
srv.stop(true);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
test("M2 — a JWT-only channel (NO webhookSecret) accepts a valid agent:send JWT → 200 + emits", async () => {
|
|
252
|
-
const { srv, base, emitted } = buildServer([{ name: "eng", secret: null }]);
|
|
253
|
-
try {
|
|
254
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
255
|
-
method: "POST",
|
|
256
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
257
|
-
body: inboundBody("jwtonly-1"),
|
|
258
|
-
});
|
|
259
|
-
expect(res.status).toBe(200);
|
|
260
|
-
expect(emitted).toHaveLength(1);
|
|
261
|
-
expect(emitted[0]!.channel).toBe("eng");
|
|
262
|
-
} finally {
|
|
263
|
-
srv.stop(true);
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
test("M2 — a JWT-only channel (NO webhookSecret) 401s a no-auth request (nothing to validate against)", async () => {
|
|
268
|
-
const { srv, base, emitted } = buildServer([{ name: "eng", secret: null }]);
|
|
269
|
-
try {
|
|
270
|
-
// No Authorization header → ?secret= fallback. The channel has no configured
|
|
271
|
-
// secret, so an empty/any `?secret=` can never match → 401 (no "undefined ===
|
|
272
|
-
// undefined" passing). Also assert an explicit empty ?secret= stays 401.
|
|
273
|
-
const noAuth = await fetch(`${base}/api/vault/inbound`, {
|
|
274
|
-
method: "POST",
|
|
275
|
-
headers: { "content-type": "application/json" },
|
|
276
|
-
body: inboundBody("jwtonly-noauth"),
|
|
277
|
-
});
|
|
278
|
-
expect(noAuth.status).toBe(401);
|
|
279
|
-
const emptySecret = await fetch(`${base}/api/vault/inbound?secret=`, {
|
|
280
|
-
method: "POST",
|
|
281
|
-
headers: { "content-type": "application/json" },
|
|
282
|
-
body: inboundBody("jwtonly-emptysecret"),
|
|
283
|
-
});
|
|
284
|
-
expect(emptySecret.status).toBe(401);
|
|
285
|
-
expect(emitted).toHaveLength(0);
|
|
286
|
-
} finally {
|
|
287
|
-
srv.stop(true);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
test("DEPRECATED ?secret= fallback still works (200 + emits + logs a warning)", async () => {
|
|
292
|
-
const warnings: string[] = [];
|
|
293
|
-
const origWarn = console.warn;
|
|
294
|
-
console.warn = (...args: unknown[]) => {
|
|
295
|
-
warnings.push(args.map(String).join(" "));
|
|
296
|
-
};
|
|
297
|
-
const { srv, base, emitted } = buildServer();
|
|
298
|
-
try {
|
|
299
|
-
const res = await fetch(`${base}/api/vault/inbound?secret=s3cret`, {
|
|
300
|
-
method: "POST",
|
|
301
|
-
headers: { "content-type": "application/json" },
|
|
302
|
-
body: inboundBody("sec-1"),
|
|
303
|
-
});
|
|
304
|
-
expect(res.status).toBe(200);
|
|
305
|
-
expect(emitted).toHaveLength(1);
|
|
306
|
-
expect(warnings.some((w) => w.includes("DEPRECATED") && w.includes("eng"))).toBe(true);
|
|
307
|
-
} finally {
|
|
308
|
-
console.warn = origWarn;
|
|
309
|
-
srv.stop(true);
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
test("valid JWT but unknown channel → uniform 401 (no enumeration), no emit", async () => {
|
|
314
|
-
const { srv, base, emitted } = buildServer();
|
|
315
|
-
try {
|
|
316
|
-
const res = await fetch(`${base}/api/vault/inbound`, {
|
|
317
|
-
method: "POST",
|
|
318
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
319
|
-
body: inboundBody("ghost", "nope"),
|
|
320
|
-
});
|
|
321
|
-
expect(res.status).toBe(401);
|
|
322
|
-
expect(emitted).toHaveLength(0);
|
|
323
|
-
} finally {
|
|
324
|
-
srv.stop(true);
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
// ===========================================================================
|
|
330
|
-
// B. Config-management API (agent:admin)
|
|
331
|
-
// ===========================================================================
|
|
332
|
-
describe("B — config-management API (agent:admin)", () => {
|
|
333
|
-
test("POST without agent:admin → 403 (other scope) / 401 (none)", async () => {
|
|
334
|
-
const { srv, base } = buildServer([]);
|
|
335
|
-
try {
|
|
336
|
-
const none = await fetch(`${base}/api/channels`, {
|
|
337
|
-
method: "POST",
|
|
338
|
-
headers: { "content-type": "application/json" },
|
|
339
|
-
body: JSON.stringify({ name: "x", transport: "http-ui" }),
|
|
340
|
-
});
|
|
341
|
-
expect(none.status).toBe(401);
|
|
342
|
-
|
|
343
|
-
const wrong = await fetch(`${base}/api/channels`, {
|
|
344
|
-
method: "POST",
|
|
345
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
346
|
-
body: JSON.stringify({ name: "x", transport: "http-ui" }),
|
|
347
|
-
});
|
|
348
|
-
expect(wrong.status).toBe(403);
|
|
349
|
-
} finally {
|
|
350
|
-
srv.stop(true);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
test("POST writes channels.json (600 perms) + hot-adds the channel LIVE (inbound routes, no restart)", async () => {
|
|
355
|
-
const { srv, base } = buildServer([]); // start with NO channels
|
|
356
|
-
try {
|
|
357
|
-
const create = await fetch(`${base}/api/channels`, {
|
|
358
|
-
method: "POST",
|
|
359
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
360
|
-
body: JSON.stringify({
|
|
361
|
-
name: "eng",
|
|
362
|
-
transport: "vault",
|
|
363
|
-
config: { vault: "default", vaultUrl: "http://127.0.0.1:1940", token: "vault-jwt", webhookSecret: "sek" },
|
|
364
|
-
}),
|
|
365
|
-
});
|
|
366
|
-
expect(create.status).toBe(200);
|
|
367
|
-
expect(await create.json()).toMatchObject({ ok: true, name: "eng", transport: "vault", live: true });
|
|
368
|
-
|
|
369
|
-
// channels.json written, with the token, at 0600.
|
|
370
|
-
const file = channelsFilePath(stateDir);
|
|
371
|
-
expect(existsSync(file)).toBe(true);
|
|
372
|
-
const onDisk = JSON.parse(readFileSync(file, "utf8")) as { channels: Array<{ name: string; config?: Record<string, unknown> }> };
|
|
373
|
-
expect(onDisk.channels.map((c) => c.name)).toContain("eng");
|
|
374
|
-
const engEntry = onDisk.channels.find((c) => c.name === "eng")!;
|
|
375
|
-
expect(engEntry.config!.token).toBe("vault-jwt"); // the file DOES hold the token
|
|
376
|
-
const mode = statSync(file).mode & 0o777;
|
|
377
|
-
expect(mode).toBe(0o600);
|
|
378
|
-
|
|
379
|
-
// HOT-ADD proof #1: the new channel is in the LIVE registry — /health lists it.
|
|
380
|
-
const health = (await (await fetch(`${base}/health`)).json()) as { channels: Array<{ name: string; kind: string }> };
|
|
381
|
-
expect(health.channels.map((c) => c.name)).toContain("eng");
|
|
382
|
-
expect(health.channels.find((c) => c.name === "eng")!.kind).toBe("vault");
|
|
383
|
-
|
|
384
|
-
// HOT-ADD proof #2: a subsequent inbound for the new channel ROUTES (200 →
|
|
385
|
-
// the channel is live + the secret authorizes) WITHOUT a restart. An unknown
|
|
386
|
-
// channel would 401; routing to its transport's ingestInbound is what makes
|
|
387
|
-
// this 200. (The hot-added transport emits into the real ClientRegistry, not
|
|
388
|
-
// this test's recorder, so we assert on the routed 200 + health, not `emitted`.)
|
|
389
|
-
const inbound = await fetch(`${base}/api/vault/inbound?secret=sek`, {
|
|
390
|
-
method: "POST",
|
|
391
|
-
headers: { "content-type": "application/json" },
|
|
392
|
-
body: inboundBody("hot-1", "eng"),
|
|
393
|
-
});
|
|
394
|
-
expect(inbound.status).toBe(200);
|
|
395
|
-
|
|
396
|
-
// HOT-ADD proof #3 (nit): a JWT-authenticated inbound (agent:send) also
|
|
397
|
-
// routes on the freshly hot-added channel — the JWT path is live too, not
|
|
398
|
-
// just the secret fallback.
|
|
399
|
-
const jwtInbound = await fetch(`${base}/api/vault/inbound`, {
|
|
400
|
-
method: "POST",
|
|
401
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
402
|
-
body: inboundBody("hot-jwt-1", "eng"),
|
|
403
|
-
});
|
|
404
|
-
expect(jwtInbound.status).toBe(200);
|
|
405
|
-
} finally {
|
|
406
|
-
srv.stop(true);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
test("POST replaces an existing channel (stops the old transport, new config wins)", async () => {
|
|
411
|
-
const { srv, base } = buildServer([{ name: "eng", secret: "old" }]);
|
|
412
|
-
try {
|
|
413
|
-
const res = await fetch(`${base}/api/channels`, {
|
|
414
|
-
method: "POST",
|
|
415
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
416
|
-
body: JSON.stringify({
|
|
417
|
-
name: "eng",
|
|
418
|
-
transport: "vault",
|
|
419
|
-
config: { vault: "default", token: "x", webhookSecret: "new" },
|
|
420
|
-
}),
|
|
421
|
-
});
|
|
422
|
-
expect(res.status).toBe(200);
|
|
423
|
-
// New secret takes effect; old one no longer authorizes.
|
|
424
|
-
const oldSecret = await fetch(`${base}/api/vault/inbound?secret=old`, {
|
|
425
|
-
method: "POST",
|
|
426
|
-
headers: { "content-type": "application/json" },
|
|
427
|
-
body: inboundBody("r-old", "eng"),
|
|
428
|
-
});
|
|
429
|
-
expect(oldSecret.status).toBe(401);
|
|
430
|
-
const newSecret = await fetch(`${base}/api/vault/inbound?secret=new`, {
|
|
431
|
-
method: "POST",
|
|
432
|
-
headers: { "content-type": "application/json" },
|
|
433
|
-
body: inboundBody("r-new", "eng"),
|
|
434
|
-
});
|
|
435
|
-
expect(newSecret.status).toBe(200);
|
|
436
|
-
} finally {
|
|
437
|
-
srv.stop(true);
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
test("POST with an invalid config (vault channel, no token) → 400, nothing persisted", async () => {
|
|
442
|
-
const { srv, base } = buildServer([]);
|
|
443
|
-
try {
|
|
444
|
-
const res = await fetch(`${base}/api/channels`, {
|
|
445
|
-
method: "POST",
|
|
446
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
447
|
-
body: JSON.stringify({ name: "bad", transport: "vault", config: { vault: "default" } }),
|
|
448
|
-
});
|
|
449
|
-
expect(res.status).toBe(400);
|
|
450
|
-
// channels.json must not have been created with the broken entry.
|
|
451
|
-
const file = channelsFilePath(stateDir);
|
|
452
|
-
if (existsSync(file)) {
|
|
453
|
-
const onDisk = JSON.parse(readFileSync(file, "utf8")) as { channels: Array<{ name: string }> };
|
|
454
|
-
expect(onDisk.channels.map((c) => c.name)).not.toContain("bad");
|
|
455
|
-
}
|
|
456
|
-
} finally {
|
|
457
|
-
srv.stop(true);
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
test("GET lists channels (name + transport + vault) — NEVER token/secret", async () => {
|
|
462
|
-
const { srv, base } = buildServer([{ name: "eng", secret: "s3cret" }]);
|
|
463
|
-
try {
|
|
464
|
-
const res = await fetch(`${base}/api/channels`, { headers: { ...adminAuth } });
|
|
465
|
-
expect(res.status).toBe(200);
|
|
466
|
-
const body = (await res.json()) as { channels: Array<Record<string, unknown>> };
|
|
467
|
-
expect(body.channels).toHaveLength(1);
|
|
468
|
-
const eng = body.channels[0]!;
|
|
469
|
-
expect(eng.name).toBe("eng");
|
|
470
|
-
expect(eng.transport).toBe("vault");
|
|
471
|
-
expect(eng.vault).toBe("default");
|
|
472
|
-
// No credential fields leak.
|
|
473
|
-
const serialized = JSON.stringify(body);
|
|
474
|
-
expect(serialized).not.toContain("s3cret");
|
|
475
|
-
expect(serialized).not.toContain("token");
|
|
476
|
-
expect(serialized).not.toContain("webhookSecret");
|
|
477
|
-
} finally {
|
|
478
|
-
srv.stop(true);
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
test("telegram channel: POST persists a per-channel bot token in config.token; GET never leaks it", async () => {
|
|
483
|
-
const { srv, base } = buildServer([]);
|
|
484
|
-
try {
|
|
485
|
-
const create = await fetch(`${base}/api/channels`, {
|
|
486
|
-
method: "POST",
|
|
487
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
488
|
-
body: JSON.stringify({
|
|
489
|
-
name: "tg",
|
|
490
|
-
transport: "telegram",
|
|
491
|
-
config: { token: "777:SUPER-SECRET-BOT-TOKEN", stateDir },
|
|
492
|
-
}),
|
|
493
|
-
});
|
|
494
|
-
expect(create.status).toBe(200);
|
|
495
|
-
expect(await create.json()).toMatchObject({ ok: true, name: "tg", transport: "telegram", live: true });
|
|
496
|
-
|
|
497
|
-
// The bot token IS persisted to channels.json (chmod 600, holds secrets).
|
|
498
|
-
const file = channelsFilePath(stateDir);
|
|
499
|
-
const onDisk = JSON.parse(readFileSync(file, "utf8")) as {
|
|
500
|
-
channels: Array<{ name: string; transport: string; config?: Record<string, unknown> }>;
|
|
501
|
-
};
|
|
502
|
-
const tg = onDisk.channels.find((c) => c.name === "tg")!;
|
|
503
|
-
expect(tg.transport).toBe("telegram");
|
|
504
|
-
expect(tg.config!.token).toBe("777:SUPER-SECRET-BOT-TOKEN");
|
|
505
|
-
const mode = statSync(file).mode & 0o777;
|
|
506
|
-
expect(mode).toBe(0o600);
|
|
507
|
-
|
|
508
|
-
// GET /api/channels must NOT echo the bot token (same redaction posture as
|
|
509
|
-
// the vault transport's token/webhookSecret).
|
|
510
|
-
const list = await fetch(`${base}/api/channels`, { headers: { ...adminAuth } });
|
|
511
|
-
expect(list.status).toBe(200);
|
|
512
|
-
const body = (await list.json()) as { channels: Array<Record<string, unknown>> };
|
|
513
|
-
const serialized = JSON.stringify(body);
|
|
514
|
-
expect(serialized).not.toContain("SUPER-SECRET-BOT-TOKEN");
|
|
515
|
-
expect(serialized).not.toContain("token");
|
|
516
|
-
// The row still surfaces name + transport.
|
|
517
|
-
const row = body.channels.find((c) => c.name === "tg")!;
|
|
518
|
-
expect(row.transport).toBe("telegram");
|
|
519
|
-
|
|
520
|
-
// /health likewise doesn't leak the token.
|
|
521
|
-
const health = await (await fetch(`${base}/health`)).text();
|
|
522
|
-
expect(health).not.toContain("SUPER-SECRET-BOT-TOKEN");
|
|
523
|
-
} finally {
|
|
524
|
-
// Stop the telegram poll loop before tearing the server down.
|
|
525
|
-
await fetch(`${base}/api/channels/tg`, { method: "DELETE", headers: { ...adminAuth } }).catch(() => {});
|
|
526
|
-
srv.stop(true);
|
|
527
|
-
}
|
|
528
|
-
});
|
|
529
|
-
|
|
530
|
-
test("GET without agent:admin → 401 (none) / 403 (wrong scope)", async () => {
|
|
531
|
-
const { srv, base } = buildServer([]);
|
|
532
|
-
try {
|
|
533
|
-
expect((await fetch(`${base}/api/channels`)).status).toBe(401);
|
|
534
|
-
expect((await fetch(`${base}/api/channels`, { headers: { ...readAuth } })).status).toBe(403);
|
|
535
|
-
} finally {
|
|
536
|
-
srv.stop(true);
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
test("DELETE removes the channel from channels.json + stops routing", async () => {
|
|
541
|
-
const { srv, base, emitted } = buildServer([]);
|
|
542
|
-
try {
|
|
543
|
-
// Add it via the API so it's both on disk and live.
|
|
544
|
-
await fetch(`${base}/api/channels`, {
|
|
545
|
-
method: "POST",
|
|
546
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
547
|
-
body: JSON.stringify({ name: "eng", transport: "vault", config: { vault: "default", token: "x", webhookSecret: "sek" } }),
|
|
548
|
-
});
|
|
549
|
-
const file = channelsFilePath(stateDir);
|
|
550
|
-
expect((JSON.parse(readFileSync(file, "utf8")) as { channels: unknown[] }).channels).toHaveLength(1);
|
|
551
|
-
|
|
552
|
-
const del = await fetch(`${base}/api/channels/eng`, { method: "DELETE", headers: { ...adminAuth } });
|
|
553
|
-
expect(del.status).toBe(200);
|
|
554
|
-
expect(await del.json()).toMatchObject({ ok: true, name: "eng", removed: true });
|
|
555
|
-
|
|
556
|
-
// Gone from disk.
|
|
557
|
-
expect((JSON.parse(readFileSync(file, "utf8")) as { channels: unknown[] }).channels).toHaveLength(0);
|
|
558
|
-
// Routing stopped — an inbound for the deleted channel is now an unknown
|
|
559
|
-
// channel → uniform 401, no emit.
|
|
560
|
-
const inbound = await fetch(`${base}/api/vault/inbound?secret=sek`, {
|
|
561
|
-
method: "POST",
|
|
562
|
-
headers: { "content-type": "application/json" },
|
|
563
|
-
body: inboundBody("after-del", "eng"),
|
|
564
|
-
});
|
|
565
|
-
expect(inbound.status).toBe(401);
|
|
566
|
-
expect(emitted).toHaveLength(0);
|
|
567
|
-
} finally {
|
|
568
|
-
srv.stop(true);
|
|
569
|
-
}
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
test("DELETE without agent:admin → 401/403", async () => {
|
|
573
|
-
const { srv, base } = buildServer([{ name: "eng" }]);
|
|
574
|
-
try {
|
|
575
|
-
expect((await fetch(`${base}/api/channels/eng`, { method: "DELETE" })).status).toBe(401);
|
|
576
|
-
expect((await fetch(`${base}/api/channels/eng`, { method: "DELETE", headers: { ...readAuth } })).status).toBe(403);
|
|
577
|
-
} finally {
|
|
578
|
-
srv.stop(true);
|
|
579
|
-
}
|
|
580
|
-
});
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
// ===========================================================================
|
|
584
|
-
// C. Prescribed trigger template exposed via /.parachute/config
|
|
585
|
-
// ===========================================================================
|
|
586
|
-
describe("C — AGENT_VAULT_TRIGGER_TEMPLATE exposed via /.parachute/config", () => {
|
|
587
|
-
test("GET /.parachute/config carries triggerTemplate (the module-owned trigger shape)", async () => {
|
|
588
|
-
const { srv, base } = buildServer([{ name: "eng" }]);
|
|
589
|
-
try {
|
|
590
|
-
const res = await fetch(`${base}/.parachute/config`);
|
|
591
|
-
expect(res.status).toBe(200);
|
|
592
|
-
const body = (await res.json()) as { channels: unknown[]; triggerTemplate: typeof AGENT_VAULT_TRIGGER_TEMPLATE };
|
|
593
|
-
expect(body.triggerTemplate).toEqual(AGENT_VAULT_TRIGGER_TEMPLATE);
|
|
594
|
-
// Sanity on the shape the hub depends on (placeholders + webhook path).
|
|
595
|
-
// The trigger NAME stays `channel_inbound_<channel>` — kept STABLE so
|
|
596
|
-
// re-registration updates the existing trigger in place. The TAG is
|
|
597
|
-
// #agent/message and the webhook is on the /agent mount.
|
|
598
|
-
expect(body.triggerTemplate.name).toBe("channel_inbound_<channel>");
|
|
599
|
-
expect(body.triggerTemplate.when.tags).toEqual(["#agent/message/inbound"]);
|
|
600
|
-
// CONTRACT: the predicate keys on the `agent` routing key (was `channel`).
|
|
601
|
-
expect(body.triggerTemplate.when.has_metadata).toEqual(["agent"]);
|
|
602
|
-
// The rendered-at marker NAME is unchanged (cosmetic/internal).
|
|
603
|
-
expect(body.triggerTemplate.when.missing_metadata).toEqual(["channel_inbound_rendered_at"]);
|
|
604
|
-
expect(body.triggerTemplate.action.webhook).toBe("<hub-origin>/agent/api/vault/inbound");
|
|
605
|
-
} finally {
|
|
606
|
-
srv.stop(true);
|
|
607
|
-
}
|
|
608
|
-
});
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
// ===========================================================================
|
|
612
|
-
// D. Claude OAuth credential store API (agent:admin) — design §6
|
|
613
|
-
// ===========================================================================
|
|
614
|
-
describe("D — Claude credential store API (agent:admin)", () => {
|
|
615
|
-
test("POST /api/credentials/claude sets the default; persisted 0600; resolve returns it", async () => {
|
|
616
|
-
const { srv, base } = buildServer([]);
|
|
617
|
-
try {
|
|
618
|
-
const res = await fetch(`${base}/api/credentials/claude`, {
|
|
619
|
-
method: "POST",
|
|
620
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
621
|
-
body: JSON.stringify({ token: "oat_DEFAULT-OPERATOR" }),
|
|
622
|
-
});
|
|
623
|
-
expect(res.status).toBe(200);
|
|
624
|
-
expect(await res.json()).toMatchObject({ ok: true, scope: "default" });
|
|
625
|
-
|
|
626
|
-
const file = credentialsFilePath(stateDir);
|
|
627
|
-
expect(existsSync(file)).toBe(true);
|
|
628
|
-
expect(statSync(file).mode & 0o777).toBe(0o600);
|
|
629
|
-
// The token resolves for any channel (no override → default).
|
|
630
|
-
expect(resolveClaudeCredential("any-channel", stateDir)).toBe("oat_DEFAULT-OPERATOR");
|
|
631
|
-
} finally {
|
|
632
|
-
srv.stop(true);
|
|
633
|
-
}
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
test("POST /api/credentials/claude/:channel sets an override that WINS over the default", async () => {
|
|
637
|
-
const { srv, base } = buildServer([]);
|
|
638
|
-
try {
|
|
639
|
-
await fetch(`${base}/api/credentials/claude`, {
|
|
640
|
-
method: "POST",
|
|
641
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
642
|
-
body: JSON.stringify({ token: "oat_DEFAULT" }),
|
|
643
|
-
});
|
|
644
|
-
const res = await fetch(`${base}/api/credentials/claude/aaron-dev`, {
|
|
645
|
-
method: "POST",
|
|
646
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
647
|
-
body: JSON.stringify({ token: "oat_AARON-OVERRIDE" }),
|
|
648
|
-
});
|
|
649
|
-
expect(res.status).toBe(200);
|
|
650
|
-
expect(await res.json()).toMatchObject({ ok: true, scope: "channel", channel: "aaron-dev" });
|
|
651
|
-
|
|
652
|
-
expect(resolveClaudeCredential("aaron-dev", stateDir)).toBe("oat_AARON-OVERRIDE"); // override wins
|
|
653
|
-
expect(resolveClaudeCredential("other", stateDir)).toBe("oat_DEFAULT"); // other → default
|
|
654
|
-
} finally {
|
|
655
|
-
srv.stop(true);
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
|
|
659
|
-
test("GET /api/credentials/claude reports presence + channel names — NEVER the token", async () => {
|
|
660
|
-
const { srv, base } = buildServer([]);
|
|
661
|
-
try {
|
|
662
|
-
await fetch(`${base}/api/credentials/claude`, {
|
|
663
|
-
method: "POST",
|
|
664
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
665
|
-
body: JSON.stringify({ token: "oat_SECRET-DEFAULT" }),
|
|
666
|
-
});
|
|
667
|
-
await fetch(`${base}/api/credentials/claude/aaron-dev`, {
|
|
668
|
-
method: "POST",
|
|
669
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
670
|
-
body: JSON.stringify({ token: "oat_SECRET-OVERRIDE" }),
|
|
671
|
-
});
|
|
672
|
-
|
|
673
|
-
const res = await fetch(`${base}/api/credentials/claude`, { headers: { ...adminAuth } });
|
|
674
|
-
expect(res.status).toBe(200);
|
|
675
|
-
const body = (await res.json()) as { defaultSet: boolean; channels: string[] };
|
|
676
|
-
expect(body.defaultSet).toBe(true);
|
|
677
|
-
expect(body.channels).toEqual(["aaron-dev"]);
|
|
678
|
-
// No secret leaks.
|
|
679
|
-
const serialized = JSON.stringify(body);
|
|
680
|
-
expect(serialized).not.toContain("oat_SECRET-DEFAULT");
|
|
681
|
-
expect(serialized).not.toContain("oat_SECRET-OVERRIDE");
|
|
682
|
-
} finally {
|
|
683
|
-
srv.stop(true);
|
|
684
|
-
}
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
test("DELETE /api/credentials/claude/:channel removes the override (falls back to default)", async () => {
|
|
688
|
-
const { srv, base } = buildServer([]);
|
|
689
|
-
try {
|
|
690
|
-
await fetch(`${base}/api/credentials/claude`, {
|
|
691
|
-
method: "POST",
|
|
692
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
693
|
-
body: JSON.stringify({ token: "oat_DEFAULT" }),
|
|
694
|
-
});
|
|
695
|
-
await fetch(`${base}/api/credentials/claude/aaron-dev`, {
|
|
696
|
-
method: "POST",
|
|
697
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
698
|
-
body: JSON.stringify({ token: "oat_OVERRIDE" }),
|
|
699
|
-
});
|
|
700
|
-
const del = await fetch(`${base}/api/credentials/claude/aaron-dev`, {
|
|
701
|
-
method: "DELETE",
|
|
702
|
-
headers: { ...adminAuth },
|
|
703
|
-
});
|
|
704
|
-
expect(del.status).toBe(200);
|
|
705
|
-
expect(await del.json()).toMatchObject({ ok: true, channel: "aaron-dev", removed: true });
|
|
706
|
-
expect(resolveClaudeCredential("aaron-dev", stateDir)).toBe("oat_DEFAULT"); // back to default
|
|
707
|
-
} finally {
|
|
708
|
-
srv.stop(true);
|
|
709
|
-
}
|
|
710
|
-
});
|
|
711
|
-
|
|
712
|
-
test("POST with an empty/missing token → 400, nothing persisted", async () => {
|
|
713
|
-
const { srv, base } = buildServer([]);
|
|
714
|
-
try {
|
|
715
|
-
const empty = await fetch(`${base}/api/credentials/claude`, {
|
|
716
|
-
method: "POST",
|
|
717
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
718
|
-
body: JSON.stringify({ token: "" }),
|
|
719
|
-
});
|
|
720
|
-
expect(empty.status).toBe(400);
|
|
721
|
-
const missing = await fetch(`${base}/api/credentials/claude`, {
|
|
722
|
-
method: "POST",
|
|
723
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
724
|
-
body: JSON.stringify({}),
|
|
725
|
-
});
|
|
726
|
-
expect(missing.status).toBe(400);
|
|
727
|
-
expect(existsSync(credentialsFilePath(stateDir))).toBe(false);
|
|
728
|
-
} finally {
|
|
729
|
-
srv.stop(true);
|
|
730
|
-
}
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
test("credential API without agent:admin → 401 (none) / 403 (wrong scope), on every verb", async () => {
|
|
734
|
-
const { srv, base } = buildServer([]);
|
|
735
|
-
try {
|
|
736
|
-
// GET default
|
|
737
|
-
expect((await fetch(`${base}/api/credentials/claude`)).status).toBe(401);
|
|
738
|
-
expect((await fetch(`${base}/api/credentials/claude`, { headers: { ...readAuth } })).status).toBe(403);
|
|
739
|
-
// POST default
|
|
740
|
-
expect(
|
|
741
|
-
(await fetch(`${base}/api/credentials/claude`, {
|
|
742
|
-
method: "POST",
|
|
743
|
-
headers: { "content-type": "application/json" },
|
|
744
|
-
body: JSON.stringify({ token: "x" }),
|
|
745
|
-
})).status,
|
|
746
|
-
).toBe(401);
|
|
747
|
-
expect(
|
|
748
|
-
(await fetch(`${base}/api/credentials/claude`, {
|
|
749
|
-
method: "POST",
|
|
750
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
751
|
-
body: JSON.stringify({ token: "x" }),
|
|
752
|
-
})).status,
|
|
753
|
-
).toBe(403);
|
|
754
|
-
// POST per-channel
|
|
755
|
-
expect(
|
|
756
|
-
(await fetch(`${base}/api/credentials/claude/c`, {
|
|
757
|
-
method: "POST",
|
|
758
|
-
headers: { "content-type": "application/json" },
|
|
759
|
-
body: JSON.stringify({ token: "x" }),
|
|
760
|
-
})).status,
|
|
761
|
-
).toBe(401);
|
|
762
|
-
expect(
|
|
763
|
-
(await fetch(`${base}/api/credentials/claude/c`, {
|
|
764
|
-
method: "POST",
|
|
765
|
-
headers: { "content-type": "application/json", ...readAuth },
|
|
766
|
-
body: JSON.stringify({ token: "x" }),
|
|
767
|
-
})).status,
|
|
768
|
-
).toBe(403);
|
|
769
|
-
// DELETE per-channel
|
|
770
|
-
expect((await fetch(`${base}/api/credentials/claude/c`, { method: "DELETE" })).status).toBe(401);
|
|
771
|
-
expect(
|
|
772
|
-
(await fetch(`${base}/api/credentials/claude/c`, { method: "DELETE", headers: { ...readAuth } })).status,
|
|
773
|
-
).toBe(403);
|
|
774
|
-
// Nothing was persisted by any unauthorized call.
|
|
775
|
-
expect(existsSync(credentialsFilePath(stateDir))).toBe(false);
|
|
776
|
-
} finally {
|
|
777
|
-
srv.stop(true);
|
|
778
|
-
}
|
|
779
|
-
});
|
|
780
|
-
});
|
|
781
|
-
|
|
782
|
-
// ===========================================================================
|
|
783
|
-
// E. Generic per-channel ENV-VAR store API (agent:admin)
|
|
784
|
-
// ===========================================================================
|
|
785
|
-
describe("E — per-channel env-var store API (agent:admin)", () => {
|
|
786
|
-
test("POST /api/credentials/env sets a default var; 0600; resolves for any channel", async () => {
|
|
787
|
-
const { srv, base } = buildServer([]);
|
|
788
|
-
try {
|
|
789
|
-
const res = await fetch(`${base}/api/credentials/env`, {
|
|
790
|
-
method: "POST",
|
|
791
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
792
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "ghp_DEFAULT-SECRET" }),
|
|
793
|
-
});
|
|
794
|
-
expect(res.status).toBe(200);
|
|
795
|
-
expect(await res.json()).toMatchObject({ ok: true, scope: "default", name: "GH_TOKEN" });
|
|
796
|
-
|
|
797
|
-
const file = credentialsFilePath(stateDir);
|
|
798
|
-
expect(existsSync(file)).toBe(true);
|
|
799
|
-
expect(statSync(file).mode & 0o777).toBe(0o600);
|
|
800
|
-
expect(resolveChannelEnv("any-channel", stateDir)).toEqual({ GH_TOKEN: "ghp_DEFAULT-SECRET" });
|
|
801
|
-
} finally {
|
|
802
|
-
srv.stop(true);
|
|
803
|
-
}
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
test("POST with a channel sets an override that WINS over the default", async () => {
|
|
807
|
-
const { srv, base } = buildServer([]);
|
|
808
|
-
try {
|
|
809
|
-
await fetch(`${base}/api/credentials/env`, {
|
|
810
|
-
method: "POST",
|
|
811
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
812
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "ghp_DEFAULT" }),
|
|
813
|
-
});
|
|
814
|
-
const res = await fetch(`${base}/api/credentials/env`, {
|
|
815
|
-
method: "POST",
|
|
816
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
817
|
-
body: JSON.stringify({ channel: "aaron-dev", name: "GH_TOKEN", value: "ghp_AARON" }),
|
|
818
|
-
});
|
|
819
|
-
expect(res.status).toBe(200);
|
|
820
|
-
expect(await res.json()).toMatchObject({ ok: true, scope: "channel", channel: "aaron-dev", name: "GH_TOKEN" });
|
|
821
|
-
|
|
822
|
-
expect(resolveChannelEnv("aaron-dev", stateDir)).toEqual({ GH_TOKEN: "ghp_AARON" }); // override wins
|
|
823
|
-
expect(resolveChannelEnv("other", stateDir)).toEqual({ GH_TOKEN: "ghp_DEFAULT" }); // other → default
|
|
824
|
-
} finally {
|
|
825
|
-
srv.stop(true);
|
|
826
|
-
}
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
test("POST a DENYLISTED name → 400, nothing persisted (subscription-billing guarantee)", async () => {
|
|
830
|
-
const { srv, base } = buildServer([]);
|
|
831
|
-
try {
|
|
832
|
-
for (const name of ["ANTHROPIC_API_KEY", "CLAUDE_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"]) {
|
|
833
|
-
const res = await fetch(`${base}/api/credentials/env`, {
|
|
834
|
-
method: "POST",
|
|
835
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
836
|
-
body: JSON.stringify({ name, value: "x" }),
|
|
837
|
-
});
|
|
838
|
-
expect(res.status).toBe(400);
|
|
839
|
-
expect(((await res.json()) as { error: string }).error).toMatch(/Claude auth|reserved|not settable/);
|
|
840
|
-
}
|
|
841
|
-
expect(existsSync(credentialsFilePath(stateDir))).toBe(false);
|
|
842
|
-
} finally {
|
|
843
|
-
srv.stop(true);
|
|
844
|
-
}
|
|
845
|
-
});
|
|
846
|
-
|
|
847
|
-
test("GET reports names per layer — NEVER the values", async () => {
|
|
848
|
-
const { srv, base } = buildServer([]);
|
|
849
|
-
try {
|
|
850
|
-
await fetch(`${base}/api/credentials/env`, {
|
|
851
|
-
method: "POST",
|
|
852
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
853
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "ghp_SECRET-DEFAULT" }),
|
|
854
|
-
});
|
|
855
|
-
await fetch(`${base}/api/credentials/env`, {
|
|
856
|
-
method: "POST",
|
|
857
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
858
|
-
body: JSON.stringify({ channel: "aaron-dev", name: "CLOUDFLARE_API_TOKEN", value: "cf_SECRET" }),
|
|
859
|
-
});
|
|
860
|
-
const res = await fetch(`${base}/api/credentials/env`, { headers: { ...adminAuth } });
|
|
861
|
-
expect(res.status).toBe(200);
|
|
862
|
-
const body = (await res.json()) as { default: string[]; channels: Record<string, string[]> };
|
|
863
|
-
expect(body.default).toEqual(["GH_TOKEN"]);
|
|
864
|
-
expect(body.channels["aaron-dev"]).toEqual(["CLOUDFLARE_API_TOKEN"]);
|
|
865
|
-
const serialized = JSON.stringify(body);
|
|
866
|
-
expect(serialized).not.toContain("ghp_SECRET-DEFAULT");
|
|
867
|
-
expect(serialized).not.toContain("cf_SECRET");
|
|
868
|
-
} finally {
|
|
869
|
-
srv.stop(true);
|
|
870
|
-
}
|
|
871
|
-
});
|
|
872
|
-
|
|
873
|
-
test("DELETE removes a var (default + channel); the default re-emerges after a channel delete", async () => {
|
|
874
|
-
const { srv, base } = buildServer([]);
|
|
875
|
-
try {
|
|
876
|
-
await fetch(`${base}/api/credentials/env`, {
|
|
877
|
-
method: "POST",
|
|
878
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
879
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "ghp_DEFAULT" }),
|
|
880
|
-
});
|
|
881
|
-
await fetch(`${base}/api/credentials/env`, {
|
|
882
|
-
method: "POST",
|
|
883
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
884
|
-
body: JSON.stringify({ channel: "aaron-dev", name: "GH_TOKEN", value: "ghp_AARON" }),
|
|
885
|
-
});
|
|
886
|
-
// Delete the channel override (via body).
|
|
887
|
-
const del = await fetch(`${base}/api/credentials/env`, {
|
|
888
|
-
method: "DELETE",
|
|
889
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
890
|
-
body: JSON.stringify({ channel: "aaron-dev", name: "GH_TOKEN" }),
|
|
891
|
-
});
|
|
892
|
-
expect(del.status).toBe(200);
|
|
893
|
-
expect(await del.json()).toMatchObject({ ok: true, scope: "channel", channel: "aaron-dev", name: "GH_TOKEN", removed: true });
|
|
894
|
-
expect(resolveChannelEnv("aaron-dev", stateDir)).toEqual({ GH_TOKEN: "ghp_DEFAULT" }); // back to default
|
|
895
|
-
|
|
896
|
-
// Delete the default too.
|
|
897
|
-
const delDef = await fetch(`${base}/api/credentials/env`, {
|
|
898
|
-
method: "DELETE",
|
|
899
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
900
|
-
body: JSON.stringify({ name: "GH_TOKEN" }),
|
|
901
|
-
});
|
|
902
|
-
expect(delDef.status).toBe(200);
|
|
903
|
-
expect(await delDef.json()).toMatchObject({ ok: true, scope: "default", name: "GH_TOKEN", removed: true });
|
|
904
|
-
expect(describeChannelEnv(stateDir)).toEqual({ default: [], channels: {} });
|
|
905
|
-
} finally {
|
|
906
|
-
srv.stop(true);
|
|
907
|
-
}
|
|
908
|
-
});
|
|
909
|
-
|
|
910
|
-
test("POST with an empty/missing name or value → 400, nothing persisted", async () => {
|
|
911
|
-
const { srv, base } = buildServer([]);
|
|
912
|
-
try {
|
|
913
|
-
const noName = await fetch(`${base}/api/credentials/env`, {
|
|
914
|
-
method: "POST",
|
|
915
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
916
|
-
body: JSON.stringify({ value: "x" }),
|
|
917
|
-
});
|
|
918
|
-
expect(noName.status).toBe(400);
|
|
919
|
-
const noVal = await fetch(`${base}/api/credentials/env`, {
|
|
920
|
-
method: "POST",
|
|
921
|
-
headers: { "content-type": "application/json", ...adminAuth },
|
|
922
|
-
body: JSON.stringify({ name: "GH_TOKEN" }),
|
|
923
|
-
});
|
|
924
|
-
expect(noVal.status).toBe(400);
|
|
925
|
-
expect(existsSync(credentialsFilePath(stateDir))).toBe(false);
|
|
926
|
-
} finally {
|
|
927
|
-
srv.stop(true);
|
|
928
|
-
}
|
|
929
|
-
});
|
|
930
|
-
|
|
931
|
-
test("env API without agent:admin → 401 (none) / 403 (wrong scope), on every verb", async () => {
|
|
932
|
-
const { srv, base } = buildServer([]);
|
|
933
|
-
try {
|
|
934
|
-
expect((await fetch(`${base}/api/credentials/env`)).status).toBe(401);
|
|
935
|
-
expect((await fetch(`${base}/api/credentials/env`, { headers: { ...readAuth } })).status).toBe(403);
|
|
936
|
-
expect(
|
|
937
|
-
(await fetch(`${base}/api/credentials/env`, {
|
|
938
|
-
method: "POST",
|
|
939
|
-
headers: { "content-type": "application/json" },
|
|
940
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "x" }),
|
|
941
|
-
})).status,
|
|
942
|
-
).toBe(401);
|
|
943
|
-
expect(
|
|
944
|
-
(await fetch(`${base}/api/credentials/env`, {
|
|
945
|
-
method: "POST",
|
|
946
|
-
headers: { "content-type": "application/json", ...sendAuth },
|
|
947
|
-
body: JSON.stringify({ name: "GH_TOKEN", value: "x" }),
|
|
948
|
-
})).status,
|
|
949
|
-
).toBe(403);
|
|
950
|
-
expect(
|
|
951
|
-
(await fetch(`${base}/api/credentials/env`, {
|
|
952
|
-
method: "DELETE",
|
|
953
|
-
headers: { "content-type": "application/json" },
|
|
954
|
-
body: JSON.stringify({ name: "GH_TOKEN" }),
|
|
955
|
-
})).status,
|
|
956
|
-
).toBe(401);
|
|
957
|
-
expect(existsSync(credentialsFilePath(stateDir))).toBe(false);
|
|
958
|
-
} finally {
|
|
959
|
-
srv.stop(true);
|
|
960
|
-
}
|
|
961
|
-
});
|
|
962
|
-
});
|