feat(server): refuse runs only for values the step uses (#16)
Drop the scope-wide required-parameter check in prepareRun; renderStep's missing and invalid lists are the single gate, matching the UI.
This commit is contained in:
@@ -23,12 +23,9 @@ export function prepareRun(notebook: Notebook, req: StartRunRequest, hosts: Host
|
|||||||
const runtime: ParamValues = Object.assign(Object.create(null) as ParamValues,
|
const runtime: ParamValues = Object.assign(Object.create(null) as ParamValues,
|
||||||
Object.fromEntries(Object.entries(req.runtime).filter(([name]) => !isBuiltin(name))), builtins);
|
Object.fromEntries(Object.entries(req.runtime).filter(([name]) => !isBuiltin(name))), builtins);
|
||||||
const resolved = resolveParams(notebook, req.params, names, runtime);
|
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).
|
// A step needs exactly the values it uses (core renderStep, spec §3.6): referenced params,
|
||||||
// Same rule as core's renderStep: required params and secrets of the step's scope gate it;
|
// required secrets its script reads and every set secret of its scope (passed as env).
|
||||||
// other params only matter when the step references them (checked by renderStep below).
|
// Unused required params never block it, so guided notebooks run early steps first.
|
||||||
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");
|
|
||||||
if (!blockActive(notebook, step.attrs.id, resolved.values)) throw httpError(400, "Step is not part of the selected options");
|
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);
|
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");
|
if (rendered.missing.length || Object.keys(rendered.invalid).length) throw httpError(400, "Step parameters are missing or invalid");
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { generateKeyPairSync } from "node:crypto";
|
import { generateKeyPairSync } from "node:crypto";
|
||||||
import type { HostInfo, NotebookResponse, StartRunRequest } from "@shellbook/core";
|
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 { buildApp } from "../src/app";
|
||||||
import { loadConfig, type ServerConfig } from "../src/config";
|
import { loadConfig, type ServerConfig } from "../src/config";
|
||||||
import { prepareRun } from "../src/runs";
|
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.rendered.script).toBe("pct create --hostname web-ct --key 'ssh-ed25519 SERVER' --pw \"$ROOTPW\"\n");
|
||||||
expect(prepared.host?.name).toBe("pve");
|
expect(prepared.host?.name).toBe("pve");
|
||||||
expect(prepared.env).toEqual({ ROOTPW: "root-pw" });
|
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);
|
const parent = prepareRun(notebook, request("install", values, { "ct.host": "web-ct" }), hosts, builtins);
|
||||||
expect(parent.host?.name).toBe("web-ct");
|
expect(parent.host?.name).toBe("web-ct");
|
||||||
expect(parent.env).toEqual({ TOKEN: "token" });
|
expect(parent.env).toEqual({ TOKEN: "token" });
|
||||||
// Container steps are refused when "existing host" is selected; the included form gates its own steps.
|
// 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", { ...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.
|
// 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");
|
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("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("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(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");
|
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");
|
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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -181,8 +181,6 @@ describe("pipeline helpers", () => {
|
|||||||
});
|
});
|
||||||
it("refuses missing or invalid parameters before execution", () => {
|
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: [] };
|
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"] });
|
vi.mocked(renderStep).mockReturnValueOnce({ ...rendered(), missing: ["required"] });
|
||||||
expect(() => prepareRun(notebook, request(), [])).toThrow("Step parameters are missing or invalid");
|
expect(() => prepareRun(notebook, request(), [])).toThrow("Step parameters are missing or invalid");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user