@notation/factory 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/contracts/channel.ts +43 -0
- package/contracts/connection.ts +15 -0
- package/contracts/hub.ts +33 -0
- package/contracts/index.ts +36 -0
- package/contracts/outpost.ts +54 -0
- package/contracts/router.ts +104 -0
- package/contracts/vault.ts +22 -0
- package/deployer/collect.ts +31 -0
- package/deployer/deploy.test.ts +50 -0
- package/deployer/index.ts +152 -0
- package/deployer/sdk.gen.ts +2 -0
- package/deployer/sdk.ts +18 -0
- package/implementations/container.ts +67 -0
- package/implementations/outpost.ts +50 -0
- package/implementations/policy.ts +26 -0
- package/implementations/trigger.ts +67 -0
- package/index.ts +3 -0
- package/lib/binding-name.ts +9 -0
- package/lib/collect.ts +21 -0
- package/lib/outpost.ts +17 -0
- package/lib/scoped-name.ts +10 -0
- package/outpost.ts +12 -0
- package/package.json +39 -3
- package/resources/outpost-config.ts +102 -0
- package/resources/random-key.ts +54 -0
- package/resources/router-sync.ts +53 -0
- package/router/channel.ts +114 -0
- package/router/handle.test.ts +419 -0
- package/router/handle.ts +442 -0
- package/router/index.ts +52 -0
- package/router/keys.ts +36 -0
- package/router/run.ts +91 -0
- package/runner/index.ts +628 -0
- package/runner/runner.test.ts +322 -0
- package/README.md +0 -3
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import type { OutpostConfig } from "../contracts/outpost.ts";
|
|
4
|
+
import type { Delivery, Received, RemoteChannel } from "./channel.ts";
|
|
5
|
+
import { handle, type RemoteDeployer, type RouterStore } from "./handle.ts";
|
|
6
|
+
import { type Run, signBuildReport, signTicket, signTokenTicket } from "./run.ts";
|
|
7
|
+
|
|
8
|
+
const ROOT_KEY = "root";
|
|
9
|
+
|
|
10
|
+
type FakeEvent = { allow: boolean };
|
|
11
|
+
|
|
12
|
+
const fake = (
|
|
13
|
+
received: Received<FakeEvent>,
|
|
14
|
+
sync: (url: string) => Promise<void> = async () => {},
|
|
15
|
+
): RemoteChannel<FakeEvent> => ({
|
|
16
|
+
receive: async () => received,
|
|
17
|
+
match: async (selector, delivery) =>
|
|
18
|
+
(selector as { pick: boolean }).pick
|
|
19
|
+
? { result: delivery.data.allow ? "allowed" : "denied" }
|
|
20
|
+
: undefined,
|
|
21
|
+
sync,
|
|
22
|
+
read: async () => ({ ref: "c0ffee", files: {} }),
|
|
23
|
+
writeStatus: async () => {},
|
|
24
|
+
writeCheck: async () => {},
|
|
25
|
+
conversation: async (delivery) => ({ id: `fake:${delivery.repo}`, prompt: "do it", thread: { repo: delivery.repo } }),
|
|
26
|
+
reply: async () => {},
|
|
27
|
+
token: async (repo, permissions) => `token:${repo}:${JSON.stringify(permissions)}`,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const accepted = (over: Partial<Delivery<FakeEvent>> = {}) =>
|
|
31
|
+
fake({
|
|
32
|
+
status: "accepted",
|
|
33
|
+
delivery: {
|
|
34
|
+
id: "d1",
|
|
35
|
+
scope: "acme/api",
|
|
36
|
+
repo: "acme/api",
|
|
37
|
+
event: "post",
|
|
38
|
+
data: { allow: true },
|
|
39
|
+
payload: { number: 1 },
|
|
40
|
+
...over,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const outpost: OutpostConfig = {
|
|
45
|
+
channels: {
|
|
46
|
+
here: { provider: "fake", connection: "main", scope: "acme/api" },
|
|
47
|
+
other: { provider: "fake", connection: "main", scope: "acme/docs" },
|
|
48
|
+
second: { provider: "fake", connection: "second", scope: "acme/api" },
|
|
49
|
+
elsewhere: { provider: "other", connection: "main", scope: "acme/api" },
|
|
50
|
+
},
|
|
51
|
+
containers: {},
|
|
52
|
+
policies: {},
|
|
53
|
+
agents: {},
|
|
54
|
+
triggers: {
|
|
55
|
+
picked: { channel: "here", event: "post", match: { pick: true }, agent: "a", container: "c" },
|
|
56
|
+
skipped: { channel: "here", event: "post", match: { pick: false }, agent: "a", container: "c" },
|
|
57
|
+
"other-scope": { channel: "other", event: "post", match: { pick: true }, agent: "a", container: "c" },
|
|
58
|
+
"other-connection": { channel: "second", event: "post", match: { pick: true }, agent: "a", container: "c" },
|
|
59
|
+
"other-provider": { channel: "elsewhere", event: "post", match: { pick: true }, agent: "a", container: "c" },
|
|
60
|
+
"other-event": { channel: "here", event: "reply", match: { pick: true }, agent: "a", container: "c" },
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const entry = (channel: string) => ({ provider: "fake", source: "1", credentials: {}, channel });
|
|
65
|
+
|
|
66
|
+
const store = (
|
|
67
|
+
bound: Record<string, RemoteChannel<FakeEvent>>,
|
|
68
|
+
outposts: Record<string, OutpostConfig> = { "acme/api": outpost },
|
|
69
|
+
): RouterStore => ({
|
|
70
|
+
config: {
|
|
71
|
+
connections: Object.fromEntries(
|
|
72
|
+
Object.keys(bound).map((name) => [`fake/${name}`, entry(`CHANNEL_${name.toUpperCase()}`)]),
|
|
73
|
+
),
|
|
74
|
+
},
|
|
75
|
+
channel: (binding) =>
|
|
76
|
+
Object.entries(bound).find(([name]) => `CHANNEL_${name.toUpperCase()}` === binding)?.[1] as
|
|
77
|
+
| RemoteChannel
|
|
78
|
+
| undefined,
|
|
79
|
+
outpost: async (repo) => outposts[repo],
|
|
80
|
+
rootKey: async () => ROOT_KEY,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/** The matches without their runs' random ids. */
|
|
84
|
+
const matched = (matches: { run?: string }[]) => matches.map(({ run, ...m }) => ({ ...m, run: typeof run }));
|
|
85
|
+
|
|
86
|
+
const post = (path: string) => new Request(`https://router.test${path}`, { method: "POST" });
|
|
87
|
+
|
|
88
|
+
const call = async (s: RouterStore, path = "/webhook/fake/main") => {
|
|
89
|
+
const response = await handle(post(path), s);
|
|
90
|
+
return { status: response.status, body: await response.json() };
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
test("matches only triggers on channels of the delivery's provider, connection, scope and event", async () => {
|
|
94
|
+
const { status, body } = await call(store({ main: accepted() }));
|
|
95
|
+
assert.equal(status, 202);
|
|
96
|
+
assert.deepEqual(matched(body.matches), [{ trigger: "picked", author: { result: "allowed" }, run: "string" }]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a delivery to another connection's path matches that connection's triggers", async () => {
|
|
100
|
+
const { body } = await call(store({ main: accepted(), second: accepted() }), "/webhook/fake/second");
|
|
101
|
+
assert.deepEqual(matched(body.matches), [{ trigger: "other-connection", author: { result: "allowed" }, run: "string" }]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a rejected delivery is a 401, and an ignored one a 200", async () => {
|
|
105
|
+
assert.equal((await call(store({ main: fake({ status: "rejected", reason: "bad signature" }) }))).status, 401);
|
|
106
|
+
assert.equal((await call(store({ main: fake({ status: "ignored", reason: "ping" }) }))).status, 200);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("a delivery for a repo with no outpost is ignored", async () => {
|
|
110
|
+
const { status, body } = await call(store({ main: accepted() }, {}));
|
|
111
|
+
assert.equal(status, 202);
|
|
112
|
+
assert.equal(body.ignored, "no outpost");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("a delivery to a connection with no channel is a 404", async () => {
|
|
116
|
+
assert.equal((await call(store({ main: accepted() }), "/webhook/fake/unknown")).status, 404);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("sync gives each channel its connection's webhook URL, and reports failures", async () => {
|
|
120
|
+
const urls: string[] = [];
|
|
121
|
+
const s = store({
|
|
122
|
+
main: fake({ status: "ignored", reason: "" }, async (url) => void urls.push(url)),
|
|
123
|
+
second: fake({ status: "ignored", reason: "" }, async () => {
|
|
124
|
+
throw new Error("no access");
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
const { status, body } = await call(s, "/sync");
|
|
128
|
+
assert.equal(status, 502);
|
|
129
|
+
assert.deepEqual(urls, ["https://router.test/webhook/fake/main"]);
|
|
130
|
+
assert.deepEqual(body.connections, { "fake/main": "ok", "fake/second": "no access" });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const sourceStore = (
|
|
134
|
+
channel: RemoteChannel<FakeEvent>,
|
|
135
|
+
calls: string[],
|
|
136
|
+
allow = [{ owner: "acme" }],
|
|
137
|
+
): RouterStore => ({
|
|
138
|
+
config: {
|
|
139
|
+
connections: { "fake/main": entry("CHANNEL_MAIN") },
|
|
140
|
+
codeHost: { provider: "fake", connection: "main", allow },
|
|
141
|
+
},
|
|
142
|
+
channel: (binding) => (binding === "CHANNEL_MAIN" ? (channel as RemoteChannel) : undefined),
|
|
143
|
+
outpost: async () => undefined,
|
|
144
|
+
rootKey: async () => ROOT_KEY,
|
|
145
|
+
deployer: {
|
|
146
|
+
worktree: async () => undefined,
|
|
147
|
+
run: async () => {},
|
|
148
|
+
deploy: async (repo, source) => {
|
|
149
|
+
calls.push(`deploy ${repo} ${source.ref}`);
|
|
150
|
+
return { state: "deployed", ref: source.ref, at: "now" };
|
|
151
|
+
},
|
|
152
|
+
remove: async (repo) => void calls.push(`remove ${repo}`),
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const sourceChannel = (
|
|
157
|
+
changes: { repo: string; ref?: string; removed?: boolean }[],
|
|
158
|
+
calls: string[],
|
|
159
|
+
read?: () => Promise<never>,
|
|
160
|
+
): RemoteChannel<FakeEvent> => ({
|
|
161
|
+
...fake({ status: "source", changes }),
|
|
162
|
+
read:
|
|
163
|
+
read ??
|
|
164
|
+
(async (repo, dir, ref) => {
|
|
165
|
+
calls.push(`read ${repo} ${dir} ${ref}`);
|
|
166
|
+
return { ref: ref ?? "head", files: {} };
|
|
167
|
+
}),
|
|
168
|
+
writeStatus: async (repo, status) =>
|
|
169
|
+
void calls.push(`status ${repo} ${status.state}${status.error ? `: ${status.error}` : ""}`),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("a source change has the deployer deploy .factory/ as the channel reads it, and shows the status on the repo", async () => {
|
|
173
|
+
const calls: string[] = [];
|
|
174
|
+
const s = sourceStore(sourceChannel([{ repo: "acme/api", ref: "abc" }], calls), calls);
|
|
175
|
+
const { status, body } = await call(s);
|
|
176
|
+
assert.equal(status, 202);
|
|
177
|
+
assert.deepEqual(body, { connection: "fake/main", deploying: ["acme/api"] });
|
|
178
|
+
assert.deepEqual(calls, ["read acme/api .factory abc", "deploy acme/api abc", "status acme/api deployed"]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a repo outside the code host's allow list loses its outpost, and says why", async () => {
|
|
182
|
+
const calls: string[] = [];
|
|
183
|
+
await call(sourceStore(sourceChannel([{ repo: "other/api" }], calls), calls));
|
|
184
|
+
assert.deepEqual(calls, [
|
|
185
|
+
"remove other/api",
|
|
186
|
+
"status other/api failed: The hub doesn't serve other/api: the code host's allow list doesn't include it.",
|
|
187
|
+
]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("a repo the hub can no longer read loses its outpost", async () => {
|
|
191
|
+
const calls: string[] = [];
|
|
192
|
+
await call(sourceStore(sourceChannel([{ repo: "acme/api", removed: true }], calls), calls));
|
|
193
|
+
assert.deepEqual(calls, ["remove acme/api"]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("a read that fails shows its error on the repo", async () => {
|
|
197
|
+
const calls: string[] = [];
|
|
198
|
+
const channel = sourceChannel([{ repo: "acme/api", ref: "abc" }], calls, async () => {
|
|
199
|
+
throw new Error("no access");
|
|
200
|
+
});
|
|
201
|
+
await call(sourceStore(channel, calls));
|
|
202
|
+
assert.deepEqual(calls, ["status acme/api failed: no access"]);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("only the code host's connection deploys", async () => {
|
|
206
|
+
const calls: string[] = [];
|
|
207
|
+
const s = sourceStore(sourceChannel([{ repo: "acme/api" }], calls), calls);
|
|
208
|
+
s.config.codeHost = { provider: "fake", connection: "other", allow: [{ owner: "acme" }] };
|
|
209
|
+
const { status, body } = await call(s);
|
|
210
|
+
assert.equal(status, 200);
|
|
211
|
+
assert.deepEqual(body, { ignored: "not the code host's connection" });
|
|
212
|
+
assert.deepEqual(calls, []);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("a deploy that starts builds shows a queued check on the commit for each container", async () => {
|
|
216
|
+
const calls: string[] = [];
|
|
217
|
+
const channel: RemoteChannel<FakeEvent> = {
|
|
218
|
+
...sourceChannel([{ repo: "acme/api", ref: "abc" }], calls),
|
|
219
|
+
writeCheck: async (repo, ref, check) => void calls.push(`check ${repo} ${ref} ${check.name} ${check.state}`),
|
|
220
|
+
};
|
|
221
|
+
const s = sourceStore(channel, calls);
|
|
222
|
+
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now", building: ["dev", "ci"] });
|
|
223
|
+
await call(s);
|
|
224
|
+
assert.deepEqual(calls, [
|
|
225
|
+
"read acme/api .factory abc",
|
|
226
|
+
"status acme/api deployed",
|
|
227
|
+
"check acme/api abc factory/build dev queued",
|
|
228
|
+
"check acme/api abc factory/build ci queued",
|
|
229
|
+
]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("a check that the channel can't write doesn't fail the deploy", async () => {
|
|
233
|
+
const calls: string[] = [];
|
|
234
|
+
const channel: RemoteChannel<FakeEvent> = {
|
|
235
|
+
...sourceChannel([{ repo: "acme/api", ref: "abc" }], calls),
|
|
236
|
+
writeCheck: async () => {
|
|
237
|
+
throw new Error("no checks permission");
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
const s = sourceStore(channel, calls);
|
|
241
|
+
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now", building: ["dev"] });
|
|
242
|
+
const { status } = await call(s);
|
|
243
|
+
assert.equal(status, 202);
|
|
244
|
+
assert.deepEqual(calls, ["read acme/api .factory abc", "status acme/api deployed"]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const buildReport = (s: RouterStore, body: unknown) =>
|
|
248
|
+
handle(new Request("https://router.test/builds", { method: "POST", body: JSON.stringify(body) }), s);
|
|
249
|
+
|
|
250
|
+
test("builds writes a check from a report the deployer signed, through the code host's channel", async () => {
|
|
251
|
+
const checks: unknown[] = [];
|
|
252
|
+
const channel: RemoteChannel<FakeEvent> = {
|
|
253
|
+
...sourceChannel([], []),
|
|
254
|
+
writeCheck: async (repo, ref, check) => void checks.push([repo, ref, check]),
|
|
255
|
+
};
|
|
256
|
+
const s = sourceStore(channel, []);
|
|
257
|
+
const report = {
|
|
258
|
+
repo: "acme/api",
|
|
259
|
+
ref: "abc",
|
|
260
|
+
container: "dev",
|
|
261
|
+
state: "failure" as const,
|
|
262
|
+
url: "https://dash.test/build",
|
|
263
|
+
expires: Date.now() + 60_000,
|
|
264
|
+
};
|
|
265
|
+
const signature = await signBuildReport(ROOT_KEY, report);
|
|
266
|
+
|
|
267
|
+
assert.equal((await buildReport(s, { report, signature })).status, 200);
|
|
268
|
+
assert.deepEqual(checks, [["acme/api", "abc", { name: "factory/build dev", state: "failure", url: "https://dash.test/build" }]]);
|
|
269
|
+
|
|
270
|
+
const forged = { ...report, state: "success" };
|
|
271
|
+
assert.equal((await buildReport(s, { report: forged, signature })).status, 401);
|
|
272
|
+
const expired = { ...report, expires: Date.now() - 1 };
|
|
273
|
+
assert.equal((await buildReport(s, { report: expired, signature: await signBuildReport(ROOT_KEY, expired) })).status, 401);
|
|
274
|
+
// A token ticket's signature doesn't sign a build report.
|
|
275
|
+
assert.equal((await buildReport(s, { report, signature: await signTokenTicket(ROOT_KEY, report as never) })).status, 401);
|
|
276
|
+
assert.equal(checks.length, 1);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const runOutpost: OutpostConfig = {
|
|
280
|
+
channels: { here: { provider: "fake", connection: "main", scope: "acme/api" } },
|
|
281
|
+
containers: { dev: { image: "./Dockerfile", runtimes: ["claude"], policies: ["code", "docs"], files: {} } },
|
|
282
|
+
policies: {
|
|
283
|
+
code: { provider: "fake", permissions: { contents: "write" } },
|
|
284
|
+
docs: { provider: "fake", repo: "acme/docs", permissions: { contents: "read" } },
|
|
285
|
+
},
|
|
286
|
+
agents: { coder: { runtime: "claude", model: "m" } },
|
|
287
|
+
triggers: {
|
|
288
|
+
mention: {
|
|
289
|
+
channel: "here",
|
|
290
|
+
event: "post",
|
|
291
|
+
match: { pick: true },
|
|
292
|
+
agent: "coder",
|
|
293
|
+
container: "dev",
|
|
294
|
+
worktree: { base: "develop" },
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const runStore = (channel: RemoteChannel<FakeEvent>, deployer: Partial<RemoteDeployer>): RouterStore => ({
|
|
300
|
+
...store({ main: channel }, { "acme/api": runOutpost }),
|
|
301
|
+
config: {
|
|
302
|
+
connections: { "fake/main": entry("CHANNEL_MAIN") },
|
|
303
|
+
codeHost: { provider: "fake", connection: "main", allow: [{ owner: "acme" }] },
|
|
304
|
+
},
|
|
305
|
+
deployer: {
|
|
306
|
+
deploy: async () => ({ state: "deployed", at: "now" }),
|
|
307
|
+
remove: async () => {},
|
|
308
|
+
worktree: async () => undefined,
|
|
309
|
+
run: async () => {},
|
|
310
|
+
...deployer,
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("a match whose author may start a run hands the deployer a run, with its branch, tokens and a reply ticket", async () => {
|
|
315
|
+
const runs: Run[] = [];
|
|
316
|
+
const worktrees: unknown[] = [];
|
|
317
|
+
const s = runStore(accepted(), {
|
|
318
|
+
worktree: async (repo, trigger, payload) => {
|
|
319
|
+
worktrees.push([repo, trigger, payload]);
|
|
320
|
+
return "issue-1";
|
|
321
|
+
},
|
|
322
|
+
run: async (run) => void runs.push(run),
|
|
323
|
+
});
|
|
324
|
+
const { body } = await call(s);
|
|
325
|
+
assert.equal(runs.length, 1);
|
|
326
|
+
const [run] = runs;
|
|
327
|
+
assert.equal(run!.id, body.matches[0].run);
|
|
328
|
+
assert.deepEqual(worktrees, [["acme/api", "mention", { number: 1 }]]);
|
|
329
|
+
assert.deepEqual(run!.worktree, { branch: "issue-1", base: "develop" });
|
|
330
|
+
assert.equal(run!.container, "dev");
|
|
331
|
+
assert.deepEqual(run!.runtimes, ["claude"]);
|
|
332
|
+
assert.deepEqual(run!.agent, { id: "coder", runtime: "claude", model: "m" });
|
|
333
|
+
assert.deepEqual(run!.conversation, { id: "fake:acme/api", prompt: "do it" });
|
|
334
|
+
assert.equal(run!.cloneToken, 'token:acme/api:{"contents":"read"}');
|
|
335
|
+
assert.deepEqual(run!.tokens, [
|
|
336
|
+
{ policy: "code", repo: "acme/api", token: 'token:acme/api:{"contents":"write"}' },
|
|
337
|
+
{ policy: "docs", repo: "acme/docs", token: 'token:acme/docs:{"contents":"read"}' },
|
|
338
|
+
]);
|
|
339
|
+
assert.equal(run!.refresh.url, "https://router.test/tokens");
|
|
340
|
+
assert.equal(run!.refresh.ticket.repo, "acme/api");
|
|
341
|
+
assert.equal(run!.refresh.ticket.container, "dev");
|
|
342
|
+
assert.equal(run!.refresh.signature, await signTokenTicket(ROOT_KEY, run!.refresh.ticket));
|
|
343
|
+
assert.equal(run!.reply.url, "https://router.test/relay");
|
|
344
|
+
assert.deepEqual(run!.reply.ticket.thread, { repo: "acme/api" });
|
|
345
|
+
assert.equal(run!.reply.signature, await signTicket(ROOT_KEY, run!.reply.ticket));
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test("a worktree function that names no branch runs in a scratch worktree", async () => {
|
|
349
|
+
const runs: Run[] = [];
|
|
350
|
+
await call(runStore(accepted(), { run: async (run) => void runs.push(run) }));
|
|
351
|
+
assert.equal(runs[0]!.worktree, undefined);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("a run that can't start replies with the error", async () => {
|
|
355
|
+
const replies: unknown[] = [];
|
|
356
|
+
const runs: Run[] = [];
|
|
357
|
+
const channel = { ...accepted(), reply: async (thread: unknown, text: string) => void replies.push([thread, text]) };
|
|
358
|
+
await call(runStore(channel, { worktree: async () => "../etc", run: async (run) => void runs.push(run) }));
|
|
359
|
+
assert.equal(runs.length, 0);
|
|
360
|
+
assert.equal(replies.length, 1);
|
|
361
|
+
assert.match((replies[0] as string[])[1]!, /returned "\.\.\/etc", which isn't a branch name/);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test("a denied author starts no run", async () => {
|
|
365
|
+
const runs: Run[] = [];
|
|
366
|
+
const { body } = await call(runStore(accepted({ data: { allow: false } }), { run: async (run) => void runs.push(run) }));
|
|
367
|
+
assert.deepEqual(body.matches, [{ trigger: "mention", author: { result: "denied" } }]);
|
|
368
|
+
assert.equal(runs.length, 0);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const relay = (s: RouterStore, body: unknown) =>
|
|
372
|
+
handle(new Request("https://router.test/relay", { method: "POST", body: JSON.stringify(body) }), s);
|
|
373
|
+
|
|
374
|
+
test("relay posts a reply with a ticket the router signed through the ticket's channel", async () => {
|
|
375
|
+
const replies: unknown[] = [];
|
|
376
|
+
const s = runStore({ ...accepted(), reply: async (thread, text) => void replies.push([thread, text]) }, {});
|
|
377
|
+
const ticket = { connection: "fake/main", thread: { repo: "acme/api" }, expires: Date.now() + 60_000 };
|
|
378
|
+
const signature = await signTicket(ROOT_KEY, ticket);
|
|
379
|
+
|
|
380
|
+
assert.equal((await relay(s, { ticket, signature, text: "done" })).status, 200);
|
|
381
|
+
assert.deepEqual(replies, [[{ repo: "acme/api" }, "done"]]);
|
|
382
|
+
|
|
383
|
+
const forged = { ...ticket, thread: { repo: "acme/other" } };
|
|
384
|
+
assert.equal((await relay(s, { ticket: forged, signature, text: "x" })).status, 401);
|
|
385
|
+
const expired = { ...ticket, expires: Date.now() - 1 };
|
|
386
|
+
assert.equal((await relay(s, { ticket: expired, signature: await signTicket(ROOT_KEY, expired), text: "x" })).status, 401);
|
|
387
|
+
assert.equal(replies.length, 1);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
const refresh = (s: RouterStore, body: unknown) =>
|
|
391
|
+
handle(new Request("https://router.test/tokens", { method: "POST", body: JSON.stringify(body) }), s);
|
|
392
|
+
|
|
393
|
+
test("tokens mints a container's policy tokens again with a ticket the router signed", async () => {
|
|
394
|
+
const s = runStore(accepted(), {});
|
|
395
|
+
const ticket = { repo: "acme/api", container: "dev", expires: Date.now() + 60_000 };
|
|
396
|
+
const signature = await signTokenTicket(ROOT_KEY, ticket);
|
|
397
|
+
|
|
398
|
+
const response = await refresh(s, { ticket, signature });
|
|
399
|
+
assert.equal(response.status, 200);
|
|
400
|
+
assert.deepEqual(await response.json(), [
|
|
401
|
+
{ policy: "code", repo: "acme/api", token: 'token:acme/api:{"contents":"write"}' },
|
|
402
|
+
{ policy: "docs", repo: "acme/docs", token: 'token:acme/docs:{"contents":"read"}' },
|
|
403
|
+
]);
|
|
404
|
+
|
|
405
|
+
const forged = { ...ticket, repo: "acme/other" };
|
|
406
|
+
assert.equal((await refresh(s, { ticket: forged, signature })).status, 401);
|
|
407
|
+
const expired = { ...ticket, expires: Date.now() - 1 };
|
|
408
|
+
assert.equal((await refresh(s, { ticket: expired, signature: await signTokenTicket(ROOT_KEY, expired) })).status, 401);
|
|
409
|
+
// A reply ticket's signature doesn't sign a token ticket.
|
|
410
|
+
assert.equal((await refresh(s, { ticket, signature: await signTicket(ROOT_KEY, ticket as never) })).status, 401);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test("tokens refuses a container that the repo's outpost no longer has", async () => {
|
|
414
|
+
const s = runStore(accepted(), {});
|
|
415
|
+
const ticket = { repo: "acme/api", container: "gone", expires: Date.now() + 60_000 };
|
|
416
|
+
const response = await refresh(s, { ticket, signature: await signTokenTicket(ROOT_KEY, ticket) });
|
|
417
|
+
assert.equal(response.status, 404);
|
|
418
|
+
assert.match((await response.json()).error, /no container gone/);
|
|
419
|
+
});
|