@mnaimfaizy/agent-kit 1.0.0-alpha.12 → 1.0.0-beta.10
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/README.md +14 -6
- package/bin/agent-kit.mjs +3 -1
- package/package.json +1 -1
- package/src/callers.mjs +73 -10
- package/src/checks/kit-owned-trees.mjs +47 -4
- package/src/checks/result.mjs +8 -2
- package/src/checks/review-response.mjs +205 -0
- package/src/checks/rulesets.mjs +7 -2
- package/src/checks/scan-recipe.mjs +56 -0
- package/src/checks/secrets-and-variables.mjs +65 -15
- package/src/cli.mjs +306 -19
- package/src/deep-scan/agent.mjs +410 -0
- package/src/deep-scan/claude-code.mjs +25 -0
- package/src/deep-scan/credential.mjs +215 -0
- package/src/deep-scan/docker-driver.mjs +696 -0
- package/src/deep-scan/egress.mjs +150 -0
- package/src/deep-scan/lifecycle.mjs +703 -0
- package/src/deep-scan/limits.mjs +126 -0
- package/src/deep-scan/model-proxy.mjs +378 -0
- package/src/deep-scan/preflight.mjs +78 -0
- package/src/deep-scan/recipe.mjs +447 -0
- package/src/deep-scan/relay-script.mjs +98 -0
- package/src/deep-scan/relay.mjs +130 -0
- package/src/deep-scan/runtime.mjs +181 -0
- package/src/deep-scan/sbx-driver.mjs +501 -0
- package/src/deep-scan/steps.mjs +47 -0
- package/src/deep-scan/tar.mjs +84 -0
- package/src/deep-scan/verify.mjs +272 -0
- package/src/deep-scan.mjs +474 -0
- package/src/doctor.mjs +57 -16
- package/src/exec.mjs +225 -23
- package/src/init.mjs +653 -0
- package/src/kit-owned-paths.mjs +5 -3
- package/src/kit-tag.mjs +175 -28
- package/src/local-tree.mjs +142 -27
- package/src/local-writes.mjs +278 -0
- package/src/prompt.mjs +42 -0
- package/src/safe-paths.mjs +68 -0
- package/src/tags.mjs +15 -5
- package/src/templates.mjs +137 -0
- package/src/upgrade.mjs +288 -0
- package/src/workflow-call.mjs +3 -5
- package/src/yaml-subset.mjs +134 -24
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
// The sandbox driver for the runtimes Docker Engine runs (#369): gVisor,
|
|
2
|
+
// Kata Containers and Firecracker registered as a Docker runtime on Linux,
|
|
3
|
+
// with Docker Engine 28.0 or later. It turns each lifecycle operation into
|
|
4
|
+
// `docker` commands; the order, the output and the report belong to
|
|
5
|
+
// lifecycle.mjs.
|
|
6
|
+
//
|
|
7
|
+
// The route to the host (#404): the sandbox has one network, attached at
|
|
8
|
+
// `docker run` and never connected or disconnected after, since a running
|
|
9
|
+
// gVisor sandbox does not see such a change. It is `--internal` with the
|
|
10
|
+
// `isolated` gateway mode, so it has no route out and the host has no
|
|
11
|
+
// address on it. The sandbox's one way to a host-side proxy is a relay
|
|
12
|
+
// container on that network and on an ordinary bridge network (the host
|
|
13
|
+
// network), which forwards one TCP port to the proxy on that bridge's
|
|
14
|
+
// gateway (relay.mjs). The relay only forwards bytes: the model credential
|
|
15
|
+
// stays in the host-side proxy. Each copy in or out goes through `docker
|
|
16
|
+
// exec`, since what `docker cp` moves is hidden from a running gVisor
|
|
17
|
+
// sandbox.
|
|
18
|
+
//
|
|
19
|
+
// The checks from the host: `docker network inspect` shows the network
|
|
20
|
+
// isolated with no gateway address, `docker inspect` shows the sandbox on it
|
|
21
|
+
// alone, and a launcher-owned probe on it, under the sandbox's own runtime,
|
|
22
|
+
// reaches what it must and nothing else: before the target starts (the setup
|
|
23
|
+
// relay), after the cut (nothing) and before the agent step (the model
|
|
24
|
+
// relay). Any failure ends the run before the agent step.
|
|
25
|
+
//
|
|
26
|
+
// Setup egress: the setup relay forwards to the egress proxy the launcher
|
|
27
|
+
// runs on the host (egress.mjs), the recipe's registries its only
|
|
28
|
+
// destinations. The cut stops the proxy and removes the setup relay.
|
|
29
|
+
//
|
|
30
|
+
// The model route (#370): for the agent step, the model relay, on the same
|
|
31
|
+
// network, forwards to the key-injecting model proxy, so the proxy is the
|
|
32
|
+
// scan's only route to the internet.
|
|
33
|
+
|
|
34
|
+
import { isIPv4 } from "node:net";
|
|
35
|
+
import { layerDockerfile } from "./claude-code.mjs";
|
|
36
|
+
import { startEgressProxy } from "./egress.mjs";
|
|
37
|
+
import {
|
|
38
|
+
OTHER_HOST_PORT,
|
|
39
|
+
probeFailure,
|
|
40
|
+
RELAY_SCRIPT_PATH,
|
|
41
|
+
relayOptions,
|
|
42
|
+
relayBuildContext,
|
|
43
|
+
} from "./relay.mjs";
|
|
44
|
+
import { failed, must, StepFailed, ENDS_HERE } from "./steps.mjs";
|
|
45
|
+
import { singleFileOf } from "./tar.mjs";
|
|
46
|
+
|
|
47
|
+
/** The label every sandbox resource carries, so a stray one can be found. */
|
|
48
|
+
export const LABEL = "agent-kit.deep-scan";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @typedef {import("../exec.mjs").StartResult} StartResult
|
|
52
|
+
* @typedef {(cmd: string, args: string[], opts?: { stdin?: Buffer | string, cwd?: string, maxOutput?: number }) => Promise<StartResult>} Call
|
|
53
|
+
* Runs a command without blocking; throws Interrupted when the run is.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/** The `docker network create` options that leave the host no address on a sandbox network. */
|
|
57
|
+
export const ISOLATED_OPTIONS = Object.freeze([
|
|
58
|
+
"com.docker.network.bridge.gateway_mode_ipv4=isolated",
|
|
59
|
+
"com.docker.network.bridge.gateway_mode_ipv6=isolated",
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {{ runtime: import("./runtime.mjs").Runtime, id: string, call: Call }} opts
|
|
64
|
+
* @returns {import("./lifecycle.mjs").Driver}
|
|
65
|
+
*/
|
|
66
|
+
export function dockerDriver({ runtime, id, call }) {
|
|
67
|
+
const name = `agent-kit-deep-${id}`;
|
|
68
|
+
const label = `${LABEL}=${id}`;
|
|
69
|
+
const docker = (args, opts) => call("docker", args, opts);
|
|
70
|
+
/** Every resource asked for, in order, whether or not its command finished. */
|
|
71
|
+
const resources = [];
|
|
72
|
+
/** The sandbox's one network, isolated, attached at `docker run`. */
|
|
73
|
+
const network = name;
|
|
74
|
+
let proxy = null;
|
|
75
|
+
let proxyEnv = [];
|
|
76
|
+
let modelProxy = null;
|
|
77
|
+
/**
|
|
78
|
+
* The sandbox network's subnet prefix ("198.18.N"), once created. The
|
|
79
|
+
* relays and probes take fixed addresses in its lower half, each its own
|
|
80
|
+
* and none reused in a run, while Docker hands out the upper half: on an
|
|
81
|
+
* isolated network nothing reserves `.1`, so an address a removed
|
|
82
|
+
* container freed would otherwise go to the next one (#404).
|
|
83
|
+
*/
|
|
84
|
+
let prefix = null;
|
|
85
|
+
/** The relays on the sandbox's network: { address, port }, once started. */
|
|
86
|
+
let setupRelay = null;
|
|
87
|
+
let modelRelay = null;
|
|
88
|
+
/** The ordinary bridge network the relays reach the host on, and its gateway. */
|
|
89
|
+
let hostNetwork = null;
|
|
90
|
+
let hostGateway = null;
|
|
91
|
+
/** The relay image, once built. */
|
|
92
|
+
let relayImage = null;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
container: name,
|
|
96
|
+
|
|
97
|
+
async prepare(steps, { archive, out, branch, short }) {
|
|
98
|
+
let base;
|
|
99
|
+
if (steps.dockerfile !== undefined) {
|
|
100
|
+
out(`Image: building ${steps.dockerfile} from ${branch} (${short}).`);
|
|
101
|
+
const context = archive({}, `export ${branch} as the build context`);
|
|
102
|
+
base = `${name}-base`;
|
|
103
|
+
resources.push(["image", base]);
|
|
104
|
+
must(
|
|
105
|
+
await docker(
|
|
106
|
+
["build", "--file", steps.dockerfile, "--label", label, "--tag", base, "-"],
|
|
107
|
+
{ stdin: context },
|
|
108
|
+
),
|
|
109
|
+
`building ${steps.dockerfile}`,
|
|
110
|
+
{ showOutput: true },
|
|
111
|
+
);
|
|
112
|
+
} else {
|
|
113
|
+
base = steps.image;
|
|
114
|
+
out(`Image: pulling ${base}.`);
|
|
115
|
+
must(await docker(["pull", base]), `pulling ${base}`, { showOutput: true });
|
|
116
|
+
}
|
|
117
|
+
out("Image: layering the pinned Claude Code, auto-updater off.");
|
|
118
|
+
resources.push(["image", name]);
|
|
119
|
+
must(
|
|
120
|
+
await docker(["build", "--label", label, "--tag", name, "-"], {
|
|
121
|
+
stdin: layerDockerfile(base),
|
|
122
|
+
}),
|
|
123
|
+
`layering Claude Code on ${base}`,
|
|
124
|
+
{ showOutput: true },
|
|
125
|
+
);
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Start the sandbox on its one isolated network, with every network it
|
|
130
|
+
* will need attached at `docker run`: under gVisor a running sandbox
|
|
131
|
+
* never sees a network connected or disconnected later (#404). Before
|
|
132
|
+
* the target starts, check the network from the host.
|
|
133
|
+
*/
|
|
134
|
+
async start(steps) {
|
|
135
|
+
await ensureHostNetwork();
|
|
136
|
+
await isolatedNetwork(network, "the setup network");
|
|
137
|
+
if (steps.registries.length) await setupEgress(steps.registries);
|
|
138
|
+
const why = await probe("setup", [
|
|
139
|
+
...(setupRelay ? [relayTarget(setupRelay, "connected", "the relay")] : []),
|
|
140
|
+
...gatewayTargets(setupRelay?.port),
|
|
141
|
+
]);
|
|
142
|
+
if (why !== null) {
|
|
143
|
+
throw new StepFailed([`FAILED the setup network is not isolated: ${why}.`, ENDS_HERE]);
|
|
144
|
+
}
|
|
145
|
+
resources.push(["container", name]);
|
|
146
|
+
must(
|
|
147
|
+
await docker([
|
|
148
|
+
"run",
|
|
149
|
+
"--detach",
|
|
150
|
+
"--init",
|
|
151
|
+
"--name",
|
|
152
|
+
name,
|
|
153
|
+
"--label",
|
|
154
|
+
label,
|
|
155
|
+
...(runtime.dockerRuntime ? ["--runtime", runtime.dockerRuntime] : []),
|
|
156
|
+
"--network",
|
|
157
|
+
network,
|
|
158
|
+
"--security-opt",
|
|
159
|
+
"no-new-privileges",
|
|
160
|
+
"--workdir",
|
|
161
|
+
"/workspace",
|
|
162
|
+
"--entrypoint",
|
|
163
|
+
"tail",
|
|
164
|
+
name,
|
|
165
|
+
"-f",
|
|
166
|
+
"/dev/null",
|
|
167
|
+
]),
|
|
168
|
+
"starting the sandbox",
|
|
169
|
+
);
|
|
170
|
+
await onAlone(network, "the setup network");
|
|
171
|
+
return {
|
|
172
|
+
isolation: setupRelay
|
|
173
|
+
? "Isolation: checked from the host before the target starts; the sandbox reaches only the setup relay's port."
|
|
174
|
+
: "Isolation: checked from the host before the target starts; the sandbox reaches nothing.",
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Unpack `tar` at the sandbox's root, through `docker exec` as root (as
|
|
180
|
+
* `docker cp` would write it). Never `docker cp`: under gVisor, what it
|
|
181
|
+
* writes into a running container is hidden from the sandbox once the
|
|
182
|
+
* sandbox has looked up that path (#404).
|
|
183
|
+
*/
|
|
184
|
+
async copyIn(tar, what) {
|
|
185
|
+
must(
|
|
186
|
+
await docker(["exec", "-i", "--user", "0:0", name, "tar", "-x", "-f", "-", "-C", "/"], {
|
|
187
|
+
stdin: tar,
|
|
188
|
+
}),
|
|
189
|
+
what,
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
/** Claude Code is already in the image. */
|
|
194
|
+
async installAgent() {},
|
|
195
|
+
|
|
196
|
+
/** The setup egress was opened with the network: the proxy's variables. */
|
|
197
|
+
async openSetupEgress(registries) {
|
|
198
|
+
return registries.length
|
|
199
|
+
? {
|
|
200
|
+
env: proxyEnv,
|
|
201
|
+
note: `egress limited to ${registries.join(", ")}, through a relay to the host-side proxy; checked from the host: the sandbox reaches the relay's port alone`,
|
|
202
|
+
}
|
|
203
|
+
: { env: [], note: "no registries, so nothing on its network to reach" };
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
refused: () => proxy?.refused ?? [],
|
|
207
|
+
|
|
208
|
+
exec: (script, { env = [], workdir = "/workspace" } = {}) =>
|
|
209
|
+
docker([
|
|
210
|
+
"exec",
|
|
211
|
+
...env.flatMap((v) => ["--env", v]),
|
|
212
|
+
...(workdir ? ["--workdir", workdir] : []),
|
|
213
|
+
name,
|
|
214
|
+
"sh",
|
|
215
|
+
"-c",
|
|
216
|
+
script,
|
|
217
|
+
]),
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The cut: stop the egress proxy and remove the setup relay. Nothing is
|
|
221
|
+
* disconnected (gVisor would not see it): the probe in `egressLeft`
|
|
222
|
+
* proves nothing on the network is left to reach.
|
|
223
|
+
*/
|
|
224
|
+
async cutEgress() {
|
|
225
|
+
await closeProxy();
|
|
226
|
+
if (setupRelay) {
|
|
227
|
+
must(await docker(["rm", "--force", `${name}-relay-setup`]), "removing the setup relay");
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Null when a probe on the sandbox's network, under the sandbox's own
|
|
233
|
+
* runtime, reaches what it must and nothing else; else why not. After
|
|
234
|
+
* the cut: not the removed setup relay, nor the host network's gateway.
|
|
235
|
+
* With the model route open: the model relay, and not those, after
|
|
236
|
+
* `docker network inspect` shows the network still isolated.
|
|
237
|
+
*/
|
|
238
|
+
async egressLeft() {
|
|
239
|
+
if (!modelRelay) {
|
|
240
|
+
return probe("cut", [
|
|
241
|
+
...(setupRelay
|
|
242
|
+
? [relayTarget(setupRelay, "unreachable", "the removed setup relay")]
|
|
243
|
+
: []),
|
|
244
|
+
...gatewayTargets(setupRelay?.port),
|
|
245
|
+
]);
|
|
246
|
+
}
|
|
247
|
+
const r = must(
|
|
248
|
+
await docker(["network", "inspect", "--format", "{{json .}}", network]),
|
|
249
|
+
"checking the sandbox's network",
|
|
250
|
+
);
|
|
251
|
+
const isolation = isolationFailure(r.stdout.toString("utf8"));
|
|
252
|
+
if (isolation !== null) return `docker network inspect shows ${isolation}`;
|
|
253
|
+
return probe("model", [
|
|
254
|
+
relayTarget(modelRelay, "connected", "the model relay"),
|
|
255
|
+
...(setupRelay ? [relayTarget(setupRelay, "unreachable", "the removed setup relay")] : []),
|
|
256
|
+
...gatewayTargets(modelRelay.port),
|
|
257
|
+
]);
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Start the model proxy and its relay on the sandbox's network, which
|
|
262
|
+
* the sandbox has been on since `docker run`.
|
|
263
|
+
*/
|
|
264
|
+
async openModelRoute(startProxy) {
|
|
265
|
+
const bind = await ensureHostNetwork();
|
|
266
|
+
try {
|
|
267
|
+
modelProxy = await startProxy(bind);
|
|
268
|
+
} catch (e) {
|
|
269
|
+
throw failed(`starting the model proxy on ${bind}`, {
|
|
270
|
+
status: null,
|
|
271
|
+
stdout: Buffer.alloc(0),
|
|
272
|
+
stderr: e.message,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
modelRelay = await startRelay("model", modelProxy.port);
|
|
276
|
+
return {
|
|
277
|
+
url: `http://${hostOf(modelRelay.address)}:${modelProxy.port}`,
|
|
278
|
+
note: `through a relay on the isolated network ${network}`,
|
|
279
|
+
isolation:
|
|
280
|
+
"Isolation: checked from the host before the agent step; the sandbox reaches only the model relay's port.",
|
|
281
|
+
};
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The file at `path`, as a tar `docker exec` writes from the sandbox:
|
|
286
|
+
* first checked in the sandbox (a regular file, not a link, of at most
|
|
287
|
+
* `maxBytes`), then, on the host, read as exactly one regular file of at
|
|
288
|
+
* most `maxBytes`. Never `docker cp`, which under gVisor cannot read
|
|
289
|
+
* what the sandbox wrote (#404).
|
|
290
|
+
*/
|
|
291
|
+
async copyOut(path, { maxBytes }) {
|
|
292
|
+
// The sandbox writes this output: never hold more of it than one
|
|
293
|
+
// report's tar can be.
|
|
294
|
+
const r = await docker(
|
|
295
|
+
["exec", "--user", "0:0", name, "sh", "-c", copyOutScript(path, maxBytes)],
|
|
296
|
+
{ maxOutput: maxBytes + TAR_OVERHEAD },
|
|
297
|
+
);
|
|
298
|
+
if (r.truncated) {
|
|
299
|
+
return { error: `it is larger than ${maxBytes / 1024 / 1024} MiB (read on the host)` };
|
|
300
|
+
}
|
|
301
|
+
if (r.status === 4) return { error: "it is not a regular file (checked in the sandbox)" };
|
|
302
|
+
if (r.status === 5) {
|
|
303
|
+
return {
|
|
304
|
+
error: `it is larger than ${maxBytes / 1024 / 1024} MiB (checked in the sandbox)`,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (r.status !== 0) return { missing: r };
|
|
308
|
+
return singleFileOf(r.stdout, { maxBytes });
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
async close() {
|
|
312
|
+
await closeProxy();
|
|
313
|
+
if (modelProxy) {
|
|
314
|
+
const p = modelProxy;
|
|
315
|
+
modelProxy = null;
|
|
316
|
+
await p.close();
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The commands that remove every resource asked for: the sandbox first,
|
|
322
|
+
* then the relays and probes (a network still in use cannot be removed),
|
|
323
|
+
* then the rest, newest first.
|
|
324
|
+
*/
|
|
325
|
+
teardown() {
|
|
326
|
+
const newest = [...resources].reverse();
|
|
327
|
+
const container = ([kind]) => kind === "container";
|
|
328
|
+
const ordered = [
|
|
329
|
+
...newest.filter((r) => container(r) && r[1] === name),
|
|
330
|
+
...newest.filter((r) => container(r) && r[1] !== name),
|
|
331
|
+
...newest.filter((r) => !container(r)),
|
|
332
|
+
];
|
|
333
|
+
return ordered.map(([kind, n]) => ({
|
|
334
|
+
what: `${kind} ${n}`,
|
|
335
|
+
cmd: "docker",
|
|
336
|
+
args:
|
|
337
|
+
kind === "container"
|
|
338
|
+
? ["rm", "--force", "--volumes", n]
|
|
339
|
+
: kind === "network"
|
|
340
|
+
? ["network", "rm", n]
|
|
341
|
+
: ["image", "rm", "--force", n],
|
|
342
|
+
}));
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
async function closeProxy() {
|
|
347
|
+
if (proxy) {
|
|
348
|
+
const p = proxy;
|
|
349
|
+
proxy = null;
|
|
350
|
+
await p.close();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** An address as a URL host. */
|
|
355
|
+
function hostOf(address) {
|
|
356
|
+
return address.includes(":") ? `[${address}]` : address;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Check from the host that `docker inspect` shows the sandbox on `net` alone. */
|
|
360
|
+
async function onAlone(net, what) {
|
|
361
|
+
const r = must(
|
|
362
|
+
await docker([
|
|
363
|
+
"container",
|
|
364
|
+
"inspect",
|
|
365
|
+
"--format",
|
|
366
|
+
"{{json .NetworkSettings.Networks}}",
|
|
367
|
+
name,
|
|
368
|
+
]),
|
|
369
|
+
"checking the sandbox's networks",
|
|
370
|
+
);
|
|
371
|
+
let attached = null;
|
|
372
|
+
try {
|
|
373
|
+
const parsed = JSON.parse(r.stdout.toString("utf8").trim() || "null");
|
|
374
|
+
if (parsed && typeof parsed === "object") attached = Object.keys(parsed);
|
|
375
|
+
} catch {
|
|
376
|
+
attached = null;
|
|
377
|
+
}
|
|
378
|
+
const left =
|
|
379
|
+
attached === null
|
|
380
|
+
? "docker inspect shows an unknown network state"
|
|
381
|
+
: attached.some((n) => n !== net)
|
|
382
|
+
? `docker inspect shows the sandbox on ${attached.filter((n) => n !== net).join(", ")}`
|
|
383
|
+
: null;
|
|
384
|
+
if (left !== null) {
|
|
385
|
+
throw new StepFailed([`FAILED the sandbox is not on ${what} alone: ${left}.`, ENDS_HERE]);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** A probe target on a relay. */
|
|
390
|
+
function relayTarget(relay, want, what) {
|
|
391
|
+
return { host: relay.address, port: relay.port, want, what };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** The host network's gateway, on the proxy's port when there is one, and on another. */
|
|
395
|
+
function gatewayTargets(port) {
|
|
396
|
+
return [...(port ? [port] : []), OTHER_HOST_PORT].map((p) => ({
|
|
397
|
+
host: hostGateway,
|
|
398
|
+
port: p,
|
|
399
|
+
want: "unreachable",
|
|
400
|
+
what: "the host network's gateway",
|
|
401
|
+
}));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* The ordinary bridge network the relays reach the host on, created once,
|
|
406
|
+
* and its gateway: the host address the host-side proxies listen on.
|
|
407
|
+
*/
|
|
408
|
+
async function ensureHostNetwork() {
|
|
409
|
+
if (hostNetwork) return hostGateway;
|
|
410
|
+
hostNetwork = `${name}-host`;
|
|
411
|
+
resources.push(["network", hostNetwork]);
|
|
412
|
+
must(
|
|
413
|
+
await docker(["network", "create", "--label", label, hostNetwork]),
|
|
414
|
+
"creating the relays' host network",
|
|
415
|
+
);
|
|
416
|
+
const inspect = must(
|
|
417
|
+
await docker(["network", "inspect", "--format", "{{json .IPAM.Config}}", hostNetwork]),
|
|
418
|
+
"reading the relays' host network's gateway",
|
|
419
|
+
);
|
|
420
|
+
// A dual-stack bridge lists an IPv6 config too: the proxies go on the
|
|
421
|
+
// IPv4 gateway, the address the relay forwards to.
|
|
422
|
+
const gateway = ipv4GatewayOf(inspect.stdout.toString("utf8"));
|
|
423
|
+
if (gateway === null) {
|
|
424
|
+
throw new StepFailed([
|
|
425
|
+
"FAILED the relays' host network has no IPv4 gateway address to put a proxy on.",
|
|
426
|
+
ENDS_HERE,
|
|
427
|
+
]);
|
|
428
|
+
}
|
|
429
|
+
hostGateway = gateway;
|
|
430
|
+
return gateway;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Create an `--internal` network in the `isolated` gateway mode, on a
|
|
435
|
+
* per-run subnet (another candidate when one overlaps a network on this
|
|
436
|
+
* host), and check from the host that `docker network inspect` shows it
|
|
437
|
+
* so, with no gateway address.
|
|
438
|
+
*/
|
|
439
|
+
async function isolatedNetwork(net, what) {
|
|
440
|
+
resources.push(["network", net]);
|
|
441
|
+
const tried = [];
|
|
442
|
+
for (const candidate of subnetCandidates(id)) {
|
|
443
|
+
const r = await docker([
|
|
444
|
+
"network",
|
|
445
|
+
"create",
|
|
446
|
+
"--internal",
|
|
447
|
+
...ISOLATED_OPTIONS.flatMap((o) => ["--opt", o]),
|
|
448
|
+
"--subnet",
|
|
449
|
+
`${candidate}.0/24`,
|
|
450
|
+
"--ip-range",
|
|
451
|
+
`${candidate}.128/25`,
|
|
452
|
+
"--label",
|
|
453
|
+
label,
|
|
454
|
+
net,
|
|
455
|
+
]);
|
|
456
|
+
if (r.status === 0) {
|
|
457
|
+
prefix = candidate;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
if (!/overlap/i.test(r.stderr)) must(r, `creating ${what}`);
|
|
461
|
+
tried.push(`${candidate}.0/24`);
|
|
462
|
+
}
|
|
463
|
+
if (prefix === null) {
|
|
464
|
+
throw new StepFailed([
|
|
465
|
+
`FAILED no free subnet for the sandbox's network: tried ${tried.join(", ")}, and each overlaps a network on this host.`,
|
|
466
|
+
ENDS_HERE,
|
|
467
|
+
]);
|
|
468
|
+
}
|
|
469
|
+
const r = must(
|
|
470
|
+
await docker(["network", "inspect", "--format", "{{json .}}", net]),
|
|
471
|
+
`checking ${what}`,
|
|
472
|
+
);
|
|
473
|
+
const why = isolationFailure(r.stdout.toString("utf8"));
|
|
474
|
+
if (why !== null) {
|
|
475
|
+
throw new StepFailed([
|
|
476
|
+
`FAILED ${what} is not isolated: docker network inspect shows ${why}.`,
|
|
477
|
+
ENDS_HERE,
|
|
478
|
+
]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** The fixed address of a relay or a probe on the sandbox's network. */
|
|
483
|
+
function fixedAddress(container) {
|
|
484
|
+
return `${prefix}.${FIXED_HOSTS[container]}`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* The relay image, built once from the pinned node image and the
|
|
489
|
+
* digest-checked relay script, its context sent on stdin.
|
|
490
|
+
*/
|
|
491
|
+
async function ensureRelayImage() {
|
|
492
|
+
if (relayImage) return relayImage;
|
|
493
|
+
const context = relayBuildContext();
|
|
494
|
+
relayImage = `${name}-relay`;
|
|
495
|
+
resources.push(["image", relayImage]);
|
|
496
|
+
must(
|
|
497
|
+
await docker(["build", "--label", label, "--tag", relayImage, "-"], { stdin: context }),
|
|
498
|
+
"building the relay image",
|
|
499
|
+
{ showOutput: true },
|
|
500
|
+
);
|
|
501
|
+
return relayImage;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Start the relay on the sandbox's network, at its fixed address, and on
|
|
506
|
+
* the host network, forwarding `port` to the host-side proxy on the host
|
|
507
|
+
* network's gateway; return its address and port. It listens on that one
|
|
508
|
+
* port, the proxy's own number.
|
|
509
|
+
*/
|
|
510
|
+
async function startRelay(phase, port) {
|
|
511
|
+
const relay = `${name}-relay-${phase}`;
|
|
512
|
+
const address = fixedAddress(`relay-${phase}`);
|
|
513
|
+
const image = await ensureRelayImage();
|
|
514
|
+
resources.push(["container", relay]);
|
|
515
|
+
must(
|
|
516
|
+
await docker([
|
|
517
|
+
"run",
|
|
518
|
+
"--detach",
|
|
519
|
+
"--name",
|
|
520
|
+
relay,
|
|
521
|
+
"--label",
|
|
522
|
+
label,
|
|
523
|
+
// The host network is its default route; the sandbox's has none.
|
|
524
|
+
"--network",
|
|
525
|
+
`name=${hostNetwork},gw-priority=1`,
|
|
526
|
+
"--network",
|
|
527
|
+
`name=${network},ip=${address}`,
|
|
528
|
+
...relayOptions(),
|
|
529
|
+
image,
|
|
530
|
+
RELAY_SCRIPT_PATH,
|
|
531
|
+
"forward",
|
|
532
|
+
String(port),
|
|
533
|
+
hostGateway,
|
|
534
|
+
String(port),
|
|
535
|
+
]),
|
|
536
|
+
`starting the ${phase} relay`,
|
|
537
|
+
);
|
|
538
|
+
return { address, port };
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Run a launcher-owned probe on the sandbox's network, at its own fixed
|
|
543
|
+
* address, under the sandbox's own runtime (so a runtime that sees the
|
|
544
|
+
* network otherwise than Docker says fails here), and return why its
|
|
545
|
+
* answer fails, or null. It runs the relay image alone: no code of the
|
|
546
|
+
* target's.
|
|
547
|
+
* @param {string} phase
|
|
548
|
+
* @param {import("./relay.mjs").ProbeTarget[]} targets
|
|
549
|
+
*/
|
|
550
|
+
async function probe(phase, targets) {
|
|
551
|
+
const container = `${name}-probe-${phase}`;
|
|
552
|
+
const image = await ensureRelayImage();
|
|
553
|
+
resources.push(["container", container]);
|
|
554
|
+
const r = must(
|
|
555
|
+
await docker([
|
|
556
|
+
"run",
|
|
557
|
+
"--rm",
|
|
558
|
+
"--name",
|
|
559
|
+
container,
|
|
560
|
+
"--label",
|
|
561
|
+
label,
|
|
562
|
+
...(runtime.dockerRuntime ? ["--runtime", runtime.dockerRuntime] : []),
|
|
563
|
+
"--network",
|
|
564
|
+
`name=${network},ip=${fixedAddress(`probe-${phase}`)}`,
|
|
565
|
+
...relayOptions(),
|
|
566
|
+
image,
|
|
567
|
+
RELAY_SCRIPT_PATH,
|
|
568
|
+
"probe",
|
|
569
|
+
...targets.flatMap((t) => [t.host, String(t.port)]),
|
|
570
|
+
]),
|
|
571
|
+
`probing the sandbox's network (${phase})`,
|
|
572
|
+
);
|
|
573
|
+
return probeFailure(r.stdout.toString("utf8"), targets);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
async function setupEgress(registries) {
|
|
577
|
+
const bind = hostGateway;
|
|
578
|
+
try {
|
|
579
|
+
proxy = await startEgressProxy({ registries, bind });
|
|
580
|
+
} catch (e) {
|
|
581
|
+
throw failed(`starting the setup registry proxy on ${bind}`, {
|
|
582
|
+
status: null,
|
|
583
|
+
stdout: Buffer.alloc(0),
|
|
584
|
+
stderr: e.message,
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
setupRelay = await startRelay("setup", proxy.port);
|
|
588
|
+
const url = `http://${hostOf(setupRelay.address)}:${proxy.port}`;
|
|
589
|
+
proxyEnv = [
|
|
590
|
+
...["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"].map((v) => `${v}=${url}`),
|
|
591
|
+
"NO_PROXY=localhost,127.0.0.1",
|
|
592
|
+
"no_proxy=localhost,127.0.0.1",
|
|
593
|
+
];
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Each relay's and probe's host number on the sandbox's /24: its own, below
|
|
599
|
+
* Docker's dynamic range (.128 and up), and never given to another.
|
|
600
|
+
*/
|
|
601
|
+
const FIXED_HOSTS = Object.freeze({
|
|
602
|
+
"relay-setup": 10,
|
|
603
|
+
"probe-setup": 11,
|
|
604
|
+
"probe-cut": 12,
|
|
605
|
+
"relay-model": 13,
|
|
606
|
+
"probe-model": 14,
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
/** How many subnets a run tries for the sandbox's network. */
|
|
610
|
+
const SUBNET_TRIES = 5;
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The /24 prefixes a run tries for the sandbox's network, in order, chosen
|
|
614
|
+
* from its id inside 198.18.0.0/15: a range set aside for testing, which
|
|
615
|
+
* Docker's default pools never hand out and few LANs or VPNs use.
|
|
616
|
+
* @param {string} id the run's hex id
|
|
617
|
+
*/
|
|
618
|
+
export function subnetCandidates(id) {
|
|
619
|
+
const start = Number.parseInt(id.slice(0, 4), 16) % 512;
|
|
620
|
+
return Array.from({ length: SUBNET_TRIES }, (_, i) => {
|
|
621
|
+
const k = (start + i * 131) % 512;
|
|
622
|
+
return `198.${18 + (k >> 8)}.${k & 255}`;
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** Room for a tar's headers, padding and end blocks around one file. */
|
|
627
|
+
const TAR_OVERHEAD = 64 * 1024;
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* The script that checks the report at `path` in the sandbox and writes it
|
|
631
|
+
* as a tar of that one file: exit 3 when there is none, 4 when it is not a
|
|
632
|
+
* regular file, 5 when it is larger than `maxBytes`.
|
|
633
|
+
* @param {string} path an absolute path the launcher names, with no quote
|
|
634
|
+
* @param {number} maxBytes
|
|
635
|
+
*/
|
|
636
|
+
export function copyOutScript(path, maxBytes) {
|
|
637
|
+
const dir = path.slice(0, path.lastIndexOf("/")) || "/";
|
|
638
|
+
const base = path.slice(path.lastIndexOf("/") + 1);
|
|
639
|
+
return [
|
|
640
|
+
`p=${path};`,
|
|
641
|
+
`if [ ! -e "$p" ] && [ ! -L "$p" ]; then echo "no file at $p" >&2; exit 3; fi;`,
|
|
642
|
+
`{ [ -f "$p" ] && [ ! -L "$p" ]; } || exit 4;`,
|
|
643
|
+
`[ "$(wc -c < "$p")" -le ${maxBytes} ] || exit 5;`,
|
|
644
|
+
`exec tar -c -f - -C ${dir} ${base}`,
|
|
645
|
+
].join(" ");
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* The first IPv4 gateway in `docker network inspect --format {{json
|
|
650
|
+
* .IPAM.Config}}`, or null.
|
|
651
|
+
* @param {string} text
|
|
652
|
+
*/
|
|
653
|
+
export function ipv4GatewayOf(text) {
|
|
654
|
+
let configs;
|
|
655
|
+
try {
|
|
656
|
+
configs = JSON.parse(text.trim());
|
|
657
|
+
} catch {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
if (!Array.isArray(configs)) return null;
|
|
661
|
+
for (const c of configs) {
|
|
662
|
+
const g = typeof c?.Gateway === "string" ? c.Gateway : "";
|
|
663
|
+
if (isIPv4(g)) return g;
|
|
664
|
+
}
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Why `docker network inspect --format {{json .}}` does not show an
|
|
670
|
+
* internal network in the isolated gateway mode with no gateway address,
|
|
671
|
+
* or null when it does.
|
|
672
|
+
* @param {string} text
|
|
673
|
+
*/
|
|
674
|
+
export function isolationFailure(text) {
|
|
675
|
+
let net;
|
|
676
|
+
try {
|
|
677
|
+
net = JSON.parse(text.trim());
|
|
678
|
+
} catch {
|
|
679
|
+
return "output that is not JSON";
|
|
680
|
+
}
|
|
681
|
+
if (Array.isArray(net) && net.length === 1) net = net[0];
|
|
682
|
+
if (!net || typeof net !== "object") return "no network";
|
|
683
|
+
if (net.Internal !== true) return "it is not internal";
|
|
684
|
+
const options = net.Options && typeof net.Options === "object" ? net.Options : {};
|
|
685
|
+
for (const o of ISOLATED_OPTIONS) {
|
|
686
|
+
const [key] = o.split("=");
|
|
687
|
+
const value = typeof options[key] === "string" ? options[key] : "";
|
|
688
|
+
if (value !== "isolated") return `${key} as ${JSON.stringify(value)}, not isolated`;
|
|
689
|
+
}
|
|
690
|
+
const configs = Array.isArray(net.IPAM?.Config) ? net.IPAM.Config : [];
|
|
691
|
+
const gateway = configs
|
|
692
|
+
.map((c) => (typeof c?.Gateway === "string" ? c.Gateway : ""))
|
|
693
|
+
.find(Boolean);
|
|
694
|
+
if (gateway) return `the gateway address ${gateway}`;
|
|
695
|
+
return null;
|
|
696
|
+
}
|