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:
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user