diff --git a/apps/server/src/runs/index.ts b/apps/server/src/runs/index.ts index 19894b9..564e1f7 100644 --- a/apps/server/src/runs/index.ts +++ b/apps/server/src/runs/index.ts @@ -23,12 +23,9 @@ export function prepareRun(notebook: Notebook, req: StartRunRequest, hosts: Host const runtime: ParamValues = Object.assign(Object.create(null) as ParamValues, Object.fromEntries(Object.entries(req.runtime).filter(([name]) => !isBuiltin(name))), builtins); const resolved = resolveParams(notebook, req.params, names, runtime); - // Only parameters of the step's own scope gate it (an included notebook's form does not block the parent). - // Same rule as core's renderStep: required params and secrets of the step's scope gate it; - // other params only matter when the step references them (checked by renderStep below). - const scope = step.attrs.scope ?? ""; - const gating = notebook.params.filter(def => (def.scope ?? "") === scope && (def.required || def.type === "secret")); - if (gating.some(def => Object.hasOwn(resolved.errors, def.name))) throw httpError(400, "Parameters are missing or invalid"); + // A step needs exactly the values it uses (core renderStep, spec ยง3.6): referenced params, + // required secrets its script reads and every set secret of its scope (passed as env). + // Unused required params never block it, so guided notebooks run early steps first. if (!blockActive(notebook, step.attrs.id, resolved.values)) throw httpError(400, "Step is not part of the selected options"); const rendered = renderStep(notebook, step, resolved.values, runtime, names); if (rendered.missing.length || Object.keys(rendered.invalid).length) throw httpError(400, "Step parameters are missing or invalid"); diff --git a/apps/server/test/chained.test.ts b/apps/server/test/chained.test.ts index ee447cc..e46c189 100644 --- a/apps/server/test/chained.test.ts +++ b/apps/server/test/chained.test.ts @@ -4,7 +4,7 @@ import os from "node:os"; import path from "node:path"; import { generateKeyPairSync } from "node:crypto"; import type { HostInfo, NotebookResponse, StartRunRequest } from "@shellbook/core"; -import { findStep } from "@shellbook/core"; +import { findStep, parseNotebook } from "@shellbook/core"; import { buildApp } from "../src/app"; import { loadConfig, type ServerConfig } from "../src/config"; import { prepareRun } from "../src/runs"; @@ -100,13 +100,13 @@ describe("chained notebooks on the server", () => { expect(prepared.rendered.script).toBe("pct create --hostname web-ct --key 'ssh-ed25519 SERVER' --pw \"$ROOTPW\"\n"); expect(prepared.host?.name).toBe("pve"); expect(prepared.env).toEqual({ ROOTPW: "root-pw" }); - expect(() => prepareRun(notebook, request("install", values), hosts, builtins)).toThrow("Parameters are missing or invalid"); + expect(() => prepareRun(notebook, request("install", values), hosts, builtins)).toThrow("Step parameters are missing or invalid"); const parent = prepareRun(notebook, request("install", values, { "ct.host": "web-ct" }), hosts, builtins); expect(parent.host?.name).toBe("web-ct"); expect(parent.env).toEqual({ TOKEN: "token" }); // Container steps are refused when "existing host" is selected; the included form gates its own steps. expect(() => prepareRun(notebook, request("ct.create", { ...values, target: "existing" }), hosts, builtins)).toThrow("Step is not part of the selected options"); - expect(() => prepareRun(notebook, request("ct.create", { target: "lxc" }), hosts, builtins)).toThrow("Parameters are missing or invalid"); + expect(() => prepareRun(notebook, request("ct.create", { target: "lxc" }), hosts, builtins)).toThrow("Step parameters are missing or invalid"); // Without a server key the builtin stays missing even if a client sends one. expect(() => prepareRun(notebook, request("ct.create", values, { "shellbook.public_key": "ssh-ed25519 CLIENT" }), hosts, {})).toThrow("Step parameters are missing or invalid"); }); @@ -146,7 +146,7 @@ pct create {{ctid}} expect(prepared.rendered.script).toContain("printf '%s\\n' 'ssh-ed25519 AAAAKEY shellbook' > \"$keys\""); expect(prepared.rendered.script).toContain("pct create 105 local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst \\\n --hostname web \\"); expect(prepared.rendered.script).toContain("--net0 name=eth0,bridge=vmbr0,'ip=10.0.30.160/24,gw=10.0.30.1'"); - expect(() => prepareRun(notebook, request("install", { ...values, target: "existing" }), hosts, {})).toThrow("Parameters are missing or invalid"); + expect(() => prepareRun(notebook, request("install", { ...values, target: "existing" }), hosts, {})).toThrow("Step parameters are missing or invalid"); expect(prepareRun(notebook, request("install", { target: "existing", host: "existing" }), hosts, {}).host?.name).toBe("existing"); }); @@ -157,3 +157,34 @@ pct create {{ctid}} expect(res.json().message).toBe("Step is not part of the selected options"); }); }); + +describe("guided notebooks (#16)", () => { + const guided = parseNotebook(`--- +params: + host: {type: host, required: true} + user: {type: name, required: true} + TOKEN: {type: secret, required: true} +--- +:::group{title="1. Server"} +::param{name=host} +\`\`\`sh {check host="{{host}}" id=first} +true +\`\`\` +::: +\`\`\`sh {run host="{{host}}" id=later} +useradd {{user}} && curl -H "Authorization: $TOKEN" https://example.invalid +\`\`\` +`); + + it("runs an early step before later required values are filled", () => { + expect(guided.diagnostics).toEqual([]); + expect(prepareRun(guided, request("first", { host: "pve" }), hosts).rendered.script).toBe("true\n"); + expect(() => prepareRun(guided, request("later", { host: "pve", TOKEN: "t" }), hosts)).toThrow("Step parameters are missing or invalid"); + expect(() => prepareRun(guided, request("later", { host: "pve", user: "ops" }), hosts)).toThrow("Step parameters are missing or invalid"); + expect(prepareRun(guided, request("later", { host: "pve", user: "ops", TOKEN: "t" }), hosts).env).toEqual({ TOKEN: "t" }); + }); + + it("still refuses an invalid secret even when the step does not read it", () => { + expect(() => prepareRun(guided, request("first", { host: "pve", TOKEN: "a\0b" }), hosts)).toThrow("Step parameters are missing or invalid"); + }); +}); diff --git a/apps/server/test/m2.test.ts b/apps/server/test/m2.test.ts index be29121..7d1d2b8 100644 --- a/apps/server/test/m2.test.ts +++ b/apps/server/test/m2.test.ts @@ -181,8 +181,6 @@ describe("pipeline helpers", () => { }); it("refuses missing or invalid parameters before execution", () => { const notebook: Notebook = { title: "Test", params: [{ name: "required", type: "string", required: true }], blocks: [{ type: "step", step: step() }], diagnostics: [] }; - vi.mocked(resolveParams).mockReturnValueOnce({ values: {}, errors: { required: "missing" } }); - expect(() => prepareRun(notebook, request(), [])).toThrow("Parameters are missing or invalid"); vi.mocked(renderStep).mockReturnValueOnce({ ...rendered(), missing: ["required"] }); expect(() => prepareRun(notebook, request(), [])).toThrow("Step parameters are missing or invalid"); });