Merge sandbox module (Agent Sandbox, Workspaces)

This commit is contained in:
2026-10-24 04:36:57 +02:00
35 changed files with 3659 additions and 3 deletions
@@ -0,0 +1,46 @@
// Routes: every /agent/sandbox/* route that would call Google answers 503
// google_not_configured without a credential, before any network traffic.
#include <cstdlib>
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
const std::string engine = "projects/test-project/locations/us-central1/reasoningEngines/1";
const std::string sandbox = engine + "/sandboxEnvironments/2";
GoogleRig rig(false);
Fixture f(rig.client);
struct Case {
const char* route;
std::string body;
};
const Case cases[] = {
{"/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\"}"},
{"/agent/sandbox/list", "{\"engine\":\"" + engine + "\"}"},
{"/agent/sandbox/get", "{\"name\":\"" + sandbox + "\"}"},
{"/agent/sandbox/pause", "{\"name\":\"" + sandbox + "\"}"},
{"/agent/sandbox/resume", "{\"name\":\"" + sandbox + "\"}"},
{"/agent/sandbox/snapshot", "{\"name\":\"" + sandbox + "\",\"display_name\":\"s\"}"},
{"/agent/sandbox/snapshots", "{\"engine\":\"" + engine + "\"}"},
{"/agent/sandbox/templates", "{\"engine\":\"" + engine + "\"}"},
{"/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"print(1)\\nprint(2)\"}"},
{"/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"echo hi\"}"},
{"/agent/sandbox/computer",
"{\"name\":\"" + sandbox + "\",\"action\":\"navigate\",\"url\":\"https://example.com\"}"},
{"/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"list\"}"},
};
for (const Case& c : cases) {
auto res = f.client->post(c.route, c.body, "application/json");
check(res.status == 503, std::string(c.route) + " answers 503");
check(contains(res.body, "google_not_configured"), std::string(c.route) + " names the error");
}
auto del = f.client->request(molo::example::fw::Method::Delete,
"/agent/sandbox/delete?name=" + sandbox);
check(del.status == 503, "delete answers 503 without a credential");
check(contains(del.body, "google_not_configured"), "delete names the error");
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
return finish("agent/sandbox/not_configured");
}
+35
View File
@@ -0,0 +1,35 @@
// Routes: GET /agent/sandbox, GET /agent/sandbox/status (2xx without a
// credential, never any upstream traffic; POST is 405)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
{
GoogleRig rig(false);
Fixture f(rig.client);
auto page = f.client->get("/agent/sandbox");
check(page.status == 200, "page 200 without a credential");
check(contains(page.headers.get("Content-Type"), "text/html"), "page is html");
check(contains(page.body, "Agent sandbox") && contains(page.body, "agent_sandbox.js"),
"page names the module and loads its script");
auto status = f.client->get("/agent/sandbox/status");
check(status.status == 200, "status 200 without a credential");
check(contains(status.headers.get("Content-Type"), "application/json"), "status is json");
check(contains(status.body, "\"configured\":false"), "not configured without a key");
check(contains(status.body, "\"max_command_bytes\":8192"), "caps are published");
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
}
{
GoogleRig rig;
Fixture f(rig.client);
auto status = f.client->get("/agent/sandbox/status");
check(contains(status.body, "\"configured\":true"), "configured with a key");
check(!contains(status.body, kTestKey), "credential never shown");
check(rig.mock->send_count() == 0, "status makes no upstream call");
}
Fixture f;
check(f.client->post("/agent/sandbox").status == 405, "post to the page is 405");
check(f.client->post("/agent/sandbox/status").status == 405, "post to status is 405");
return finish("agent/sandbox/page");
}
@@ -0,0 +1,57 @@
// Routes: POST /agent/sandbox/* answer 422 on bad input (names, caps, enums)
// before any upstream call; GET on a POST route is 405.
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
const std::string engine = "projects/p1/locations/us-central1/reasoningEngines/1";
const std::string sandbox = engine + "/sandboxEnvironments/2";
GoogleRig rig;
Fixture f(rig.client);
auto post = [&](const std::string& route, const std::string& body) {
return f.client->post(route, body, "application/json");
};
auto bad = [&](const std::string& route, const std::string& body, const std::string& what) {
auto res = post(route, body);
check(res.status == 422, what + " is 422");
};
bad("/agent/sandbox/create", "{\"engine\":\"nope\",\"display_name\":\"d\"}", "bad engine name");
bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\"}", "missing display name");
bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"kind\":\"gpu\"}", "unknown kind");
bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"ttl_seconds\":\"5\"}", "ttl under the floor");
bad("/agent/sandbox/create", "{\"engine\":\"" + engine + "\",\"display_name\":\"d\",\"kind\":\"computer\"}", "computer sandbox without template");
bad("/agent/sandbox/create", "not json", "malformed body");
bad("/agent/sandbox/get", "{\"name\":\"" + engine + "\"}", "engine name used as sandbox name");
bad("/agent/sandbox/list", "{}", "missing engine");
bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"\"}", "empty code");
bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"x\",\"file_name\":\"../a\",\"file_b64\":\"eA==\"}", "path traversal file name");
bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"x\",\"file_name\":\"a\",\"file_b64\":\"!!\"}", "file not base64");
bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"\"}", "empty command");
bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"ls\",\"timeout\":\"9999\"}", "timeout over the cap");
bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"ls\",\"cwd\":\"/a b\"}", "cwd with a space");
bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"fly\"}", "unknown action");
bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"navigate\",\"url\":\"javascript:1\"}", "script url");
bad("/agent/sandbox/computer", "{\"name\":\"" + sandbox + "\",\"action\":\"click\",\"x\":\"5\"}", "click without y");
bad("/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"read\",\"path\":\"/etc/passwd\"}", "path outside the workspace");
bad("/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"zap\"}", "unknown file op");
bad("/agent/sandbox/snapshot", "{\"name\":\"" + sandbox + "\",\"display_name\":\"s\",\"post_action\":\"STOP\"}", "bad post action");
// Limit cases: the caps hold at the route.
std::string huge_cmd(8193, 'a');
bad("/agent/sandbox/shell", "{\"name\":\"" + sandbox + "\",\"command\":\"" + huge_cmd + "\"}", "command over 8192 bytes");
std::string huge_code(256 * 1024 + 1, 'a');
bad("/agent/sandbox/execute", "{\"name\":\"" + sandbox + "\",\"code\":\"" + huge_code + "\"}", "code over 256 KiB");
auto del = f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/delete?name=bad");
check(del.status == 422, "delete with a bad name is 422");
check(f.client->get("/agent/sandbox/create").status == 405, "GET on create is 405");
check(f.client->get("/agent/sandbox/execute").status == 405, "GET on execute is 405");
check(f.client->post("/agent/sandbox/delete").status == 405, "POST on delete is 405");
check(rig.mock->send_count() == 0, "side effect: no upstream traffic for bad input");
return finish("agent/sandbox/validation");
}