From 435664ce990b99f01cec423716497a219e138d57 Mon Sep 17 00:00:00 2001 From: mmckeen-nv Date: Wed, 5 Aug 2026 11:27:25 -0600 Subject: [PATCH] fix: use OpenShell sandbox exec for MCP sync --- app/lib/sandboxOpenClawMcpConfig.ts | 42 ++++++++++++++------------ app/lib/sandboxPrivilegedFiles.ts | 42 ++++++++++++-------------- tests/mcp-configuration-page-check.mjs | 3 +- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/app/lib/sandboxOpenClawMcpConfig.ts b/app/lib/sandboxOpenClawMcpConfig.ts index d2afe2e..d82ccba 100644 --- a/app/lib/sandboxOpenClawMcpConfig.ts +++ b/app/lib/sandboxOpenClawMcpConfig.ts @@ -1,14 +1,10 @@ import { spawn } from "node:child_process" -import { HOST_PATH } from "./hostCommands" +import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands" export const OPENSHELL_CONTROL_MCP_SERVER_NAME = "openshell-control" export const OPENCLAW_CONFIG_PATH = "/sandbox/.openclaw/openclaw.json" const OPENCLAW_CONFIG_HASH_PATH = "/sandbox/.openclaw/.config-hash" -const DOCKER_BIN = process.env.DOCKER_BIN || "docker" -const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw" -const OPENSHELL_NAMESPACE = process.env.OPENSHELL_SANDBOX_NAMESPACE || "openshell" - function isRecord(value: unknown): value is Record { return Boolean(value) && typeof value === "object" && !Array.isArray(value) } @@ -17,25 +13,34 @@ function normalizeBrokerBaseUrl(value: string) { return value.replace(/\/+$/, "") } -function runDockerKubectl(args: string[], input?: string) { +function runSandboxShell(sandboxName: string, script: string, input?: string, timeoutMs = 60000) { return new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => { - const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], { - env: { ...process.env, PATH: HOST_PATH }, + const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], { + env: hostCommandEnv({ + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + }), stdio: ["pipe", "pipe", "pipe"], }) let stdout = "" let stderr = "" + const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs) child.stdout.on("data", (chunk) => { stdout += String(chunk) }) child.stderr.on("data", (chunk) => { stderr += String(chunk) }) - child.on("error", reject) - child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })) + child.on("error", (error) => { + clearTimeout(timer) + reject(error) + }) + child.on("close", (code) => { + clearTimeout(timer) + resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }) + }) if (input) child.stdin.end(input) else child.stdin.end() }) } async function readOpenClawConfig(sandboxName: string) { - const result = await runDockerKubectl(["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "cat", OPENCLAW_CONFIG_PATH]) + const result = await runSandboxShell(sandboxName, `cat ${OPENCLAW_CONFIG_PATH}`) if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config") const parsed = JSON.parse(result.stdout) if (!isRecord(parsed)) throw new Error("OpenClaw config must be a JSON object") @@ -46,22 +51,19 @@ async function writeOpenClawConfig(sandboxName: string, config: Record ${OPENCLAW_CONFIG_PATH}`, - `chmod 444 ${OPENCLAW_CONFIG_PATH}`, - `chown root:root ${OPENCLAW_CONFIG_PATH}`, + `chmod 0660 ${OPENCLAW_CONFIG_PATH}`, + `chown sandbox:sandbox ${OPENCLAW_CONFIG_PATH} 2>/dev/null || chown 998:998 ${OPENCLAW_CONFIG_PATH}`, `sha256sum ${OPENCLAW_CONFIG_PATH} > ${OPENCLAW_CONFIG_HASH_PATH}`, - `chmod 444 ${OPENCLAW_CONFIG_HASH_PATH}`, - `chown root:root ${OPENCLAW_CONFIG_HASH_PATH}`, + `chmod 0660 ${OPENCLAW_CONFIG_HASH_PATH}`, + `chown sandbox:sandbox ${OPENCLAW_CONFIG_HASH_PATH} 2>/dev/null || chown 998:998 ${OPENCLAW_CONFIG_HASH_PATH}`, ].join(" && ") - const result = await runDockerKubectl( - ["exec", "-i", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], - payload, - ) + const result = await runSandboxShell(sandboxName, script, payload) if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw MCP config") } async function restartOpenClawGatewayIfRunning(sandboxName: string) { const script = "for p in /proc/[0-9]*; do cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true); case \"$cmd\" in *'openclaw gateway'*) kill \"${p##*/}\" 2>/dev/null || true;; esac; done" - await runDockerKubectl(["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script]) + await runSandboxShell(sandboxName, script) } export function buildOpenClawMcpServerConfig(brokerBaseUrl: string, token: string) { diff --git a/app/lib/sandboxPrivilegedFiles.ts b/app/lib/sandboxPrivilegedFiles.ts index e60b7a2..8ef2664 100644 --- a/app/lib/sandboxPrivilegedFiles.ts +++ b/app/lib/sandboxPrivilegedFiles.ts @@ -1,26 +1,31 @@ import { spawn } from "node:child_process" -import { HOST_PATH } from "./hostCommands" - -const DOCKER_BIN = process.env.DOCKER_BIN || "docker" -const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw" -const OPENSHELL_NAMESPACE = process.env.OPENSHELL_SANDBOX_NAMESPACE || "openshell" +import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands" function shellQuote(value: string) { return `'${value.replace(/'/g, `'\\''`)}'` } -function runDockerKubectl(args: string[], input?: Buffer | string) { +function runSandboxShell(sandboxName: string, script: string, input?: Buffer | string, timeoutMs = 60000) { return new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => { - const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], { - env: { ...process.env, PATH: HOST_PATH }, + const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], { + env: hostCommandEnv({ + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + }), stdio: ["pipe", "pipe", "pipe"], }) let stdout = "" let stderr = "" + const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs) child.stdout.on("data", (chunk) => { stdout += String(chunk) }) child.stderr.on("data", (chunk) => { stderr += String(chunk) }) - child.on("error", reject) - child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })) + child.on("error", (error) => { + clearTimeout(timer) + reject(error) + }) + child.on("close", (code) => { + clearTimeout(timer) + resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }) + }) if (input) child.stdin.end(input) else child.stdin.end() }) @@ -38,10 +43,7 @@ export async function writeSandboxFilePrivileged( `chmod ${shellQuote(mode)} ${shellQuote(targetPath)}`, `chown root:root ${shellQuote(targetPath)} 2>/dev/null || true`, ].join(" && ") - const result = await runDockerKubectl( - ["exec", "-i", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], - payload, - ) + const result = await runSandboxShell(sandboxName, script, payload) if (result.code !== 0) throw new Error(result.stderr || `failed to write ${targetPath}`) return { sandboxName, @@ -71,9 +73,7 @@ export async function repairOpenClawExecApprovalsFile(sandboxName: string) { `chown sandbox:sandbox /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw/exec-approvals.json`, `chmod 0600 /sandbox/.openclaw/exec-approvals.json`, ].join(" && ") - const result = await runDockerKubectl( - ["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], - ) + const result = await runSandboxShell(sandboxName, script) if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw exec approvals file") return { sandboxName, @@ -104,9 +104,7 @@ export async function stabilizeOpenClawGatewayConfig(sandboxName: string) { `openclaw gateway stop >/dev/null 2>&1 || true`, `if command -v gosu >/dev/null 2>&1; then nohup gosu sandbox openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & else nohup openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & fi`, ].join(" && ") - const result = await runDockerKubectl( - ["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], - ) + const result = await runSandboxShell(sandboxName, script) if (result.code !== 0) throw new Error(result.stderr || "failed to stabilize OpenClaw gateway config") return { sandboxName, @@ -146,9 +144,7 @@ export async function repairOpenClawWorkspacePermissions(sandboxName: string) { `chown sandbox:sandbox /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json`, `chmod 0755 /sandbox/.openclaw && chmod 0600 /sandbox/.openclaw/exec-approvals.json`, ].join(" && ") - const result = await runDockerKubectl( - ["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script], - ) + const result = await runSandboxShell(sandboxName, script) if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths") return { sandboxName, diff --git a/tests/mcp-configuration-page-check.mjs b/tests/mcp-configuration-page-check.mjs index b6052fb..5d42086 100644 --- a/tests/mcp-configuration-page-check.mjs +++ b/tests/mcp-configuration-page-check.mjs @@ -157,7 +157,8 @@ assert.match(manifestSource, /syncSandboxOpenClawMcpConfig/, 'MCP sandbox manife assert.match(manifestSource, /\/mcp/, 'MCP sandbox handoff must include the protocol-compatible MCP endpoint') assert.match(manifestSource, /Quick Check/, 'MCP sandbox handoff must include a token-safe local verification snippet') assert.match(manifestSource, /tools\/list/, 'MCP sandbox handoff quick check should verify tool discovery') -assert.match(privilegedFilesSource, /kubectl/, 'privileged sandbox file writes must use the OpenShell cluster control path') +assert.match(privilegedFilesSource, /\["sandbox", "exec", "-n", sandboxName/, 'privileged sandbox file writes must use the OpenShell sandbox exec path') +assert.match(privilegedFilesSource, /hostCommandEnv/, 'privileged sandbox file writes must use the shared OpenShell command environment') assert.doesNotMatch(manifestSource, /allowedServers|renderServer|commandLine/, 'MCP sandbox handoff must not disclose server inventory or launch specs') assert.match(manifestSource, /control plane enforces/, 'MCP sandbox handoff must explain broker-side enforcement') assert.match(brokerCapabilitiesRouteSource, /listAllowedBrokerServers/, 'MCP broker capabilities must only list allowed servers')