fix: use OpenShell sandbox exec for MCP sync
This commit is contained in:
@@ -1,14 +1,10 @@
|
|||||||
import { spawn } from "node:child_process"
|
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 OPENSHELL_CONTROL_MCP_SERVER_NAME = "openshell-control"
|
||||||
export const OPENCLAW_CONFIG_PATH = "/sandbox/.openclaw/openclaw.json"
|
export const OPENCLAW_CONFIG_PATH = "/sandbox/.openclaw/openclaw.json"
|
||||||
const OPENCLAW_CONFIG_HASH_PATH = "/sandbox/.openclaw/.config-hash"
|
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<string, unknown> {
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value)
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value)
|
||||||
}
|
}
|
||||||
@@ -17,25 +13,34 @@ function normalizeBrokerBaseUrl(value: string) {
|
|||||||
return value.replace(/\/+$/, "")
|
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) => {
|
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], {
|
const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], {
|
||||||
env: { ...process.env, PATH: HOST_PATH },
|
env: hostCommandEnv({
|
||||||
|
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||||
|
}),
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
})
|
})
|
||||||
let stdout = ""
|
let stdout = ""
|
||||||
let stderr = ""
|
let stderr = ""
|
||||||
|
const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs)
|
||||||
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
||||||
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
||||||
child.on("error", reject)
|
child.on("error", (error) => {
|
||||||
child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }))
|
clearTimeout(timer)
|
||||||
|
reject(error)
|
||||||
|
})
|
||||||
|
child.on("close", (code) => {
|
||||||
|
clearTimeout(timer)
|
||||||
|
resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })
|
||||||
|
})
|
||||||
if (input) child.stdin.end(input)
|
if (input) child.stdin.end(input)
|
||||||
else child.stdin.end()
|
else child.stdin.end()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readOpenClawConfig(sandboxName: string) {
|
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")
|
if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config")
|
||||||
const parsed = JSON.parse(result.stdout)
|
const parsed = JSON.parse(result.stdout)
|
||||||
if (!isRecord(parsed)) throw new Error("OpenClaw config must be a JSON object")
|
if (!isRecord(parsed)) throw new Error("OpenClaw config must be a JSON object")
|
||||||
@@ -46,22 +51,19 @@ async function writeOpenClawConfig(sandboxName: string, config: Record<string, u
|
|||||||
const payload = `${JSON.stringify(config, null, 2)}\n`
|
const payload = `${JSON.stringify(config, null, 2)}\n`
|
||||||
const script = [
|
const script = [
|
||||||
`cat > ${OPENCLAW_CONFIG_PATH}`,
|
`cat > ${OPENCLAW_CONFIG_PATH}`,
|
||||||
`chmod 444 ${OPENCLAW_CONFIG_PATH}`,
|
`chmod 0660 ${OPENCLAW_CONFIG_PATH}`,
|
||||||
`chown root:root ${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}`,
|
`sha256sum ${OPENCLAW_CONFIG_PATH} > ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||||
`chmod 444 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
`chmod 0660 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||||
`chown root:root ${OPENCLAW_CONFIG_HASH_PATH}`,
|
`chown sandbox:sandbox ${OPENCLAW_CONFIG_HASH_PATH} 2>/dev/null || chown 998:998 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||||
].join(" && ")
|
].join(" && ")
|
||||||
const result = await runDockerKubectl(
|
const result = await runSandboxShell(sandboxName, script, payload)
|
||||||
["exec", "-i", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
|
||||||
payload,
|
|
||||||
)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw MCP config")
|
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw MCP config")
|
||||||
}
|
}
|
||||||
|
|
||||||
async function restartOpenClawGatewayIfRunning(sandboxName: string) {
|
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"
|
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) {
|
export function buildOpenClawMcpServerConfig(brokerBaseUrl: string, token: string) {
|
||||||
|
|||||||
@@ -1,26 +1,31 @@
|
|||||||
import { spawn } from "node:child_process"
|
import { spawn } from "node:child_process"
|
||||||
import { HOST_PATH } from "./hostCommands"
|
import { OPENSHELL_BIN, hostCommandEnv } 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"
|
|
||||||
|
|
||||||
function shellQuote(value: string) {
|
function shellQuote(value: string) {
|
||||||
return `'${value.replace(/'/g, `'\\''`)}'`
|
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) => {
|
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], {
|
const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], {
|
||||||
env: { ...process.env, PATH: HOST_PATH },
|
env: hostCommandEnv({
|
||||||
|
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||||
|
}),
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
})
|
})
|
||||||
let stdout = ""
|
let stdout = ""
|
||||||
let stderr = ""
|
let stderr = ""
|
||||||
|
const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs)
|
||||||
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
||||||
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
||||||
child.on("error", reject)
|
child.on("error", (error) => {
|
||||||
child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }))
|
clearTimeout(timer)
|
||||||
|
reject(error)
|
||||||
|
})
|
||||||
|
child.on("close", (code) => {
|
||||||
|
clearTimeout(timer)
|
||||||
|
resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })
|
||||||
|
})
|
||||||
if (input) child.stdin.end(input)
|
if (input) child.stdin.end(input)
|
||||||
else child.stdin.end()
|
else child.stdin.end()
|
||||||
})
|
})
|
||||||
@@ -38,10 +43,7 @@ export async function writeSandboxFilePrivileged(
|
|||||||
`chmod ${shellQuote(mode)} ${shellQuote(targetPath)}`,
|
`chmod ${shellQuote(mode)} ${shellQuote(targetPath)}`,
|
||||||
`chown root:root ${shellQuote(targetPath)} 2>/dev/null || true`,
|
`chown root:root ${shellQuote(targetPath)} 2>/dev/null || true`,
|
||||||
].join(" && ")
|
].join(" && ")
|
||||||
const result = await runDockerKubectl(
|
const result = await runSandboxShell(sandboxName, script, payload)
|
||||||
["exec", "-i", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
|
||||||
payload,
|
|
||||||
)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || `failed to write ${targetPath}`)
|
if (result.code !== 0) throw new Error(result.stderr || `failed to write ${targetPath}`)
|
||||||
return {
|
return {
|
||||||
sandboxName,
|
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`,
|
`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`,
|
`chmod 0600 /sandbox/.openclaw/exec-approvals.json`,
|
||||||
].join(" && ")
|
].join(" && ")
|
||||||
const result = await runDockerKubectl(
|
const result = await runSandboxShell(sandboxName, script)
|
||||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
|
||||||
)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw exec approvals file")
|
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw exec approvals file")
|
||||||
return {
|
return {
|
||||||
sandboxName,
|
sandboxName,
|
||||||
@@ -104,9 +104,7 @@ export async function stabilizeOpenClawGatewayConfig(sandboxName: string) {
|
|||||||
`openclaw gateway stop >/dev/null 2>&1 || true`,
|
`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`,
|
`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(" && ")
|
].join(" && ")
|
||||||
const result = await runDockerKubectl(
|
const result = await runSandboxShell(sandboxName, script)
|
||||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
|
||||||
)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "failed to stabilize OpenClaw gateway config")
|
if (result.code !== 0) throw new Error(result.stderr || "failed to stabilize OpenClaw gateway config")
|
||||||
return {
|
return {
|
||||||
sandboxName,
|
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`,
|
`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`,
|
`chmod 0755 /sandbox/.openclaw && chmod 0600 /sandbox/.openclaw/exec-approvals.json`,
|
||||||
].join(" && ")
|
].join(" && ")
|
||||||
const result = await runDockerKubectl(
|
const result = await runSandboxShell(sandboxName, script)
|
||||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
|
||||||
)
|
|
||||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths")
|
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths")
|
||||||
return {
|
return {
|
||||||
sandboxName,
|
sandboxName,
|
||||||
|
|||||||
@@ -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, /\/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, /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(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.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(manifestSource, /control plane enforces/, 'MCP sandbox handoff must explain broker-side enforcement')
|
||||||
assert.match(brokerCapabilitiesRouteSource, /listAllowedBrokerServers/, 'MCP broker capabilities must only list allowed servers')
|
assert.match(brokerCapabilitiesRouteSource, /listAllowedBrokerServers/, 'MCP broker capabilities must only list allowed servers')
|
||||||
|
|||||||
Reference in New Issue
Block a user