From 38b651d4600ab6c9620bd3bacef7840cc0cb09aa Mon Sep 17 00:00:00 2001 From: mmckeen Date: Tue, 28 Apr 2026 15:29:18 +0000 Subject: [PATCH] Harden controller node deploy wizard --- README.md | 9 +- app/api/controller-node/deploy/route.ts | 166 +++++++++++++++ app/api/controller-node/plan/route.ts | 162 +------------- app/components/WizardPanel.tsx | 147 ++++++++++++- app/lib/controllerNodePlan.ts | 268 ++++++++++++++++++++++++ next.config.mjs | 6 +- package-lock.json | 101 ++++++++- package.json | 2 + tests/controller-node-wizard-check.mjs | 30 ++- 9 files changed, 712 insertions(+), 179 deletions(-) create mode 100644 app/api/controller-node/deploy/route.ts create mode 100644 app/lib/controllerNodePlan.ts diff --git a/README.md b/README.md index 73c6315..8f13cf1 100644 --- a/README.md +++ b/README.md @@ -197,13 +197,12 @@ Those upgrade paths are protected by the same auth cookie as the HTTP routes. The Wizards page includes **Spawn a Controller Node** for preparing a small OpenShell Control install on another VPS. This is intended for topologies where the browser-facing dashboard and the OpenShell gateway/sandbox hosts are not the same machine. -The wizard generates: +The wizard supports two deployment paths: -- an SSH bootstrap command for the remote VPS; -- a controller `.env` block with `OPENSHELL_GATEWAY`, `OPENCLAW_DASHBOARD_URL`, terminal sidecar settings, and `OPENCLAW_INSTANCE_REGISTRY_JSON`; -- readiness checks for OpenShell CLI reachability, OpenClaw dashboard access, and port exposure. +- **Manual Deploy** generates an SSH/bootstrap script, controller `.env` block, parent-controller URL, node shared secret, OpenShell/OpenClaw routing settings, and readiness checks. +- **Autodeploy** connects to the remote host over SSH using a one-time password supplied in the browser, optionally runs the bootstrap with sudo, installs a systemd service when available, and returns bounded stdout/stderr plus the observed host-key fingerprint. -This first step does not run arbitrary SSH from the web UI. It gives the operator a repeatable launch kit so the remote controller can be installed over SSH, WireGuard, Tailscale, or a private management network. After the controller is running, use its local OpenShell CLI context to manage the sandboxes reachable from that VPS. +Autodeploy does not store the SSH password or write it into generated scripts. For host-key safety, provide an expected SHA256 host-key fingerprint or explicitly select trust-on-first-deploy on a trusted management network. After the controller is running, use its local OpenShell CLI context to manage the sandboxes reachable from that VPS. ## Inference Endpoints diff --git a/app/api/controller-node/deploy/route.ts b/app/api/controller-node/deploy/route.ts new file mode 100644 index 0000000..8ed0939 --- /dev/null +++ b/app/api/controller-node/deploy/route.ts @@ -0,0 +1,166 @@ +import { NextResponse } from "next/server" +import { Client } from "ssh2" +import { buildControllerNodePlan, type ControllerPlanRequest } from "@/app/lib/controllerNodePlan" + +export const runtime = "nodejs" + +type DeployRequest = ControllerPlanRequest & { + remoteHost?: unknown + remotePort?: unknown + remoteUser?: unknown + remotePassword?: unknown + allowSudo?: unknown + acceptUnknownHostKey?: unknown + expectedHostKeySha256?: unknown +} + +const SAFE_REMOTE_USER = /^[a-z_][a-z0-9_-]{0,31}$/i +const MAX_OUTPUT_BYTES = 160_000 + +function text(value: unknown, fallback = "") { + return typeof value === "string" ? value.trim() : fallback +} + +function port(value: unknown) { + const parsed = Number.parseInt(String(value ?? "22"), 10) + return Number.isFinite(parsed) && parsed > 0 && parsed <= 65535 ? parsed : 22 +} + +function appendBounded(current: string, chunk: Buffer | string) { + const next = current + String(chunk) + return next.length > MAX_OUTPUT_BYTES ? next.slice(next.length - MAX_OUTPUT_BYTES) : next +} + +function normalizeFingerprint(value: string) { + return value.trim().replace(/^SHA256:/i, "") +} + +function runRemoteDeploy(params: { + host: string + port: number + username: string + password: string + script: string + allowSudo: boolean + acceptUnknownHostKey: boolean + expectedHostKeySha256: string +}) { + return new Promise<{ stdout: string; stderr: string; hostKeySha256: string | null }>((resolve, reject) => { + const conn = new Client() + let stdout = "" + let stderr = "" + let hostKeySha256: string | null = null + let settled = false + + const finish = (error?: Error) => { + if (settled) return + settled = true + conn.end() + if (error) reject(error) + else resolve({ stdout: stdout.trim(), stderr: stderr.trim(), hostKeySha256 }) + } + + const timer = setTimeout(() => { + finish(new Error("Remote deploy timed out after 10 minutes")) + }, 10 * 60 * 1000) + + conn + .on("ready", () => { + const command = params.allowSudo ? "sudo -S -p '' bash -s" : "bash -s" + conn.exec(command, { pty: params.allowSudo }, (error, stream) => { + if (error) { + clearTimeout(timer) + finish(error) + return + } + + stream + .on("close", (code: number | null, signal: string | null) => { + clearTimeout(timer) + if (code === 0) { + finish() + } else { + finish(new Error(`Remote deploy failed with exit code ${code ?? "unknown"}${signal ? ` signal ${signal}` : ""}`)) + } + }) + .on("data", (chunk: Buffer) => { + stdout = appendBounded(stdout, chunk) + }) + + stream.stderr.on("data", (chunk: Buffer) => { + stderr = appendBounded(stderr, chunk) + }) + + if (params.allowSudo) stream.write(`${params.password}\n`) + stream.end(`${params.script}\n`) + }) + }) + .on("error", (error) => { + clearTimeout(timer) + finish(error) + }) + .connect({ + host: params.host, + port: params.port, + username: params.username, + password: params.password, + readyTimeout: 30000, + hostHash: "sha256", + hostVerifier: (hashedKey: string) => { + hostKeySha256 = hashedKey + const expected = normalizeFingerprint(params.expectedHostKeySha256) + if (expected) return hashedKey === expected + return params.acceptUnknownHostKey + }, + }) + }) +} + +export async function POST(request: Request) { + try { + const body = (await request.json().catch(() => ({}))) as DeployRequest + const remoteHost = text(body.remoteHost, text(body.controllerHost)) + const remoteUser = text(body.remoteUser) + const remotePassword = typeof body.remotePassword === "string" ? body.remotePassword : "" + const expectedHostKeySha256 = text(body.expectedHostKeySha256) + const acceptUnknownHostKey = Boolean(body.acceptUnknownHostKey) + const allowSudo = Boolean(body.allowSudo) + + if (!remoteHost) throw new Error("remoteHost is required") + if (!remoteUser || !SAFE_REMOTE_USER.test(remoteUser)) throw new Error("remoteUser must be a valid Linux username") + if (!remotePassword) throw new Error("remotePassword is required for autodeploy") + if (!expectedHostKeySha256 && !acceptUnknownHostKey) { + throw new Error("Provide expectedHostKeySha256 or explicitly allow trust-on-first-deploy.") + } + + const plan = buildControllerNodePlan({ + ...body, + controllerHost: text(body.controllerHost, remoteHost), + }) + const deployed = await runRemoteDeploy({ + host: remoteHost, + port: port(body.remotePort), + username: remoteUser, + password: remotePassword, + script: plan.commands.localBootstrap, + allowSudo, + acceptUnknownHostKey, + expectedHostKeySha256, + }) + + return NextResponse.json({ + ok: true, + ...plan, + controller: plan.controller, + hostKeySha256: deployed.hostKeySha256, + stdout: deployed.stdout, + stderr: deployed.stderr, + note: allowSudo + ? "Autodeploy completed using sudo. The controller service was installed when systemd was available." + : "Autodeploy completed without sudo. Start the controller manually if systemd service installation was skipped.", + }) + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to autodeploy controller node" + return NextResponse.json({ ok: false, error: message }, { status: 400 }) + } +} diff --git a/app/api/controller-node/plan/route.ts b/app/api/controller-node/plan/route.ts index c899957..c44ef6e 100644 --- a/app/api/controller-node/plan/route.ts +++ b/app/api/controller-node/plan/route.ts @@ -1,168 +1,10 @@ import { NextResponse } from "next/server" - -type ControllerPlanRequest = { - controllerName?: unknown - controllerHost?: unknown - sshTarget?: unknown - installDir?: unknown - repoUrl?: unknown - dashboardPort?: unknown - terminalPort?: unknown - openClawDashboardUrl?: unknown - openshellGateway?: unknown - exposePublicly?: unknown -} - -const DEFAULT_REPO_URL = process.env.OPENSHELL_CONTROL_REPO_URL || "https://github.com/NVIDIA/nemoclaw-dashboard.git" -const SAFE_NAME = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/ -const SAFE_SSH_TARGET = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+(?::[0-9]{1,5})?$/ - -function text(value: unknown, fallback = "") { - return typeof value === "string" ? value.trim() : fallback -} - -function numberValue(value: unknown, fallback: number) { - const parsed = typeof value === "number" ? value : Number.parseInt(String(value ?? ""), 10) - return Number.isFinite(parsed) && parsed > 0 && parsed <= 65535 ? parsed : fallback -} - -function shellSingleQuote(value: string) { - return `'${value.replaceAll("'", "'\\''")}'` -} - -function validateUrl(value: string, label: string) { - try { - const parsed = new URL(value) - if (!["http:", "https:"].includes(parsed.protocol)) throw new Error() - return parsed.toString() - } catch { - throw new Error(`${label} must be an http(s) URL`) - } -} - -function validateInstallDir(value: string) { - if (!value || value.includes("\n") || value.includes("\0") || value.startsWith("-") || value.includes("..")) { - throw new Error("installDir must be a safe absolute path or home-relative directory") - } - if (!value.startsWith("/") && !/^[a-zA-Z0-9._/-]+$/.test(value)) { - throw new Error("installDir can only contain letters, numbers, dots, underscores, hyphens, and slashes") - } - return value.replace(/\/+$/, "") || "openshell-control" -} - -function buildEnvBlock(params: { - controllerName: string - controllerHost: string - dashboardPort: number - terminalPort: number - openClawDashboardUrl: string - openshellGateway: string - exposePublicly: boolean -}) { - const dashboardOrigin = `http://${params.controllerHost}:${params.dashboardPort}` - const registry = [ - { - id: params.controllerName, - label: `${params.controllerName} controller`, - dashboardUrl: params.openClawDashboardUrl, - controlUiOrigin: dashboardOrigin, - terminalServerUrl: `http://127.0.0.1:${params.terminalPort}`, - loopbackOnly: !params.exposePublicly, - default: true, - }, - ] - - return [ - `NEXT_PUBLIC_DASHBOARD_PORT=${params.dashboardPort}`, - "NEXT_PUBLIC_API_BASE=/api", - "NEXT_PUBLIC_ENABLE_SANDBOX_OPERATIONS=true", - `OPENSHELL_GATEWAY=${params.openshellGateway}`, - "OPEN_SHELL_CONTAINER=openshell-cluster-nemoclaw", - "TERMINAL_SERVER_AUTOSTART=true", - `TERMINAL_SERVER_PORT=${params.terminalPort}`, - `OPENCLAW_DASHBOARD_URL=${params.openClawDashboardUrl}`, - `OPENCLAW_INSTANCE_REGISTRY_JSON=${JSON.stringify(registry)}`, - ].join("\n") -} +import { buildControllerNodePlan, type ControllerPlanRequest } from "@/app/lib/controllerNodePlan" export async function POST(request: Request) { try { const body = (await request.json().catch(() => ({}))) as ControllerPlanRequest - const controllerName = text(body.controllerName, "remote-controller").toLowerCase() - if (!SAFE_NAME.test(controllerName) || controllerName.length > 63) { - throw new Error("controllerName must be lowercase letters, numbers, and internal hyphens") - } - - const controllerHost = text(body.controllerHost) - if (!controllerHost) throw new Error("controllerHost is required") - - const sshTarget = text(body.sshTarget) - if (sshTarget && !SAFE_SSH_TARGET.test(sshTarget)) { - throw new Error("sshTarget must look like user@host or user@host:port") - } - - const installDir = validateInstallDir(text(body.installDir, "openshell-control")) - const repoUrl = validateUrl(text(body.repoUrl, DEFAULT_REPO_URL), "repoUrl") - const dashboardPort = numberValue(body.dashboardPort, 3000) - const terminalPort = numberValue(body.terminalPort, 3011) - const openClawDashboardUrl = validateUrl(text(body.openClawDashboardUrl, "http://127.0.0.1:18789/"), "openClawDashboardUrl") - const openshellGateway = text(body.openshellGateway, "nemoclaw") - const exposePublicly = Boolean(body.exposePublicly) - const envBlock = buildEnvBlock({ - controllerName, - controllerHost, - dashboardPort, - terminalPort, - openClawDashboardUrl, - openshellGateway, - exposePublicly, - }) - - const remoteBootstrap = [ - "set -euo pipefail", - `INSTALL_DIR=${shellSingleQuote(installDir)}`, - `REPO_URL=${shellSingleQuote(repoUrl)}`, - 'if ! command -v git >/dev/null 2>&1; then echo "git is required" >&2; exit 1; fi', - 'if ! command -v node >/dev/null 2>&1; then echo "Node.js 20+ is required" >&2; exit 1; fi', - 'if [ -d "$INSTALL_DIR/.git" ]; then git -C "$INSTALL_DIR" pull --ff-only; else mkdir -p "$(dirname "$INSTALL_DIR")"; git clone --depth=1 "$REPO_URL" "$INSTALL_DIR"; fi', - 'cd "$INSTALL_DIR"', - "./install.sh --no-build --no-audit", - "cat > .env.controller-node <<'ENV'", - envBlock, - "ENV", - "cat .env.controller-node >> .env.local", - "npm run build", - `echo "Controller node ready on http://${controllerHost}:${dashboardPort}"`, - ].join("\n") - - const sshCommand = sshTarget - ? `ssh ${sshTarget.includes(":") ? `-p ${sshTarget.split(":").pop()} ${sshTarget.split(":")[0]}` : sshTarget} 'bash -s' <<'REMOTE_CONTROLLER'\n${remoteBootstrap}\nREMOTE_CONTROLLER` - : remoteBootstrap - - return NextResponse.json({ - ok: true, - controller: { - name: controllerName, - url: `http://${controllerHost}:${dashboardPort}`, - host: controllerHost, - dashboardPort, - terminalPort, - installDir, - }, - env: envBlock, - commands: { - ssh: sshCommand, - localBootstrap: remoteBootstrap, - start: `cd ${shellSingleQuote(installDir)} && npm run start`, - terminal: `cd ${shellSingleQuote(installDir)} && npm run terminal-server`, - }, - checks: [ - "OpenShell CLI is installed on the controller node and can reach the gateway.", - `The controller can reach ${openClawDashboardUrl}.`, - `Port ${dashboardPort} is reachable from your browser or through an SSH tunnel.`, - `Port ${terminalPort} stays private unless you intentionally expose terminal transport.`, - ], - }) + return NextResponse.json({ ok: true, ...buildControllerNodePlan(body) }) } catch (error) { const message = error instanceof Error ? error.message : "Failed to build controller node plan" return NextResponse.json({ ok: false, error: message }, { status: 400 }) diff --git a/app/components/WizardPanel.tsx b/app/components/WizardPanel.tsx index 2fcb9ae..7a5de8f 100644 --- a/app/components/WizardPanel.tsx +++ b/app/components/WizardPanel.tsx @@ -11,6 +11,7 @@ type BlueprintOption = { } type WizardStep = "source" | "target" | "options" | "review" | "run" +type ControllerDeployMode = "manual" | "auto" type ControllerPlan = { controller: { @@ -20,6 +21,7 @@ type ControllerPlan = { dashboardPort: number terminalPort: number installDir: string + parentControllerUrl: string } env: string commands: { @@ -73,16 +75,26 @@ export default function WizardPanel({ const [controllerName, setControllerName] = useState("remote-controller") const [controllerHost, setControllerHost] = useState("") const [sshTarget, setSshTarget] = useState("") - const [installDir, setInstallDir] = useState("openshell-control") - const [repoUrl, setRepoUrl] = useState("https://github.com/NVIDIA/nemoclaw-dashboard.git") + const [installDir, setInstallDir] = useState("/opt/openshell-control") + const [repoUrl, setRepoUrl] = useState("https://github.com/mmckeen-nv/openshell_controller.git") const [dashboardPort, setDashboardPort] = useState("3000") const [terminalPort, setTerminalPort] = useState("3011") const [openClawDashboardUrl, setOpenClawDashboardUrl] = useState("http://127.0.0.1:18789/") const [openshellGateway, setOpenshellGateway] = useState("nemoclaw") const [exposePublicly, setExposePublicly] = useState(false) + const [parentControllerUrl, setParentControllerUrl] = useState("http://localhost:3000") + const [controllerDeployMode, setControllerDeployMode] = useState("manual") + const [remotePort, setRemotePort] = useState("22") + const [remoteUser, setRemoteUser] = useState("") + const [remotePassword, setRemotePassword] = useState("") + const [allowSudo, setAllowSudo] = useState(false) + const [acceptUnknownHostKey, setAcceptUnknownHostKey] = useState(false) + const [expectedHostKeySha256, setExpectedHostKeySha256] = useState("") const [controllerPlan, setControllerPlan] = useState(null) const [controllerMessage, setControllerMessage] = useState("") const [controllerPlanning, setControllerPlanning] = useState(false) + const [controllerDeploying, setControllerDeploying] = useState(false) + const [controllerDeployLog, setControllerDeployLog] = useState("") const sourceSandbox = useMemo( () => sandboxes.find((sandbox) => sandbox.id === sourceSandboxId) || null, @@ -100,6 +112,11 @@ export default function WizardPanel({ .catch(() => {}) }, []) + useEffect(() => { + if (typeof window === "undefined") return + setParentControllerUrl(window.location.origin) + }, []) + useEffect(() => { if (!sourceSandboxId && sandboxes[0]) { setSourceSandboxId(sandboxes[0].id) @@ -216,6 +233,7 @@ export default function WizardPanel({ openClawDashboardUrl, openshellGateway, exposePublicly, + parentControllerUrl, }), }) const data = await response.json() @@ -229,6 +247,49 @@ export default function WizardPanel({ } } + async function autodeployControllerNode() { + try { + setControllerDeploying(true) + setControllerMessage("") + setControllerDeployLog("") + + const response = await fetch("/api/controller-node/deploy", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + controllerName, + controllerHost, + sshTarget, + installDir, + repoUrl, + dashboardPort, + terminalPort, + openClawDashboardUrl, + openshellGateway, + exposePublicly, + parentControllerUrl, + remoteHost: controllerHost, + remotePort, + remoteUser, + remotePassword, + allowSudo, + acceptUnknownHostKey, + expectedHostKeySha256, + }), + }) + const data = await response.json() + if (!response.ok) throw new Error(data.error || "Autodeploy failed") + setControllerPlan((current) => current ?? data) + setControllerDeployLog([data.note, data.hostKeySha256 ? `Host key SHA256: ${data.hostKeySha256}` : "", data.stdout, data.stderr].filter(Boolean).join("\n\n")) + setRemotePassword("") + setControllerMessage(`Autodeploy complete for ${data.controller?.url || controllerHost}.`) + } catch (error) { + setControllerMessage(error instanceof Error ? error.message : "Autodeploy failed") + } finally { + setControllerDeploying(false) + } + } + async function copyText(value: string, label: string) { try { await navigator.clipboard.writeText(value) @@ -256,13 +317,29 @@ export default function WizardPanel({

