Harden controller node deploy wizard

This commit is contained in:
mmckeen
2026-04-28 15:29:18 +00:00
parent 401b646561
commit 38b651d460
9 changed files with 712 additions and 179 deletions
+166
View File
@@ -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 })
}
}
+2 -160
View File
@@ -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 })