@alineo-labs/sandbox 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +86 -4
- package/dist/index.mjs +115 -32
- package/package.json +4 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BashSession, CheckpointInfo, CommandError, DiagnosticEvent, DiagnosticLog, EnvironmentRecord, EnvironmentRecord as EnvironmentRecord$1, EnvironmentRecord as EnvironmentRecord$2, ExecCodeOptions, ExecConnectionError, ExecHandle, ExecOptions, ExecResult, FileInfo, IStorageAdapter, IStorageAdapter as IStorageAdapter$1, InteractiveExecHandle, LedgerEntry, LedgerEvent, ListSandboxOptions, ListSandboxOptions as ListSandboxOptions$1, Metrics, PendingInteractiveExec, SandboxDetails, SandboxDetails as SandboxDetails$1, SandboxError, SandboxHandle, SandboxHandle as SandboxHandle$1, SandboxHooks, SandboxStatus, StepTimeoutError } from "@alineo-labs/core";
|
|
2
|
-
import { CodeLanguage } from "@alineo-labs/opensandbox";
|
|
1
|
+
import { BashSession, CheckpointInfo, CommandError, CredentialBinding, CredentialBroker, CredentialBroker as CredentialBroker$1, CredentialResolver, DiagnosticEvent, DiagnosticLog, EnvironmentRecord, EnvironmentRecord as EnvironmentRecord$1, EnvironmentRecord as EnvironmentRecord$2, ExecCodeOptions, ExecConnectionError, ExecHandle, ExecOptions, ExecResult, FileInfo, IStorageAdapter, IStorageAdapter as IStorageAdapter$1, InteractiveExecHandle, LedgerEntry, LedgerEvent, ListSandboxOptions, ListSandboxOptions as ListSandboxOptions$1, Metrics, PendingInteractiveExec, SandboxDetails, SandboxDetails as SandboxDetails$1, SandboxError, SandboxHandle, SandboxHandle as SandboxHandle$1, SandboxHooks, SandboxStatus, StepTimeoutError } from "@alineo-labs/core";
|
|
2
|
+
import { CodeLanguage, CredentialProxyConfig, NetworkPolicy, NetworkPolicy as NetworkPolicy$1, NetworkRule } from "@alineo-labs/opensandbox";
|
|
3
|
+
import { OpenSandboxCredentialBroker } from "@alineo-labs/vault";
|
|
3
4
|
|
|
4
5
|
//#region src/types.d.ts
|
|
5
6
|
/**
|
|
@@ -39,11 +40,30 @@ interface SandboxClientOptions {
|
|
|
39
40
|
* in Docker (e.g. started via `alineo init`). Defaults to `false`.
|
|
40
41
|
*/
|
|
41
42
|
useServerProxy?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Broker for `sb.credentials.*` — registers credentials with, and resolves endpoints
|
|
45
|
+
* against, whatever backend actually injects them. Defaults to
|
|
46
|
+
* `OpenSandboxCredentialBroker` (from `@alineo-labs/vault`), wired to this client's own
|
|
47
|
+
* `ControlClient`/`useServerProxy`, if omitted. Only relevant for sandboxes created with
|
|
48
|
+
* `credentialProxy: true`.
|
|
49
|
+
*/
|
|
50
|
+
credentialBroker?: CredentialBroker$1;
|
|
42
51
|
}
|
|
43
52
|
/** Options for `Sandbox.resume()`. */
|
|
44
53
|
interface ResumeOptions {
|
|
45
54
|
/** Resume from the checkpoint with this tag. Defaults to the most recent checkpoint. */
|
|
46
55
|
tag?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Resolves values for credentials bound via `sb.credentials.set()` on the original sandbox
|
|
58
|
+
* whose `CredentialSource` isn't `"env"` (or whose env var has since become unset) — the
|
|
59
|
+
* vault's own state doesn't survive a resume (it's sidecar-runtime-only), so anything
|
|
60
|
+
* previously bound needs a value from somewhere. `"env"`-sourced credentials resolve
|
|
61
|
+
* automatically without this. Only relevant if the original sandbox had any
|
|
62
|
+
* `sb.credentials.set()` calls; a no-op otherwise. `resume()` throws `SandboxError` if a
|
|
63
|
+
* bound credential can't be resolved (no matching env var, and no callback or one that
|
|
64
|
+
* returns `undefined`) rather than silently omitting it.
|
|
65
|
+
*/
|
|
66
|
+
resolveCredential?: CredentialResolver;
|
|
47
67
|
}
|
|
48
68
|
/** Options for `Sandbox.sandbox()`. */
|
|
49
69
|
interface SandboxOptions {
|
|
@@ -82,6 +102,21 @@ interface SandboxOptions {
|
|
|
82
102
|
* same host script for one logical run).
|
|
83
103
|
*/
|
|
84
104
|
runId?: string;
|
|
105
|
+
/**
|
|
106
|
+
* Durable resource identity this session's memory should be scoped by — see
|
|
107
|
+
* `@alineo-labs/memory`'s `ResourceRef.resourceId`. Unset by default (unlike `runId`, which
|
|
108
|
+
* always gets a generated fallback) — most sandboxes aren't tied to a memory resource.
|
|
109
|
+
* Threaded through the ledger's `sandbox_created` payload the same way `runId` is, and
|
|
110
|
+
* inherited by `resume()`/`fork()`/`restoreSnapshot()` the same way `runId` is.
|
|
111
|
+
*/
|
|
112
|
+
resourceId?: string;
|
|
113
|
+
/**
|
|
114
|
+
* Durable team identity this session's memory should be scoped by — see
|
|
115
|
+
* `@alineo-labs/memory`'s `ResourceRef.teamId`. Unset by default, same as `resourceId`.
|
|
116
|
+
* Threaded through the ledger's `sandbox_created` payload the same way `resourceId` is, and
|
|
117
|
+
* inherited by `resume()`/`fork()`/`restoreSnapshot()` the same way.
|
|
118
|
+
*/
|
|
119
|
+
teamId?: string;
|
|
85
120
|
/** SandboxHandle lifetime in seconds. Defaults to the OpenSandbox server default. */
|
|
86
121
|
timeout?: number;
|
|
87
122
|
/** Observability hooks (e.g. `otelHooks(tracer)` from `@alineo-labs/otel`). */
|
|
@@ -101,6 +136,18 @@ interface SandboxOptions {
|
|
|
101
136
|
* to start the Jupyter kernel service that `execCode()` depends on.
|
|
102
137
|
*/
|
|
103
138
|
entrypoint?: string[];
|
|
139
|
+
/**
|
|
140
|
+
* Outbound network policy, enforced by an egress sidecar attached to this sandbox. Requires
|
|
141
|
+
* the OpenSandbox server to have `egress.image` configured (`alineo init` does this by
|
|
142
|
+
* default). Omit for unrestricted egress and no sidecar at all — the default, unchanged
|
|
143
|
+
* behavior.
|
|
144
|
+
*/
|
|
145
|
+
networkPolicy?: NetworkPolicy$1;
|
|
146
|
+
/**
|
|
147
|
+
* Opts this sandbox into transparent credential injection via `sb.credentials.*`. Requires
|
|
148
|
+
* `networkPolicy` to also be set and the server to be running `egress.mode = "dns+nft"`.
|
|
149
|
+
*/
|
|
150
|
+
credentialProxy?: boolean;
|
|
104
151
|
}
|
|
105
152
|
//#endregion
|
|
106
153
|
//#region src/environment.d.ts
|
|
@@ -146,6 +193,15 @@ interface EnvironmentSandboxOptions {
|
|
|
146
193
|
* Falls back to `EnvironmentOptions.shell`, then `"/bin/sh"`.
|
|
147
194
|
*/
|
|
148
195
|
shell?: string;
|
|
196
|
+
/**
|
|
197
|
+
* Resource scope for this spawned sandbox — see `SandboxOptions.resourceId`. Unlike
|
|
198
|
+
* `sandbox()`/`resume()`/`restoreSnapshot()`, a sandbox spawned from an environment has no
|
|
199
|
+
* prior ledger session of its own to inherit from, so this must be passed explicitly here.
|
|
200
|
+
*/
|
|
201
|
+
resourceId?: string;
|
|
202
|
+
/** Team scope for this spawned sandbox — see `SandboxOptions.teamId`. Same rationale as
|
|
203
|
+
* `resourceId` above. */
|
|
204
|
+
teamId?: string;
|
|
149
205
|
}
|
|
150
206
|
/**
|
|
151
207
|
* A named, reusable sandbox environment.
|
|
@@ -225,6 +281,7 @@ declare class Environment {
|
|
|
225
281
|
declare class Sandbox {
|
|
226
282
|
private readonly _control;
|
|
227
283
|
private readonly _adapter;
|
|
284
|
+
private readonly _credentialBroker;
|
|
228
285
|
private readonly _maxConcurrency;
|
|
229
286
|
private readonly _useServerProxy;
|
|
230
287
|
private _activeCount;
|
|
@@ -313,6 +370,13 @@ declare class Sandbox {
|
|
|
313
370
|
gpu?: string;
|
|
314
371
|
};
|
|
315
372
|
runId?: string;
|
|
373
|
+
/** Default resource scope for any later `.fork()` call — see `SandboxOptions.resourceId`.
|
|
374
|
+
* Same rationale as `opts.runId`: `connect()` does no ledger lookup, so it can't
|
|
375
|
+
* discover the sandbox's original `resourceId` on its own. */
|
|
376
|
+
resourceId?: string;
|
|
377
|
+
/** Default team scope for any later `.fork()` call — see `SandboxOptions.teamId`. Same
|
|
378
|
+
* rationale as `opts.resourceId`. */
|
|
379
|
+
teamId?: string;
|
|
316
380
|
}): Promise<SandboxHandle$1>;
|
|
317
381
|
/**
|
|
318
382
|
* Create a fresh sandbox from a snapshot ID without exec replay.
|
|
@@ -342,7 +406,25 @@ declare class Sandbox {
|
|
|
342
406
|
cpu: string;
|
|
343
407
|
memory: string;
|
|
344
408
|
gpu?: string;
|
|
345
|
-
}, runId?: string
|
|
409
|
+
}, runId?: string, opts?: {
|
|
410
|
+
networkPolicy?: NetworkPolicy$1;
|
|
411
|
+
credentialProxy?: boolean;
|
|
412
|
+
/**
|
|
413
|
+
* Environment for the restored sandbox — normally omitted (a snapshot already carries
|
|
414
|
+
* its container env). Use it only for `OPENSANDBOX_EGRESS_*` sidecar vars, which are
|
|
415
|
+
* process-scoped and must be re-supplied on every restore (the sidecar is not
|
|
416
|
+
* snapshotted): the server routes those to the sidecar and drops them from the
|
|
417
|
+
* container.
|
|
418
|
+
*/
|
|
419
|
+
env?: Record<string, string>;
|
|
420
|
+
/** Resource scope for the restored sandbox — see `SandboxOptions.resourceId`. Unlike
|
|
421
|
+
* `resume()`, `restoreSnapshot()` has no prior ledger session of its own to inherit
|
|
422
|
+
* from (the snapshot may have come from anywhere), so this must be passed explicitly. */
|
|
423
|
+
resourceId?: string;
|
|
424
|
+
/** Team scope for the restored sandbox — see `SandboxOptions.teamId`. Same rationale as
|
|
425
|
+
* `resourceId` above. */
|
|
426
|
+
teamId?: string;
|
|
427
|
+
}): Promise<SandboxHandle$1>;
|
|
346
428
|
/**
|
|
347
429
|
* SandboxHandle history management. List, inspect, and delete past sandbox records.
|
|
348
430
|
*
|
|
@@ -415,4 +497,4 @@ declare class Sandbox {
|
|
|
415
497
|
private _releaseSlot;
|
|
416
498
|
}
|
|
417
499
|
//#endregion
|
|
418
|
-
export { BashSession, type CheckpointInfo, CodeLanguage, CommandError, type DiagnosticEvent, type DiagnosticLog, Environment, type EnvironmentOptions, type EnvironmentRecord, type EnvironmentSandboxOptions, type ExecCodeOptions, ExecConnectionError, type ExecHandle, type ExecOptions, type ExecResult, type FileInfo, type IStorageAdapter, type InteractiveExecHandle, type LedgerEntry, LedgerEvent, type ListSandboxOptions, type Metrics, type PendingInteractiveExec, type ResumeOptions, Sandbox, SandboxClientError, type SandboxClientOptions, type SandboxDetails, SandboxError, SandboxHandle, type SandboxOptions, SandboxStatus, StepTimeoutError };
|
|
500
|
+
export { BashSession, type CheckpointInfo, CodeLanguage, CommandError, type CredentialBinding, type CredentialBroker, type CredentialProxyConfig, type DiagnosticEvent, type DiagnosticLog, Environment, type EnvironmentOptions, type EnvironmentRecord, type EnvironmentSandboxOptions, type ExecCodeOptions, ExecConnectionError, type ExecHandle, type ExecOptions, type ExecResult, type FileInfo, type IStorageAdapter, type InteractiveExecHandle, type LedgerEntry, LedgerEvent, type ListSandboxOptions, type Metrics, type NetworkPolicy, type NetworkRule, OpenSandboxCredentialBroker, type PendingInteractiveExec, type ResumeOptions, Sandbox, SandboxClientError, type SandboxClientOptions, type SandboxDetails, SandboxError, SandboxHandle, type SandboxOptions, SandboxStatus, StepTimeoutError };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BashSession, CommandError, ExecConnectionError, LedgerEvent, LedgerEvent as LedgerEvent$1, SandboxError, SandboxHandle, SandboxHandle as SandboxHandle$1, SandboxStatus, StepTimeoutError } from "@alineo-labs/core";
|
|
2
|
-
import { CodeLanguage, ControlClient, SandboxState, SnapshotState } from "@alineo-labs/opensandbox";
|
|
1
|
+
import { BashSession, CommandError, ExecConnectionError, LedgerEvent, LedgerEvent as LedgerEvent$1, SandboxError, SandboxHandle, SandboxHandle as SandboxHandle$1, SandboxStatus, StepTimeoutError, reconstructBoundCredentials, reconstructEgressRules, resolveBoundCredential } from "@alineo-labs/core";
|
|
2
|
+
import { CodeLanguage, ControlClient, SandboxState, SnapshotState, isValidEgressTarget } from "@alineo-labs/opensandbox";
|
|
3
|
+
import { OpenSandboxCredentialBroker, OpenSandboxCredentialBroker as OpenSandboxCredentialBroker$1 } from "@alineo-labs/vault";
|
|
3
4
|
//#region src/types.ts
|
|
4
5
|
/**
|
|
5
6
|
* Client-side SDK error carrying an HTTP-style status code, thrown for
|
|
@@ -82,6 +83,14 @@ var Environment = class {
|
|
|
82
83
|
//#endregion
|
|
83
84
|
//#region src/client.ts
|
|
84
85
|
/**
|
|
86
|
+
* Reject a `networkPolicy` with a malformed `target` before it reaches the server — a fast
|
|
87
|
+
* local error instead of a round-trip. `undefined` policies pass through untouched.
|
|
88
|
+
*/
|
|
89
|
+
function assertValidNetworkPolicy(policy) {
|
|
90
|
+
if (!policy) return;
|
|
91
|
+
for (const rule of policy.egress ?? []) if (!isValidEgressTarget(rule.target)) throw new SandboxClientError(`Invalid networkPolicy egress target ${JSON.stringify(rule.target)} — expected an FQDN, a "*."-prefixed wildcard domain, a bare IP address, or a CIDR block.`, 400);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
85
94
|
* Main entry point for the sandbox client. Manages sandbox lifecycle and session history.
|
|
86
95
|
*
|
|
87
96
|
* @example
|
|
@@ -104,6 +113,7 @@ var Environment = class {
|
|
|
104
113
|
var Sandbox = class {
|
|
105
114
|
_control;
|
|
106
115
|
_adapter;
|
|
116
|
+
_credentialBroker;
|
|
107
117
|
_maxConcurrency;
|
|
108
118
|
_useServerProxy;
|
|
109
119
|
_activeCount = 0;
|
|
@@ -119,6 +129,7 @@ var Sandbox = class {
|
|
|
119
129
|
this._adapter = options.adapter;
|
|
120
130
|
this._maxConcurrency = options.maxConcurrency;
|
|
121
131
|
this._useServerProxy = options.useServerProxy ?? false;
|
|
132
|
+
this._credentialBroker = options.credentialBroker ?? new OpenSandboxCredentialBroker$1(this._control, this._useServerProxy);
|
|
122
133
|
process.setMaxListeners(process.getMaxListeners() + 1);
|
|
123
134
|
process.on("beforeExit", () => {
|
|
124
135
|
if (!this._adapterClosed) {
|
|
@@ -150,10 +161,13 @@ var Sandbox = class {
|
|
|
150
161
|
* ```
|
|
151
162
|
*/
|
|
152
163
|
async sandbox(opts) {
|
|
164
|
+
assertValidNetworkPolicy(opts.networkPolicy);
|
|
153
165
|
await this._ensureConnected();
|
|
154
166
|
await this._acquireSlot();
|
|
155
167
|
const image = typeof opts.image === "string" ? { uri: opts.image } : opts.image;
|
|
156
168
|
const runId = opts.runId ?? crypto.randomUUID();
|
|
169
|
+
const resourceId = opts.resourceId;
|
|
170
|
+
const teamId = opts.teamId;
|
|
157
171
|
let sandboxId;
|
|
158
172
|
try {
|
|
159
173
|
sandboxId = (await this._control.createSandbox({
|
|
@@ -169,7 +183,9 @@ var Sandbox = class {
|
|
|
169
183
|
"/dev/null"
|
|
170
184
|
],
|
|
171
185
|
resourceLimits: opts.resources,
|
|
172
|
-
timeout: opts.timeout
|
|
186
|
+
timeout: opts.timeout,
|
|
187
|
+
networkPolicy: opts.networkPolicy,
|
|
188
|
+
credentialProxy: opts.credentialProxy ? { enabled: true } : void 0
|
|
173
189
|
})).id;
|
|
174
190
|
await this._waitForRunning(sandboxId);
|
|
175
191
|
const name = opts.name ?? `sandbox-${sandboxId.slice(0, 8)}`;
|
|
@@ -182,16 +198,23 @@ var Sandbox = class {
|
|
|
182
198
|
payload: {
|
|
183
199
|
sandboxId,
|
|
184
200
|
resources: opts.resources,
|
|
185
|
-
runId
|
|
201
|
+
runId,
|
|
202
|
+
resourceId,
|
|
203
|
+
teamId,
|
|
204
|
+
networkPolicy: opts.networkPolicy,
|
|
205
|
+
credentialProxy: opts.credentialProxy
|
|
186
206
|
}
|
|
187
207
|
});
|
|
188
208
|
const sb = new SandboxHandle$1(sandboxId, name, {
|
|
189
209
|
control: this._control,
|
|
190
210
|
adapter: this._adapter,
|
|
211
|
+
credentialBroker: this._credentialBroker,
|
|
191
212
|
hooks: opts.hooks,
|
|
192
|
-
onClose: () =>
|
|
213
|
+
onClose: () => {
|
|
214
|
+
this._releaseSlot();
|
|
215
|
+
},
|
|
193
216
|
shell: opts.shell,
|
|
194
|
-
fork: (snapshotId, tag, overrideRunId) => this._forkFromSnapshot(snapshotId, name, opts.resources, opts.shell, overrideRunId ?? runId),
|
|
217
|
+
fork: (snapshotId, tag, overrideRunId, forkOpts) => this._forkFromSnapshot(snapshotId, name, opts.resources, opts.shell, overrideRunId ?? runId, sandboxId, resourceId, teamId, forkOpts),
|
|
195
218
|
useServerProxy: this._useServerProxy
|
|
196
219
|
});
|
|
197
220
|
opts.hooks?.onSandboxCreated?.(sandboxId, name);
|
|
@@ -228,9 +251,9 @@ var Sandbox = class {
|
|
|
228
251
|
await this._ensureConnected();
|
|
229
252
|
const session = (await this._adapter.listAllSandboxDetails()).find((s) => s.sandboxId === sandboxId);
|
|
230
253
|
if (!session) throw new SandboxClientError(`Session ${sandboxId} not found`, 404);
|
|
231
|
-
return this._resumeSession(session.name, sandboxId, opts?.tag);
|
|
254
|
+
return this._resumeSession(session.name, sandboxId, opts?.tag, opts?.resolveCredential);
|
|
232
255
|
}
|
|
233
|
-
async _resumeSession(name, sandboxId, tag) {
|
|
256
|
+
async _resumeSession(name, sandboxId, tag, resolveCredential) {
|
|
234
257
|
const entries = await this._adapter.readAll(name, sandboxId);
|
|
235
258
|
let checkpointIdx;
|
|
236
259
|
if (tag) {
|
|
@@ -241,9 +264,22 @@ var Sandbox = class {
|
|
|
241
264
|
if (checkpointIdx === -1) throw new SandboxClientError(`No checkpoint found for session ${sandboxId}`, 404);
|
|
242
265
|
}
|
|
243
266
|
const { snapshotId } = entries[checkpointIdx].payload;
|
|
244
|
-
const
|
|
245
|
-
const resources =
|
|
246
|
-
const runId =
|
|
267
|
+
const createdPayload = entries.find((e) => e.event === LedgerEvent$1.SandboxCreated)?.payload;
|
|
268
|
+
const resources = createdPayload?.resources;
|
|
269
|
+
const runId = createdPayload?.runId ?? crypto.randomUUID();
|
|
270
|
+
const resourceId = createdPayload?.resourceId;
|
|
271
|
+
const teamId = createdPayload?.teamId;
|
|
272
|
+
const originalNetworkPolicy = createdPayload?.networkPolicy;
|
|
273
|
+
const credentialProxy = createdPayload?.credentialProxy;
|
|
274
|
+
const replayedEgress = originalNetworkPolicy ? reconstructEgressRules(entries) : {
|
|
275
|
+
apply: [],
|
|
276
|
+
remove: []
|
|
277
|
+
};
|
|
278
|
+
const networkPolicy = originalNetworkPolicy ? {
|
|
279
|
+
...originalNetworkPolicy,
|
|
280
|
+
egress: [...replayedEgress.apply, ...(originalNetworkPolicy.egress ?? []).filter((r) => !replayedEgress.apply.some((rr) => rr.target === r.target) && !replayedEgress.remove.includes(r.target))]
|
|
281
|
+
} : void 0;
|
|
282
|
+
const boundCredentials = reconstructBoundCredentials(entries);
|
|
247
283
|
const replayCache = /* @__PURE__ */ new Map();
|
|
248
284
|
const pendingStdout = /* @__PURE__ */ new Map();
|
|
249
285
|
const pendingStderr = /* @__PURE__ */ new Map();
|
|
@@ -290,7 +326,9 @@ var Sandbox = class {
|
|
|
290
326
|
const newSessionId = (await this._control.createSandbox({
|
|
291
327
|
snapshotId,
|
|
292
328
|
resourceLimits: resources,
|
|
293
|
-
metadata: { runId }
|
|
329
|
+
metadata: { runId },
|
|
330
|
+
networkPolicy,
|
|
331
|
+
credentialProxy: credentialProxy ? { enabled: true } : void 0
|
|
294
332
|
})).id;
|
|
295
333
|
await this._waitForRunning(newSessionId);
|
|
296
334
|
await this._adapter.append({
|
|
@@ -303,16 +341,28 @@ var Sandbox = class {
|
|
|
303
341
|
sandboxId: newSessionId,
|
|
304
342
|
resumedFrom: sandboxId,
|
|
305
343
|
snapshotId,
|
|
306
|
-
runId
|
|
344
|
+
runId,
|
|
345
|
+
resourceId,
|
|
346
|
+
teamId,
|
|
347
|
+
networkPolicy,
|
|
348
|
+
credentialProxy
|
|
307
349
|
}
|
|
308
350
|
});
|
|
309
|
-
|
|
351
|
+
const sb = new SandboxHandle$1(newSessionId, name, {
|
|
310
352
|
control: this._control,
|
|
311
353
|
adapter: this._adapter,
|
|
312
|
-
|
|
313
|
-
|
|
354
|
+
credentialBroker: this._credentialBroker,
|
|
355
|
+
onClose: () => {
|
|
356
|
+
this._releaseSlot();
|
|
357
|
+
},
|
|
358
|
+
fork: resources?.cpu && resources.memory ? (snapshotId, tag, overrideRunId, forkOpts) => this._forkFromSnapshot(snapshotId, name, resources, void 0, overrideRunId ?? runId, newSessionId, resourceId, teamId, forkOpts) : void 0,
|
|
314
359
|
useServerProxy: this._useServerProxy
|
|
315
360
|
}, replayCache, pendingInteractive);
|
|
361
|
+
for (const [credName, { binding, source }] of boundCredentials) {
|
|
362
|
+
const value = await resolveBoundCredential(credName, source, resolveCredential, newSessionId);
|
|
363
|
+
await sb.credentials.set(credName, value, binding, source);
|
|
364
|
+
}
|
|
365
|
+
return sb;
|
|
316
366
|
} catch (err) {
|
|
317
367
|
this._releaseSlot();
|
|
318
368
|
throw err;
|
|
@@ -355,8 +405,11 @@ var Sandbox = class {
|
|
|
355
405
|
return new SandboxHandle$1(sandboxId, name, {
|
|
356
406
|
control: this._control,
|
|
357
407
|
adapter: this._adapter,
|
|
358
|
-
|
|
359
|
-
|
|
408
|
+
credentialBroker: this._credentialBroker,
|
|
409
|
+
onClose: () => {
|
|
410
|
+
this._releaseSlot();
|
|
411
|
+
},
|
|
412
|
+
fork: resources ? (snapshotId, tag, overrideRunId, forkOpts) => this._forkFromSnapshot(snapshotId, name, resources, void 0, overrideRunId ?? opts?.runId, sandboxId, opts?.resourceId, opts?.teamId, forkOpts) : void 0,
|
|
360
413
|
useServerProxy: this._useServerProxy
|
|
361
414
|
});
|
|
362
415
|
}
|
|
@@ -384,15 +437,19 @@ var Sandbox = class {
|
|
|
384
437
|
* await sb2.close();
|
|
385
438
|
* ```
|
|
386
439
|
*/
|
|
387
|
-
async restoreSnapshot(snapshotId, name, resources, runId) {
|
|
440
|
+
async restoreSnapshot(snapshotId, name, resources, runId, opts) {
|
|
441
|
+
assertValidNetworkPolicy(opts?.networkPolicy);
|
|
388
442
|
await this._ensureConnected();
|
|
389
443
|
await this._acquireSlot();
|
|
390
444
|
try {
|
|
391
445
|
const finalRunId = runId ?? crypto.randomUUID();
|
|
392
446
|
const newId = (await this._control.createSandbox({
|
|
393
447
|
snapshotId,
|
|
448
|
+
env: opts?.env,
|
|
394
449
|
resourceLimits: resources,
|
|
395
|
-
metadata: { runId: finalRunId }
|
|
450
|
+
metadata: { runId: finalRunId },
|
|
451
|
+
networkPolicy: opts?.networkPolicy,
|
|
452
|
+
credentialProxy: opts?.credentialProxy ? { enabled: true } : void 0
|
|
396
453
|
})).id;
|
|
397
454
|
await this._waitForRunning(newId);
|
|
398
455
|
await this._adapter.append({
|
|
@@ -404,14 +461,21 @@ var Sandbox = class {
|
|
|
404
461
|
payload: {
|
|
405
462
|
sandboxId: newId,
|
|
406
463
|
fromSnapshot: snapshotId,
|
|
407
|
-
runId: finalRunId
|
|
464
|
+
runId: finalRunId,
|
|
465
|
+
resourceId: opts?.resourceId,
|
|
466
|
+
teamId: opts?.teamId,
|
|
467
|
+
networkPolicy: opts?.networkPolicy,
|
|
468
|
+
credentialProxy: opts?.credentialProxy
|
|
408
469
|
}
|
|
409
470
|
});
|
|
410
471
|
return new SandboxHandle$1(newId, name, {
|
|
411
472
|
control: this._control,
|
|
412
473
|
adapter: this._adapter,
|
|
413
|
-
|
|
414
|
-
|
|
474
|
+
credentialBroker: this._credentialBroker,
|
|
475
|
+
onClose: () => {
|
|
476
|
+
this._releaseSlot();
|
|
477
|
+
},
|
|
478
|
+
fork: (snapshotId, tag, overrideRunId, forkOpts) => this._forkFromSnapshot(snapshotId, name, resources, void 0, overrideRunId ?? finalRunId, newId, opts?.resourceId, opts?.teamId, forkOpts),
|
|
415
479
|
useServerProxy: this._useServerProxy
|
|
416
480
|
});
|
|
417
481
|
} catch (err) {
|
|
@@ -563,6 +627,8 @@ var Sandbox = class {
|
|
|
563
627
|
})).id;
|
|
564
628
|
await this._waitForRunning(newId);
|
|
565
629
|
const sessionName = `env-${envName}-${newId.slice(0, 8)}`;
|
|
630
|
+
const resourceId = extra?.resourceId;
|
|
631
|
+
const teamId = extra?.teamId;
|
|
566
632
|
await this._adapter.append({
|
|
567
633
|
ts: Date.now(),
|
|
568
634
|
name: sessionName,
|
|
@@ -573,16 +639,21 @@ var Sandbox = class {
|
|
|
573
639
|
sandboxId: newId,
|
|
574
640
|
fromEnvironment: envName,
|
|
575
641
|
snapshotId,
|
|
576
|
-
runId
|
|
642
|
+
runId,
|
|
643
|
+
resourceId,
|
|
644
|
+
teamId
|
|
577
645
|
}
|
|
578
646
|
});
|
|
579
647
|
const sb = new SandboxHandle$1(newId, sessionName, {
|
|
580
648
|
control: this._control,
|
|
581
649
|
adapter: this._adapter,
|
|
650
|
+
credentialBroker: this._credentialBroker,
|
|
582
651
|
hooks: extra?.hooks,
|
|
583
|
-
onClose: () =>
|
|
652
|
+
onClose: () => {
|
|
653
|
+
this._releaseSlot();
|
|
654
|
+
},
|
|
584
655
|
shell: extra?.shell ?? envShell,
|
|
585
|
-
fork: (snapshotId, tag, overrideRunId) => this._forkFromSnapshot(snapshotId, sessionName, resources, extra?.shell ?? envShell, overrideRunId ?? runId),
|
|
656
|
+
fork: (snapshotId, tag, overrideRunId, forkOpts) => this._forkFromSnapshot(snapshotId, sessionName, resources, extra?.shell ?? envShell, overrideRunId ?? runId, newId, resourceId, teamId, forkOpts),
|
|
586
657
|
useServerProxy: this._useServerProxy
|
|
587
658
|
});
|
|
588
659
|
extra?.hooks?.onSandboxCreated?.(newId, sessionName);
|
|
@@ -592,14 +663,18 @@ var Sandbox = class {
|
|
|
592
663
|
throw err;
|
|
593
664
|
}
|
|
594
665
|
}
|
|
595
|
-
async _forkFromSnapshot(snapshotId, parentName, resources, shell, runId) {
|
|
666
|
+
async _forkFromSnapshot(snapshotId, parentName, resources, shell, runId, parentSandboxId, resourceId, teamId, forkOpts) {
|
|
596
667
|
await this._acquireSlot();
|
|
597
668
|
try {
|
|
598
669
|
const finalRunId = runId ?? crypto.randomUUID();
|
|
670
|
+
const finalResourceId = forkOpts?.resourceId ?? resourceId;
|
|
671
|
+
const finalTeamId = forkOpts?.teamId ?? teamId;
|
|
599
672
|
const newId = (await this._control.createSandbox({
|
|
600
673
|
snapshotId,
|
|
601
674
|
resourceLimits: resources,
|
|
602
|
-
metadata: { runId: finalRunId }
|
|
675
|
+
metadata: { runId: finalRunId },
|
|
676
|
+
networkPolicy: forkOpts?.networkPolicy,
|
|
677
|
+
credentialProxy: forkOpts?.credentialProxy ? { enabled: true } : void 0
|
|
603
678
|
})).id;
|
|
604
679
|
await this._waitForRunning(newId);
|
|
605
680
|
const sessionName = `fork-${parentName}-${newId.slice(0, 8)}`;
|
|
@@ -612,15 +687,23 @@ var Sandbox = class {
|
|
|
612
687
|
payload: {
|
|
613
688
|
sandboxId: newId,
|
|
614
689
|
forkedFrom: snapshotId,
|
|
615
|
-
runId: finalRunId
|
|
690
|
+
runId: finalRunId,
|
|
691
|
+
resourceId: finalResourceId,
|
|
692
|
+
teamId: finalTeamId,
|
|
693
|
+
parentSandboxId,
|
|
694
|
+
networkPolicy: forkOpts?.networkPolicy,
|
|
695
|
+
credentialProxy: forkOpts?.credentialProxy
|
|
616
696
|
}
|
|
617
697
|
});
|
|
618
698
|
return new SandboxHandle$1(newId, sessionName, {
|
|
619
699
|
control: this._control,
|
|
620
700
|
adapter: this._adapter,
|
|
621
|
-
|
|
701
|
+
credentialBroker: this._credentialBroker,
|
|
702
|
+
onClose: () => {
|
|
703
|
+
this._releaseSlot();
|
|
704
|
+
},
|
|
622
705
|
shell,
|
|
623
|
-
fork: (sid, tag, overrideRunId) => this._forkFromSnapshot(sid, sessionName, resources, shell, overrideRunId ?? finalRunId),
|
|
706
|
+
fork: (sid, tag, overrideRunId, nextForkOpts) => this._forkFromSnapshot(sid, sessionName, resources, shell, overrideRunId ?? finalRunId, newId, finalResourceId, finalTeamId, nextForkOpts),
|
|
624
707
|
useServerProxy: this._useServerProxy
|
|
625
708
|
});
|
|
626
709
|
} catch (err) {
|
|
@@ -654,4 +737,4 @@ var Sandbox = class {
|
|
|
654
737
|
}
|
|
655
738
|
};
|
|
656
739
|
//#endregion
|
|
657
|
-
export { BashSession, CodeLanguage, CommandError, Environment, ExecConnectionError, LedgerEvent, Sandbox, SandboxClientError, SandboxError, SandboxHandle, SandboxStatus, StepTimeoutError };
|
|
740
|
+
export { BashSession, CodeLanguage, CommandError, Environment, ExecConnectionError, LedgerEvent, OpenSandboxCredentialBroker, Sandbox, SandboxClientError, SandboxError, SandboxHandle, SandboxStatus, StepTimeoutError };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alineo-labs/sandbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,8 +29,9 @@
|
|
|
29
29
|
"test:watch": "vitest"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@alineo-labs/core": "0.
|
|
33
|
-
"@alineo-labs/opensandbox": "0.
|
|
32
|
+
"@alineo-labs/core": "0.4.0",
|
|
33
|
+
"@alineo-labs/opensandbox": "0.3.0",
|
|
34
|
+
"@alineo-labs/vault": "0.3.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"bun-types": "1.3.14",
|