Prepare a remote VPS to run OpenShell Control near another OpenShell gateway or sandbox host.

- remote control plane + {controllerDeployMode === "auto" ? "autodeploy" : "manual deploy"}
+
+ {(["manual", "auto"] as ControllerDeployMode[]).map((mode) => ( + + ))} +
+
+ {controllerDeployMode === "auto" && ( +
+
+

Autodeploy SSH

+

Password is used once for this SSH session and is not written into the deploy script or saved config.

+
+
+ + + +
+ +
+ + +
+
+ )} + {controllerDeployMode === "auto" && ( + + )} {controllerMessage &&
{controllerMessage}
} + {controllerDeployLog && ( +
{controllerDeployLog}
+ )}
@@ -321,6 +452,7 @@ export default function WizardPanel({

Controller VPS: {controllerHost || "not set"}

Sandbox host/gateway: {openshellGateway || "nemoclaw"}

OpenClaw upstream: {openClawDashboardUrl}

+

Parent controller: {parentControllerUrl}

{controllerPlan ? ( @@ -332,6 +464,13 @@ export default function WizardPanel({
{controllerPlan.commands.ssh}
+
+
+

Bootstrap Script

+ +
+
{controllerPlan.commands.localBootstrap}
+

Controller Env

diff --git a/app/lib/controllerNodePlan.ts b/app/lib/controllerNodePlan.ts new file mode 100644 index 0000000..9c0e071 --- /dev/null +++ b/app/lib/controllerNodePlan.ts @@ -0,0 +1,268 @@ +import { randomBytes } from "node:crypto" + +export type ControllerPlanRequest = { + controllerName?: unknown + controllerHost?: unknown + sshTarget?: unknown + installDir?: unknown + repoUrl?: unknown + dashboardPort?: unknown + terminalPort?: unknown + openclawUrl?: unknown + openClawDashboardUrl?: unknown + openshellGateway?: unknown + parentControllerUrl?: unknown + existingToken?: unknown +} + +export type ControllerNodePlan = { + controller: { + name: string + host: string + url: string + sshTarget: string + installDir: string + repoUrl: string + parentControllerUrl: string + dashboardPort: number + terminalPort: number + } + env: string + serviceUnit: string + commands: { + ssh: string + localBootstrap: string + start: string + terminal: string + } + checks: string[] +} + +const DEFAULT_REPO_URL = process.env.OPENSHELL_CONTROL_REPO_URL || "https://github.com/mmckeen-nv/openshell_controller.git" +const SAFE_NAME = /^[a-z0-9][a-z0-9-]{1,62}$/i +const SAFE_HOST = /^[a-z0-9.-]+$/i +const SAFE_SSH_TARGET = /^([a-z0-9._-]+@)?[a-z0-9.-]+(:[0-9]{1,5})?$/i + +function text(value: unknown, fallback = "") { + return typeof value === "string" ? value.trim() : fallback +} + +function numberValue(value: unknown, fallback: number) { + const parsed = Number.parseInt(String(value ?? ""), 10) + return Number.isFinite(parsed) && parsed > 0 && parsed <= 65535 ? parsed : fallback +} + +export function shellSingleQuote(value: string) { + return `'${value.replace(/'/g, "'\\''")}'` +} + +function sshBootstrapInvocation(target: string) { + const match = target.match(/^((?:[a-z0-9._-]+@)?[a-z0-9.-]+)(?::([0-9]{1,5}))?$/i) + if (!match) throw new Error("sshTarget must look like user@host or user@host:port") + const [, host, targetPort] = match + return targetPort ? `ssh -p ${targetPort} ${shellSingleQuote(host)}` : `ssh ${shellSingleQuote(host)}` +} + +function validateUrl(value: string, label: string) { + if (/^git@[a-z0-9.-]+:[a-z0-9._/-]+\.git$/i.test(value)) return value + if (/^ssh:\/\/[^\s]+$/i.test(value)) return value + try { + const parsed = new URL(value) + if (parsed.protocol === "https:" || parsed.protocol === "http:") return value + } catch { + // handled below + } + throw new Error(`${label} must be an http(s), ssh, or git SSH URL`) +} + +function validateHttpUrl(value: string, label: string) { + try { + const parsed = new URL(value) + if (parsed.protocol === "https:" || parsed.protocol === "http:") return value.replace(/\/$/, "") + } catch { + // handled below + } + throw new Error(`${label} must be an http(s) URL`) +} + +function validateInstallDir(value: string) { + if (!value || value.includes("\0") || value.includes("\n")) throw new Error("installDir is invalid") + if (!value.startsWith("/")) throw new Error("installDir must be an absolute path") + return value.replace(/\/$/, "") +} + +function controllerToken(value: unknown) { + const existing = text(value) + if (existing) { + if (!/^[a-zA-Z0-9._~:-]{20,256}$/.test(existing)) throw new Error("existingToken contains unsupported characters") + return existing + } + return randomBytes(32).toString("base64url") +} + +function buildEnvBlock(input: { + controllerName: string + parentControllerUrl: string + token: string + dashboardPort: number + terminalPort: number + openclawUrl: string + openshellGateway: string +}) { + const registry = JSON.stringify({ + nodes: [ + { + id: input.controllerName, + name: input.controllerName, + url: `http://127.0.0.1:${input.dashboardPort}`, + role: "controller-node", + }, + ], + }) + + return [ + `CONTROLLER_NODE_ID=${input.controllerName}`, + `CONTROLLER_NODE_PARENT_URL=${input.parentControllerUrl}`, + `CONTROLLER_NODE_SHARED_SECRET=${input.token}`, + `NEXT_PUBLIC_DASHBOARD_PORT=${input.dashboardPort}`, + "NEXT_PUBLIC_API_BASE=", + "NEXT_PUBLIC_ENABLE_SANDBOX_OPERATIONS=true", + `OPENSHELL_GATEWAY=${input.openshellGateway}`, + "OPEN_SHELL_CONTAINER=openshell-control", + "TERMINAL_SERVER_AUTOSTART=true", + `TERMINAL_SERVER_PORT=${input.terminalPort}`, + `OPENCLAW_DASHBOARD_URL=${input.openclawUrl}`, + `OPENCLAW_INSTANCE_REGISTRY_JSON=${registry}`, + ].join("\n") +} + +function serviceUnit(installDir: string) { + return `[Unit] +Description=OpenShell Controller Node +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +WorkingDirectory=${installDir} +EnvironmentFile=${installDir}/.env.local +ExecStart=/usr/bin/npm run start +Restart=always +RestartSec=5 +User=root + +[Install] +WantedBy=multi-user.target +` +} + +export function buildControllerNodePlan(input: ControllerPlanRequest = {}): ControllerNodePlan { + const controllerName = text(input.controllerName, "remote-controller-01").toLowerCase() + if (!SAFE_NAME.test(controllerName)) throw new Error("controllerName must be 2-63 letters, numbers, or hyphens") + + const host = text(input.controllerHost) + if (!host || !SAFE_HOST.test(host)) throw new Error("controllerHost must be a hostname or IP address") + + const installDir = validateInstallDir(text(input.installDir, "/opt/openshell-control")) + const repoUrl = validateUrl(text(input.repoUrl, DEFAULT_REPO_URL), "repoUrl") + const dashboardPort = numberValue(input.dashboardPort, 3000) + const terminalPort = numberValue(input.terminalPort, 3011) + const openclawUrl = validateHttpUrl(text(input.openclawUrl, text(input.openClawDashboardUrl, "http://localhost:3002")), "openclawUrl") + const openshellGateway = text(input.openshellGateway, "local") + if (!/^[a-z0-9._:-]+$/i.test(openshellGateway)) throw new Error("openshellGateway contains unsupported characters") + const parentControllerUrl = validateHttpUrl(text(input.parentControllerUrl, "http://localhost:3000"), "parentControllerUrl") + const token = controllerToken(input.existingToken) + const sshTarget = text(input.sshTarget, host) + if (!SAFE_SSH_TARGET.test(sshTarget)) throw new Error("sshTarget must look like user@host or user@host:port") + const envBlock = buildEnvBlock({ + controllerName, + parentControllerUrl, + token, + dashboardPort, + terminalPort, + openclawUrl, + openshellGateway, + }) + const unit = serviceUnit(installDir) + const remoteBootstrap = `#!/usr/bin/env bash +set -euo pipefail + +INSTALL_DIR=${shellSingleQuote(installDir)} +REPO_URL=${shellSingleQuote(repoUrl)} +DASHBOARD_PORT=${dashboardPort} +TERMINAL_PORT=${terminalPort} + +for binary in git node npm; do + if ! command -v "$binary" >/dev/null 2>&1; then + echo "Missing required binary: $binary" >&2 + echo "Install Node.js 20+, npm, and git before running this bootstrap." >&2 + exit 1 + fi +done + +mkdir -p "$(dirname "$INSTALL_DIR")" +if [ -d "$INSTALL_DIR/.git" ]; then + git -C "$INSTALL_DIR" fetch --depth=1 origin + git -C "$INSTALL_DIR" pull --ff-only +else + git clone --depth=1 "$REPO_URL" "$INSTALL_DIR" +fi + +cd "$INSTALL_DIR" +./install.sh --no-build --no-audit + +cat > .env.controller-node <<'ENV_CONTROLLER_NODE' +${envBlock} +ENV_CONTROLLER_NODE + +touch .env.local +TMP_ENV="$(mktemp)" +grep -Ev '^(CONTROLLER_NODE_ID|CONTROLLER_NODE_PARENT_URL|CONTROLLER_NODE_SHARED_SECRET|NEXT_PUBLIC_DASHBOARD_PORT|NEXT_PUBLIC_API_BASE|NEXT_PUBLIC_ENABLE_SANDBOX_OPERATIONS|OPENSHELL_GATEWAY|OPEN_SHELL_CONTAINER|TERMINAL_SERVER_AUTOSTART|TERMINAL_SERVER_PORT|OPENCLAW_DASHBOARD_URL|OPENCLAW_INSTANCE_REGISTRY_JSON)=' .env.local > "$TMP_ENV" || true +cat "$TMP_ENV" .env.controller-node > .env.local +rm -f "$TMP_ENV" +chmod 600 .env.local .env.controller-node + +npm run build + +if command -v systemctl >/dev/null 2>&1 && [ -w /etc/systemd/system ]; then + cat > /etc/systemd/system/openshell-controller-node.service <<'SYSTEMD' +${unit} +SYSTEMD + systemctl daemon-reload + systemctl enable --now openshell-controller-node.service + systemctl restart openshell-controller-node.service +else + echo "Systemd service was not installed. Start manually with: npm run start -- --hostname 0.0.0.0 --port $DASHBOARD_PORT" +fi + +echo "Controller node ready at http://${host}:$DASHBOARD_PORT" +echo "OpenShell CLI is installed in $INSTALL_DIR and can manage sandboxes reachable from this VPS." +` + + return { + controller: { + name: controllerName, + host, + url: `http://${host}:${dashboardPort}`, + sshTarget, + installDir, + repoUrl, + parentControllerUrl, + dashboardPort, + terminalPort, + }, + env: envBlock, + serviceUnit: unit, + commands: { + ssh: `${sshBootstrapInvocation(sshTarget)} 'bash -s' <<'REMOTE_CONTROLLER'\n${remoteBootstrap}\nREMOTE_CONTROLLER`, + localBootstrap: remoteBootstrap, + start: `cd ${shellSingleQuote(installDir)} && npm run start -- --hostname 0.0.0.0 --port ${dashboardPort}`, + terminal: `TERMINAL_SERVER_PORT=${terminalPort} npm run terminal-server`, + }, + checks: [ + `curl -fsS http://${host}:${dashboardPort}/api/controller-node/health`, + `curl -fsS ${openclawUrl}/api/instances`, + `ssh ${host} 'cd ${shellSingleQuote(installDir)} && npm run status'`, + ], + } +} diff --git a/next.config.mjs b/next.config.mjs index fb438d1..a176c57 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,4 +1,6 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {} +const nextConfig = { + serverExternalPackages: ["ssh2"], +} -export default nextConfig \ No newline at end of file +export default nextConfig diff --git a/package-lock.json b/package-lock.json index 61b85ef..889a6e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "react": "^18.3.1", "react-dom": "^18.3.1", "react-use": "^17.5.0", + "ssh2": "^1.17.0", "ws": "^8.20.0", "xterm": "^5.3.0", "xterm-addon-fit": "^0.8.0", @@ -25,6 +26,7 @@ "@types/node": "^20.16.13", "@types/react": "^18.3.11", "@types/react-dom": "^18.3.1", + "@types/ssh2": "^1.15.5", "autoprefixer": "^10.4.20", "eslint": "^8.57.1", "eslint-config-next": "^15.5.15", @@ -1040,6 +1042,33 @@ "@types/react": "^18.0.0" } }, + "node_modules/@types/ssh2": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/@types/ssh2/-/ssh2-1.15.5.tgz", + "integrity": "sha512-N1ASjp/nXH3ovBHddRJpli4ozpk6UdDYIX4RJWFa9L1YKnzdhTlVmiGHm4DZnj/jLbqZpes4aeR30EFGQtvhQQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "^18.11.18" + } + }, + "node_modules/@types/ssh2/node_modules/@types/node": { + "version": "18.19.130", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.130.tgz", + "integrity": "sha512-GRaXQx6jGfL8sKfaIDD6OupbIHBr9jv7Jnaml9tB7l4v068PAOXqfcujMMo5PhbIs6ggR1XODELqahT2R8v0fg==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/@types/ssh2/node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true, + "license": "MIT" + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "8.58.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.58.0.tgz", @@ -1711,6 +1740,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/asn1": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", + "integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==", + "license": "MIT", + "dependencies": { + "safer-buffer": "~2.1.0" + } + }, "node_modules/ast-types-flow": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.8.tgz", @@ -1838,6 +1876,15 @@ "node": ">=6.0.0" } }, + "node_modules/bcrypt-pbkdf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", + "integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==", + "license": "BSD-3-Clause", + "dependencies": { + "tweetnacl": "^0.14.3" + } + }, "node_modules/binary-extensions": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", @@ -1933,6 +1980,15 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/buildcheck": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.7.tgz", + "integrity": "sha512-lHblz4ahamxpTmnsk+MNTRWsjYKv965MwOrSJyeD588rR3Jcu7swE+0wN5F+PbL5cjgu/9ObkhfzEPuofEMwLA==", + "optional": true, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -2206,6 +2262,20 @@ "url": "https://opencollective.com/express" } }, + "node_modules/cpu-features": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.10.tgz", + "integrity": "sha512-9IkYqtX3YHPCzoVg1Py+o9057a3i0fp7S530UWokCSaFVTc7CwXPRiOjRjBQQ18ZCNafx78YfnG+HALxtVmOGA==", + "hasInstallScript": true, + "optional": true, + "dependencies": { + "buildcheck": "~0.0.6", + "nan": "^2.19.0" + }, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -4707,6 +4777,13 @@ "thenify-all": "^1.0.0" } }, + "node_modules/nan": { + "version": "2.26.2", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.26.2.tgz", + "integrity": "sha512-0tTvBTYkt3tdGw22nrAy50x7gpbGCCFH3AFcyS5WiUu7Eu4vWlri1woE6qHBSfy11vksDqkiwjOnlR7WV8G1Hw==", + "license": "MIT", + "optional": true + }, "node_modules/nano-css": { "version": "5.6.2", "resolved": "https://registry.npmjs.org/nano-css/-/nano-css-5.6.2.tgz", @@ -5241,7 +5318,6 @@ "version": "8.5.10", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.10.tgz", "integrity": "sha512-pMMHxBOZKFU6HgAZ4eyGnwXF/EvPGGqUr0MnZ5+99485wwW41kW91A4LOGxSHhgugZmSChL5AlElNdwlNgcnLQ==", - "dev": true, "funding": [ { "type": "opencollective", @@ -6212,6 +6288,23 @@ "node": ">=0.10.0" } }, + "node_modules/ssh2": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.17.0.tgz", + "integrity": "sha512-wPldCk3asibAjQ/kziWQQt1Wh3PgDFpC0XpwclzKcdT1vql6KeYxf5LIt4nlFkUeR8WuphYMKqUA56X4rjbfgQ==", + "hasInstallScript": true, + "dependencies": { + "asn1": "^0.2.6", + "bcrypt-pbkdf": "^1.0.2" + }, + "engines": { + "node": ">=10.16.0" + }, + "optionalDependencies": { + "cpu-features": "~0.0.10", + "nan": "^2.23.0" + } + }, "node_modules/stable-hash": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stable-hash/-/stable-hash-0.0.5.tgz", @@ -6733,6 +6826,12 @@ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "license": "0BSD" }, + "node_modules/tweetnacl": { + "version": "0.14.5", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", + "integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==", + "license": "Unlicense" + }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", diff --git a/package.json b/package.json index a0d0374..37ea0c2 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "react": "^18.3.1", "react-dom": "^18.3.1", "react-use": "^17.5.0", + "ssh2": "^1.17.0", "ws": "^8.20.0", "xterm": "^5.3.0", "xterm-addon-fit": "^0.8.0", @@ -28,6 +29,7 @@ "@types/node": "^20.16.13", "@types/react": "^18.3.11", "@types/react-dom": "^18.3.1", + "@types/ssh2": "^1.15.5", "autoprefixer": "^10.4.20", "eslint": "^8.57.1", "eslint-config-next": "^15.5.15", diff --git a/tests/controller-node-wizard-check.mjs b/tests/controller-node-wizard-check.mjs index 4aaa758..9e4adce 100644 --- a/tests/controller-node-wizard-check.mjs +++ b/tests/controller-node-wizard-check.mjs @@ -4,24 +4,40 @@ import path from 'node:path' const root = process.cwd() -const [wizardSource, routeSource] = await Promise.all([ +const [wizardSource, planRouteSource, deployRouteSource, planLibSource] = await Promise.all([ readFile(path.join(root, 'app/components/WizardPanel.tsx'), 'utf8'), readFile(path.join(root, 'app/api/controller-node/plan/route.ts'), 'utf8'), + readFile(path.join(root, 'app/api/controller-node/deploy/route.ts'), 'utf8'), + readFile(path.join(root, 'app/lib/controllerNodePlan.ts'), 'utf8'), ]) assert.match(wizardSource, /Spawn a Controller Node/, 'wizard must expose a controller-node workflow') assert.match(wizardSource, /Generate Launch Kit/, 'controller workflow must generate a launch kit') +assert.match(wizardSource, /Manual Deploy/, 'wizard must expose manual deploy mode') +assert.match(wizardSource, /Autodeploy/, 'wizard must expose autodeploy mode') assert.match(wizardSource, /\/api\/controller-node\/plan/, 'wizard must call the controller-node plan API') +assert.match(wizardSource, /\/api\/controller-node\/deploy/, 'wizard must call the controller-node deploy API') +assert.match(wizardSource, /SSH Password/, 'autodeploy must collect a password for one-time SSH use') +assert.match(wizardSource, /Allow sudo/, 'autodeploy must require explicit sudo permission') +assert.match(wizardSource, /Trust first host key/, 'autodeploy must expose explicit host-key trust') assert.match(wizardSource, /OPENCLAW_DASHBOARD_URL|OpenClaw Dashboard URL/, 'wizard must collect the remote OpenClaw upstream') assert.match(wizardSource, /OPENSHELL_GATEWAY|OpenShell Gateway/, 'wizard must collect the remote OpenShell gateway context') assert.match(wizardSource, /Controller Env/, 'wizard must expose the generated controller environment') assert.match(wizardSource, /Readiness Checks/, 'wizard must show remote readiness checks') -assert.match(routeSource, /OPENCLAW_INSTANCE_REGISTRY_JSON/, 'plan API must configure the OpenClaw instance registry') -assert.match(routeSource, /TERMINAL_SERVER_AUTOSTART=true/, 'plan API must enable the terminal sidecar for controller nodes') -assert.match(routeSource, /sshTarget/, 'plan API must support SSH bootstrap output') -assert.match(routeSource, /npm run build/, 'plan API must produce a complete install/build command') -assert.match(routeSource, /OpenShell CLI is installed/, 'plan API must return operational readiness checks') -assert.doesNotMatch(routeSource, /\/home\/nvidia|\/Users\/markmckeen/, 'plan API must not bake in developer machine paths') +assert.match(planRouteSource, /buildControllerNodePlan/, 'plan API must use shared controller plan builder') +assert.match(planLibSource, /OPENCLAW_INSTANCE_REGISTRY_JSON/, 'plan builder must configure the OpenClaw instance registry') +assert.match(planLibSource, /CONTROLLER_NODE_PARENT_URL/, 'plan builder must configure parent controller connectivity') +assert.match(planLibSource, /CONTROLLER_NODE_SHARED_SECRET/, 'plan builder must generate a node shared secret') +assert.match(planLibSource, /TERMINAL_SERVER_AUTOSTART=true/, 'plan builder must enable the terminal sidecar for controller nodes') +assert.match(planLibSource, /openshell-controller-node\.service/, 'plan builder must include a production service unit when systemd is available') +assert.match(planLibSource, /npm run build/, 'plan builder must produce a complete install/build command') +assert.match(planLibSource, /OpenShell CLI is installed/, 'plan builder must return operational readiness checks') +assert.match(deployRouteSource, /ssh2/, 'deploy API must use server-side SSH for autodeploy') +assert.match(deployRouteSource, /remotePassword/, 'deploy API must accept one-time remote password input') +assert.match(deployRouteSource, /sudo -S/, 'deploy API must support explicit sudo elevation') +assert.match(deployRouteSource, /expectedHostKeySha256/, 'deploy API must support host-key fingerprint verification') +assert.match(deployRouteSource, /acceptUnknownHostKey/, 'deploy API must require explicit trust-on-first-deploy') +assert.doesNotMatch(`${planRouteSource}\n${deployRouteSource}\n${planLibSource}`, /\/home\/nvidia|\/Users\/markmckeen/, 'controller node code must not bake in developer machine paths') console.log('controller-node-wizard-check: PASS controller node wizard assertions')