Write MCP broker config with privileged sandbox path
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { revokeSandboxMcpBrokerSession, rotateSandboxMcpBrokerSession } from "./mcpBrokerStore"
|
import { revokeSandboxMcpBrokerSession, rotateSandboxMcpBrokerSession } from "./mcpBrokerStore"
|
||||||
import { uploadSandboxFile } from "./sandboxFiles"
|
import { resolveSandboxName } from "./sandboxFiles"
|
||||||
|
import { writeSandboxFilePrivileged } from "./sandboxPrivilegedFiles"
|
||||||
|
|
||||||
export const SANDBOX_MCP_MANIFEST_PATH = "/sandbox/openshell_control_mcp.md"
|
export const SANDBOX_MCP_MANIFEST_PATH = "/sandbox/openshell_control_mcp.md"
|
||||||
|
|
||||||
@@ -66,10 +67,10 @@ export async function syncSandboxMcpManifest(
|
|||||||
options: BrokerHandoffOptions,
|
options: BrokerHandoffOptions,
|
||||||
) {
|
) {
|
||||||
const handoff = await buildSandboxMcpBrokerHandoff(sandbox, options)
|
const handoff = await buildSandboxMcpBrokerHandoff(sandbox, options)
|
||||||
const uploaded = await uploadSandboxFile(
|
const sandboxName = sandbox.name || await resolveSandboxName(sandbox.id)
|
||||||
sandbox.id,
|
const uploaded = await writeSandboxFilePrivileged(
|
||||||
|
sandboxName,
|
||||||
SANDBOX_MCP_MANIFEST_PATH,
|
SANDBOX_MCP_MANIFEST_PATH,
|
||||||
"openshell_control_mcp.md",
|
|
||||||
Buffer.from(handoff.markdown, "utf8"),
|
Buffer.from(handoff.markdown, "utf8"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -103,10 +104,10 @@ export async function revokeSandboxMcpManifest(sandbox: SandboxRef) {
|
|||||||
"The previous broker token has been revoked. Ask the operator to enable an MCP server and issue a new broker config if MCP access is needed.",
|
"The previous broker token has been revoked. Ask the operator to enable an MCP server and issue a new broker config if MCP access is needed.",
|
||||||
"",
|
"",
|
||||||
].join("\n")
|
].join("\n")
|
||||||
const uploaded = await uploadSandboxFile(
|
const sandboxName = sandbox.name || await resolveSandboxName(sandbox.id)
|
||||||
sandbox.id,
|
const uploaded = await writeSandboxFilePrivileged(
|
||||||
|
sandboxName,
|
||||||
SANDBOX_MCP_MANIFEST_PATH,
|
SANDBOX_MCP_MANIFEST_PATH,
|
||||||
"openshell_control_mcp.md",
|
|
||||||
Buffer.from(markdown, "utf8"),
|
Buffer.from(markdown, "utf8"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
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"
|
||||||
|
|
||||||
|
function shellQuote(value: string) {
|
||||||
|
return `'${value.replace(/'/g, `'\\''`)}'`
|
||||||
|
}
|
||||||
|
|
||||||
|
function runDockerKubectl(args: string[], input?: Buffer | string) {
|
||||||
|
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 },
|
||||||
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
})
|
||||||
|
let stdout = ""
|
||||||
|
let stderr = ""
|
||||||
|
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 }))
|
||||||
|
if (input) child.stdin.end(input)
|
||||||
|
else child.stdin.end()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function writeSandboxFilePrivileged(
|
||||||
|
sandboxName: string,
|
||||||
|
targetPath: string,
|
||||||
|
payload: Buffer,
|
||||||
|
mode = "0644",
|
||||||
|
) {
|
||||||
|
const script = [
|
||||||
|
`mkdir -p ${shellQuote(targetPath.split("/").slice(0, -1).join("/") || "/")}`,
|
||||||
|
`cat > ${shellQuote(targetPath)}`,
|
||||||
|
`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,
|
||||||
|
)
|
||||||
|
if (result.code !== 0) throw new Error(result.stderr || `failed to write ${targetPath}`)
|
||||||
|
return {
|
||||||
|
sandboxName,
|
||||||
|
path: targetPath,
|
||||||
|
bytes: payload.byteLength,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ const storePath = path.join(root, 'app/lib/mcpServerStore.ts')
|
|||||||
const brokerStorePath = path.join(root, 'app/lib/mcpBrokerStore.ts')
|
const brokerStorePath = path.join(root, 'app/lib/mcpBrokerStore.ts')
|
||||||
const brokerClientPath = path.join(root, 'app/lib/mcpBrokerClient.ts')
|
const brokerClientPath = path.join(root, 'app/lib/mcpBrokerClient.ts')
|
||||||
const manifestPath = path.join(root, 'app/lib/sandboxMcpManifest.ts')
|
const manifestPath = path.join(root, 'app/lib/sandboxMcpManifest.ts')
|
||||||
|
const privilegedFilesPath = path.join(root, 'app/lib/sandboxPrivilegedFiles.ts')
|
||||||
const routePath = path.join(root, 'app/api/mcp/route.ts')
|
const routePath = path.join(root, 'app/api/mcp/route.ts')
|
||||||
const uploadRoutePath = path.join(root, 'app/api/mcp/upload/route.ts')
|
const uploadRoutePath = path.join(root, 'app/api/mcp/upload/route.ts')
|
||||||
const registryRoutePath = path.join(root, 'app/api/mcp/registry/route.ts')
|
const registryRoutePath = path.join(root, 'app/api/mcp/registry/route.ts')
|
||||||
@@ -21,11 +22,12 @@ const sidebarPath = path.join(root, 'app/components/Sidebar.tsx')
|
|||||||
const sandboxListPath = path.join(root, 'app/components/SandboxList.tsx')
|
const sandboxListPath = path.join(root, 'app/components/SandboxList.tsx')
|
||||||
const pagePath = path.join(root, 'app/page.tsx')
|
const pagePath = path.join(root, 'app/page.tsx')
|
||||||
|
|
||||||
const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, routeSource, uploadRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, middlewareSource, panelSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([
|
const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, middlewareSource, panelSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([
|
||||||
readFile(storePath, 'utf8'),
|
readFile(storePath, 'utf8'),
|
||||||
readFile(brokerStorePath, 'utf8'),
|
readFile(brokerStorePath, 'utf8'),
|
||||||
readFile(brokerClientPath, 'utf8'),
|
readFile(brokerClientPath, 'utf8'),
|
||||||
readFile(manifestPath, 'utf8'),
|
readFile(manifestPath, 'utf8'),
|
||||||
|
readFile(privilegedFilesPath, 'utf8'),
|
||||||
readFile(routePath, 'utf8'),
|
readFile(routePath, 'utf8'),
|
||||||
readFile(uploadRoutePath, 'utf8'),
|
readFile(uploadRoutePath, 'utf8'),
|
||||||
readFile(registryRoutePath, 'utf8'),
|
readFile(registryRoutePath, 'utf8'),
|
||||||
@@ -60,6 +62,8 @@ assert.match(brokerClientSource, /StreamableHTTPClientTransport/, 'MCP broker mu
|
|||||||
assert.match(brokerClientSource, /callBrokerServerTool/, 'MCP broker must forward allowed tool calls')
|
assert.match(brokerClientSource, /callBrokerServerTool/, 'MCP broker must forward allowed tool calls')
|
||||||
assert.match(manifestSource, /openshell_control_mcp\.md/, 'MCP sandbox manifest must use the requested filename')
|
assert.match(manifestSource, /openshell_control_mcp\.md/, 'MCP sandbox manifest must use the requested filename')
|
||||||
assert.match(manifestSource, /syncSandboxMcpManifest/, 'MCP sandbox manifest must be syncable into a sandbox')
|
assert.match(manifestSource, /syncSandboxMcpManifest/, 'MCP sandbox manifest must be syncable into a sandbox')
|
||||||
|
assert.match(manifestSource, /writeSandboxFilePrivileged/, 'MCP sandbox manifest must use a privileged write path for root-owned /sandbox mounts')
|
||||||
|
assert.match(privilegedFilesSource, /kubectl/, 'privileged sandbox file writes must use the OpenShell cluster control path')
|
||||||
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