feat(server): add one-shot sensitive artifacts and retention (#4)

This commit is contained in:
Florian Schmidt
2026-11-16 00:19:26 +01:00
parent 1dbea28b43
commit 7ba8aabb37
7 changed files with 453 additions and 94 deletions
+2 -64
View File
@@ -4,7 +4,7 @@ import os from "node:os";
import path from "node:path";
import { generateKeyPairSync, randomUUID } from "node:crypto";
import { spawnSync } from "node:child_process";
import { Server, utils, type Connection } from "ssh2";
import { createSshFixture } from "./ssh-fixture";
import type { HostInfo, Notebook, Step, RenderedStep, StartRunRequest, ServerEvent } from "@shellbook/core";
import { parseNotebook, resolveParams, renderStep } from "@shellbook/core";
import { buildApp } from "../src/app";
@@ -39,69 +39,7 @@ function step(): Step {
}
function rendered(): RenderedStep { return { stepId: "test", kind: "run", host: "test", local: false, script: "echo ok\n", artifacts: [], uploads: [], missing: [], invalid: {} }; }
function request(): StartRunRequest { return { notebookPath: "nested/test.md", stepId: "test", params: {}, runtime: {}, artifactSources: {} }; }
async function sshFixture() {
const config = await fixture();
const key = generateKeyPairSync("rsa", { modulusLength: 2048 }).privateKey.export({ type: "pkcs1", format: "pem" }).toString();
const parsed = utils.parseKey(key);
if (parsed instanceof Error) throw parsed;
const fp = fingerprint(parsed.getPublicSSH());
const clients = new Set<Connection>();
const received: { command: string; input: string }[] = [];
const files = new Map<string, Buffer>([["/tmp/shellbook-test/out.txt", Buffer.from("artifact bytes")]]);
const server = new Server({ hostKeys: [key] }, client => {
clients.add(client);
client.on("error", () => {});
client.on("close", () => clients.delete(client));
client.on("authentication", ctx => ctx.accept());
client.on("ready", () => client.on("session", accept => {
const session = accept();
session.on("exec", (acceptExec, _reject, info) => {
const stream = acceptExec();
const record = { command: info.command, input: "" };
received.push(record);
stream.on("data", (data: Buffer) => { record.input += data.toString(); });
stream.on("end", () => {
if (record.input.includes("HANG")) return;
if (info.command === "mktemp -d") stream.write("/tmp/shellbook-test\n");
else if (info.command.startsWith("rm -rf")) { /* deterministic cleanup */ }
else { stream.write("hello "); stream.stderr.write("warning\n"); stream.write("world\n"); }
stream.exit(record.input.includes("FAIL") ? 7 : 0);
stream.end();
});
});
session.on("sftp", acceptSftp => {
const session = acceptSftp();
const { OK, EOF, NO_SUCH_FILE } = utils.sftp.STATUS_CODE;
const attrs = (name: string) => ({ mode: 0o100600, uid: 0, gid: 0, size: files.get(name)?.length ?? 0, atime: 0, mtime: 0 });
session.on("REALPATH", (id, name) => session.name(id, [{ filename: name, longname: name, attrs: attrs(name) }]));
session.on("STAT", (id, name) => files.has(name) ? session.attrs(id, attrs(name)) : session.status(id, NO_SUCH_FILE));
session.on("OPEN", (id, name, flags) => {
if (flags & 8) files.set(name, Buffer.alloc(0));
if (!files.has(name)) return session.status(id, NO_SUCH_FILE);
session.handle(id, Buffer.from(name));
});
session.on("FSTAT", (id, handle) => session.attrs(id, attrs(handle.toString())));
session.on("READ", (id, handle, offset, length) => {
const data = files.get(handle.toString())!;
if (offset >= data.length) session.status(id, EOF);
else session.data(id, data.subarray(offset, offset + length));
});
session.on("WRITE", (id, handle, offset, data) => {
const name = handle.toString(); const old = files.get(name)!;
const next = Buffer.alloc(Math.max(old.length, offset + data.length)); old.copy(next); data.copy(next, offset); files.set(name, next);
session.status(id, OK);
});
session.on("CLOSE", id => session.status(id, OK));
});
}));
});
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
cleanup.push(async () => { for (const client of clients) client.end(); await new Promise<void>(resolve => server.close(() => resolve())); });
const address = server.address();
if (typeof address === "string" || !address) throw new Error("Missing test address");
const host: HostInfo = { name: "test", address: "127.0.0.1", port: address.port, user: "admin", tags: [] };
return { config, host, fp, received, files };
}
async function sshFixture() { return createSshFixture(await fixture(), cleanup); }
describe("workspace", () => {
it("reads nested notebooks, lists them, hashes versions, rejects stale concurrent saves", async () => {