|
| 1 | +import { |
| 2 | + NodeRuntime, |
| 3 | + allowAllFs, |
| 4 | + createNodeDriver, |
| 5 | + createNodeRuntimeDriverFactory, |
| 6 | +} from "secure-exec"; |
| 7 | +import { |
| 8 | + S3Client, |
| 9 | + CreateBucketCommand, |
| 10 | + DeleteBucketCommand, |
| 11 | + ListObjectsV2Command, |
| 12 | + DeleteObjectCommand, |
| 13 | +} from "@aws-sdk/client-s3"; |
| 14 | +import { S3FileSystem } from "./s3-filesystem.js"; |
| 15 | + |
| 16 | +const BUCKET = "secure-exec-vfs-test"; |
| 17 | + |
| 18 | +// Connect to MinIO (S3-compatible) running on localhost |
| 19 | +const client = new S3Client({ |
| 20 | + endpoint: "http://localhost:9000", |
| 21 | + region: "us-east-1", |
| 22 | + credentials: { |
| 23 | + accessKeyId: "minioadmin", |
| 24 | + secretAccessKey: "minioadmin", |
| 25 | + }, |
| 26 | + forcePathStyle: true, |
| 27 | +}); |
| 28 | + |
| 29 | +// Create test bucket |
| 30 | +try { |
| 31 | + await client.send(new CreateBucketCommand({ Bucket: BUCKET })); |
| 32 | +} catch (err: unknown) { |
| 33 | + const e = err as { name?: string }; |
| 34 | + if ( |
| 35 | + e.name !== "BucketAlreadyOwnedByYou" && |
| 36 | + e.name !== "BucketAlreadyExists" |
| 37 | + ) { |
| 38 | + throw err; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const filesystem = new S3FileSystem({ client, bucket: BUCKET }); |
| 43 | + |
| 44 | +const runtime = new NodeRuntime({ |
| 45 | + systemDriver: createNodeDriver({ |
| 46 | + filesystem, |
| 47 | + permissions: { ...allowAllFs }, |
| 48 | + }), |
| 49 | + runtimeDriverFactory: createNodeRuntimeDriverFactory(), |
| 50 | +}); |
| 51 | + |
| 52 | +try { |
| 53 | + // Sandbox writes files — stored in MinIO via S3 API |
| 54 | + const writeResult = await runtime.exec(` |
| 55 | + const fs = require("node:fs"); |
| 56 | + fs.mkdirSync("/workspace", { recursive: true }); |
| 57 | + fs.writeFileSync("/workspace/hello.txt", "hello from sandbox via S3"); |
| 58 | + fs.writeFileSync("/workspace/data.json", JSON.stringify({ count: 42 })); |
| 59 | + console.log("files written"); |
| 60 | + `); |
| 61 | + |
| 62 | + if (writeResult.code !== 0) { |
| 63 | + throw new Error(`Write step failed: ${writeResult.errorMessage}`); |
| 64 | + } |
| 65 | + |
| 66 | + // Sandbox reads files back |
| 67 | + let readOutput = ""; |
| 68 | + const readResult = await runtime.exec( |
| 69 | + ` |
| 70 | + const fs = require("node:fs"); |
| 71 | + const text = fs.readFileSync("/workspace/hello.txt", "utf8"); |
| 72 | + const data = JSON.parse(fs.readFileSync("/workspace/data.json", "utf8")); |
| 73 | + const entries = fs.readdirSync("/workspace"); |
| 74 | + console.log(JSON.stringify({ text, count: data.count, entries })); |
| 75 | + `, |
| 76 | + { |
| 77 | + onStdio: (event) => { |
| 78 | + if (event.channel === "stdout") readOutput += event.message; |
| 79 | + }, |
| 80 | + }, |
| 81 | + ); |
| 82 | + |
| 83 | + if (readResult.code !== 0) { |
| 84 | + throw new Error(`Read step failed: ${readResult.errorMessage}`); |
| 85 | + } |
| 86 | + |
| 87 | + // Verify from host side via S3 API |
| 88 | + const hostContent = await filesystem.readTextFile("/workspace/hello.txt"); |
| 89 | + const parsed = JSON.parse(readOutput.trim()); |
| 90 | + |
| 91 | + const ok = |
| 92 | + hostContent === "hello from sandbox via S3" && |
| 93 | + parsed.text === "hello from sandbox via S3" && |
| 94 | + parsed.count === 42 && |
| 95 | + parsed.entries.includes("hello.txt") && |
| 96 | + parsed.entries.includes("data.json"); |
| 97 | + |
| 98 | + console.log( |
| 99 | + JSON.stringify({ |
| 100 | + ok, |
| 101 | + hostContent, |
| 102 | + sandboxResult: parsed, |
| 103 | + summary: |
| 104 | + "S3-backed VFS: sandbox wrote files to MinIO, read them back, host verified via S3 API", |
| 105 | + }), |
| 106 | + ); |
| 107 | +} finally { |
| 108 | + runtime.dispose(); |
| 109 | + |
| 110 | + // Clean up: delete all objects and the bucket |
| 111 | + const list = await client.send( |
| 112 | + new ListObjectsV2Command({ Bucket: BUCKET }), |
| 113 | + ); |
| 114 | + for (const obj of list.Contents ?? []) { |
| 115 | + await client.send( |
| 116 | + new DeleteObjectCommand({ Bucket: BUCKET, Key: obj.Key! }), |
| 117 | + ); |
| 118 | + } |
| 119 | + await client.send(new DeleteBucketCommand({ Bucket: BUCKET })); |
| 120 | +} |
0 commit comments