feat(clients): sandbox: data plane (JWT proxy, CDP WebSocket), operations, SDK chunk shapes
Data plane per the computer use quickstart and SDK 2.6.0 source: signJwt minted JWT, /cdp /tabs /cdp_ws_endpoint over HTTP with routing headers, CDP over the live WsClient with capped reconnect. Operations methods wrapped, input/output chunk shapes settled from the SDK, ComputerUse moved off execute. Execution paths remain verified by Discovery, docs and SDK only. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
#include "google/sandbox/caps.h"
|
||||
#include "google/sandbox/code_executor.h"
|
||||
#include "google/sandbox/computer_use.h"
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
#include "google/sandbox/operations.h"
|
||||
#include "google/sandbox/stream_executor.h"
|
||||
#include "google/sandbox/sandboxes.h"
|
||||
#include "google/sandbox/shell_runner.h"
|
||||
#include "google/sandbox/snapshots.h"
|
||||
@@ -63,6 +66,20 @@ long number(const Fields& in, const char* key, long fallback) {
|
||||
return std::stol(v);
|
||||
}
|
||||
|
||||
bool flag(const Fields& in, const char* key) { return field(in, key) == "true"; }
|
||||
|
||||
// A JSON object given as text (params of a CDP command).
|
||||
Json object_of(const Fields& in, const char* key) {
|
||||
std::string text = field(in, key);
|
||||
if (text.empty()) return Json::object();
|
||||
try {
|
||||
Json doc = Json::parse(text);
|
||||
if (doc.is_object()) return doc;
|
||||
} catch (const clients::body::JsonError&) {
|
||||
}
|
||||
throw fw::HttpError(422, "validation_error", std::string(key) + " must be a JSON object");
|
||||
}
|
||||
|
||||
[[noreturn]] void rethrow(const google::GoogleError& e) {
|
||||
if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED")
|
||||
throw fw::HttpError(503, "google_not_configured", e.message());
|
||||
@@ -164,6 +181,51 @@ OAuth token (GOOGLE_ACCESS_TOKEN); every call is capped.</p>
|
||||
<textarea name="content" rows="3" placeholder="content (write)"></textarea>
|
||||
<button type="submit">Go</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/dataplane">
|
||||
<h2>Data plane (computer use)</h2>
|
||||
<input name="name" placeholder="sandbox name" required>
|
||||
<select name="action"><option value="health">health</option><option value="tabs">list tabs</option><option value="new_tab">new tab</option><option value="activate">activate tab</option><option value="close">close tab</option><option value="command">CDP command</option></select>
|
||||
<input name="tab_id" placeholder="tab id">
|
||||
<input name="command" placeholder="Page.navigate">
|
||||
<input name="params" placeholder='{"url":"https://example.com"}'>
|
||||
<button type="submit">Send</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/cdp">
|
||||
<h2>CDP WebSocket</h2>
|
||||
<input name="name" placeholder="sandbox name" required>
|
||||
<input name="method" placeholder="Page.navigate" required>
|
||||
<input name="params" placeholder='{"url":"https://example.com"}'>
|
||||
<select name="page"><option value="true">attach first page</option><option value="false">browser level</option></select>
|
||||
<button type="submit">Call</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/stream">
|
||||
<h2>Streamed execute (bidiExecute)</h2>
|
||||
<input name="name" placeholder="sandbox name" required>
|
||||
<select name="mode"><option value="shell">shell</option><option value="code">code</option></select>
|
||||
<textarea name="input" rows="3" placeholder="command or code" required></textarea>
|
||||
<button type="submit">Run</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/operations">
|
||||
<h2>Operations</h2>
|
||||
<input name="name" placeholder="operation name, or a resource name for list" required>
|
||||
<select name="op"><option value="get">get</option><option value="wait">wait</option><option value="list">list</option><option value="cancel">cancel</option></select>
|
||||
<input name="timeout" placeholder="wait seconds (30)">
|
||||
<button type="submit">Go</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/resource">
|
||||
<h2>Template or snapshot</h2>
|
||||
<input name="name" placeholder="template or snapshot name" required>
|
||||
<select name="kind"><option value="template">template</option><option value="snapshot">snapshot</option></select>
|
||||
<button type="submit">Get</button>
|
||||
</form>
|
||||
<form class="sb-form" data-route="/agent/sandbox/template">
|
||||
<h2>Create template</h2>
|
||||
<input name="engine" placeholder="reasoning engine name" required>
|
||||
<input name="display_name" placeholder="display name" required>
|
||||
<select name="category"><option value="computer">computer use</option><option value="shell">shell</option></select>
|
||||
<select name="internet"><option value="false">no internet</option><option value="true">internet access</option></select>
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
<pre id="sb-out" class="sb-out"></pre>
|
||||
<script src="/embedded/agent_sandbox.js"></script>
|
||||
{% endblock %}
|
||||
@@ -181,7 +243,10 @@ void register_agent_sandbox_demo(fw::Router& router, Services& services) {
|
||||
auto sandboxes = std::make_shared<sb::Sandboxes>(client);
|
||||
auto code = std::make_shared<sb::CodeExecutor>(client);
|
||||
auto shell = std::make_shared<sb::ShellRunner>(client);
|
||||
auto computer = std::make_shared<sb::ComputerUse>(client);
|
||||
auto connector = std::make_shared<sb::DataPlaneConnector>(client, sb::DataPlaneOptions::from_env());
|
||||
auto computer = std::make_shared<sb::ComputerUse>(client, connector);
|
||||
auto operations = std::make_shared<sb::Operations>(client);
|
||||
auto streamer = std::make_shared<sb::StreamExecutor>(client);
|
||||
auto snapshots = std::make_shared<sb::Snapshots>(client);
|
||||
auto templates = std::make_shared<sb::Templates>(client);
|
||||
|
||||
@@ -192,14 +257,16 @@ void register_agent_sandbox_demo(fw::Router& router, Services& services) {
|
||||
"text/html; charset=utf-8");
|
||||
});
|
||||
|
||||
router.get("/agent/sandbox/status", "agent-sandbox-status", [client](fw::Request) {
|
||||
router.get("/agent/sandbox/status", "agent-sandbox-status", [client, connector](fw::Request) {
|
||||
Json o = Json::object();
|
||||
o.set("configured", Json(client->configured()));
|
||||
// The data plane signs JWTs with a service account: name only whether one is set.
|
||||
o.set("data_plane_signer", Json(!connector->options().service_account_email.empty()));
|
||||
o.set("caps", caps_json());
|
||||
Json kinds = Json::array();
|
||||
for (const char* k : {"code", "shell", "computer"}) kinds.push(Json(k));
|
||||
o.set("kinds", kinds);
|
||||
o.set("api", Json("aiplatform v1 (computer use and templates: v1beta1)"));
|
||||
o.set("api", Json("aiplatform v1 (computer use, templates, bidiExecute: v1beta1); data plane: JWT proxy and CDP WebSocket"));
|
||||
fw::Response res = fw::JsonResponse(o.dump(), 200);
|
||||
res.headers.set("Cache-Control", "no-store");
|
||||
return res;
|
||||
@@ -322,13 +389,150 @@ void register_agent_sandbox_demo(fw::Router& router, Services& services) {
|
||||
else r = computer->screenshot(name);
|
||||
Json o = Json::object();
|
||||
o.set("result", r.result);
|
||||
o.set("exec", r.exec.to_json());
|
||||
o.set("status", Json(r.status));
|
||||
if (!r.screenshot_png.empty())
|
||||
o.set("screenshot_png_b64", Json(security::crypto::Base64::encode(r.screenshot_png)));
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/dataplane", "agent-sandbox-dataplane", [computer](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
std::string action = field(in, "action");
|
||||
if (action != "health" && action != "tabs" && action != "new_tab" && action != "activate" &&
|
||||
action != "close" && action != "command")
|
||||
throw fw::HttpError(422, "validation_error",
|
||||
"action must be health, tabs, new_tab, activate, close or command");
|
||||
Json params = object_of(in, "params");
|
||||
return guarded([&] {
|
||||
std::string name = field(in, "name");
|
||||
if (action == "health") return computer->health(name);
|
||||
if (action == "tabs") return computer->list_tabs(name);
|
||||
if (action == "new_tab") return computer->create_tab(name);
|
||||
if (action == "activate") return computer->activate_tab(name, field(in, "tab_id"));
|
||||
if (action == "close") return computer->close_tab(name, field(in, "tab_id"));
|
||||
sb::ActionResult r = computer->command(name, field(in, "command"), params);
|
||||
Json o = Json::object();
|
||||
o.set("status", Json(r.status));
|
||||
o.set("result", r.result);
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
// One WebSocket round trip: connect, optionally attach the first page, one
|
||||
// CDP call, close. The session never outlives the request.
|
||||
router.post("/agent/sandbox/cdp", "agent-sandbox-cdp", [computer](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
Json params = object_of(in, "params");
|
||||
std::string method = field(in, "method");
|
||||
return guarded([&] {
|
||||
sb::DataPlane::validate_command(method);
|
||||
std::unique_ptr<sb::CdpSession> cdp = computer->open_cdp(field(in, "name"));
|
||||
std::string session;
|
||||
if (field(in, "page") != "false") session = cdp->attach_first_page();
|
||||
Json o = Json::object();
|
||||
o.set("result", cdp->call(method, params, session));
|
||||
Json events = Json::array();
|
||||
for (const Json& e : cdp->take_events()) events.push(e);
|
||||
o.set("events", events);
|
||||
o.set("dropped_events", Json(static_cast<long>(cdp->dropped_events())));
|
||||
cdp->close();
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/stream", "agent-sandbox-stream", [streamer](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
std::string mode = field(in, "mode").empty() ? "shell" : field(in, "mode");
|
||||
if (mode != "shell" && mode != "code")
|
||||
throw fw::HttpError(422, "validation_error", "mode must be shell or code");
|
||||
return guarded([&] {
|
||||
std::vector<sb::Chunk> inputs =
|
||||
mode == "shell" ? sb::ChunkCodec::shell_inputs(field(in, "input"), field(in, "cwd"),
|
||||
static_cast<int>(number(in, "timeout", sb::SandboxCaps::kDefaultShellTimeoutSeconds)))
|
||||
: sb::ChunkCodec::code_inputs(field(in, "input"), {});
|
||||
if (mode == "shell") sb::ShellRunner::validate({field(in, "input"), field(in, "cwd"),
|
||||
static_cast<int>(number(in, "timeout", sb::SandboxCaps::kDefaultShellTimeoutSeconds))},
|
||||
sb::SandboxCaps::kMaxCommandBytes);
|
||||
else sb::CodeExecutor::validate(field(in, "input"), {});
|
||||
return streamer->run(field(in, "name"), inputs).to_json();
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/operations", "agent-sandbox-operations", [operations](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
std::string op = field(in, "op");
|
||||
if (op != "get" && op != "wait" && op != "list" && op != "cancel")
|
||||
throw fw::HttpError(422, "validation_error", "op must be get, wait, list or cancel");
|
||||
return guarded([&] {
|
||||
std::string name = field(in, "name");
|
||||
if (op == "get") return sb::Operations::to_json(operations->get(name));
|
||||
if (op == "wait")
|
||||
return sb::Operations::to_json(operations->wait(name, static_cast<int>(number(in, "timeout", 30))));
|
||||
if (op == "cancel") {
|
||||
operations->cancel(name);
|
||||
Json o = Json::object();
|
||||
o.set("cancel_requested", Json(name));
|
||||
return o;
|
||||
}
|
||||
Json arr = Json::array();
|
||||
for (const auto& e : operations->list(name, static_cast<int>(number(in, "page_size", 0))))
|
||||
arr.push(sb::Operations::to_json(e));
|
||||
Json o = Json::object();
|
||||
o.set("operations", arr);
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
// Removes the operation record, not the sandbox, template or snapshot.
|
||||
router.del("/agent/sandbox/operation", "agent-sandbox-operation-delete", [operations](fw::Request req) {
|
||||
std::string name = req.query_or("name", "");
|
||||
return guarded([&] {
|
||||
operations->remove(name);
|
||||
Json o = Json::object();
|
||||
o.set("deleted", Json(name));
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/resource", "agent-sandbox-resource", [snapshots, templates](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
std::string kind = field(in, "kind");
|
||||
if (kind != "template" && kind != "snapshot")
|
||||
throw fw::HttpError(422, "validation_error", "kind must be template or snapshot");
|
||||
return guarded([&] {
|
||||
return kind == "template" ? templates->get(field(in, "name")).to_json()
|
||||
: snapshots->get(field(in, "name")).to_json();
|
||||
});
|
||||
});
|
||||
|
||||
router.del("/agent/sandbox/resource", "agent-sandbox-resource-delete", [snapshots, templates](fw::Request req) {
|
||||
std::string kind = req.query_or("kind", "");
|
||||
std::string name = req.query_or("name", "");
|
||||
if (kind != "template" && kind != "snapshot")
|
||||
throw fw::HttpError(422, "validation_error", "kind must be template or snapshot");
|
||||
return guarded([&] {
|
||||
if (kind == "template") templates->remove(name);
|
||||
else snapshots->remove(name);
|
||||
Json o = Json::object();
|
||||
o.set("deleted", Json(name));
|
||||
return o;
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/template", "agent-sandbox-template", [templates](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
sb::CreateTemplateRequest r;
|
||||
r.display_name = field(in, "display_name");
|
||||
std::string category = field(in, "category");
|
||||
if (category == "computer") r.category = sb::ContainerCategory::kComputerUse;
|
||||
else if (category == "shell") r.category = sb::ContainerCategory::kShellSandbox;
|
||||
else if (!category.empty())
|
||||
throw fw::HttpError(422, "validation_error", "category must be computer or shell");
|
||||
r.internet_access = flag(in, "internet");
|
||||
return guarded([&] { return templates->create(field(in, "engine"), r).to_json(); });
|
||||
});
|
||||
|
||||
router.post("/agent/sandbox/files", "agent-sandbox-files", [shell](fw::Request req) {
|
||||
Fields in = fields_of(req);
|
||||
std::string op = field(in, "op");
|
||||
|
||||
+199
-69
@@ -2,7 +2,8 @@
|
||||
|
||||
Owned directory: `src/google/sandbox/`. Namespace `molo::google::sandbox`.
|
||||
Depends on the Google foundation (`GoogleClient`, `Service`, `GoogleError`,
|
||||
`wait_operation`, `list_all`, `CloudContext`) and the Base64 codec. No other
|
||||
`wait_operation`, `list_all`, `CloudContext`), the Base64 codec and the RFC 6455 client of
|
||||
`src/google/live` (`WsClient`, `open_stream`, used only by `CdpSession`). No other
|
||||
module is included.
|
||||
|
||||
Purpose: create, inspect, pause, snapshot and delete Agent Sandbox
|
||||
@@ -20,24 +21,46 @@ delete) and `sandboxEnvironmentSnapshots` (get, list, delete). A sandbox's
|
||||
category is one of `codeExecutionEnvironment` (Python or JavaScript),
|
||||
`computerUseEnvironment` (browser) or `shellEnvironment` (bash).
|
||||
|
||||
**Workspaces.** No resource, method or schema named "workspace" exists in the
|
||||
`aiplatform` Discovery documents (v1 and v1beta1, checked 2026-10-24) and the
|
||||
sandbox docs index lists no workspace page. The product-level "Workspaces"
|
||||
(bash commands and file management isolated from core systems) is what the
|
||||
docs call the **shell sandbox**: `execute_bash` returns `stdout`, `stderr`,
|
||||
`returncode`, `duration_ms`, runs as an unprivileged user in `/workspace`, one
|
||||
fresh shell per command. This module implements that as `ShellRunner`.
|
||||
File management has no REST surface: `WorkspaceFiles` is a **local
|
||||
abstraction** that does file work with shell commands through `ShellRunner`.
|
||||
It is named local everywhere and is not a Google API.
|
||||
**Workspaces.** Re-checked 2026-10-24 (second pass): no resource, method or
|
||||
schema named "workspace" exists in the `aiplatform` Discovery documents (v1
|
||||
and v1beta1, 4.0 MB and 5.3 MB, case-insensitive search), none in the Python
|
||||
SDK 2.6.0 (`agentplatform/_genai`, `vertexai/_genai`: only `sandboxes`,
|
||||
`sandbox_templates`, `sandbox_snapshots`), and the docs index lists no
|
||||
workspace page. The platform overview still describes Workspaces as sandboxed
|
||||
environments to "run bash commands and manage files"; the docs call that the
|
||||
**shell sandbox**, and the SDK's own agent tool catalog
|
||||
(`_evals_builtin_tools.py`) shows file tools (`view_file`, `create_file`,
|
||||
`list_dir`, ...) as agent tools, not as REST. So there is still no REST
|
||||
surface. `ShellRunner` runs bash (`/exec`, see section 4) and `WorkspaceFiles`
|
||||
stays a **local abstraction** that does file work with shell commands. It is
|
||||
named local everywhere and is not a Google API.
|
||||
|
||||
**Computer use.** The documented actions (navigate, click, type, screenshot)
|
||||
are Chrome DevTools Protocol commands sent to the sandbox data plane with a
|
||||
service-account JWT (`generate_access_token`, `send_command`, `/cdp`,
|
||||
`/tabs`, a CDP WebSocket for Playwright, VNC view). The data plane is not in
|
||||
Discovery. `ComputerUse` carries the same CDP commands through
|
||||
`sandboxEnvironments.execute`, the only sandbox call in the REST surface; that
|
||||
mapping is unverified (section 7).
|
||||
**Computer use.** Google documents two ways, both on the sandbox data plane
|
||||
(not in Discovery, documented by the computer use quickstart and implemented
|
||||
by the Python SDK 2.6.0, `Sandboxes.send_command` and
|
||||
`generate_browser_ws_headers`, read from the SDK source):
|
||||
|
||||
- HTTP proxy: `https://{connectionInfo.loadBalancerHostname}` (or
|
||||
`http://{loadBalancerIp}`, or the customer PSC endpoint for VPC-SC) with
|
||||
headers `Authorization: Bearer <JWT>`, `X-Sandbox-Routing-Token:
|
||||
<connectionInfo.routingToken>`, `X-Sandbox-Port: 8080`. Routes: `GET /`
|
||||
health, `POST /tabs`, `GET /tabs`, `POST /tabs/{id}/activate`, `DELETE
|
||||
/tabs/{id}`, `GET /cdp_ws_endpoint`, `POST /cdp` (one CDP command on the
|
||||
active tab, body `{"command","params"}`), `POST /cdps` (several).
|
||||
- CDP WebSocket for Playwright `connect_over_cdp`: URL is the data plane host
|
||||
(`wss://`) plus the `endpoint` path from `GET /cdp_ws_endpoint`; the only
|
||||
auth is the header `Sec-WebSocket-Protocol: v1.stream, <JWT>,
|
||||
<routingToken>, 9222`.
|
||||
|
||||
The JWT is minted by `iamcredentials.googleapis.com` `projects.serviceAccounts.signJwt`
|
||||
(`POST v1/projects/-/serviceAccounts/{email}:signJwt`, body `{"payload":
|
||||
"<json string>"}`) with claims `iat, exp (iat+3600), iss=sub=<service account
|
||||
email>, nonce, aud="https://aiplatform.googleapis.com/"`. The caller needs
|
||||
`roles/aiplatform.user` and `roles/iam.serviceAccountTokenCreator` (docs).
|
||||
`ComputerUse` (navigate, click, type, screenshot, tabs, health, any CDP
|
||||
command) and `CdpSession` (CDP over the WebSocket) implement this. The shell
|
||||
sandbox needs none of it: `execute_bash` authenticates with the caller's own
|
||||
credentials through `sandboxEnvironments.execute` (docs and SDK).
|
||||
|
||||
## 2. Module map (one class or concern per file)
|
||||
|
||||
@@ -55,8 +78,13 @@ mapping is unverified (section 7).
|
||||
| `code_executor.h/.cpp` | `CodeExecutor`: code plus input files in, typed result out |
|
||||
| `shell_runner.h/.cpp` | `ShellRunner`: bash command with cwd and timeout |
|
||||
| `workspace_files.h/.cpp` | `WorkspaceFiles` (local): write, read, list, remove_file |
|
||||
| `computer_use.h/.cpp` | `ComputerUse`: navigate, click, type_text, screenshot |
|
||||
| `stream_executor.h/.cpp` | `StreamExecutor`: `bidiExecute` (v1beta1), streamed reply |
|
||||
| `jwt.h/.cpp` | `ITokenSource`, `IamJwtSource`: signJwt-minted data plane JWT, cache, refresh margin, invalidate |
|
||||
| `data_plane.h/.cpp` | `DataPlaneTarget` (address from connectionInfo), `DataPlane` (HTTP proxy: health, tabs, `/cdp`, `/cdps`, `/cdp_ws_endpoint`, header placement, one refresh on 401) |
|
||||
| `cdp_session.h/.cpp` | `CdpSession`: CDP JSON-RPC over the WebSocket (reuses `live::WsClient`), events, reconnect cap |
|
||||
| `data_plane_connector.h/.cpp` | `DataPlaneOptions`, `DataPlaneConnector`: sandbox name to `DataPlane` / `CdpSession` |
|
||||
| `computer_use.h/.cpp` | `ComputerUse`: navigate, click, type_text, screenshot, tabs, health, CDP command, over the data plane |
|
||||
| `operations.h/.cpp` | `Operations`: get, wait, cancel, delete (v1), list (v1beta1) for the three resources |
|
||||
| `stream_executor.h/.cpp` | `StreamExecutor`: `bidiExecute` (v1beta1), one request message, streamed reply (REST form, see section 7) |
|
||||
| `tools/check_discovery.py` | Discovery check for `calls.h` |
|
||||
|
||||
## 3. Static caps (see caps.h)
|
||||
@@ -80,7 +108,15 @@ mapping is unverified (section 7).
|
||||
| List page size / pages | 100 (Google default and ceiling) / 10 |
|
||||
| Template ports / disk | 16 / 1..1024 GB |
|
||||
| URL / typed text / coordinate | 2048 / 4096 / 0..10000 |
|
||||
| Input chunks per execute | 17 (one code chunk plus 16 files) |
|
||||
| Stream events | 256 |
|
||||
| Data plane JWT ttl / refresh margin | 60..3600 s (default 3600) / refresh inside the last 300 s, one refresh per call after a 401 |
|
||||
| Data plane reply / error excerpt / batch | 8 MiB / 512 bytes / 32 commands |
|
||||
| Data plane timeout | default 30 s, max 120 s; no redirects, no automatic retries |
|
||||
| Routing token / host / path / tab id | 4096 / 253 / 256 / 128 chars |
|
||||
| CDP message / buffered events / call timeout | 8 MiB / 256 (oldest dropped and counted) / 30 s default, 120 s max |
|
||||
| CDP reconnects / floor between connects | 3 per session, ever / 250 ms |
|
||||
| Operations wait | 1..120 s server side |
|
||||
| LRO wait | foundation `wait_operation`: 600 s, 400 polls |
|
||||
|
||||
Google's own limit is 100 MB of files per request or response. Ours are
|
||||
@@ -109,24 +145,55 @@ again inside the Chunk, and the codec caps base64 input at 16 MiB.
|
||||
Over-cap or unsafe files are never dropped silently: each gets a reason in
|
||||
`skipped`. `ok()` is true when the exit status is absent or 0.
|
||||
- `Chunk.data` and `Metadata.attributes` values are base64 on the wire
|
||||
(Discovery: `format: byte`); the codec exposes raw bytes.
|
||||
- Computer use accepts only `http://` and `https://` URLs.
|
||||
(Discovery: `format: byte`; the SDK models both as bytes); the codec exposes
|
||||
raw bytes. An attribute value that is not base64 makes the chunk invalid and
|
||||
it is skipped loudly, never guessed.
|
||||
- Input chunks (SDK `execute_code` / `execute_bash`): code is one
|
||||
`application/json` chunk `{"code": ...}` followed by one chunk per input file
|
||||
(its mime type, raw bytes, attribute `file_name`); shell is three chunks, mime
|
||||
`application/x.sandbox-request-uri` data `/exec`, mime
|
||||
`application/x.sandbox-request-port` data `8080`, then `application/json`
|
||||
`{"command","cwd","timeout"}`.
|
||||
- Data plane: the JWT is sent only to the address derived from the sandbox's
|
||||
own `connectionInfo` (or a caller-supplied PSC endpoint); redirects are off;
|
||||
a loopback override is accepted for loopback hosts only. Order of checks in
|
||||
`DataPlaneConnector::open`: name, credential (503), signer (503), sandbox
|
||||
read, `STATE_RUNNING` (409), address. A 401 refreshes the JWT once.
|
||||
- CDP commands are never replayed: a connection lost during a call throws
|
||||
`CDP_CONNECTION_LOST`; the next call reconnects (fresh JWT, endpoint
|
||||
re-resolved, at most 3 reconnects per session). Session ids from before a
|
||||
reconnect are void.
|
||||
- Operations: `delete` removes the operation record, not the resource; `cancel`
|
||||
and `delete` are explicit methods.
|
||||
- Computer use accepts only `http://` and `https://` URLs. The data plane needs a service account to sign
|
||||
with (`GOOGLE_SERVICE_ACCOUNT_EMAIL`, else `client_email` of
|
||||
`GOOGLE_APPLICATION_CREDENTIALS`); without one calls answer 503 NOT_CONFIGURED.
|
||||
|
||||
## 5. Verified against discovery
|
||||
|
||||
`python3 -I src/google/sandbox/tools/check_discovery.py` fetches
|
||||
`https://aiplatform.googleapis.com/$discovery/rest?version=v1` and `v1beta1`
|
||||
and fails when any row of `calls.h` is absent: 18 calls (rpc id, HTTP method,
|
||||
path, request body field paths including nested ones), 21 schemas (field
|
||||
names) and 7 enums (values).
|
||||
(and `iamcredentials` `v1`) and fails when any row of `calls.h` is absent:
|
||||
34 calls (rpc id, HTTP method, path, request body field paths including nested
|
||||
ones), 27 schemas (field names) and 7 enums (values).
|
||||
|
||||
Result on 2026-10-24: `18 calls, 21 schemas, 7 enums, 204 checks, 0 missing`.
|
||||
Result on 2026-10-24: `34 calls, 27 schemas, 7 enums, 268 checks, 0 missing`.
|
||||
Findings from the check that shaped the code: `computerUseEnvironment`,
|
||||
`useGkeTd` and `persistentDiskConfig` exist only in `v1beta1`; `bidiExecute`
|
||||
exists only in `v1beta1`; `PauseSandboxEnvironmentRequest`,
|
||||
`ResumeSandboxEnvironmentRequest` and `AuthorizeSandboxEnvironmentAccessRequest`
|
||||
have no fields; the snapshot request body is a `SandboxEnvironmentSnapshot`
|
||||
whose `sourceSandboxEnvironment` is required.
|
||||
whose `sourceSandboxEnvironment` is required; the operations methods
|
||||
(`get`, `wait`, `cancel`, `delete`) exist in `v1` and `v1beta1`, `list` only in
|
||||
`v1beta1` (`GET v1beta1/{+name}/operations`, `name` is the sandbox, template or
|
||||
snapshot); `wait` takes a `timeout` query parameter (google-duration).
|
||||
|
||||
Method coverage: every Discovery method under `sandboxEnvironments`,
|
||||
`sandboxEnvironmentTemplates` and `sandboxEnvironmentSnapshots` of
|
||||
`projects.locations.reasoningEngines` is wrapped or declared: 10 sandbox
|
||||
methods, 4 template, 3 snapshot, 5 operations per resource (15). Not wrapped
|
||||
on purpose: the project-less aliases (`reasoningEngines.sandbox*`), and the
|
||||
operations of the reasoning engine itself (owned by the runtime module).
|
||||
|
||||
## 6. Verified live
|
||||
|
||||
@@ -155,8 +222,26 @@ statuses and bodies, so the key reaches these read routes too. A made-up
|
||||
collection (`.../bogusThings`) answers an HTML 404 page, and a non-numeric
|
||||
engine id (`none`) answers 400 INVALID_ARGUMENT; engine ids are numeric.
|
||||
|
||||
Second pass 2026-10-24 (same credentials, read-only):
|
||||
|
||||
| Call | Status and body |
|
||||
|---|---|
|
||||
| `GET v1beta1/.../sandboxEnvironments/{absent}/operations?pageSize=5` | 200 `{}` (empty list, not 404) |
|
||||
| `GET v1/.../sandboxEnvironments/{absent}/operations/1` | 404 NOT_FOUND "Operation projects/P/.../operations/1 not found." |
|
||||
| same for `.../sandboxEnvironmentTemplates/{absent}/operations/1` and `.../sandboxEnvironmentSnapshots/{absent}/operations/1` | 404 NOT_FOUND, same message shape |
|
||||
| `POST iamcredentials v1/projects/-/serviceAccounts/{email}:signJwt` with the SDK payload | 403 PERMISSION_DENIED: "IAM Service Account Credentials API has not been used in project N before or it is disabled" |
|
||||
|
||||
The signJwt answer proves the route, path and body shape reach the service;
|
||||
it stops at the project not having the IAM Credentials API enabled. It was not
|
||||
enabled here (that changes the project; an owner decision). Hence no data plane
|
||||
JWT has ever been minted by this module against Google, and the data plane was
|
||||
never reached: the project has no engine, so no sandbox, so no `connectionInfo`
|
||||
and no proxy address. The whole data plane is verified by the SDK source and
|
||||
docs (section 9) plus loopback mechanics tests, not by Google replies.
|
||||
|
||||
Never exercised live (Discovery only): create, pause, resume, snapshot,
|
||||
delete, execute, bidiExecute, authorizeAccess, template create and delete,
|
||||
delete, execute, bidiExecute, authorizeAccess, wait/cancel/delete of operations,
|
||||
template create and delete, the data plane (HTTP proxy and CDP WebSocket),
|
||||
and every response shape of an existing sandbox. Whether a key may mutate
|
||||
sandboxes is unknown.
|
||||
|
||||
@@ -166,57 +251,88 @@ google-test` (a key in `GOOGLE_API_KEY` also works). Test
|
||||
sandboxes of an engine id that cannot exist and gets a sandbox that cannot
|
||||
exist, expecting 404 NOT_FOUND through the real client; it creates nothing.
|
||||
It passed on 2026-10-24 with the OAuth token and with the key.
|
||||
`google_live_sandbox.operations_read_only` and `google_live_sandbox.sign_jwt_probe`
|
||||
cover the second-pass rows above (the second accepts 403 or 400 and prints the status).
|
||||
Gemini's built-in `codeExecution` tool inside `generateContent` belongs to the
|
||||
models module and is not used here.
|
||||
|
||||
## 7. Unverified
|
||||
## 7. Settled and Unverified
|
||||
|
||||
Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
`ChunkCodec` so one edit fixes it:
|
||||
Settled from primary sources on 2026-10-24 (no Google reply seen, because no
|
||||
sandbox exists and none may be created):
|
||||
|
||||
- The payload of an input Chunk. Docs show only the SDK call
|
||||
(`execute_code(input_data={"code":..., "files":[{"name","content"}]})`) and
|
||||
an older doc revision says input file content is base64. Assumed: one
|
||||
`application/json` Chunk holding `{"code", "files":[{"name","content"(base64)}]}`.
|
||||
- The payload of a shell input Chunk. Assumed `{"command","cwd","timeout"}`
|
||||
(names from the SDK helper `execute_bash(command, cwd, timeout)`).
|
||||
- The payload of a computer use input Chunk: `{"command":"Page.navigate",
|
||||
"params":{...}}` is the documented `/cdp` body; sending it through `execute`
|
||||
is assumed. Click is `Input.dispatchMouseEvent` pressed then released, typing
|
||||
is `Input.insertText`, screenshot is `Page.captureScreenshot` (CDP names, not
|
||||
Google names).
|
||||
- Output Chunk payloads: docs show an `application/json` Chunk with `msg_out`,
|
||||
`msg_err`, `output_files[{content (base64)}]`, plus Chunks with a `file_name`
|
||||
attribute. The `name` key inside `output_files` and the shell keys `stdout`,
|
||||
`stderr`, `returncode`, `duration_ms` (seen only in the SDK dict) are
|
||||
assumed. Unknown shapes are skipped loudly, never guessed.
|
||||
- Whether `attributes` values are really base64 on the wire (Discovery says
|
||||
`byte`; docs show plain text). The decoder tries base64 and falls back to the
|
||||
text as written.
|
||||
- Whether `bidiExecute` over this client's POST stream behaves as a server
|
||||
stream; it is a bidirectional method and may need a persistent channel.
|
||||
- Which responses carry which `@type` inside an Operation `response`
|
||||
(`SandboxEnvironment`, `SandboxEnvironmentSnapshot`, template); parsed by
|
||||
- Input chunks for code and shell, the shell output keys (`stdout`, `stderr`,
|
||||
`returncode`, `duration_ms`), the code output keys (`msg_out`, `msg_err`,
|
||||
`output_files[].content` base64) and file chunks with the `file_name`
|
||||
attribute: Python SDK 2.6.0 `execute_code` / `execute_bash` source, the shell
|
||||
and code execution quickstarts. Attribute values are bytes, base64 on the
|
||||
wire: Discovery `format: byte` for `Metadata.attributes`, SDK `bytes`.
|
||||
- CDP command body `{"command","params"}` for `POST /cdp`, the endpoint list,
|
||||
the `/cdp_ws_endpoint` reply key `endpoint`, the three headers, the
|
||||
`Sec-WebSocket-Protocol` form, the JWT claims: computer use quickstart and
|
||||
SDK source.
|
||||
- Workspaces have no REST surface (Discovery, SDK, docs index).
|
||||
|
||||
Still unverified; each is inferred and isolated in one function:
|
||||
|
||||
- The reply body shapes of the data plane. Docs and SDK show only that the
|
||||
body is returned as text. `ComputerUse::result_of` accepts `{"result": {...}}`
|
||||
or the object itself; the screenshot is the base64 `data` of a CDP
|
||||
`Page.captureScreenshot` result. A reply with neither is `EMPTY_RESPONSE`.
|
||||
- The request body of `POST /cdps` (`{"commands":[{"command","params"}...]}` is
|
||||
our reading of "runs multiple CDP commands") and whether `POST /tabs` takes a
|
||||
body (we send none).
|
||||
- The output key `name` inside `output_files` (docs show only `content`). The
|
||||
decoder reads `name` or `file_name` and skips a nameless file loudly.
|
||||
- `Chunk.mimeType` of file input chunks when the caller gives none: we guess
|
||||
from the extension, the SDK sends an empty string.
|
||||
- Whether the CDP WebSocket answers browser-level (`Target.*`) or page-level
|
||||
commands first; `CdpSession::attach_first_page` uses flat sessions
|
||||
(`Target.attachToTarget {flatten:true}`), the standard Chrome mode that
|
||||
Playwright also uses over this endpoint.
|
||||
- Frame size: `live::LiveCaps::kMaxFramePayload` (4 MiB) caps one WebSocket
|
||||
frame. A base64 screenshot larger than that fails over the WebSocket
|
||||
(`TooBig`); use `ComputerUse::screenshot` (HTTP) for large pages.
|
||||
- `bidiExecute`: Discovery gives one `POST ...:bidiExecute` with a request body
|
||||
of `inputs` and a response of `outputs`, described as "bidirectional
|
||||
streaming". Over REST the request is one message and the reply a stream;
|
||||
`StreamExecutor` does exactly that over the real HTTP transport (mechanics
|
||||
tested against a loopback server). Whether Google's REST gateway accepts
|
||||
further input messages on the same call is not documented, so true
|
||||
full-duplex input is not offered.
|
||||
- Which responses carry which `@type` inside an Operation `response`; parsed by
|
||||
field name only.
|
||||
- The `filter` list parameter exists (AIP-160) but its grammar is not
|
||||
documented; it is not exposed.
|
||||
- The `filter` list parameter (AIP-160 grammar not documented) and
|
||||
`returnPartialSuccess` of operations list are not exposed.
|
||||
- Execution paths (execute, bidiExecute, data plane) have never run against
|
||||
Google; they are verified by Discovery, docs and SDK source, and by local
|
||||
mechanics tests only.
|
||||
|
||||
## 8. Gaps
|
||||
|
||||
- The sandbox data plane (HTTP proxy with JWT, `/tabs`, `/cdp`, CDP
|
||||
WebSocket, VNC) is not implemented; it needs a service-account JWT signer
|
||||
(foundation gap in `credentials.cpp`) and a WebSocket client (module `live`).
|
||||
Owner of the follow-up: this module, `computer_use.cpp`.
|
||||
- True bidirectional `bidiExecute` input (see Unverified).
|
||||
- `generate_access_token` and `generate_browser_ws_headers` (SDK helpers) have
|
||||
no REST method in Discovery.
|
||||
- The data plane needs the IAM Credentials API enabled on the project and
|
||||
`roles/iam.serviceAccountTokenCreator` for the signing account; neither could
|
||||
be checked (the API is disabled on `lisa-448004`, section 6). Owner:
|
||||
project administrator.
|
||||
- The data plane cannot be reached without a running sandbox, which this
|
||||
project does not have and must not create from here. First live run:
|
||||
create an engine and a computer use template and sandbox, then
|
||||
`POST /agent/sandbox/dataplane {action:health}`; the reply shapes in
|
||||
section 7 are then read and settled.
|
||||
- VNC streaming view (noVNC over WebSocket) is documented but without any
|
||||
connection detail; not implemented.
|
||||
- Template update, custom container resources/requests, DNS peering, private
|
||||
service connect ingress, CMEK and VPC-SC settings are not typed. Templates
|
||||
keep the whole document in `SandboxTemplate::raw`.
|
||||
keep the whole document in `SandboxTemplate::raw`. VPC-SC data plane access
|
||||
works only with a caller-supplied PSC endpoint (`DataPlaneOptions::psc_endpoint`).
|
||||
- Foundation: `Service::call` always posts to the one endpoint a service was
|
||||
built with; this module needs a different location and version per call, so
|
||||
`SandboxService` builds endpoints itself and talks to `GoogleClient`
|
||||
directly.
|
||||
directly. The data plane host is not a Google API host, so it uses its own
|
||||
`clients::Client` over the real HTTP transport with the JWT and routing
|
||||
headers (the foundation client applies only Google credentials).
|
||||
- `live::LiveCaps` (module `live`, not editable here) fixes the WebSocket frame
|
||||
cap at 4 MiB; a request to raise it belongs to that module.
|
||||
- The example server's flat JSON reader does not decode `\n`; the demo uses
|
||||
the real JSON parser for request bodies.
|
||||
|
||||
@@ -229,6 +345,12 @@ Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/shell-sandbox-quickstart
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/manage-sandboxes
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/reference/rest/v1beta1/projects.locations.reasoningEngines.sandboxEnvironments (and its `create`, `execute`, `list`, `delete`, `pause`, `authorizeAccess` pages) and `.../v1beta1/Chunk`
|
||||
- Python SDK `google-cloud-aiplatform` 2.6.0 (PyPI), files `agentplatform/_genai/sandboxes.py` (`execute_code`, `execute_bash`, `generate_access_token`, `send_command`, `generate_browser_ws_headers`), `sandbox_templates.py`, `sandbox_snapshots.py`, `_evals_builtin_tools.py`
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/computer-use-quickstart (JWT, `send_command`, endpoint table) and `.../computer-use` (`generate_browser_ws_headers`, `connect_over_cdp`, VNC)
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/release-notes (2026-09-09: Computer Use and Shell sandboxes GA, direct API `/exec`)
|
||||
- https://iamcredentials.googleapis.com/$discovery/rest?version=v1 (`signJwt`)
|
||||
- https://chromedevtools.github.io/devtools-protocol/ (CDP JSON-RPC, flat sessions, `Target`, `Page`, `Input`)
|
||||
- https://www.rfc-editor.org/rfc/rfc6455 (WebSocket, via `src/google/live`)
|
||||
- https://aiplatform.googleapis.com/$discovery/rest?version=v1
|
||||
- https://aiplatform.googleapis.com/$discovery/rest?version=v1beta1
|
||||
- Not read (listed on the docs index): `manage-templates`, `manage-snapshots`,
|
||||
@@ -239,7 +361,15 @@ Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
|
||||
`example/app/agent/SandboxDemo.cpp`, page script `example/assets/agent_sandbox.js`.
|
||||
GET `/agent/sandbox` (page, matrix row `agent-sandbox`), GET
|
||||
`/agent/sandbox/status`. POST `/agent/sandbox/{create,list,get,pause,resume,
|
||||
snapshot,snapshots,templates,execute,shell,computer,files}`, DELETE
|
||||
`/agent/sandbox/delete?name=`. Without a credential every Google route is 503
|
||||
`google_not_configured`; bad input is 422. Tests: `tests/example/agent/sandbox/`.
|
||||
`/agent/sandbox/status` (includes `data_plane_signer`, a boolean). POST
|
||||
`/agent/sandbox/{create,list,get,pause,resume,snapshot,snapshots,templates,
|
||||
execute,shell,computer,files,dataplane,cdp,stream,operations,resource,template}`,
|
||||
DELETE `/agent/sandbox/delete?name=`, DELETE `/agent/sandbox/operation?name=`,
|
||||
DELETE `/agent/sandbox/resource?kind=template|snapshot&name=`. `dataplane`
|
||||
covers health, tabs and any CDP command over HTTP; `cdp` opens the WebSocket
|
||||
for one call (optionally attaching the first page) and closes it; `stream` is
|
||||
`bidiExecute`; `operations` is get, wait, list, cancel. Without a credential
|
||||
(or without a service account for the data plane) every Google route is 503
|
||||
`google_not_configured`; bad input is 422. Tests: `tests/example/agent/sandbox/`
|
||||
(route tests) and `tests/src/google/sandbox/` (module tests, loopback data
|
||||
plane in `test_sandbox_dataplane.cpp`).
|
||||
|
||||
@@ -53,6 +53,22 @@ inline constexpr DeclaredCall kDeclaredCalls[] = {
|
||||
{"snapshot.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.get", ""},
|
||||
{"snapshot.list", "GET", "{+parent}/sandboxEnvironmentSnapshots", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.list", ""},
|
||||
{"snapshot.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.delete", ""},
|
||||
{"sandbox.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.get", ""},
|
||||
{"sandbox.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.wait", ""},
|
||||
{"sandbox.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.cancel", ""},
|
||||
{"sandbox.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.delete", ""},
|
||||
{"sandbox.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.list", ""},
|
||||
{"template.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.get", ""},
|
||||
{"template.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.wait", ""},
|
||||
{"template.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.cancel", ""},
|
||||
{"template.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.delete", ""},
|
||||
{"template.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.list", ""},
|
||||
{"snapshot.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.get", ""},
|
||||
{"snapshot.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.wait", ""},
|
||||
{"snapshot.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.cancel", ""},
|
||||
{"snapshot.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.delete", ""},
|
||||
{"snapshot.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.list", ""},
|
||||
{"iam.signJwt", "POST", "{+name}:signJwt", "iamcredentials", "v1", "iamcredentials.projects.serviceAccounts.signJwt", "payload,delegates"},
|
||||
};
|
||||
// CALLS-END
|
||||
|
||||
@@ -79,6 +95,12 @@ inline constexpr DeclaredSchema kDeclaredSchemas[] = {
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentTemplateEgressControlConfig", "internetAccess,networkAttachment,dnsPeeringConfigs"},
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentTemplatePersistentDiskConfig", "enabled,mountPath,sizeGb"},
|
||||
{"aiplatform", "v1beta1", "ListSandboxEnvironmentTemplatesResponse", "sandboxEnvironmentTemplates,nextPageToken"},
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentConnectionInfo", "loadBalancerIp,loadBalancerHostname,sandboxInternalIp,routingToken,serviceAttachment"},
|
||||
{"aiplatform", "v1beta1", "BidiExecuteSandboxEnvironmentRequest", "inputs"},
|
||||
{"aiplatform", "v1beta1", "Chunk", "mimeType,data,metadata"},
|
||||
{"aiplatform", "v1beta1", "Metadata", "attributes"},
|
||||
{"iamcredentials", "v1", "SignJwtRequest", "payload,delegates"},
|
||||
{"iamcredentials", "v1", "SignJwtResponse", "keyId,signedJwt"},
|
||||
};
|
||||
// SCHEMAS-END
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ struct SandboxCaps {
|
||||
static constexpr std::size_t kMaxCommandBytes = 8192;
|
||||
static constexpr std::size_t kMaxInternalCommandBytes = 65536; // Workspace helpers only.
|
||||
static constexpr std::size_t kMaxInputFiles = 16;
|
||||
// One JSON code chunk plus one chunk per file.
|
||||
static constexpr std::size_t kMaxInputChunks = 1 + kMaxInputFiles;
|
||||
static constexpr std::size_t kMaxFileBytes = 4u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxInputTotalBytes = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxFileNameChars = 255;
|
||||
@@ -49,6 +51,37 @@ struct SandboxCaps {
|
||||
static constexpr std::size_t kMaxUrlChars = 2048;
|
||||
static constexpr std::size_t kMaxTypedChars = 4096;
|
||||
static constexpr int kMaxCoordinate = 10000;
|
||||
|
||||
// Data plane (HTTP proxy and CDP WebSocket). All static ceilings.
|
||||
static constexpr long kJwtTtlSeconds = 3600; // SDK default
|
||||
static constexpr long kMinJwtTtlSeconds = 60;
|
||||
static constexpr long kMaxJwtTtlSeconds = 3600; // never longer than an hour
|
||||
static constexpr long kJwtRefreshMarginSeconds = 300; // refresh when this close to exp
|
||||
static constexpr std::size_t kMaxEmailChars = 254;
|
||||
static constexpr std::size_t kMaxRoutingTokenChars = 4096;
|
||||
static constexpr std::size_t kMaxHostChars = 253;
|
||||
static constexpr std::size_t kMaxDataPathChars = 256;
|
||||
static constexpr std::size_t kMaxTabIdChars = 128;
|
||||
static constexpr std::size_t kMaxDataReplyBytes = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxErrorExcerpt = 512;
|
||||
static constexpr std::size_t kMaxBatchCommands = 32;
|
||||
static constexpr int kDataPlaneTimeoutMs = 30000;
|
||||
static constexpr int kMaxDataPlaneTimeoutMs = 120000;
|
||||
static constexpr int kMaxAuthRetries = 1; // one JWT refresh after a 401
|
||||
|
||||
// CDP over WebSocket.
|
||||
static constexpr int kCdpConnectTimeoutMs = 10000;
|
||||
static constexpr int kCdpCallTimeoutMs = 30000;
|
||||
static constexpr int kMaxCdpCallTimeoutMs = 120000;
|
||||
static constexpr int kCdpPollMs = 200;
|
||||
static constexpr std::size_t kMaxCdpEvents = 256; // buffered, oldest dropped and counted
|
||||
static constexpr std::size_t kMaxCdpMessageChars = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxCdpMethodChars = 128;
|
||||
static constexpr int kMaxCdpReconnects = 3; // per session, ever
|
||||
static constexpr int kMinReconnectGapMs = 250; // floor between connects
|
||||
static constexpr std::size_t kMaxCdpSessionIdChars = 128;
|
||||
// Operations.
|
||||
static constexpr int kMaxOperationWaitSeconds = 120;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/live/ws_error.h"
|
||||
#include "google/live/ws_url.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kService = "sandbox-cdp";
|
||||
|
||||
long now_ms() {
|
||||
return static_cast<long>(std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
|
||||
GoogleError lost(const std::string& why) {
|
||||
return GoogleError(502, "CDP_CONNECTION_LOST", why, kService);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CdpSession::CdpSession(std::shared_ptr<DataPlane> plane, CdpOptions options)
|
||||
: plane_(std::move(plane)), options_(std::move(options)) {
|
||||
if (!plane_) throw GoogleError::invalid("a data plane is required");
|
||||
if (options_.connect_timeout_ms == 0) options_.connect_timeout_ms = SandboxCaps::kCdpConnectTimeoutMs;
|
||||
if (options_.call_timeout_ms == 0) options_.call_timeout_ms = SandboxCaps::kCdpCallTimeoutMs;
|
||||
if (options_.connect_timeout_ms < 100 || options_.connect_timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs ||
|
||||
options_.call_timeout_ms < 100 || options_.call_timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeouts must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
DataPlaneTarget::validate_port(options_.browser_port);
|
||||
}
|
||||
|
||||
CdpSession::~CdpSession() {
|
||||
try {
|
||||
close();
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::validate_session_id(const std::string& id) {
|
||||
if (id.size() > SandboxCaps::kMaxCdpSessionIdChars)
|
||||
throw GoogleError::invalid("session id too long");
|
||||
for (char c : id)
|
||||
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '-' && c != '_')
|
||||
throw GoogleError::invalid("session id has an invalid character");
|
||||
}
|
||||
|
||||
Json CdpSession::request(long id, const std::string& method, const Json& params,
|
||||
const std::string& session_id) {
|
||||
DataPlane::validate_command(method);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
validate_session_id(session_id);
|
||||
Json m = Json::object();
|
||||
m.set("id", Json(id));
|
||||
m.set("method", Json(method));
|
||||
m.set("params", params);
|
||||
if (!session_id.empty()) m.set("sessionId", Json(session_id));
|
||||
return m;
|
||||
}
|
||||
|
||||
bool CdpSession::connected() const { return ws_ && !ws_->closed(); }
|
||||
|
||||
void CdpSession::open_socket() {
|
||||
// Floor between connection attempts: a flapping peer is never hammered.
|
||||
if (last_connect_ms_ != 0) {
|
||||
long gap = now_ms() - last_connect_ms_;
|
||||
if (gap < SandboxCaps::kMinReconnectGapMs)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SandboxCaps::kMinReconnectGapMs - gap));
|
||||
}
|
||||
last_connect_ms_ = now_ms();
|
||||
if (endpoint_path_.empty()) endpoint_path_ = plane_->cdp_ws_endpoint();
|
||||
for (int attempt = 0;; ++attempt) {
|
||||
try {
|
||||
live::WsUrl url = live::WsUrl::parse(plane_->ws_url(endpoint_path_));
|
||||
live::WsHeaders headers = plane_->ws_headers(options_.browser_port);
|
||||
auto stream = live::open_stream(url, options_.connect_timeout_ms, 1000);
|
||||
auto ws = std::make_unique<live::WsClient>(std::move(stream));
|
||||
ws->handshake(url, headers, options_.connect_timeout_ms);
|
||||
ws->set_read_timeout_ms(SandboxCaps::kCdpPollMs);
|
||||
ws_ = std::move(ws);
|
||||
++connects_;
|
||||
return;
|
||||
} catch (const live::WsError& e) {
|
||||
bool auth = e.kind() == live::WsError::Kind::Handshake &&
|
||||
(e.http_status() == 401 || e.http_status() == 403);
|
||||
if (auth && attempt < SandboxCaps::kMaxAuthRetries) {
|
||||
plane_->tokens().invalidate();
|
||||
continue;
|
||||
}
|
||||
throw GoogleError(auth ? e.http_status() : 502, auth ? "CDP_UNAUTHORIZED" : "UNAVAILABLE",
|
||||
e.what(), kService);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::connect() {
|
||||
if (connected()) return;
|
||||
open_socket();
|
||||
}
|
||||
|
||||
void CdpSession::ensure_connected() {
|
||||
if (connected()) return;
|
||||
if (connects_ > 0) {
|
||||
// A socket that existed and died: a capped reconnect with a fresh JWT.
|
||||
if (reconnects_ >= SandboxCaps::kMaxCdpReconnects)
|
||||
throw GoogleError(502, "CDP_RECONNECT_LIMIT",
|
||||
"more than " + std::to_string(SandboxCaps::kMaxCdpReconnects) +
|
||||
" reconnects; open a new session",
|
||||
kService);
|
||||
++reconnects_;
|
||||
plane_->tokens().invalidate();
|
||||
endpoint_path_.clear();
|
||||
}
|
||||
open_socket();
|
||||
}
|
||||
|
||||
void CdpSession::close() {
|
||||
if (ws_) {
|
||||
try {
|
||||
ws_->close(1000, "");
|
||||
ws_->shutdown();
|
||||
} catch (...) {
|
||||
}
|
||||
ws_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::push_event(Json event) {
|
||||
if (events_.size() >= SandboxCaps::kMaxCdpEvents) {
|
||||
events_.pop_front();
|
||||
++dropped_events_;
|
||||
}
|
||||
events_.push_back(std::move(event));
|
||||
}
|
||||
|
||||
// Reads for up to budget_ms. Returns true once the reply with wanted_id
|
||||
// (>= 0) arrived in *reply. Every other message with an id is dropped, every
|
||||
// message without one is buffered as an event.
|
||||
bool CdpSession::pump(int budget_ms, long wanted_id, Json* reply) {
|
||||
long deadline = now_ms() + budget_ms;
|
||||
for (;;) {
|
||||
if (now_ms() >= deadline) return false;
|
||||
std::optional<live::WsMessage> m;
|
||||
try {
|
||||
m = ws_->receive();
|
||||
} catch (const live::WsError& e) {
|
||||
ws_.reset();
|
||||
throw lost(e.what());
|
||||
}
|
||||
if (!m) continue;
|
||||
if (m->kind == live::WsMessage::Kind::Close) {
|
||||
ws_.reset();
|
||||
throw lost("peer closed the CDP socket (code " + std::to_string(m->code) + ")");
|
||||
}
|
||||
if (m->data.size() > SandboxCaps::kMaxCdpMessageChars)
|
||||
throw GoogleError(502, "TOO_BIG", "CDP message over cap", kService);
|
||||
Json doc;
|
||||
try {
|
||||
doc = Json::parse(m->data);
|
||||
} catch (const clients::body::JsonError&) {
|
||||
continue; // Not CDP; never guessed at.
|
||||
}
|
||||
if (!doc.is_object()) continue;
|
||||
const Json* id = doc.find("id");
|
||||
if (id && id->is_number()) {
|
||||
if (wanted_id >= 0 && id->as_int() == wanted_id) {
|
||||
*reply = std::move(doc);
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
push_event(std::move(doc));
|
||||
if (wanted_id < 0) return true; // Caller scans the buffer.
|
||||
}
|
||||
}
|
||||
|
||||
Json CdpSession::call(const std::string& method, const Json& params, const std::string& session_id,
|
||||
int timeout_ms) {
|
||||
long id = next_id_;
|
||||
Json msg = request(id, method, params, session_id);
|
||||
if (timeout_ms == 0) timeout_ms = options_.call_timeout_ms;
|
||||
if (timeout_ms < 100 || timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeout must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
ensure_connected();
|
||||
++next_id_;
|
||||
try {
|
||||
ws_->send_text(msg.dump());
|
||||
} catch (const live::WsError& e) {
|
||||
ws_.reset();
|
||||
throw GoogleError(e.kind() == live::WsError::Kind::TooBig ? 400 : 502,
|
||||
e.kind() == live::WsError::Kind::TooBig ? "INVALID_ARGUMENT" : "CDP_CONNECTION_LOST",
|
||||
e.what(), e.kind() == live::WsError::Kind::TooBig ? "" : kService);
|
||||
}
|
||||
Json reply;
|
||||
if (!pump(timeout_ms, id, &reply))
|
||||
throw GoogleError(504, "DEADLINE_EXCEEDED", "no CDP reply to " + method + " in " +
|
||||
std::to_string(timeout_ms) + " ms", kService);
|
||||
if (const Json* err = reply.find("error"); err && err->is_object()) {
|
||||
const Json* m = err->find("message");
|
||||
throw GoogleError(502, "CDP_ERROR", m && m->is_string() ? m->as_string() : "CDP error", kService);
|
||||
}
|
||||
const Json* result = reply.find("result");
|
||||
return result && result->is_object() ? *result : Json::object();
|
||||
}
|
||||
|
||||
std::string CdpSession::attach_first_page() {
|
||||
Json targets = call("Target.getTargets", Json::object());
|
||||
const Json* infos = targets.find("targetInfos");
|
||||
if (!infos || !infos->is_array())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "Target.getTargets returned no targetInfos", kService);
|
||||
for (const Json& t : infos->as_array()) {
|
||||
const Json* type = t.find("type");
|
||||
const Json* tid = t.find("targetId");
|
||||
if (type && type->is_string() && type->as_string() == "page" && tid && tid->is_string()) {
|
||||
Json p = Json::object();
|
||||
p.set("targetId", Json(tid->as_string()));
|
||||
p.set("flatten", Json(true));
|
||||
Json r = call("Target.attachToTarget", p);
|
||||
const Json* sid = r.find("sessionId");
|
||||
if (!sid || !sid->is_string() || sid->as_string().empty())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "attachToTarget returned no sessionId", kService);
|
||||
validate_session_id(sid->as_string());
|
||||
return sid->as_string();
|
||||
}
|
||||
}
|
||||
throw GoogleError(404, "NOT_FOUND", "the browser has no page target", kService);
|
||||
}
|
||||
|
||||
std::vector<Json> CdpSession::take_events() {
|
||||
std::vector<Json> out(events_.begin(), events_.end());
|
||||
events_.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
Json CdpSession::wait_event(const std::string& method, int timeout_ms) {
|
||||
DataPlane::validate_command(method);
|
||||
if (timeout_ms == 0) timeout_ms = options_.call_timeout_ms;
|
||||
if (timeout_ms < 100 || timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeout must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
ensure_connected();
|
||||
long deadline = now_ms() + timeout_ms;
|
||||
for (;;) {
|
||||
for (auto it = events_.begin(); it != events_.end(); ++it) {
|
||||
const Json* m = it->find("method");
|
||||
if (m && m->is_string() && m->as_string() == method) {
|
||||
Json ev = *it;
|
||||
events_.erase(it);
|
||||
return ev;
|
||||
}
|
||||
}
|
||||
long left = deadline - now_ms();
|
||||
if (left <= 0)
|
||||
throw GoogleError(504, "DEADLINE_EXCEEDED", "no CDP event " + method, kService);
|
||||
Json unused;
|
||||
pump(static_cast<int>(left), -1, &unused);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
|
||||
// Chrome DevTools Protocol over the sandbox's CDP WebSocket (computer use).
|
||||
// Connects like the Google SDK's `generate_browser_ws_headers` plus
|
||||
// Playwright's connect_over_cdp: GET /cdp_ws_endpoint on the HTTP data plane
|
||||
// gives a path, the WebSocket URL is the data plane host plus that path, and
|
||||
// the JWT travels in the Sec-WebSocket-Protocol header
|
||||
// ("v1.stream, <jwt>, <routing token>, 9222"). Messages are CDP JSON-RPC
|
||||
// ({"id","method","params"[,"sessionId"]}), see
|
||||
// https://chromedevtools.github.io/devtools-protocol/ (flat session mode).
|
||||
//
|
||||
// Reuses the RFC 6455 client of src/google/live (WsClient, open_stream).
|
||||
// Hard caps: message size, buffered events, call time, reconnects, and a
|
||||
// floor between connection attempts. One thread uses a session at a time.
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "google/live/ws_client.h"
|
||||
#include "google/sandbox/data_plane.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct CdpOptions {
|
||||
int connect_timeout_ms = 0; // 0: SandboxCaps default
|
||||
int call_timeout_ms = 0; // 0: SandboxCaps default
|
||||
std::string browser_port = "9222";
|
||||
};
|
||||
|
||||
class CdpSession {
|
||||
public:
|
||||
CdpSession(std::shared_ptr<DataPlane> plane, CdpOptions options = {});
|
||||
~CdpSession();
|
||||
|
||||
CdpSession(const CdpSession&) = delete;
|
||||
CdpSession& operator=(const CdpSession&) = delete;
|
||||
|
||||
// Resolves the endpoint, mints a JWT and runs the upgrade. Idempotent.
|
||||
void connect();
|
||||
bool connected() const;
|
||||
// Sends a polite close and drops the socket.
|
||||
void close();
|
||||
|
||||
// Sends one CDP command and waits for its reply (matched by id). Returns the
|
||||
// "result" object. A CDP "error" object throws GoogleError 502
|
||||
// CDP_ERROR with Chrome's message. A lost socket before the send is
|
||||
// reconnected (fresh JWT, capped); a loss mid-call throws CDP_CONNECTION_LOST
|
||||
// and the next call reconnects, because commands are never replayed. Session
|
||||
// ids from before a reconnect are void.
|
||||
clients::body::Json call(const std::string& method, const clients::body::Json& params,
|
||||
const std::string& session_id = "", int timeout_ms = 0);
|
||||
|
||||
// Browser-level helper: Target.getTargets, Target.attachToTarget
|
||||
// {flatten:true} for the first "page" target; returns the sessionId.
|
||||
std::string attach_first_page();
|
||||
|
||||
// Events (messages without id) seen while waiting, oldest first; at most
|
||||
// kMaxCdpEvents are kept, older ones are dropped and counted.
|
||||
std::vector<clients::body::Json> take_events();
|
||||
// Waits for an event with this method (already buffered or newly read).
|
||||
clients::body::Json wait_event(const std::string& method, int timeout_ms = 0);
|
||||
|
||||
int connects() const { return connects_; }
|
||||
int reconnects() const { return reconnects_; }
|
||||
std::size_t dropped_events() const { return dropped_events_; }
|
||||
const std::string& endpoint_path() const { return endpoint_path_; }
|
||||
|
||||
static void validate_session_id(const std::string& id);
|
||||
static clients::body::Json request(long id, const std::string& method,
|
||||
const clients::body::Json& params,
|
||||
const std::string& session_id);
|
||||
|
||||
private:
|
||||
void open_socket();
|
||||
void ensure_connected();
|
||||
bool pump(int budget_ms, long wanted_id, clients::body::Json* reply);
|
||||
void push_event(clients::body::Json event);
|
||||
|
||||
std::shared_ptr<DataPlane> plane_;
|
||||
CdpOptions options_;
|
||||
std::unique_ptr<live::WsClient> ws_;
|
||||
std::string endpoint_path_;
|
||||
long next_id_ = 1;
|
||||
int connects_ = 0;
|
||||
int reconnects_ = 0;
|
||||
long last_connect_ms_ = 0;
|
||||
std::deque<clients::body::Json> events_;
|
||||
std::size_t dropped_events_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -1,5 +1,10 @@
|
||||
#include "google/sandbox/chunk.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
#include "google/errors.h"
|
||||
|
||||
#include "google/sandbox/caps.h"
|
||||
#include "google/sandbox/names.h"
|
||||
#include "security/crypto/Base64.h"
|
||||
@@ -30,13 +35,6 @@ void append_capped(std::string& dst, const std::string& add, bool& truncated) {
|
||||
}
|
||||
}
|
||||
|
||||
// Attribute values are "bytes format" (base64). A value that is not valid
|
||||
// base64 is kept as written, so a plain-text server still works.
|
||||
std::string attribute_text(const std::string& wire) {
|
||||
std::string raw;
|
||||
if (!wire.empty() && Base64::decode(wire, raw) && !raw.empty()) return raw;
|
||||
return wire;
|
||||
}
|
||||
|
||||
struct FileBudget {
|
||||
std::size_t total = 0;
|
||||
@@ -123,9 +121,21 @@ Chunk Chunk::from_json(const Json& doc, bool* invalid) {
|
||||
}
|
||||
const Json* meta = doc.is_object() ? doc.find("metadata") : nullptr;
|
||||
const Json* attrs = meta && meta->is_object() ? meta->find("attributes") : nullptr;
|
||||
// Attribute values are "bytes format" (base64): Discovery says byte and the
|
||||
// SDK models them as bytes. A value that is not base64 marks the chunk
|
||||
// invalid, so the decoder skips it loudly instead of guessing.
|
||||
if (attrs && attrs->is_object())
|
||||
for (const auto& [k, v] : attrs->as_object())
|
||||
if (v.is_string()) c.attributes[k] = attribute_text(v.as_string());
|
||||
for (const auto& [k, v] : attrs->as_object()) {
|
||||
if (!v.is_string()) continue;
|
||||
std::string raw;
|
||||
if (!v.as_string().empty() && !Base64::decode(v.as_string(), raw)) {
|
||||
c.data.clear();
|
||||
c.attributes.clear();
|
||||
if (invalid) *invalid = true;
|
||||
return c;
|
||||
}
|
||||
c.attributes[k] = std::move(raw);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -137,6 +147,10 @@ Chunk Chunk::json_payload(const Json& payload) {
|
||||
}
|
||||
|
||||
Json ChunkCodec::request(const std::vector<Chunk>& inputs) {
|
||||
if (inputs.empty()) throw GoogleError::invalid("no input chunks");
|
||||
if (inputs.size() > SandboxCaps::kMaxInputChunks)
|
||||
throw GoogleError::invalid("more than " + std::to_string(SandboxCaps::kMaxInputChunks) +
|
||||
" input chunks");
|
||||
Json arr = Json::array();
|
||||
for (const Chunk& c : inputs) arr.push(c.to_json());
|
||||
Json body = Json::object();
|
||||
@@ -144,37 +158,50 @@ Json ChunkCodec::request(const std::vector<Chunk>& inputs) {
|
||||
return body;
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::code_input(const std::string& code,
|
||||
const std::vector<InputFile>& files) {
|
||||
Json payload = Json::object();
|
||||
payload.set("code", Json(code));
|
||||
if (!files.empty()) {
|
||||
Json arr = Json::array();
|
||||
for (const InputFile& f : files) {
|
||||
Json one = Json::object();
|
||||
one.set("name", Json(f.name));
|
||||
one.set("content", Json(Base64::encode(f.content)));
|
||||
arr.push(one);
|
||||
}
|
||||
payload.set("files", arr);
|
||||
}
|
||||
return Chunk::json_payload(payload);
|
||||
std::string ChunkCodec::guess_mime(const std::string& name) {
|
||||
std::size_t dot = name.rfind('.');
|
||||
std::string ext = dot == std::string::npos ? "" : name.substr(dot + 1);
|
||||
for (char& c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
static const std::map<std::string, const char*> kTypes = {
|
||||
{"txt", "text/plain"}, {"md", "text/markdown"}, {"csv", "text/csv"},
|
||||
{"json", "application/json"}, {"html", "text/html"}, {"xml", "application/xml"},
|
||||
{"py", "text/x-python"}, {"js", "text/javascript"}, {"png", "image/png"},
|
||||
{"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"}, {"gif", "image/gif"},
|
||||
{"svg", "image/svg+xml"}, {"pdf", "application/pdf"}, {"zip", "application/zip"}};
|
||||
auto it = kTypes.find(ext);
|
||||
return it == kTypes.end() ? "application/octet-stream" : it->second;
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::shell_input(const std::string& command, const std::string& cwd,
|
||||
int timeout_seconds) {
|
||||
std::vector<Chunk> ChunkCodec::code_inputs(const std::string& code,
|
||||
const std::vector<InputFile>& files) {
|
||||
Json payload = Json::object();
|
||||
payload.set("code", Json(code));
|
||||
std::vector<Chunk> out;
|
||||
out.push_back(Chunk::json_payload(payload));
|
||||
for (const InputFile& f : files) {
|
||||
Chunk c;
|
||||
c.mime_type = f.mime_type.empty() ? guess_mime(f.name) : f.mime_type;
|
||||
c.data = f.content;
|
||||
c.attributes["file_name"] = f.name;
|
||||
out.push_back(std::move(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<Chunk> ChunkCodec::shell_inputs(const std::string& command,
|
||||
const std::string& cwd, int timeout_seconds,
|
||||
const std::string& port) {
|
||||
Json payload = Json::object();
|
||||
payload.set("command", Json(command));
|
||||
if (!cwd.empty()) payload.set("cwd", Json(cwd));
|
||||
payload.set("timeout", Json(timeout_seconds));
|
||||
return Chunk::json_payload(payload);
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::cdp_input(const std::string& command, const Json& params) {
|
||||
Json payload = Json::object();
|
||||
payload.set("command", Json(command));
|
||||
payload.set("params", params);
|
||||
return Chunk::json_payload(payload);
|
||||
Chunk uri;
|
||||
uri.mime_type = kUriMime;
|
||||
uri.data = "/exec";
|
||||
Chunk prt;
|
||||
prt.mime_type = kPortMime;
|
||||
prt.data = port;
|
||||
return {uri, prt, Chunk::json_payload(payload)};
|
||||
}
|
||||
|
||||
std::vector<Chunk> ChunkCodec::outputs(const Json& reply,
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
// The Chunk wire type of sandboxEnvironments.execute plus the codec between
|
||||
// it and the typed values. Chunk.data and Metadata.attributes values are
|
||||
// base64 on the wire (REST "bytes format"); here they are raw bytes.
|
||||
// base64 on the wire (REST "bytes format", also what the Google SDK sends for
|
||||
// its bytes fields); here they are raw bytes.
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -28,16 +29,24 @@ struct Chunk {
|
||||
|
||||
class ChunkCodec {
|
||||
public:
|
||||
// {"inputs":[chunk,...]}
|
||||
// {"inputs":[chunk,...]}; 1..SandboxCaps::kMaxInputChunks chunks.
|
||||
static clients::body::Json request(const std::vector<Chunk>& inputs);
|
||||
|
||||
// Input chunks. The payload shapes are inferred, see SPEC Unverified.
|
||||
static Chunk code_input(const std::string& code,
|
||||
const std::vector<InputFile>& files);
|
||||
static Chunk shell_input(const std::string& command, const std::string& cwd,
|
||||
int timeout_seconds);
|
||||
static Chunk cdp_input(const std::string& command,
|
||||
const clients::body::Json& params);
|
||||
// Input chunks, as the Google Python SDK builds them (SPEC section 7):
|
||||
// - code: one application/json chunk {"code": ...}, then one chunk per
|
||||
// file (its mime type, raw bytes, attribute file_name).
|
||||
// - shell: three chunks, the request URI "/exec" and the container port
|
||||
// (mime types below) then an application/json {"command","cwd","timeout"}.
|
||||
static std::vector<Chunk> code_inputs(const std::string& code,
|
||||
const std::vector<InputFile>& files);
|
||||
static std::vector<Chunk> shell_inputs(const std::string& command,
|
||||
const std::string& cwd, int timeout_seconds,
|
||||
const std::string& port = "8080");
|
||||
static constexpr const char* kUriMime = "application/x.sandbox-request-uri";
|
||||
static constexpr const char* kPortMime = "application/x.sandbox-request-port";
|
||||
// Extension to media type for the few types sandboxes produce; else
|
||||
// application/octet-stream.
|
||||
static std::string guess_mime(const std::string& file_name);
|
||||
|
||||
// reply["outputs"] to chunks, at most SandboxCaps::kMaxOutputChunks.
|
||||
static std::vector<Chunk> outputs(const clients::body::Json& reply,
|
||||
|
||||
@@ -41,7 +41,7 @@ ExecutionResult CodeExecutor::run(const std::string& sandbox_name,
|
||||
const std::vector<InputFile>& files) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate(code, files);
|
||||
return execute(sandbox_name, {ChunkCodec::code_input(code, files)});
|
||||
return execute(sandbox_name, ChunkCodec::code_inputs(code, files));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -8,8 +8,12 @@ namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
ComputerUse::ComputerUse(std::shared_ptr<GoogleClient> client)
|
||||
: SandboxService(std::move(client)) {}
|
||||
ComputerUse::ComputerUse(std::shared_ptr<GoogleClient> client,
|
||||
std::shared_ptr<DataPlaneConnector> connector)
|
||||
: SandboxService(client),
|
||||
connector_(connector ? std::move(connector)
|
||||
: std::make_shared<DataPlaneConnector>(
|
||||
client, DataPlaneOptions::from_env())) {}
|
||||
|
||||
void ComputerUse::validate_url(const std::string& url) {
|
||||
bool http = url.rfind("http://", 0) == 0;
|
||||
@@ -59,38 +63,52 @@ Json ComputerUse::insert_text_params(const std::string& text) {
|
||||
return p;
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::send(const std::string& sandbox_name, const std::string& command,
|
||||
const Json& params) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
Json reply = execute_raw(sandbox_name, {ChunkCodec::cdp_input(command, params)});
|
||||
Json ComputerUse::screenshot_params() {
|
||||
Json p = Json::object();
|
||||
p.set("format", Json("png"));
|
||||
return p;
|
||||
}
|
||||
|
||||
Json ComputerUse::result_of(const DataPlaneReply& reply) {
|
||||
if (!reply.has_json || !reply.json.is_object()) return Json::object();
|
||||
const Json* r = reply.json.find("result");
|
||||
return r && r->is_object() ? *r : reply.json;
|
||||
}
|
||||
|
||||
std::string ComputerUse::screenshot_of(const Json& result) {
|
||||
// CDP Page.captureScreenshot answers {"data": "<base64 png>"}.
|
||||
const Json* d = result.is_object() ? result.find("data") : nullptr;
|
||||
if (!d || !d->is_string()) return "";
|
||||
if (d->as_string().size() > (SandboxCaps::kMaxScreenshotBytes / 3 + 1) * 4) return "";
|
||||
std::string png;
|
||||
if (!security::crypto::Base64::decode(d->as_string(), png) ||
|
||||
png.size() > SandboxCaps::kMaxScreenshotBytes)
|
||||
return "";
|
||||
return png;
|
||||
}
|
||||
|
||||
namespace {
|
||||
ActionResult run(const DataPlane& plane, const std::string& cdp_method, const Json& params) {
|
||||
DataPlaneReply reply = plane.cdp(cdp_method, params);
|
||||
ActionResult out;
|
||||
out.result = Json::object();
|
||||
out.exec = ChunkCodec::decode(reply);
|
||||
for (const Chunk& c : ChunkCodec::outputs(reply)) {
|
||||
if (c.mime_type == "image/png" && out.screenshot_png.empty() &&
|
||||
c.data.size() <= SandboxCaps::kMaxScreenshotBytes)
|
||||
out.screenshot_png = c.data;
|
||||
if (c.mime_type == "application/json") {
|
||||
try {
|
||||
Json doc = Json::parse(c.data);
|
||||
if (!doc.is_object()) continue;
|
||||
const Json* r = doc.find("result");
|
||||
const Json& obj = r && r->is_object() ? *r : doc;
|
||||
out.result = obj;
|
||||
// CDP Page.captureScreenshot answers {"data": "<base64 png>"}.
|
||||
if (const Json* d = obj.find("data"); d && d->is_string() && out.screenshot_png.empty()) {
|
||||
std::string png;
|
||||
if (d->as_string().size() <= (SandboxCaps::kMaxScreenshotBytes / 3 + 1) * 4 &&
|
||||
security::crypto::Base64::decode(d->as_string(), png) &&
|
||||
png.size() <= SandboxCaps::kMaxScreenshotBytes)
|
||||
out.screenshot_png = std::move(png);
|
||||
}
|
||||
} catch (const clients::body::JsonError&) {
|
||||
}
|
||||
}
|
||||
}
|
||||
out.status = reply.status;
|
||||
out.result = ComputerUse::result_of(reply);
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ActionResult ComputerUse::send(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const Json& params) const {
|
||||
return run(*connector_->open(sandbox_name), cdp_method, params);
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::command(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const Json& params) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_command(cdp_method);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
return send(sandbox_name, cdp_method, params);
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::navigate(const std::string& sandbox_name,
|
||||
const std::string& url) const {
|
||||
@@ -102,8 +120,9 @@ ActionResult ComputerUse::navigate(const std::string& sandbox_name,
|
||||
ActionResult ComputerUse::click(const std::string& sandbox_name, int x, int y) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate_point(x, y);
|
||||
send(sandbox_name, "Input.dispatchMouseEvent", mouse_params("mousePressed", x, y));
|
||||
return send(sandbox_name, "Input.dispatchMouseEvent", mouse_params("mouseReleased", x, y));
|
||||
std::shared_ptr<DataPlane> plane = connector_->open(sandbox_name);
|
||||
run(*plane, "Input.dispatchMouseEvent", mouse_params("mousePressed", x, y));
|
||||
return run(*plane, "Input.dispatchMouseEvent", mouse_params("mouseReleased", x, y));
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::type_text(const std::string& sandbox_name,
|
||||
@@ -115,12 +134,57 @@ ActionResult ComputerUse::type_text(const std::string& sandbox_name,
|
||||
|
||||
ActionResult ComputerUse::screenshot(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
Json p = Json::object();
|
||||
p.set("format", Json("png"));
|
||||
ActionResult r = send(sandbox_name, "Page.captureScreenshot", p);
|
||||
ActionResult r = send(sandbox_name, "Page.captureScreenshot", screenshot_params());
|
||||
r.screenshot_png = screenshot_of(r.result);
|
||||
if (r.screenshot_png.empty())
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "no screenshot in the reply", endpoint_.name);
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "no screenshot in the reply", "sandbox-data-plane");
|
||||
return r;
|
||||
}
|
||||
|
||||
Json ComputerUse::health(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlaneReply r = connector_->open(sandbox_name)->health();
|
||||
Json o = Json::object();
|
||||
o.set("status", Json(r.status));
|
||||
o.set("body", Json(r.body.substr(0, 1024)));
|
||||
return o;
|
||||
}
|
||||
|
||||
namespace {
|
||||
Json reply_json(const DataPlaneReply& r) {
|
||||
Json o = Json::object();
|
||||
o.set("status", Json(r.status));
|
||||
o.set("reply", r.has_json ? r.json : Json(r.body.substr(0, 4096)));
|
||||
return o;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json ComputerUse::list_tabs(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return reply_json(connector_->open(sandbox_name)->list_tabs());
|
||||
}
|
||||
|
||||
Json ComputerUse::create_tab(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return reply_json(connector_->open(sandbox_name)->create_tab());
|
||||
}
|
||||
|
||||
Json ComputerUse::activate_tab(const std::string& sandbox_name, const std::string& tab_id) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_tab_id(tab_id);
|
||||
return reply_json(connector_->open(sandbox_name)->activate_tab(tab_id));
|
||||
}
|
||||
|
||||
Json ComputerUse::close_tab(const std::string& sandbox_name, const std::string& tab_id) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_tab_id(tab_id);
|
||||
return reply_json(connector_->open(sandbox_name)->close_tab(tab_id));
|
||||
}
|
||||
|
||||
std::unique_ptr<CdpSession> ComputerUse::open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return connector_->open_cdp(sandbox_name, std::move(options));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -1,34 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
// Computer use: browser actions in a computer use sandbox (navigate, click,
|
||||
// type, screenshot). Docs describe the action set and that each action is a
|
||||
// Chrome DevTools Protocol command ({"command","params"}); they send it to
|
||||
// the sandbox data plane with a signed JWT. This class carries the same CDP
|
||||
// commands as an application/json Chunk through sandboxEnvironments.execute,
|
||||
// the only sandbox call in the REST surface. Whether the server routes that
|
||||
// Chunk to the browser is unverified (SPEC Unverified).
|
||||
// type, screenshot) over the sandbox data plane, as Google documents it:
|
||||
// POST /cdp with {"command": "<CDP method>", "params": {...}} and a signed
|
||||
// JWT (see data_plane.h). The commands are Chrome DevTools Protocol names.
|
||||
// Tabs and the CDP WebSocket are exposed through the same connector.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct ActionResult {
|
||||
clients::body::Json result; // The CDP result object ({} when none).
|
||||
ExecutionResult exec; // Text, files and skip reasons of the reply.
|
||||
int status = 0; // HTTP status of the data plane call.
|
||||
std::string screenshot_png; // Raw PNG bytes, set by screenshot() only.
|
||||
};
|
||||
|
||||
class ComputerUse : public SandboxService {
|
||||
public:
|
||||
explicit ComputerUse(std::shared_ptr<GoogleClient> client);
|
||||
// connector null: one built from DataPlaneOptions::from_env().
|
||||
explicit ComputerUse(std::shared_ptr<GoogleClient> client,
|
||||
std::shared_ptr<DataPlaneConnector> connector = nullptr);
|
||||
|
||||
ActionResult navigate(const std::string& sandbox_name, const std::string& url) const;
|
||||
// Left click at viewport coordinates: mousePressed then mouseReleased.
|
||||
ActionResult click(const std::string& sandbox_name, int x, int y) const;
|
||||
ActionResult type_text(const std::string& sandbox_name, const std::string& text) const;
|
||||
ActionResult screenshot(const std::string& sandbox_name) const;
|
||||
// Any CDP command (Domain.method) on the active tab.
|
||||
ActionResult command(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const clients::body::Json& params) const;
|
||||
|
||||
// Data plane routes.
|
||||
clients::body::Json health(const std::string& sandbox_name) const;
|
||||
clients::body::Json list_tabs(const std::string& sandbox_name) const;
|
||||
clients::body::Json create_tab(const std::string& sandbox_name) const;
|
||||
clients::body::Json activate_tab(const std::string& sandbox_name, const std::string& tab_id) const;
|
||||
clients::body::Json close_tab(const std::string& sandbox_name, const std::string& tab_id) const;
|
||||
// The CDP WebSocket (not yet connected; call connect()).
|
||||
std::unique_ptr<CdpSession> open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options = {}) const;
|
||||
|
||||
static void validate_url(const std::string& url); // http(s) only, capped
|
||||
static void validate_point(int x, int y);
|
||||
@@ -38,10 +54,19 @@ class ComputerUse : public SandboxService {
|
||||
static clients::body::Json navigate_params(const std::string& url);
|
||||
static clients::body::Json mouse_params(const char* type, int x, int y);
|
||||
static clients::body::Json insert_text_params(const std::string& text);
|
||||
static clients::body::Json screenshot_params();
|
||||
// Pulls the result object out of a /cdp reply: {"result": {...}} or the
|
||||
// object itself (the reply shape is unverified, SPEC section 7).
|
||||
static clients::body::Json result_of(const DataPlaneReply& reply);
|
||||
// Base64 PNG in "data" of a captureScreenshot result; empty when absent or
|
||||
// over SandboxCaps::kMaxScreenshotBytes.
|
||||
static std::string screenshot_of(const clients::body::Json& result);
|
||||
|
||||
private:
|
||||
ActionResult send(const std::string& sandbox_name, const std::string& command,
|
||||
ActionResult send(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const clients::body::Json& params) const;
|
||||
|
||||
std::shared_ptr<DataPlaneConnector> connector_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
#include "google/sandbox/data_plane.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
#include "clients/transports/http_transport.h"
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kService = "sandbox-data-plane";
|
||||
|
||||
bool plain_host_char(char c) {
|
||||
return std::isalnum(static_cast<unsigned char>(c)) || c == '.' || c == '-';
|
||||
}
|
||||
|
||||
bool loopback_host(const std::string& host) {
|
||||
return host == "localhost" || host.rfind("127.", 0) == 0;
|
||||
}
|
||||
|
||||
clients::ClientConfig make_config(int timeout_ms) {
|
||||
clients::ClientConfig config;
|
||||
config.persist_cookies = false;
|
||||
config.follow_redirects = false;
|
||||
config.proxy_from_env = false;
|
||||
config.retry.max_attempts(1);
|
||||
config.timeouts.total = std::chrono::milliseconds(timeout_ms);
|
||||
config.timeouts.read = std::chrono::milliseconds(timeout_ms);
|
||||
return config;
|
||||
}
|
||||
|
||||
std::string excerpt(const std::string& body) {
|
||||
std::string out;
|
||||
for (char c : body.substr(0, SandboxCaps::kMaxErrorExcerpt))
|
||||
out.push_back(static_cast<unsigned char>(c) < 0x20 && c != '\n' ? ' ' : c);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void DataPlaneTarget::validate_port(const std::string& port) {
|
||||
if (port.empty() || port.size() > 5) throw GoogleError::invalid("port must be 1..65535");
|
||||
int v = 0;
|
||||
for (char c : port) {
|
||||
if (!std::isdigit(static_cast<unsigned char>(c))) throw GoogleError::invalid("port must be 1..65535");
|
||||
v = v * 10 + (c - '0');
|
||||
}
|
||||
if (v < 1 || v > 65535) throw GoogleError::invalid("port must be 1..65535");
|
||||
}
|
||||
|
||||
void DataPlaneTarget::validate_host(const std::string& host) {
|
||||
if (host.empty() || host.size() > SandboxCaps::kMaxHostChars)
|
||||
throw GoogleError::invalid("data plane host is empty or too long");
|
||||
for (char c : host)
|
||||
if (!plain_host_char(c)) throw GoogleError::invalid("data plane host has an invalid character");
|
||||
}
|
||||
|
||||
DataPlaneTarget DataPlaneTarget::from_environment(const SandboxEnvironment& env,
|
||||
const std::string& psc_endpoint,
|
||||
const std::string& loopback_override,
|
||||
const std::string& port) {
|
||||
validate_port(port);
|
||||
DataPlaneTarget t;
|
||||
t.port = port;
|
||||
const std::string& token = env.routing_token;
|
||||
if (token.empty()) throw GoogleError::invalid("the sandbox has no routing token (is it running?)");
|
||||
if (token.size() > SandboxCaps::kMaxRoutingTokenChars)
|
||||
throw GoogleError::invalid("routing token too long");
|
||||
for (char c : token)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f || c == ',')
|
||||
throw GoogleError::invalid("routing token has an invalid character");
|
||||
t.routing_token = token;
|
||||
|
||||
if (!loopback_override.empty()) {
|
||||
std::size_t colon = loopback_override.rfind(':');
|
||||
std::string host = loopback_override.substr(0, colon);
|
||||
if (colon == std::string::npos || !loopback_host(host))
|
||||
throw GoogleError::invalid("loopback override must be localhost:PORT or 127.x.x.x:PORT");
|
||||
validate_host(host);
|
||||
validate_port(loopback_override.substr(colon + 1));
|
||||
t.http_base = "http://" + loopback_override;
|
||||
t.ws_base = "ws://" + loopback_override;
|
||||
return t;
|
||||
}
|
||||
if (!psc_endpoint.empty()) {
|
||||
validate_host(psc_endpoint);
|
||||
t.http_base = "https://" + psc_endpoint;
|
||||
t.ws_base = "wss://" + psc_endpoint;
|
||||
} else if (!env.load_balancer_hostname.empty()) {
|
||||
validate_host(env.load_balancer_hostname);
|
||||
t.http_base = "https://" + env.load_balancer_hostname;
|
||||
t.ws_base = "wss://" + env.load_balancer_hostname;
|
||||
} else if (!env.load_balancer_ip.empty()) {
|
||||
validate_host(env.load_balancer_ip);
|
||||
t.http_base = "http://" + env.load_balancer_ip;
|
||||
t.ws_base = "ws://" + env.load_balancer_ip;
|
||||
} else {
|
||||
throw GoogleError::invalid(
|
||||
"no data plane address: connectionInfo has no loadBalancerHostname or loadBalancerIp "
|
||||
"(VPC-SC sandboxes need a psc_endpoint for connectionInfo.serviceAttachment)");
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
DataPlane::DataPlane(DataPlaneTarget target, std::shared_ptr<ITokenSource> tokens,
|
||||
std::unique_ptr<clients::Transport> transport, int timeout_ms)
|
||||
: target_(std::move(target)),
|
||||
tokens_(std::move(tokens)),
|
||||
client_(transport ? std::move(transport)
|
||||
: std::make_unique<clients::transports::HttpTransport>(),
|
||||
make_config(timeout_ms > 0 ? timeout_ms : SandboxCaps::kDataPlaneTimeoutMs)),
|
||||
timeout_ms_(timeout_ms > 0 ? timeout_ms : SandboxCaps::kDataPlaneTimeoutMs) {
|
||||
if (!tokens_) throw GoogleError::invalid("a token source is required");
|
||||
if (timeout_ms_ > SandboxCaps::kMaxDataPlaneTimeoutMs)
|
||||
throw GoogleError::invalid("timeout over " +
|
||||
std::to_string(SandboxCaps::kMaxDataPlaneTimeoutMs) + " ms");
|
||||
}
|
||||
|
||||
void DataPlane::validate_path(const std::string& path) {
|
||||
if (path.empty() || path[0] != '/' || path.size() > SandboxCaps::kMaxDataPathChars)
|
||||
throw GoogleError::invalid("data plane path must start with / and stay under " +
|
||||
std::to_string(SandboxCaps::kMaxDataPathChars) + " chars");
|
||||
for (char c : path)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f)
|
||||
throw GoogleError::invalid("data plane path has an invalid character");
|
||||
if (path.find("..") != std::string::npos) throw GoogleError::invalid("data plane path has ..");
|
||||
}
|
||||
|
||||
void DataPlane::validate_tab_id(const std::string& id) {
|
||||
if (id.empty() || id.size() > SandboxCaps::kMaxTabIdChars || !valid_segment(id))
|
||||
throw GoogleError::invalid("tab id must be 1.." + std::to_string(SandboxCaps::kMaxTabIdChars) +
|
||||
" of [A-Za-z0-9._-]");
|
||||
}
|
||||
|
||||
void DataPlane::validate_command(const std::string& command) {
|
||||
// Chrome DevTools Protocol method: Domain.method.
|
||||
std::size_t dot = command.find('.');
|
||||
bool ok = !command.empty() && command.size() <= SandboxCaps::kMaxCdpMethodChars &&
|
||||
dot != std::string::npos && dot > 0 && dot + 1 < command.size() &&
|
||||
command.find('.', dot + 1) == std::string::npos;
|
||||
for (char c : command)
|
||||
if (c != '.' && !std::isalnum(static_cast<unsigned char>(c))) ok = false;
|
||||
if (!ok) throw GoogleError::invalid("CDP command must look like Domain.method");
|
||||
}
|
||||
|
||||
Json DataPlane::cdp_body(const std::string& command, const Json& params) {
|
||||
validate_command(command);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
Json b = Json::object();
|
||||
b.set("command", Json(command));
|
||||
b.set("params", params);
|
||||
return b;
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::send(clients::Method method, const std::string& path,
|
||||
const Json* body) const {
|
||||
validate_path(path);
|
||||
for (int attempt = 0;; ++attempt) {
|
||||
std::string jwt = tokens_->token();
|
||||
DataPlaneReply out;
|
||||
clients::ClientResponse response;
|
||||
try {
|
||||
clients::ClientRequest req(method, clients::Url::parse(target_.http_base + path));
|
||||
req.header("Authorization", "Bearer " + jwt);
|
||||
req.header("X-Sandbox-Routing-Token", target_.routing_token);
|
||||
req.header("X-Sandbox-Port", target_.port);
|
||||
if (body) req.json(*body);
|
||||
++requests_;
|
||||
response = client_.send_blocking(std::move(req));
|
||||
} catch (const GoogleError&) {
|
||||
throw;
|
||||
} catch (const std::exception& e) {
|
||||
throw GoogleError(502, "UNAVAILABLE", e.what(), kService);
|
||||
}
|
||||
if (response.status() == 401 && attempt < SandboxCaps::kMaxAuthRetries) {
|
||||
tokens_->invalidate();
|
||||
++refreshes_;
|
||||
continue;
|
||||
}
|
||||
if (response.body().size() > SandboxCaps::kMaxDataReplyBytes)
|
||||
throw GoogleError(502, "TOO_BIG",
|
||||
"data plane reply over " + std::to_string(SandboxCaps::kMaxDataReplyBytes) +
|
||||
" bytes",
|
||||
kService);
|
||||
if (!response.is_success())
|
||||
throw GoogleError(response.status() >= 400 && response.status() < 600 ? response.status() : 502,
|
||||
"DATA_PLANE_" + std::to_string(response.status()), excerpt(response.body()),
|
||||
kService);
|
||||
out.status = response.status();
|
||||
out.body = response.body();
|
||||
try {
|
||||
out.json = Json::parse(out.body);
|
||||
out.has_json = true;
|
||||
} catch (const clients::body::JsonError&) {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::health() const { return send(clients::Method::kGet, "/", nullptr); }
|
||||
DataPlaneReply DataPlane::create_tab() const { return send(clients::Method::kPost, "/tabs", nullptr); }
|
||||
DataPlaneReply DataPlane::list_tabs() const { return send(clients::Method::kGet, "/tabs", nullptr); }
|
||||
|
||||
DataPlaneReply DataPlane::activate_tab(const std::string& tab_id) const {
|
||||
validate_tab_id(tab_id);
|
||||
return send(clients::Method::kPost, "/tabs/" + tab_id + "/activate", nullptr);
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::close_tab(const std::string& tab_id) const {
|
||||
validate_tab_id(tab_id);
|
||||
return send(clients::Method::kDelete, "/tabs/" + tab_id, nullptr);
|
||||
}
|
||||
|
||||
std::string DataPlane::cdp_ws_endpoint() const {
|
||||
DataPlaneReply r = send(clients::Method::kGet, "/cdp_ws_endpoint", nullptr);
|
||||
const Json* e = r.has_json && r.json.is_object() ? r.json.find("endpoint") : nullptr;
|
||||
if (!e || !e->is_string() || e->as_string().empty())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "cdp_ws_endpoint reply has no endpoint", kService);
|
||||
return e->as_string();
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::cdp(const std::string& command, const Json& params) const {
|
||||
Json b = cdp_body(command, params);
|
||||
return send(clients::Method::kPost, "/cdp", &b);
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::cdps(const std::vector<CdpCommand>& commands) const {
|
||||
if (commands.empty() || commands.size() > SandboxCaps::kMaxBatchCommands)
|
||||
throw GoogleError::invalid("commands must be 1.." +
|
||||
std::to_string(SandboxCaps::kMaxBatchCommands));
|
||||
Json arr = Json::array();
|
||||
for (const CdpCommand& c : commands) arr.push(cdp_body(c.command, c.params));
|
||||
Json b = Json::object();
|
||||
b.set("commands", arr);
|
||||
return send(clients::Method::kPost, "/cdps", &b);
|
||||
}
|
||||
|
||||
std::string DataPlane::ws_url(const std::string& endpoint_path) const {
|
||||
if (endpoint_path.empty() || endpoint_path.size() > 512)
|
||||
throw GoogleError::invalid("websocket endpoint path is empty or too long");
|
||||
for (char c : endpoint_path)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f)
|
||||
throw GoogleError::invalid("websocket endpoint path has an invalid character");
|
||||
if (endpoint_path.find("..") != std::string::npos || endpoint_path.find("://") != std::string::npos)
|
||||
throw GoogleError::invalid("websocket endpoint path must be a plain path");
|
||||
std::string p = endpoint_path[0] == '/' ? endpoint_path.substr(1) : endpoint_path;
|
||||
return target_.ws_base + "/" + p;
|
||||
}
|
||||
|
||||
live::WsHeaders DataPlane::ws_headers(const std::string& browser_port) const {
|
||||
DataPlaneTarget::validate_port(browser_port);
|
||||
std::string jwt = tokens_->token();
|
||||
for (char c : jwt)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f || c == ',')
|
||||
throw GoogleError::invalid("token has an invalid character for a header");
|
||||
live::WsHeaders h;
|
||||
h.emplace_back("Sec-WebSocket-Protocol",
|
||||
"v1.stream, " + jwt + ", " + target_.routing_token + ", " + browser_port);
|
||||
return h;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
// The sandbox data plane: the HTTP proxy in front of a running sandbox, as
|
||||
// the Google Python SDK (`Sandboxes.send_command`, google-cloud-aiplatform
|
||||
// 2.6.0) and the computer use quickstart document it. Not in Discovery.
|
||||
//
|
||||
// address https://{connectionInfo.loadBalancerHostname} (or
|
||||
// http://{connectionInfo.loadBalancerIp}, or https://{psc_endpoint}
|
||||
// for VPC-SC sandboxes)
|
||||
// headers Authorization: Bearer <signed JWT>
|
||||
// X-Sandbox-Routing-Token: <connectionInfo.routingToken>
|
||||
// X-Sandbox-Port: <container port, default 8080>
|
||||
// routes GET / POST /tabs GET /tabs POST /tabs/{id}/activate
|
||||
// DELETE /tabs/{id} GET /cdp_ws_endpoint POST /cdp POST /cdps
|
||||
//
|
||||
// Every call is capped (reply size, time, one JWT refresh on 401). A JWT is
|
||||
// sent only to the address derived from the sandbox's own connectionInfo.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "clients/client.h"
|
||||
#include "google/live/ws_handshake.h"
|
||||
#include "google/sandbox/jwt.h"
|
||||
#include "google/sandbox/types.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct DataPlaneTarget {
|
||||
std::string http_base; // scheme://host[:port], no trailing slash
|
||||
std::string ws_base; // ws(s)://host[:port]
|
||||
std::string routing_token;
|
||||
std::string port = "8080"; // X-Sandbox-Port (one of the template's ports)
|
||||
|
||||
// Throws GoogleError::invalid when the environment has no usable address or
|
||||
// routing token. `psc_endpoint` wins (VPC-SC), then the hostname (https),
|
||||
// then the IP (http, as the SDK does). `loopback_override` ("127.0.0.1:PORT"
|
||||
// or "localhost:PORT") replaces the address for local test servers and is
|
||||
// refused for any non-loopback host.
|
||||
static DataPlaneTarget from_environment(const SandboxEnvironment& env,
|
||||
const std::string& psc_endpoint = "",
|
||||
const std::string& loopback_override = "",
|
||||
const std::string& port = "8080");
|
||||
static void validate_port(const std::string& port);
|
||||
static void validate_host(const std::string& host);
|
||||
};
|
||||
|
||||
struct DataPlaneReply {
|
||||
int status = 0;
|
||||
std::string body;
|
||||
clients::body::Json json; // Parsed body; null when the body is not JSON.
|
||||
bool has_json = false;
|
||||
};
|
||||
|
||||
struct CdpCommand {
|
||||
std::string command; // e.g. "Page.navigate"
|
||||
clients::body::Json params = clients::body::Json::object();
|
||||
};
|
||||
|
||||
class DataPlane {
|
||||
public:
|
||||
// transport null: the real HTTP(S) transport. Redirects and retries are off
|
||||
// (a JWT must not follow a redirect; CDP commands are not idempotent).
|
||||
DataPlane(DataPlaneTarget target, std::shared_ptr<ITokenSource> tokens,
|
||||
std::unique_ptr<clients::Transport> transport = nullptr, int timeout_ms = 0);
|
||||
|
||||
// GET /
|
||||
DataPlaneReply health() const;
|
||||
// POST /tabs (no body), GET /tabs
|
||||
DataPlaneReply create_tab() const;
|
||||
DataPlaneReply list_tabs() const;
|
||||
// POST /tabs/{id}/activate, DELETE /tabs/{id}
|
||||
DataPlaneReply activate_tab(const std::string& tab_id) const;
|
||||
DataPlaneReply close_tab(const std::string& tab_id) const;
|
||||
// GET /cdp_ws_endpoint -> the "endpoint" path string of the reply.
|
||||
std::string cdp_ws_endpoint() const;
|
||||
// POST /cdp {"command","params"}: one command on the active tab.
|
||||
DataPlaneReply cdp(const std::string& command, const clients::body::Json& params) const;
|
||||
// POST /cdps: several commands on the active tab (body shape unverified).
|
||||
DataPlaneReply cdps(const std::vector<CdpCommand>& commands) const;
|
||||
|
||||
// The URL and upgrade headers of the CDP WebSocket for `endpoint_path`.
|
||||
std::string ws_url(const std::string& endpoint_path) const;
|
||||
// Sec-WebSocket-Protocol: "v1.stream, <jwt>, <routing token>, 9222".
|
||||
live::WsHeaders ws_headers(const std::string& browser_port = "9222") const;
|
||||
|
||||
const DataPlaneTarget& target() const { return target_; }
|
||||
ITokenSource& tokens() const { return *tokens_; }
|
||||
|
||||
static void validate_path(const std::string& path);
|
||||
static void validate_tab_id(const std::string& id);
|
||||
static clients::body::Json cdp_body(const std::string& command,
|
||||
const clients::body::Json& params);
|
||||
static void validate_command(const std::string& command);
|
||||
|
||||
int requests_sent() const { return requests_; }
|
||||
int auth_refreshes() const { return refreshes_; }
|
||||
|
||||
private:
|
||||
DataPlaneReply send(clients::Method method, const std::string& path,
|
||||
const clients::body::Json* body) const;
|
||||
|
||||
DataPlaneTarget target_;
|
||||
std::shared_ptr<ITokenSource> tokens_;
|
||||
mutable clients::Client client_;
|
||||
int timeout_ms_;
|
||||
mutable int requests_ = 0;
|
||||
mutable int refreshes_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
|
||||
#include "google/credentials.h"
|
||||
#include "google/errors.h"
|
||||
#include "google/sandbox/sandboxes.h"
|
||||
#include "google/service_account.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
DataPlaneOptions DataPlaneOptions::from_env() {
|
||||
DataPlaneOptions o;
|
||||
o.service_account_email = Credentials::env("GOOGLE_SERVICE_ACCOUNT_EMAIL");
|
||||
if (o.service_account_email.empty()) {
|
||||
std::string path = Credentials::env("GOOGLE_APPLICATION_CREDENTIALS");
|
||||
if (!path.empty()) {
|
||||
try {
|
||||
o.service_account_email = ServiceAccountKey::load_file(path).client_email;
|
||||
} catch (const std::exception&) {
|
||||
// No usable key file: the data plane stays unavailable, loudly, at use.
|
||||
}
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
DataPlaneConnector::DataPlaneConnector(std::shared_ptr<GoogleClient> client,
|
||||
DataPlaneOptions options,
|
||||
std::shared_ptr<ITokenSource> tokens)
|
||||
: SandboxService(client), options_(std::move(options)), tokens_(std::move(tokens)) {
|
||||
if (!tokens_ && !options_.service_account_email.empty())
|
||||
tokens_ = std::make_shared<IamJwtSource>(client, options_.service_account_email,
|
||||
options_.jwt_ttl_seconds);
|
||||
}
|
||||
|
||||
SandboxEnvironment DataPlaneConnector::environment(const std::string& sandbox_name) const {
|
||||
return Sandboxes(client_).get(sandbox_name);
|
||||
}
|
||||
|
||||
std::shared_ptr<DataPlane> DataPlaneConnector::open(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
if (!client_->configured()) throw NotConfiguredError("sandbox-data-plane");
|
||||
// No signer is a configuration gap, not bad input: 503 like a missing credential.
|
||||
if (!tokens_) throw NotConfiguredError("sandbox-data-plane (set GOOGLE_SERVICE_ACCOUNT_EMAIL)");
|
||||
SandboxEnvironment env = environment(sandbox_name);
|
||||
if (!env.running())
|
||||
throw GoogleError(409, "FAILED_PRECONDITION",
|
||||
"sandbox is " + (env.state.empty() ? std::string("in an unknown state") : env.state) +
|
||||
", not STATE_RUNNING",
|
||||
"sandbox-data-plane");
|
||||
DataPlaneTarget target = DataPlaneTarget::from_environment(
|
||||
env, options_.psc_endpoint, options_.loopback_override, options_.port);
|
||||
return std::make_shared<DataPlane>(std::move(target), tokens_, nullptr, options_.timeout_ms);
|
||||
}
|
||||
|
||||
std::unique_ptr<CdpSession> DataPlaneConnector::open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options) const {
|
||||
return std::make_unique<CdpSession>(open(sandbox_name), std::move(options));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
// Opens the data plane of a named sandbox: reads the sandbox resource (its
|
||||
// connectionInfo) through the control plane, derives the address, and signs
|
||||
// JWTs with a service account. One connector per service account.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "google/client.h"
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
#include "google/sandbox/data_plane.h"
|
||||
#include "google/sandbox/jwt.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct DataPlaneOptions {
|
||||
// Signs the JWTs. Empty: the data plane is unavailable (calls throw 400).
|
||||
std::string service_account_email;
|
||||
std::string psc_endpoint; // VPC-SC sandboxes only
|
||||
std::string loopback_override; // tests: "127.0.0.1:PORT"
|
||||
std::string port = "8080";
|
||||
long jwt_ttl_seconds = 3600;
|
||||
int timeout_ms = 0;
|
||||
|
||||
// GOOGLE_SERVICE_ACCOUNT_EMAIL, else client_email of the key file named by
|
||||
// GOOGLE_APPLICATION_CREDENTIALS (only the email is read, never the key).
|
||||
static DataPlaneOptions from_env();
|
||||
};
|
||||
|
||||
class DataPlaneConnector : public SandboxService {
|
||||
public:
|
||||
// tokens null: IamJwtSource over `client` for options.service_account_email.
|
||||
DataPlaneConnector(std::shared_ptr<GoogleClient> client, DataPlaneOptions options = {},
|
||||
std::shared_ptr<ITokenSource> tokens = nullptr);
|
||||
|
||||
// Order: validate the name, require a credential (503), require a signer
|
||||
// (400), then read the sandbox. Nothing leaves the process before that.
|
||||
std::shared_ptr<DataPlane> open(const std::string& sandbox_name) const;
|
||||
std::unique_ptr<CdpSession> open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options = {}) const;
|
||||
|
||||
const DataPlaneOptions& options() const { return options_; }
|
||||
|
||||
protected:
|
||||
// The sandbox resource for the name. Virtual so a test can supply a local
|
||||
// environment pointing at a loopback server.
|
||||
virtual SandboxEnvironment environment(const std::string& sandbox_name) const;
|
||||
|
||||
private:
|
||||
DataPlaneOptions options_;
|
||||
std::shared_ptr<ITokenSource> tokens_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "google/sandbox/jwt.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
long wall_clock() {
|
||||
return static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
IamJwtSource::IamJwtSource(std::shared_ptr<GoogleClient> client,
|
||||
std::string service_account_email, long ttl_seconds, Clock clock,
|
||||
ServiceEndpoint endpoint)
|
||||
: client_(std::move(client)),
|
||||
email_(std::move(service_account_email)),
|
||||
ttl_(ttl_seconds),
|
||||
clock_(clock ? std::move(clock) : Clock(wall_clock)),
|
||||
endpoint_(std::move(endpoint)) {
|
||||
validate_email(email_);
|
||||
if (ttl_ < SandboxCaps::kMinJwtTtlSeconds || ttl_ > SandboxCaps::kMaxJwtTtlSeconds)
|
||||
throw GoogleError::invalid("jwt ttl must be " + std::to_string(SandboxCaps::kMinJwtTtlSeconds) +
|
||||
".." + std::to_string(SandboxCaps::kMaxJwtTtlSeconds) + " seconds");
|
||||
}
|
||||
|
||||
void IamJwtSource::validate_email(const std::string& email) {
|
||||
std::size_t at = email.find('@');
|
||||
if (email.empty() || email.size() > SandboxCaps::kMaxEmailChars || at == std::string::npos ||
|
||||
at == 0 || at + 1 >= email.size() || email.find('@', at + 1) != std::string::npos)
|
||||
throw GoogleError::invalid(
|
||||
"service account email must look like name@project.iam.gserviceaccount.com");
|
||||
for (char c : email)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '.' || c == '-' || c == '_' || c == '@'))
|
||||
throw GoogleError::invalid("service account email has an invalid character");
|
||||
}
|
||||
|
||||
Json IamJwtSource::claims(const std::string& email, long issued_at, long ttl, long nonce) {
|
||||
Json c = Json::object();
|
||||
c.set("iat", Json(issued_at));
|
||||
c.set("exp", Json(issued_at + ttl));
|
||||
c.set("iss", Json(email));
|
||||
c.set("sub", Json(email));
|
||||
c.set("nonce", Json(nonce));
|
||||
c.set("aud", Json("https://aiplatform.googleapis.com/"));
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string IamJwtSource::sign_path(const std::string& email) {
|
||||
return "/projects/-/serviceAccounts/" + email + ":signJwt";
|
||||
}
|
||||
|
||||
ServiceEndpoint IamJwtSource::iam_endpoint() {
|
||||
return endpoints::custom("iamcredentials", "https://iamcredentials.googleapis.com/v1");
|
||||
}
|
||||
|
||||
std::string IamJwtSource::token() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
long now = clock_();
|
||||
if (!jwt_.empty() && expires_at_ - now > SandboxCaps::kJwtRefreshMarginSeconds) return jwt_;
|
||||
static thread_local std::mt19937 rng{std::random_device{}()};
|
||||
long nonce = static_cast<long>(rng() % 1000000000u) + 1;
|
||||
Json body = Json::object();
|
||||
body.set("payload", Json(claims(email_, now, ttl_, nonce).dump()));
|
||||
Json reply = client_->request(clients::Method::kPost, endpoint_, sign_path(email_), &body);
|
||||
const Json* signed_jwt = reply.is_object() ? reply.find("signedJwt") : nullptr;
|
||||
if (!signed_jwt || !signed_jwt->is_string() || signed_jwt->as_string().empty())
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "signJwt returned no signedJwt", "iamcredentials");
|
||||
jwt_ = signed_jwt->as_string();
|
||||
expires_at_ = now + ttl_;
|
||||
++mints_;
|
||||
return jwt_;
|
||||
}
|
||||
|
||||
void IamJwtSource::invalidate() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
jwt_.clear();
|
||||
expires_at_ = 0;
|
||||
}
|
||||
|
||||
int IamJwtSource::mints() const {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return mints_;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
// Sandbox data plane tokens. Google's SDK (`generate_access_token`) asks the
|
||||
// IAM Credentials API to sign a small JWT with a service account
|
||||
// (projects.serviceAccounts.signJwt) and sends that JWT to the sandbox proxy.
|
||||
// The audience is "https://aiplatform.googleapis.com/". The caller needs
|
||||
// roles/iam.serviceAccountTokenCreator on the service account.
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "clients/body/json.h"
|
||||
#include "google/client.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
// Where a data plane JWT comes from. Production: IamJwtSource. Tests and
|
||||
// custom signers implement it directly.
|
||||
class ITokenSource {
|
||||
public:
|
||||
virtual ~ITokenSource() = default;
|
||||
virtual std::string token() = 0; // Valid for at least the refresh margin.
|
||||
virtual void invalidate() = 0; // The next token() mints a fresh JWT.
|
||||
};
|
||||
|
||||
class IamJwtSource : public ITokenSource {
|
||||
public:
|
||||
using Clock = std::function<long()>; // Seconds since the epoch.
|
||||
|
||||
// ttl: kMinJwtTtlSeconds..kMaxJwtTtlSeconds. Throws GoogleError::invalid.
|
||||
IamJwtSource(std::shared_ptr<GoogleClient> client, std::string service_account_email,
|
||||
long ttl_seconds = 3600, Clock clock = nullptr,
|
||||
ServiceEndpoint endpoint = iam_endpoint());
|
||||
|
||||
std::string token() override;
|
||||
void invalidate() override;
|
||||
|
||||
int mints() const; // How many signJwt calls were made.
|
||||
|
||||
static void validate_email(const std::string& email);
|
||||
// The JWT payload exactly as the SDK builds it (iat, exp, iss, sub, nonce, aud).
|
||||
static clients::body::Json claims(const std::string& email, long issued_at, long ttl,
|
||||
long nonce);
|
||||
// POST path under the IAM Credentials endpoint.
|
||||
static std::string sign_path(const std::string& email);
|
||||
static ServiceEndpoint iam_endpoint();
|
||||
|
||||
private:
|
||||
std::shared_ptr<GoogleClient> client_;
|
||||
std::string email_;
|
||||
long ttl_;
|
||||
Clock clock_;
|
||||
ServiceEndpoint endpoint_;
|
||||
mutable std::mutex mu_;
|
||||
std::string jwt_;
|
||||
long expires_at_ = 0;
|
||||
int mints_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -132,6 +132,31 @@ SandboxNames::Parts SandboxNames::parse_sandbox(const std::string& name) {
|
||||
return p;
|
||||
}
|
||||
|
||||
SandboxNames::OperationParts SandboxNames::parse_operation_parent(const std::string& resource) {
|
||||
OperationParts o;
|
||||
Parts p;
|
||||
o.resource = resource;
|
||||
if (parse(resource, 8, p)) o.family = "sandboxEnvironments";
|
||||
else if (parse(resource, 8, p, "sandboxEnvironmentTemplates")) o.family = "sandboxEnvironmentTemplates";
|
||||
else if (parse(resource, 8, p, "sandboxEnvironmentSnapshots")) o.family = "sandboxEnvironmentSnapshots";
|
||||
else
|
||||
throw GoogleError::invalid(
|
||||
"resource must be a sandbox, template or snapshot name "
|
||||
"(projects/{p}/locations/{l}/reasoningEngines/{e}/{collection}/{id})");
|
||||
o.location = p.location;
|
||||
return o;
|
||||
}
|
||||
|
||||
SandboxNames::OperationParts SandboxNames::parse_operation(const std::string& name) {
|
||||
std::size_t at = name.rfind("/operations/");
|
||||
if (at == std::string::npos)
|
||||
throw GoogleError::invalid("operation must look like {sandbox|template|snapshot}/operations/{id}");
|
||||
OperationParts o = parse_operation_parent(name.substr(0, at));
|
||||
o.id = name.substr(at + 12);
|
||||
if (!valid_segment(o.id)) throw GoogleError::invalid("operation id must be 1..128 of [A-Za-z0-9._-]");
|
||||
return o;
|
||||
}
|
||||
|
||||
std::string SandboxNames::engine_of(const std::string& sandbox_name) {
|
||||
Parts p = parse_sandbox(sandbox_name);
|
||||
return "projects/" + p.project + "/locations/" + p.location +
|
||||
|
||||
@@ -31,6 +31,18 @@ struct SandboxNames {
|
||||
static Parts parse_template(const std::string& name);
|
||||
static Parts parse_snapshot(const std::string& name);
|
||||
|
||||
// Long-running operation of a sandbox, template or snapshot:
|
||||
// {resource}/operations/{id}.
|
||||
struct OperationParts {
|
||||
std::string resource; // The sandbox, template or snapshot name.
|
||||
std::string family; // sandboxEnvironments, sandboxEnvironmentTemplates, ...Snapshots
|
||||
std::string location;
|
||||
std::string id;
|
||||
};
|
||||
static OperationParts parse_operation(const std::string& name);
|
||||
// Any of the three resource names (the parent of an operations list).
|
||||
static OperationParts parse_operation_parent(const std::string& resource);
|
||||
|
||||
// "projects/p/locations/l/reasoningEngines/e" from a sandbox name.
|
||||
static std::string engine_of(const std::string& sandbox_name);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "google/sandbox/operations.h"
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
Operations::Operations(std::shared_ptr<GoogleClient> client)
|
||||
: SandboxService(std::move(client)) {}
|
||||
|
||||
std::string Operations::wait_query(int timeout_seconds) {
|
||||
if (timeout_seconds < 1 || timeout_seconds > SandboxCaps::kMaxOperationWaitSeconds)
|
||||
throw GoogleError::invalid("timeout must be 1.." +
|
||||
std::to_string(SandboxCaps::kMaxOperationWaitSeconds) + " seconds");
|
||||
return "?timeout=" + std::to_string(timeout_seconds) + "s";
|
||||
}
|
||||
|
||||
Operation Operations::get(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
return Operation::from_json(
|
||||
client_->get_json(endpoint_in(parts.location), "/" + operation_name));
|
||||
}
|
||||
|
||||
Operation Operations::wait(const std::string& operation_name, int timeout_seconds) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
std::string query = wait_query(timeout_seconds);
|
||||
Json body = Json::object();
|
||||
return Operation::from_json(client_->request(clients::Method::kPost, endpoint_in(parts.location),
|
||||
"/" + operation_name + ":wait" + query, &body));
|
||||
}
|
||||
|
||||
void Operations::cancel(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
Json body = Json::object();
|
||||
client_->request(clients::Method::kPost, endpoint_in(parts.location),
|
||||
"/" + operation_name + ":cancel", &body);
|
||||
}
|
||||
|
||||
void Operations::remove(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
client_->delete_json(endpoint_in(parts.location), "/" + operation_name);
|
||||
}
|
||||
|
||||
std::vector<Operation> Operations::list(const std::string& resource_name, int page_size) const {
|
||||
auto parts = SandboxNames::parse_operation_parent(resource_name);
|
||||
if (page_size == 0) page_size = SandboxCaps::kDefaultPageSize;
|
||||
if (page_size < 1 || page_size > SandboxCaps::kMaxPageSize)
|
||||
throw GoogleError::invalid("page_size must be 1.." + std::to_string(SandboxCaps::kMaxPageSize));
|
||||
Json items = list_all(*client_, beta_endpoint_in(parts.location),
|
||||
"/" + resource_name + "/operations?pageSize=" + std::to_string(page_size),
|
||||
"operations", SandboxCaps::kMaxListPages);
|
||||
std::vector<Operation> out;
|
||||
for (const Json& item : items.as_array()) out.push_back(Operation::from_json(item));
|
||||
return out;
|
||||
}
|
||||
|
||||
Json Operations::to_json(const Operation& op) {
|
||||
Json o = Json::object();
|
||||
o.set("name", Json(op.name));
|
||||
o.set("done", Json(op.done));
|
||||
o.set("failed", Json(op.failed()));
|
||||
o.set("error_code", Json(op.error_code));
|
||||
o.set("error_message", Json(op.error_message));
|
||||
return o;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
// The operations methods Discovery offers on the three sandbox resources
|
||||
// (sandboxEnvironments, sandboxEnvironmentTemplates, sandboxEnvironmentSnapshots):
|
||||
// get, wait, cancel, delete (v1) and list (v1beta1 only). Lifecycle calls in
|
||||
// this module already poll operations internally; these are the explicit,
|
||||
// caller-driven forms. cancel and remove(delete) are explicit methods.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "google/operation.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
class Operations : public SandboxService {
|
||||
public:
|
||||
explicit Operations(std::shared_ptr<GoogleClient> client);
|
||||
|
||||
// GET {name}
|
||||
Operation get(const std::string& operation_name) const;
|
||||
// POST {name}:wait?timeout=Ns. Server-side wait of at most
|
||||
// SandboxCaps::kMaxOperationWaitSeconds (1..), then returns the operation as
|
||||
// it is (done or not).
|
||||
Operation wait(const std::string& operation_name, int timeout_seconds) const;
|
||||
// POST {name}:cancel (best effort on Google's side).
|
||||
void cancel(const std::string& operation_name) const;
|
||||
// DELETE {name}: removes the operation record, not the resource.
|
||||
void remove(const std::string& operation_name) const;
|
||||
// GET {resource}/operations (v1beta1), pages capped.
|
||||
std::vector<Operation> list(const std::string& resource_name, int page_size = 0) const;
|
||||
|
||||
static clients::body::Json to_json(const Operation& op);
|
||||
static std::string wait_query(int timeout_seconds); // "?timeout=30s"
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
@@ -11,15 +12,24 @@ SandboxService::SandboxService(std::shared_ptr<GoogleClient> client)
|
||||
endpoint_.name = "aiplatform-sandbox";
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::endpoint_in(const std::string& location) {
|
||||
void SandboxService::use_base_url(const std::string& base) {
|
||||
if (!base.empty() && base.rfind("http://", 0) != 0 && base.rfind("https://", 0) != 0)
|
||||
throw GoogleError::invalid("base url must be http:// or https://");
|
||||
if (!base.empty() && base.back() == '/') throw GoogleError::invalid("base url ends with /");
|
||||
base_url_ = base;
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::endpoint_in(const std::string& location) const {
|
||||
ServiceEndpoint e = endpoints::aiplatform(location);
|
||||
e.name = "aiplatform-sandbox";
|
||||
if (!base_url_.empty()) e.base_url = base_url_ + "/v1";
|
||||
return e;
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::beta_endpoint_in(const std::string& location) {
|
||||
ServiceEndpoint SandboxService::beta_endpoint_in(const std::string& location) const {
|
||||
ServiceEndpoint e = endpoints::aiplatform_beta(location);
|
||||
e.name = "aiplatform-sandbox-beta";
|
||||
if (!base_url_.empty()) e.base_url = base_url_ + "/v1beta1";
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,17 +18,25 @@ class SandboxService : public Service {
|
||||
public:
|
||||
explicit SandboxService(std::shared_ptr<GoogleClient> client);
|
||||
|
||||
// Points every call at `base` (scheme://host[:port], no path) instead of
|
||||
// https://{location}-aiplatform.googleapis.com. For tests against a local
|
||||
// server and for proxies; empty restores the default. Only http(s) URLs.
|
||||
void use_base_url(const std::string& base);
|
||||
|
||||
protected:
|
||||
// https://{location}-aiplatform.googleapis.com/v1 (every call but bidiExecute).
|
||||
static ServiceEndpoint endpoint_in(const std::string& location);
|
||||
ServiceEndpoint endpoint_in(const std::string& location) const;
|
||||
// .../v1beta1: bidiExecute exists only there (see calls.h).
|
||||
static ServiceEndpoint beta_endpoint_in(const std::string& location);
|
||||
ServiceEndpoint beta_endpoint_in(const std::string& location) const;
|
||||
|
||||
// POST {sandbox}:execute with the given inputs; returns the raw reply.
|
||||
clients::body::Json execute_raw(const std::string& sandbox_name,
|
||||
const std::vector<Chunk>& inputs) const;
|
||||
ExecutionResult execute(const std::string& sandbox_name,
|
||||
const std::vector<Chunk>& inputs) const;
|
||||
|
||||
private:
|
||||
std::string base_url_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -27,8 +27,8 @@ ExecutionResult ShellRunner::run_capped(const std::string& sandbox_name,
|
||||
std::size_t max_command) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate(request, max_command);
|
||||
return execute(sandbox_name, {ChunkCodec::shell_input(
|
||||
request.command, request.cwd, request.timeout_seconds)});
|
||||
return execute(sandbox_name, ChunkCodec::shell_inputs(request.command, request.cwd,
|
||||
request.timeout_seconds));
|
||||
}
|
||||
|
||||
ExecutionResult ShellRunner::run(const std::string& sandbox_name,
|
||||
|
||||
@@ -14,7 +14,6 @@ ExecutionResult StreamExecutor::run(
|
||||
const std::function<bool(const std::vector<Chunk>&)>& on_event) const {
|
||||
SandboxNames::Parts parts = SandboxNames::parse_sandbox(sandbox_name);
|
||||
if (inputs.empty()) throw GoogleError::invalid("no inputs");
|
||||
if (inputs.size() > 16) throw GoogleError::invalid("more than 16 input chunks");
|
||||
Json body = ChunkCodec::request(inputs);
|
||||
Json merged = Json::array();
|
||||
std::size_t events = 0;
|
||||
|
||||
@@ -69,8 +69,9 @@ def methods(doc):
|
||||
|
||||
|
||||
def schema_id(service, version, name):
|
||||
prefix = {"aiplatform": "GoogleCloudAiplatform"}[service]
|
||||
return prefix + version[0].upper() + version[1:] + name
|
||||
if service == "aiplatform":
|
||||
return "GoogleCloudAiplatform" + version[0].upper() + version[1:] + name
|
||||
return name # iamcredentials schemas carry no prefix
|
||||
|
||||
|
||||
def resolve(doc, prop):
|
||||
|
||||
@@ -82,6 +82,9 @@ SandboxEnvironment SandboxEnvironment::from_json(const Json& doc) {
|
||||
e.owner = str(doc, "owner");
|
||||
if (const Json* ci = doc.find("connectionInfo"); ci && ci->is_object()) {
|
||||
e.load_balancer_hostname = str(*ci, "loadBalancerHostname");
|
||||
e.load_balancer_ip = str(*ci, "loadBalancerIp");
|
||||
e.service_attachment = str(*ci, "serviceAttachment");
|
||||
e.sandbox_internal_ip = str(*ci, "sandboxInternalIp");
|
||||
e.routing_token = str(*ci, "routingToken");
|
||||
}
|
||||
if (const Json* spec = doc.find("spec"); spec && spec->is_object()) {
|
||||
|
||||
@@ -54,6 +54,9 @@ struct SandboxEnvironment {
|
||||
std::string latest_snapshot_name; // latestSandboxEnvironmentSnapshot
|
||||
std::string owner;
|
||||
std::string load_balancer_hostname; // connectionInfo (data plane address)
|
||||
std::string load_balancer_ip;
|
||||
std::string service_attachment; // PSC-E, VPC-SC sandboxes only
|
||||
std::string sandbox_internal_ip;
|
||||
std::string routing_token;
|
||||
std::optional<SandboxKind> kind;
|
||||
clients::body::Json spec; // Raw spec, unknown fields kept.
|
||||
@@ -66,6 +69,8 @@ struct SandboxEnvironment {
|
||||
struct InputFile {
|
||||
std::string name;
|
||||
std::string content; // Raw bytes.
|
||||
// Optional; empty: guessed from the extension, else application/octet-stream.
|
||||
std::string mime_type = {};
|
||||
};
|
||||
|
||||
struct ProducedFile {
|
||||
|
||||
@@ -31,6 +31,25 @@ int main() {
|
||||
{"/agent/sandbox/computer",
|
||||
"{\"name\":\"" + sandbox + "\",\"action\":\"navigate\",\"url\":\"https://example.com\"}"},
|
||||
{"/agent/sandbox/files", "{\"name\":\"" + sandbox + "\",\"op\":\"list\"}"},
|
||||
{"/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"health\"}"},
|
||||
{"/agent/sandbox/dataplane",
|
||||
"{\"name\":\"" + sandbox + "\",\"action\":\"command\",\"command\":\"Page.reload\"}"},
|
||||
{"/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"Page.reload\"}"},
|
||||
{"/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"shell\",\"input\":\"echo hi\"}"},
|
||||
{"/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"code\",\"input\":\"print(1)\"}"},
|
||||
{"/agent/sandbox/operations",
|
||||
"{\"name\":\"" + sandbox + "/operations/1\",\"op\":\"get\"}"},
|
||||
{"/agent/sandbox/operations",
|
||||
"{\"name\":\"" + sandbox + "/operations/1\",\"op\":\"wait\",\"timeout\":\"5\"}"},
|
||||
{"/agent/sandbox/operations", "{\"name\":\"" + sandbox + "\",\"op\":\"list\"}"},
|
||||
{"/agent/sandbox/operations",
|
||||
"{\"name\":\"" + sandbox + "/operations/1\",\"op\":\"cancel\"}"},
|
||||
{"/agent/sandbox/resource",
|
||||
"{\"name\":\"" + engine + "/sandboxEnvironmentTemplates/7\",\"kind\":\"template\"}"},
|
||||
{"/agent/sandbox/resource",
|
||||
"{\"name\":\"" + engine + "/sandboxEnvironmentSnapshots/8\",\"kind\":\"snapshot\"}"},
|
||||
{"/agent/sandbox/template",
|
||||
"{\"engine\":\"" + engine + "\",\"display_name\":\"t\",\"category\":\"computer\"}"},
|
||||
};
|
||||
for (const Case& c : cases) {
|
||||
auto res = f.client->post(c.route, c.body, "application/json");
|
||||
@@ -40,6 +59,15 @@ int main() {
|
||||
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");
|
||||
auto del_op = f.client->request(molo::example::fw::Method::Delete,
|
||||
"/agent/sandbox/operation?name=" + sandbox + "/operations/1");
|
||||
check(del_op.status == 503 && contains(del_op.body, "google_not_configured"),
|
||||
"operation delete answers 503 without a credential");
|
||||
auto del_res = f.client->request(
|
||||
molo::example::fw::Method::Delete,
|
||||
"/agent/sandbox/resource?kind=template&name=" + engine + "/sandboxEnvironmentTemplates/7");
|
||||
check(del_res.status == 503 && contains(del_res.body, "google_not_configured"),
|
||||
"template 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");
|
||||
|
||||
@@ -39,6 +39,44 @@ int main() {
|
||||
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");
|
||||
|
||||
// Data plane, CDP, stream, operations, resources, templates.
|
||||
const std::string op = sandbox + "/operations/1";
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"fly\"}", "unknown data plane action");
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + engine + "\",\"action\":\"health\"}", "engine used as sandbox name");
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"activate\",\"tab_id\":\"../x\"}", "tab id with dots");
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"activate\"}", "missing tab id");
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"command\",\"command\":\"nope\"}", "command without a domain");
|
||||
bad("/agent/sandbox/dataplane", "{\"name\":\"" + sandbox + "\",\"action\":\"command\",\"command\":\"Page.reload\",\"params\":\"[1]\"}", "params that are not an object");
|
||||
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"\"}", "empty CDP method");
|
||||
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"Page.a.b\"}", "CDP method with two dots");
|
||||
bad("/agent/sandbox/cdp", "{\"name\":\"x\",\"method\":\"Page.reload\"}", "bad sandbox name for CDP");
|
||||
bad("/agent/sandbox/cdp", "{\"name\":\"" + sandbox + "\",\"method\":\"Page.reload\",\"params\":\"{bad\"}", "params that are not JSON");
|
||||
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"vm\",\"input\":\"x\"}", "unknown stream mode");
|
||||
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"shell\",\"input\":\"\"}", "empty stream command");
|
||||
bad("/agent/sandbox/stream", "{\"name\":\"" + sandbox + "\",\"mode\":\"code\",\"input\":\"\"}", "empty stream code");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"delete\"}", "delete is not a POST op");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + sandbox + "\",\"op\":\"get\"}", "sandbox name used as operation name");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"wait\",\"timeout\":\"0\"}", "wait timeout under the floor");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + op + "\",\"op\":\"wait\",\"timeout\":\"121\"}", "wait timeout over the cap");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + engine + "\",\"op\":\"list\"}", "engine used as operations parent");
|
||||
bad("/agent/sandbox/operations", "{\"name\":\"" + sandbox + "\",\"op\":\"list\",\"page_size\":\"101\"}", "page size over the cap");
|
||||
bad("/agent/sandbox/resource", "{\"name\":\"" + sandbox + "\",\"kind\":\"template\"}", "sandbox name as template");
|
||||
bad("/agent/sandbox/resource", "{\"name\":\"" + sandbox + "\",\"kind\":\"disk\"}", "unknown resource kind");
|
||||
bad("/agent/sandbox/template", "{\"engine\":\"" + engine + "\",\"display_name\":\"t\",\"category\":\"gpu\"}", "unknown template category");
|
||||
bad("/agent/sandbox/template", "{\"engine\":\"" + engine + "\",\"display_name\":\"t\"}", "template without a category or image");
|
||||
bad("/agent/sandbox/template", "{\"engine\":\"x\",\"display_name\":\"t\",\"category\":\"shell\"}", "bad engine for template");
|
||||
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/operation?name=bad").status == 422,
|
||||
"operation delete with a bad name is 422");
|
||||
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/resource?kind=disk&name=x").status == 422,
|
||||
"resource delete with an unknown kind is 422");
|
||||
check(f.client->request(molo::example::fw::Method::Delete, "/agent/sandbox/resource?kind=snapshot&name=bad").status == 422,
|
||||
"resource delete with a bad name is 422");
|
||||
check(f.client->get("/agent/sandbox/dataplane").status == 405, "GET on dataplane is 405");
|
||||
check(f.client->get("/agent/sandbox/cdp").status == 405, "GET on cdp is 405");
|
||||
check(f.client->get("/agent/sandbox/stream").status == 405, "GET on stream is 405");
|
||||
check(f.client->get("/agent/sandbox/operations").status == 405, "GET on operations is 405");
|
||||
check(f.client->post("/agent/sandbox/operation").status == 405, "POST on operation delete is 405");
|
||||
|
||||
// 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");
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
#pragma once
|
||||
|
||||
// A test-owned loopback server for the sandbox data plane client: plain HTTP
|
||||
// with keep-alive plus RFC 6455 upgrades, many connections, every request
|
||||
// recorded. It is NOT a Google reply: it exists to prove client mechanics
|
||||
// (header placement, framing, refresh, caps, reconnect). Nothing it answers is
|
||||
// evidence about how Google's data plane behaves.
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../live/loopback.h"
|
||||
|
||||
namespace plane_test {
|
||||
|
||||
struct Req {
|
||||
std::string method;
|
||||
std::string target;
|
||||
std::map<std::string, std::string> headers; // lower-case names
|
||||
std::string body;
|
||||
bool websocket = false;
|
||||
std::string header(const std::string& name) const {
|
||||
auto it = headers.find(name);
|
||||
return it == headers.end() ? "" : it->second;
|
||||
}
|
||||
};
|
||||
|
||||
struct Res {
|
||||
int status = 200;
|
||||
std::string body;
|
||||
std::string content_type = "application/json";
|
||||
};
|
||||
|
||||
class Server {
|
||||
public:
|
||||
using Http = std::function<Res(const Req&)>;
|
||||
// `index` counts websocket connections from 0.
|
||||
using Ws = std::function<void(live_test::ServerConn&, int index)>;
|
||||
|
||||
Server(Http http, Ws ws = nullptr) : http_(std::move(http)), ws_(std::move(ws)) {
|
||||
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
int one = 1;
|
||||
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
|
||||
sockaddr_in a{};
|
||||
a.sin_family = AF_INET;
|
||||
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
::bind(listen_fd_, (sockaddr*)&a, sizeof a);
|
||||
::listen(listen_fd_, 16);
|
||||
socklen_t len = sizeof a;
|
||||
::getsockname(listen_fd_, (sockaddr*)&a, &len);
|
||||
port_ = ntohs(a.sin_port);
|
||||
acceptor_ = std::thread([this] { accept_loop(); });
|
||||
}
|
||||
~Server() {
|
||||
stop_ = true;
|
||||
if (acceptor_.joinable()) acceptor_.join();
|
||||
for (auto& t : workers_) t.join();
|
||||
::close(listen_fd_);
|
||||
}
|
||||
|
||||
int port() const { return port_; }
|
||||
std::string hostport() const { return "127.0.0.1:" + std::to_string(port_); }
|
||||
std::string base_url() const { return "http://" + hostport(); }
|
||||
|
||||
std::vector<Req> requests() const {
|
||||
std::lock_guard<std::mutex> l(mu_);
|
||||
return seen_;
|
||||
}
|
||||
int ws_connections() const { return ws_count_; }
|
||||
|
||||
private:
|
||||
void accept_loop() {
|
||||
while (!stop_) {
|
||||
pollfd p{listen_fd_, POLLIN, 0};
|
||||
if (::poll(&p, 1, 50) != 1) continue;
|
||||
int fd = ::accept(listen_fd_, nullptr, nullptr);
|
||||
if (fd < 0) continue;
|
||||
workers_.emplace_back([this, fd] { serve(fd); });
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_head(int fd, std::string& buf, std::string& head, const std::atomic<bool>& stop) {
|
||||
while (buf.find("\r\n\r\n") == std::string::npos) {
|
||||
pollfd p{fd, POLLIN, 0};
|
||||
if (::poll(&p, 1, 100) != 1) {
|
||||
if (stop) return false;
|
||||
continue;
|
||||
}
|
||||
char tmp[8192];
|
||||
ssize_t n = ::recv(fd, tmp, sizeof tmp, 0);
|
||||
if (n <= 0) return false;
|
||||
buf.append(tmp, (std::size_t)n);
|
||||
}
|
||||
std::size_t end = buf.find("\r\n\r\n") + 4;
|
||||
head = buf.substr(0, end);
|
||||
buf.erase(0, end);
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string lower(std::string s) {
|
||||
for (char& c : s) c = (char)std::tolower((unsigned char)c);
|
||||
return s;
|
||||
}
|
||||
|
||||
void serve(int fd) {
|
||||
timeval tv{2, 0};
|
||||
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
|
||||
std::string buf, head;
|
||||
while (!stop_ && read_head(fd, buf, head, stop_)) {
|
||||
Req r;
|
||||
std::size_t eol = head.find("\r\n");
|
||||
std::string line = head.substr(0, eol);
|
||||
std::size_t sp1 = line.find(' '), sp2 = line.find(' ', sp1 + 1);
|
||||
r.method = line.substr(0, sp1);
|
||||
r.target = line.substr(sp1 + 1, sp2 - sp1 - 1);
|
||||
std::size_t pos = eol + 2;
|
||||
while (pos < head.size()) {
|
||||
std::size_t e = head.find("\r\n", pos);
|
||||
if (e == pos || e == std::string::npos) break;
|
||||
std::string h = head.substr(pos, e - pos);
|
||||
std::size_t c = h.find(':');
|
||||
if (c != std::string::npos) {
|
||||
std::string v = h.substr(c + 1);
|
||||
while (!v.empty() && v.front() == ' ') v.erase(0, 1);
|
||||
r.headers[lower(h.substr(0, c))] = v;
|
||||
}
|
||||
pos = e + 2;
|
||||
}
|
||||
r.websocket = lower(r.header("upgrade")) == "websocket";
|
||||
std::size_t want = r.header("content-length").empty() ? 0 : std::stoul(r.header("content-length"));
|
||||
while (buf.size() < want) {
|
||||
char tmp[8192];
|
||||
ssize_t n = ::recv(fd, tmp, sizeof tmp, 0);
|
||||
if (n <= 0) {
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
buf.append(tmp, (std::size_t)n);
|
||||
}
|
||||
r.body = buf.substr(0, want);
|
||||
buf.erase(0, want);
|
||||
{
|
||||
std::lock_guard<std::mutex> l(mu_);
|
||||
seen_.push_back(r);
|
||||
}
|
||||
if (r.websocket) {
|
||||
upgrade(fd, r, head, buf);
|
||||
return;
|
||||
}
|
||||
Res res = http_(r);
|
||||
std::string out = "HTTP/1.1 " + std::to_string(res.status) + " X\r\nContent-Type: " +
|
||||
res.content_type + "\r\nContent-Length: " + std::to_string(res.body.size()) +
|
||||
"\r\nConnection: keep-alive\r\n\r\n" + res.body;
|
||||
std::size_t sent = 0;
|
||||
while (sent < out.size()) {
|
||||
ssize_t n = ::send(fd, out.data() + sent, out.size() - sent, MSG_NOSIGNAL);
|
||||
if (n <= 0) {
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
sent += (std::size_t)n;
|
||||
}
|
||||
}
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
void upgrade(int fd, const Req& r, const std::string& head, const std::string& rest) {
|
||||
int index = ws_count_++;
|
||||
live_test::ServerConn conn;
|
||||
conn.fd = fd;
|
||||
conn.request = head;
|
||||
if (!rest.empty()) conn.decoder.feed(rest.data(), rest.size());
|
||||
std::string key = r.header("sec-websocket-key");
|
||||
if (!ws_) {
|
||||
conn.send_raw("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n");
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
Res gate;
|
||||
gate.status = 101;
|
||||
if (reject_ws_) gate = reject_ws_(r, index);
|
||||
if (gate.status != 101) {
|
||||
conn.send_raw("HTTP/1.1 " + std::to_string(gate.status) + " X\r\nContent-Length: 0\r\n\r\n");
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
conn.send_raw("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: " + molo::google::live::WsHandshake::accept_for(key) +
|
||||
"\r\n\r\n");
|
||||
try {
|
||||
ws_(conn, index);
|
||||
} catch (...) {
|
||||
}
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
public:
|
||||
// Optional gate run before the 101: return status != 101 to refuse.
|
||||
std::function<Res(const Req&, int)> reject_ws_;
|
||||
|
||||
private:
|
||||
Http http_;
|
||||
Ws ws_;
|
||||
int listen_fd_ = -1;
|
||||
int port_ = 0;
|
||||
std::atomic<bool> stop_{false};
|
||||
std::atomic<int> ws_count_{0};
|
||||
std::thread acceptor_;
|
||||
std::vector<std::thread> workers_;
|
||||
mutable std::mutex mu_;
|
||||
std::vector<Req> seen_;
|
||||
};
|
||||
|
||||
} // namespace plane_test
|
||||
@@ -0,0 +1,787 @@
|
||||
// Sandbox data plane: client mechanics against a test-owned loopback server
|
||||
// (framing, header placement, JWT refresh, caps, reconnect). The server is not
|
||||
// a Google reply and proves nothing about Google's behavior; what Google does
|
||||
// is verified by Discovery and by the docs and SDK cited in SPEC.md.
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "framework.h"
|
||||
#include "clients/transports/http_transport.h"
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "google/client.h"
|
||||
#include "google/credentials.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
#include "google/sandbox/chunk.h"
|
||||
#include "security/crypto/Base64.h"
|
||||
#include "google/sandbox/computer_use.h"
|
||||
#include "google/sandbox/data_plane.h"
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
#include "google/sandbox/jwt.h"
|
||||
#include "google/sandbox/operations.h"
|
||||
#include "google/sandbox/sandboxes.h"
|
||||
#include "google/sandbox/stream_executor.h"
|
||||
#include "loopback_plane.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::sandbox;
|
||||
using molo::clients::body::Json;
|
||||
using plane_test::Req;
|
||||
using plane_test::Res;
|
||||
using plane_test::Server;
|
||||
|
||||
#define ASSERT_INVALID_ARG(stmt) \
|
||||
do { \
|
||||
bool caught_ = false; \
|
||||
try { \
|
||||
stmt; \
|
||||
} catch (const GoogleError& e) { \
|
||||
caught_ = e.http_status() == 400 && e.status() == "INVALID_ARGUMENT"; \
|
||||
} \
|
||||
ASSERT_TRUE(caught_); \
|
||||
} while (0)
|
||||
|
||||
namespace {
|
||||
|
||||
const std::string kSandbox =
|
||||
"projects/p-1/locations/us-central1/reasoningEngines/123/sandboxEnvironments/456";
|
||||
|
||||
struct FakeTokens : ITokenSource {
|
||||
std::atomic<int> generation{1};
|
||||
std::atomic<int> invalidations{0};
|
||||
std::string token() override { return "jwt-" + std::to_string(generation.load()); }
|
||||
void invalidate() override {
|
||||
++invalidations;
|
||||
++generation;
|
||||
}
|
||||
};
|
||||
|
||||
DataPlaneTarget loopback_target(const Server& s, const std::string& token = "route-tok") {
|
||||
SandboxEnvironment env;
|
||||
env.routing_token = token;
|
||||
env.state = "STATE_RUNNING";
|
||||
return DataPlaneTarget::from_environment(env, "", s.hostport());
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
bool throws_status(F&& f, int http, const std::string& status) {
|
||||
try {
|
||||
f();
|
||||
} catch (const GoogleError& e) {
|
||||
return e.http_status() == http && e.status() == status;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Echoes a CDP reply for every message: {"id":N,"result":{"echo":"<method>"}}.
|
||||
void echo_script(live_test::ServerConn& c, int) {
|
||||
while (auto m = c.read_message()) {
|
||||
Json doc = Json::parse(*m);
|
||||
Json r = Json::object();
|
||||
r.set("id", *doc.find("id"));
|
||||
Json res = Json::object();
|
||||
res.set("echo", *doc.find("method"));
|
||||
if (auto* s = doc.find("sessionId")) res.set("sessionId", *s);
|
||||
r.set("result", res);
|
||||
c.send_text(r.dump());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(sandbox_target, address_derivation_and_refusals) {
|
||||
SandboxEnvironment env;
|
||||
env.routing_token = "rt";
|
||||
env.load_balancer_hostname = "lb.example.com";
|
||||
env.load_balancer_ip = "10.1.2.3";
|
||||
DataPlaneTarget t = DataPlaneTarget::from_environment(env);
|
||||
ASSERT_EQ(t.http_base, std::string("https://lb.example.com"));
|
||||
ASSERT_EQ(t.ws_base, std::string("wss://lb.example.com"));
|
||||
ASSERT_EQ(t.port, std::string("8080"));
|
||||
env.load_balancer_hostname.clear();
|
||||
t = DataPlaneTarget::from_environment(env);
|
||||
ASSERT_EQ(t.http_base, std::string("http://10.1.2.3")); // as the SDK does
|
||||
ASSERT_EQ(t.ws_base, std::string("ws://10.1.2.3"));
|
||||
t = DataPlaneTarget::from_environment(env, "psc.internal", "", "9000");
|
||||
ASSERT_EQ(t.http_base, std::string("https://psc.internal"));
|
||||
ASSERT_EQ(t.port, std::string("9000"));
|
||||
// Loopback override only for loopback hosts.
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(env, "", "evil.example.com:80"));
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(env, "", "127.0.0.1"));
|
||||
// Missing or unsafe routing token, no address, bad port.
|
||||
SandboxEnvironment none;
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(none));
|
||||
none.routing_token = "has space";
|
||||
none.load_balancer_ip = "1.2.3.4";
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(none));
|
||||
none.routing_token = "a,b";
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(none));
|
||||
env.routing_token = "ok";
|
||||
env.load_balancer_ip = "1.2.3.4/x";
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(env));
|
||||
env.load_balancer_ip = "1.2.3.4";
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(env, "", "", "0"));
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(env, "", "", "70000"));
|
||||
SandboxEnvironment private_sandbox; // VPC-SC: only a service attachment
|
||||
private_sandbox.routing_token = "rt";
|
||||
private_sandbox.service_attachment = "projects/x/regions/y/serviceAttachments/z";
|
||||
ASSERT_INVALID_ARG(DataPlaneTarget::from_environment(private_sandbox));
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, headers_paths_and_bodies) {
|
||||
Server server([](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"devtools/browser/abc"})";
|
||||
else res.body = R"({"ok":true})";
|
||||
return res;
|
||||
});
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
DataPlane plane(loopback_target(server), tokens);
|
||||
plane.health();
|
||||
plane.create_tab();
|
||||
plane.list_tabs();
|
||||
plane.activate_tab("tab-1");
|
||||
plane.close_tab("tab-1");
|
||||
ASSERT_EQ(plane.cdp_ws_endpoint(), std::string("devtools/browser/abc"));
|
||||
plane.cdp("Page.navigate", Json::parse(R"({"url":"https://example.com"})"));
|
||||
plane.cdps({{"Page.reload", Json::object()}, {"Page.stopLoading", Json::object()}});
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen.size(), 8u);
|
||||
const char* want[8][2] = {{"GET", "/"}, {"POST", "/tabs"},
|
||||
{"GET", "/tabs"}, {"POST", "/tabs/tab-1/activate"},
|
||||
{"DELETE", "/tabs/tab-1"}, {"GET", "/cdp_ws_endpoint"},
|
||||
{"POST", "/cdp"}, {"POST", "/cdps"}};
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
ASSERT_EQ(seen[i].method, std::string(want[i][0]));
|
||||
ASSERT_EQ(seen[i].target, std::string(want[i][1]));
|
||||
// The three documented headers on every call.
|
||||
ASSERT_EQ(seen[i].header("authorization"), std::string("Bearer jwt-1"));
|
||||
ASSERT_EQ(seen[i].header("x-sandbox-routing-token"), std::string("route-tok"));
|
||||
ASSERT_EQ(seen[i].header("x-sandbox-port"), std::string("8080"));
|
||||
}
|
||||
Json cdp = Json::parse(seen[6].body);
|
||||
ASSERT_EQ(cdp.find("command")->as_string(), std::string("Page.navigate"));
|
||||
ASSERT_EQ(cdp.find("params")->find("url")->as_string(), std::string("https://example.com"));
|
||||
Json batch = Json::parse(seen[7].body);
|
||||
ASSERT_EQ(batch.find("commands")->as_array().size(), 2u);
|
||||
ASSERT_EQ(seen[6].header("content-type").substr(0, 16), std::string("application/json"));
|
||||
ASSERT_EQ(tokens->invalidations.load(), 0);
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, validation_before_traffic) {
|
||||
Server server([](const Req&) { return Res{}; });
|
||||
DataPlane plane(loopback_target(server), std::make_shared<FakeTokens>());
|
||||
ASSERT_INVALID_ARG(plane.activate_tab(""));
|
||||
ASSERT_INVALID_ARG(plane.activate_tab("../x"));
|
||||
ASSERT_INVALID_ARG(plane.close_tab("a/b"));
|
||||
ASSERT_INVALID_ARG(plane.cdp("nodomain", Json::object()));
|
||||
ASSERT_INVALID_ARG(plane.cdp("A.b.c", Json::object()));
|
||||
ASSERT_INVALID_ARG(plane.cdp("Page.navigate", Json::array()));
|
||||
ASSERT_INVALID_ARG(plane.cdps({}));
|
||||
std::vector<CdpCommand> many(SandboxCaps::kMaxBatchCommands + 1, CdpCommand{"Page.reload", Json::object()});
|
||||
ASSERT_INVALID_ARG(plane.cdps(many));
|
||||
ASSERT_INVALID_ARG(DataPlane::validate_path("tabs"));
|
||||
ASSERT_INVALID_ARG(DataPlane::validate_path("/a/../b"));
|
||||
ASSERT_INVALID_ARG(plane.ws_url(""));
|
||||
ASSERT_INVALID_ARG(plane.ws_url("wss://elsewhere/x"));
|
||||
ASSERT_EQ(server.requests().size(), 0u);
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, jwt_refreshes_once_on_401) {
|
||||
std::atomic<int> calls{0};
|
||||
Server server([&](const Req& r) {
|
||||
++calls;
|
||||
Res res;
|
||||
if (r.header("authorization") != "Bearer jwt-2") {
|
||||
res.status = 401;
|
||||
res.body = R"({"error":"expired"})";
|
||||
} else {
|
||||
res.body = R"({"ok":true})";
|
||||
}
|
||||
return res;
|
||||
});
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
DataPlane plane(loopback_target(server), tokens);
|
||||
DataPlaneReply r = plane.health();
|
||||
ASSERT_EQ(r.status, 200);
|
||||
ASSERT_EQ(calls.load(), 2);
|
||||
ASSERT_EQ(tokens->invalidations.load(), 1);
|
||||
ASSERT_EQ(plane.auth_refreshes(), 1);
|
||||
ASSERT_EQ(server.requests()[0].header("authorization"), std::string("Bearer jwt-1"));
|
||||
ASSERT_EQ(server.requests()[1].header("authorization"), std::string("Bearer jwt-2"));
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, persistent_401_is_not_retried_forever) {
|
||||
Server server([](const Req&) {
|
||||
Res res;
|
||||
res.status = 401;
|
||||
res.body = "no";
|
||||
return res;
|
||||
});
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
DataPlane plane(loopback_target(server), tokens);
|
||||
ASSERT_TRUE(throws_status([&] { plane.health(); }, 401, "DATA_PLANE_401"));
|
||||
ASSERT_EQ(server.requests().size(), 2u); // one try plus one refresh
|
||||
ASSERT_EQ(tokens->invalidations.load(), 1);
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, errors_and_reply_cap) {
|
||||
Server server([](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/tabs") {
|
||||
res.status = 500;
|
||||
res.body = std::string(2000, 'x');
|
||||
} else {
|
||||
res.body = std::string(SandboxCaps::kMaxDataReplyBytes + 1, 'y');
|
||||
}
|
||||
return res;
|
||||
});
|
||||
DataPlane plane(loopback_target(server), std::make_shared<FakeTokens>());
|
||||
try {
|
||||
plane.list_tabs();
|
||||
FAIL("a 500 must throw");
|
||||
} catch (const GoogleError& e) {
|
||||
ASSERT_EQ(e.http_status(), 500);
|
||||
ASSERT_EQ(e.service(), std::string("sandbox-data-plane"));
|
||||
ASSERT_TRUE(e.message().size() <= SandboxCaps::kMaxErrorExcerpt);
|
||||
}
|
||||
ASSERT_TRUE(throws_status([&] { plane.health(); }, 502, "TOO_BIG"));
|
||||
}
|
||||
|
||||
TEST(sandbox_dataplane, unreachable_host_is_a_loud_502) {
|
||||
SandboxEnvironment env;
|
||||
env.routing_token = "rt";
|
||||
DataPlaneTarget t = DataPlaneTarget::from_environment(env, "", "127.0.0.1:1");
|
||||
DataPlane plane(t, std::make_shared<FakeTokens>(), nullptr, 2000);
|
||||
ASSERT_TRUE(throws_status([&] { plane.health(); }, 502, "UNAVAILABLE"));
|
||||
}
|
||||
|
||||
TEST(sandbox_jwt, claims_cache_and_refresh_margin) {
|
||||
// The signer endpoint is a loopback stand-in so the cache and refresh logic
|
||||
// runs through the real client. It says nothing about the real IAM API.
|
||||
std::atomic<int> signs{0};
|
||||
Server server([&](const Req&) {
|
||||
++signs;
|
||||
Res res;
|
||||
res.body = R"({"keyId":"k","signedJwt":"signed-)" + std::to_string(signs.load()) + R"("})";
|
||||
return res;
|
||||
});
|
||||
auto client = std::make_shared<GoogleClient>(
|
||||
std::make_unique<molo::clients::transports::HttpTransport>(), Credentials::bearer("tok"));
|
||||
std::atomic<long> now{1000};
|
||||
ServiceEndpoint ep{"iamcredentials", server.base_url() + "/v1"};
|
||||
IamJwtSource src(client, "sa@p.iam.gserviceaccount.com", 3600, [&] { return now.load(); }, ep);
|
||||
ASSERT_EQ(src.token(), std::string("signed-1"));
|
||||
ASSERT_EQ(src.token(), std::string("signed-1")); // cached
|
||||
ASSERT_EQ(src.mints(), 1);
|
||||
now = 1000 + 3600 - SandboxCaps::kJwtRefreshMarginSeconds - 1; // still outside the margin
|
||||
ASSERT_EQ(src.token(), std::string("signed-1"));
|
||||
now = 1000 + 3600 - SandboxCaps::kJwtRefreshMarginSeconds + 1; // inside: refresh
|
||||
ASSERT_EQ(src.token(), std::string("signed-2"));
|
||||
src.invalidate();
|
||||
ASSERT_EQ(src.token(), std::string("signed-3"));
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen[0].method, std::string("POST"));
|
||||
ASSERT_EQ(seen[0].target, std::string("/v1/projects/-/serviceAccounts/sa@p.iam.gserviceaccount.com:signJwt"));
|
||||
ASSERT_EQ(seen[0].header("authorization"), std::string("Bearer tok"));
|
||||
Json body = Json::parse(seen[0].body);
|
||||
Json claims = Json::parse(body.find("payload")->as_string());
|
||||
ASSERT_EQ(claims.find("iss")->as_string(), std::string("sa@p.iam.gserviceaccount.com"));
|
||||
ASSERT_EQ(claims.find("sub")->as_string(), std::string("sa@p.iam.gserviceaccount.com"));
|
||||
ASSERT_EQ(claims.find("aud")->as_string(), std::string("https://aiplatform.googleapis.com/"));
|
||||
ASSERT_EQ(claims.find("iat")->as_int(), 1000);
|
||||
ASSERT_EQ(claims.find("exp")->as_int(), 4600);
|
||||
ASSERT_TRUE(claims.find("nonce")->as_int() >= 1);
|
||||
// Two mints use two different nonces.
|
||||
Json c2 = Json::parse(Json::parse(seen[1].body).find("payload")->as_string());
|
||||
ASSERT_TRUE(c2.find("nonce")->as_int() != claims.find("nonce")->as_int() ||
|
||||
c2.find("iat")->as_int() != claims.find("iat")->as_int());
|
||||
}
|
||||
|
||||
TEST(sandbox_jwt, validation) {
|
||||
auto client = std::make_shared<GoogleClient>(std::make_unique<molo::clients::transports::MockTransport>(),
|
||||
Credentials::bearer("tok"));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "", 3600));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "no-at-sign", 3600));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "a@b@c", 3600));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "a b@c.d", 3600));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "a@c.d/../x", 3600));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "a@c.d", 10));
|
||||
ASSERT_INVALID_ARG(IamJwtSource(client, "a@c.d", 3601));
|
||||
// No credential: the signer fails before traffic with the standard 503.
|
||||
auto t = std::make_unique<molo::clients::transports::MockTransport>();
|
||||
auto* mock = t.get();
|
||||
auto bare = std::make_shared<GoogleClient>(std::move(t), nullptr);
|
||||
IamJwtSource src(bare, "a@c.d", 3600);
|
||||
ASSERT_TRUE(throws_status([&] { src.token(); }, 503, "NOT_CONFIGURED"));
|
||||
ASSERT_EQ(mock->send_count(), 0u);
|
||||
ASSERT_EQ(IamJwtSource::sign_path("a@c.d"), std::string("/projects/-/serviceAccounts/a@c.d:signJwt"));
|
||||
}
|
||||
|
||||
TEST(sandbox_cdp, handshake_header_and_call_round_trip) {
|
||||
Server server(
|
||||
[](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"devtools/browser/xyz"})";
|
||||
return res;
|
||||
},
|
||||
echo_script);
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
auto plane = std::make_shared<DataPlane>(loopback_target(server), tokens);
|
||||
CdpSession cdp(plane);
|
||||
cdp.connect();
|
||||
ASSERT_TRUE(cdp.connected());
|
||||
Json r = cdp.call("Page.navigate", Json::parse(R"({"url":"https://a.b"})"));
|
||||
ASSERT_EQ(r.find("echo")->as_string(), std::string("Page.navigate"));
|
||||
Json s = cdp.call("Page.reload", Json::object(), "sess-1");
|
||||
ASSERT_EQ(s.find("sessionId")->as_string(), std::string("sess-1"));
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen.size(), 2u); // GET /cdp_ws_endpoint, then the upgrade
|
||||
ASSERT_EQ(seen[1].target, std::string("/devtools/browser/xyz"));
|
||||
ASSERT_TRUE(seen[1].websocket);
|
||||
ASSERT_EQ(seen[1].header("sec-websocket-protocol"), std::string("v1.stream, jwt-1, route-tok, 9222"));
|
||||
ASSERT_EQ(seen[1].header("sec-websocket-version"), std::string("13"));
|
||||
ASSERT_EQ(cdp.connects(), 1);
|
||||
ASSERT_EQ(cdp.reconnects(), 0);
|
||||
// Validation: nothing is sent.
|
||||
ASSERT_INVALID_ARG(cdp.call("bad", Json::object()));
|
||||
ASSERT_INVALID_ARG(cdp.call("Page.reload", Json::object(), "bad id!"));
|
||||
ASSERT_INVALID_ARG(cdp.call("Page.reload", Json::object(), "", 5));
|
||||
cdp.close();
|
||||
ASSERT_TRUE(!cdp.connected());
|
||||
}
|
||||
|
||||
TEST(sandbox_cdp, events_errors_timeout_and_event_cap) {
|
||||
Server server(
|
||||
[](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"ws/b"})";
|
||||
return res;
|
||||
},
|
||||
[](live_test::ServerConn& c, int) {
|
||||
while (auto m = c.read_message()) {
|
||||
Json doc = Json::parse(*m);
|
||||
std::string method = doc.find("method")->as_string();
|
||||
if (method == "Test.silent") continue; // never answers
|
||||
if (method == "Test.fail") {
|
||||
c.send_text(R"({"id":)" + std::to_string(doc.find("id")->as_int()) +
|
||||
R"(,"error":{"code":-32000,"message":"boom"}})");
|
||||
continue;
|
||||
}
|
||||
if (method == "Test.flood") {
|
||||
for (std::size_t i = 0; i < SandboxCaps::kMaxCdpEvents + 10; ++i)
|
||||
c.send_text(R"({"method":"Test.event","params":{"n":)" + std::to_string(i) + "}}");
|
||||
}
|
||||
if (method == "Test.stale") c.send_text(R"({"id":9999,"result":{}})");
|
||||
c.send_text(R"({"method":"Page.loadEventFired","params":{}})");
|
||||
c.send_text(R"({"id":)" + std::to_string(doc.find("id")->as_int()) + R"(,"result":{"ok":true}})");
|
||||
}
|
||||
});
|
||||
auto plane = std::make_shared<DataPlane>(loopback_target(server), std::make_shared<FakeTokens>());
|
||||
CdpSession cdp(plane);
|
||||
Json r = cdp.call("Test.go", Json::object());
|
||||
ASSERT_TRUE(r.find("ok")->as_bool());
|
||||
auto events = cdp.take_events();
|
||||
ASSERT_EQ(events.size(), 1u);
|
||||
ASSERT_EQ(events[0].find("method")->as_string(), std::string("Page.loadEventFired"));
|
||||
ASSERT_TRUE(cdp.take_events().empty());
|
||||
// CDP error object.
|
||||
try {
|
||||
cdp.call("Test.fail", Json::object());
|
||||
FAIL("a CDP error must throw");
|
||||
} catch (const GoogleError& e) {
|
||||
ASSERT_EQ(e.status(), std::string("CDP_ERROR"));
|
||||
ASSERT_EQ(e.message(), std::string("boom"));
|
||||
}
|
||||
// Unmatched ids are ignored, the right reply still arrives.
|
||||
ASSERT_TRUE(cdp.call("Test.stale", Json::object()).find("ok")->as_bool());
|
||||
cdp.take_events();
|
||||
// Deadline.
|
||||
ASSERT_TRUE(throws_status([&] { cdp.call("Test.silent", Json::object(), "", 300); }, 504,
|
||||
"DEADLINE_EXCEEDED"));
|
||||
// Event cap: oldest are dropped and counted.
|
||||
cdp.call("Test.flood", Json::object());
|
||||
ASSERT_TRUE(cdp.take_events().size() <= SandboxCaps::kMaxCdpEvents);
|
||||
ASSERT_TRUE(cdp.dropped_events() >= 10);
|
||||
}
|
||||
|
||||
TEST(sandbox_cdp, wait_event_and_attach_first_page) {
|
||||
Server server(
|
||||
[](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"ws/b"})";
|
||||
return res;
|
||||
},
|
||||
[](live_test::ServerConn& c, int) {
|
||||
while (auto m = c.read_message()) {
|
||||
Json doc = Json::parse(*m);
|
||||
std::string method = doc.find("method")->as_string();
|
||||
std::string id = std::to_string(doc.find("id")->as_int());
|
||||
if (method == "Target.getTargets")
|
||||
c.send_text(R"({"id":)" + id +
|
||||
R"(,"result":{"targetInfos":[{"type":"service_worker","targetId":"w"},{"type":"page","targetId":"T1"}]}})");
|
||||
else if (method == "Target.attachToTarget") {
|
||||
bool flat = doc.find("params")->find("flatten")->as_bool();
|
||||
std::string tid = doc.find("params")->find("targetId")->as_string();
|
||||
c.send_text(R"({"id":)" + id + R"(,"result":{"sessionId":")" + (flat ? tid + "-S" : "x") + R"("}})");
|
||||
} else if (method == "Page.enable") {
|
||||
c.send_text(R"({"method":"Page.frameNavigated","params":{}})");
|
||||
c.send_text(R"({"id":)" + id + R"(,"result":{}})");
|
||||
}
|
||||
}
|
||||
});
|
||||
auto plane = std::make_shared<DataPlane>(loopback_target(server), std::make_shared<FakeTokens>());
|
||||
CdpSession cdp(plane);
|
||||
ASSERT_EQ(cdp.attach_first_page(), std::string("T1-S"));
|
||||
cdp.call("Page.enable", Json::object(), "T1-S");
|
||||
Json ev = cdp.wait_event("Page.frameNavigated", 1000); // already buffered
|
||||
ASSERT_EQ(ev.find("method")->as_string(), std::string("Page.frameNavigated"));
|
||||
ASSERT_TRUE(throws_status([&] { cdp.wait_event("Page.never", 300); }, 504, "DEADLINE_EXCEEDED"));
|
||||
}
|
||||
|
||||
TEST(sandbox_cdp, reconnect_uses_a_fresh_jwt_and_is_capped) {
|
||||
std::atomic<int> endpoint_calls{0};
|
||||
Server server(
|
||||
[&](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") {
|
||||
++endpoint_calls;
|
||||
res.body = R"({"endpoint":"ws/b"})";
|
||||
}
|
||||
return res;
|
||||
},
|
||||
[](live_test::ServerConn& c, int) {
|
||||
// Answer one command, then drop the connection (no close frame needed).
|
||||
if (auto m = c.read_message()) {
|
||||
Json doc = Json::parse(*m);
|
||||
c.send_text(R"({"id":)" + std::to_string(doc.find("id")->as_int()) + R"(,"result":{"ok":true}})");
|
||||
}
|
||||
c.read_message(); // wait for the next command and hang up mid-call
|
||||
});
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
auto plane = std::make_shared<DataPlane>(loopback_target(server), tokens);
|
||||
CdpSession cdp(plane);
|
||||
ASSERT_TRUE(cdp.call("A.one", Json::object()).find("ok")->as_bool());
|
||||
// The server hangs up while this call waits: loud loss, no replay.
|
||||
ASSERT_TRUE(throws_status([&] { cdp.call("A.two", Json::object()); }, 502, "CDP_CONNECTION_LOST"));
|
||||
ASSERT_TRUE(!cdp.connected());
|
||||
ASSERT_EQ(plane->tokens().token(), std::string("jwt-1"));
|
||||
// Next call reconnects with a fresh JWT.
|
||||
ASSERT_TRUE(cdp.call("A.three", Json::object()).find("ok")->as_bool());
|
||||
ASSERT_EQ(cdp.reconnects(), 1);
|
||||
ASSERT_EQ(tokens->invalidations.load(), 1);
|
||||
auto seen = server.requests();
|
||||
std::string second_upgrade;
|
||||
for (const Req& r : seen)
|
||||
if (r.websocket) second_upgrade = r.header("sec-websocket-protocol");
|
||||
ASSERT_EQ(second_upgrade, std::string("v1.stream, jwt-2, route-tok, 9222"));
|
||||
ASSERT_EQ(endpoint_calls.load(), 2); // the endpoint is re-resolved
|
||||
// Keep losing the connection: the reconnect budget is a hard cap.
|
||||
bool limited = false;
|
||||
for (int i = 0; i < 8 && !limited; ++i) {
|
||||
try {
|
||||
cdp.call("A.more", Json::object());
|
||||
} catch (const GoogleError& e) {
|
||||
limited = e.status() == "CDP_RECONNECT_LIMIT";
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(limited);
|
||||
ASSERT_EQ(cdp.reconnects(), SandboxCaps::kMaxCdpReconnects);
|
||||
}
|
||||
|
||||
TEST(sandbox_cdp, upgrade_refusal_refreshes_once_then_fails) {
|
||||
Server server(
|
||||
[](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"ws/b"})";
|
||||
return res;
|
||||
},
|
||||
echo_script);
|
||||
server.reject_ws_ = [](const Req& r, int) {
|
||||
Res g;
|
||||
g.status = r.header("sec-websocket-protocol").find("jwt-2") != std::string::npos ? 101 : 401;
|
||||
return g;
|
||||
};
|
||||
auto tokens = std::make_shared<FakeTokens>();
|
||||
CdpSession cdp(std::make_shared<DataPlane>(loopback_target(server), tokens));
|
||||
cdp.connect(); // 401 once, fresh JWT, accepted
|
||||
ASSERT_TRUE(cdp.connected());
|
||||
ASSERT_EQ(tokens->invalidations.load(), 1);
|
||||
|
||||
Server deny([](const Req& r) {
|
||||
Res res;
|
||||
if (r.target == "/cdp_ws_endpoint") res.body = R"({"endpoint":"ws/b"})";
|
||||
return res;
|
||||
}, echo_script);
|
||||
deny.reject_ws_ = [](const Req&, int) {
|
||||
Res g;
|
||||
g.status = 403;
|
||||
return g;
|
||||
};
|
||||
CdpSession denied(std::make_shared<DataPlane>(loopback_target(deny), std::make_shared<FakeTokens>()));
|
||||
ASSERT_TRUE(throws_status([&] { denied.connect(); }, 403, "CDP_UNAUTHORIZED"));
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// A connector whose control plane read is local: the sandbox resource points
|
||||
// at the loopback server. Everything after that (JWT, headers, routes) is real.
|
||||
class LoopbackConnector : public DataPlaneConnector {
|
||||
public:
|
||||
LoopbackConnector(std::shared_ptr<GoogleClient> client, DataPlaneOptions options,
|
||||
std::shared_ptr<ITokenSource> tokens, std::string state = "STATE_RUNNING")
|
||||
: DataPlaneConnector(std::move(client), std::move(options), std::move(tokens)),
|
||||
state_(std::move(state)) {}
|
||||
|
||||
protected:
|
||||
SandboxEnvironment environment(const std::string& name) const override {
|
||||
SandboxEnvironment e;
|
||||
e.name = name;
|
||||
e.state = state_;
|
||||
e.routing_token = "route-tok";
|
||||
e.load_balancer_hostname = "unused.invalid";
|
||||
return e;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string state_;
|
||||
};
|
||||
|
||||
std::shared_ptr<GoogleClient> bearer_client() {
|
||||
return std::make_shared<GoogleClient>(std::make_unique<molo::clients::transports::HttpTransport>(),
|
||||
Credentials::bearer("tok"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(sandbox_computer, actions_over_the_data_plane) {
|
||||
// 1x1 transparent PNG.
|
||||
const std::string png =
|
||||
"\x89PNG\r\n\x1a\n" + std::string("0123456789", 10);
|
||||
Server server([&](const Req& r) {
|
||||
Res res;
|
||||
Json body = r.body.empty() ? Json::object() : Json::parse(r.body);
|
||||
const Json* cmd = body.find("command");
|
||||
if (cmd && cmd->as_string() == "Page.captureScreenshot")
|
||||
res.body = R"({"result":{"data":")" + molo::security::crypto::Base64::encode(png) + R"("}})";
|
||||
else
|
||||
res.body = R"({"result":{"frameId":"F"}})";
|
||||
return res;
|
||||
});
|
||||
DataPlaneOptions opt;
|
||||
opt.service_account_email = "sa@p.iam.gserviceaccount.com";
|
||||
opt.loopback_override = server.hostport();
|
||||
auto connector = std::make_shared<LoopbackConnector>(bearer_client(), opt, std::make_shared<FakeTokens>());
|
||||
ComputerUse cu(bearer_client(), connector);
|
||||
ActionResult nav = cu.navigate(kSandbox, "https://example.com");
|
||||
ASSERT_EQ(nav.result.find("frameId")->as_string(), std::string("F"));
|
||||
cu.click(kSandbox, 10, 20);
|
||||
cu.type_text(kSandbox, "hello");
|
||||
ActionResult shot = cu.screenshot(kSandbox);
|
||||
ASSERT_EQ(shot.screenshot_png, png);
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen.size(), 5u); // navigate, press, release, insertText, screenshot
|
||||
for (const Req& r : seen) {
|
||||
ASSERT_EQ(r.method, std::string("POST"));
|
||||
ASSERT_EQ(r.target, std::string("/cdp"));
|
||||
ASSERT_EQ(r.header("authorization"), std::string("Bearer jwt-1"));
|
||||
}
|
||||
ASSERT_EQ(Json::parse(seen[0].body).find("command")->as_string(), std::string("Page.navigate"));
|
||||
ASSERT_EQ(Json::parse(seen[1].body).find("params")->find("type")->as_string(), std::string("mousePressed"));
|
||||
ASSERT_EQ(Json::parse(seen[2].body).find("params")->find("type")->as_string(), std::string("mouseReleased"));
|
||||
ASSERT_EQ(Json::parse(seen[3].body).find("command")->as_string(), std::string("Input.insertText"));
|
||||
ASSERT_EQ(Json::parse(seen[4].body).find("params")->find("format")->as_string(), std::string("png"));
|
||||
}
|
||||
|
||||
TEST(sandbox_computer, tabs_and_health_and_guards) {
|
||||
Server server([](const Req& r) {
|
||||
Res res;
|
||||
res.body = r.target == "/" ? "healthy" : R"([{"id":"t1"}])";
|
||||
return res;
|
||||
});
|
||||
DataPlaneOptions opt;
|
||||
opt.service_account_email = "sa@p.iam.gserviceaccount.com";
|
||||
opt.loopback_override = server.hostport();
|
||||
auto connector = std::make_shared<LoopbackConnector>(bearer_client(), opt, std::make_shared<FakeTokens>());
|
||||
ComputerUse cu(bearer_client(), connector);
|
||||
ASSERT_EQ(cu.health(kSandbox).find("body")->as_string(), std::string("healthy"));
|
||||
ASSERT_EQ(cu.list_tabs(kSandbox).find("reply")->as_array().size(), 1u);
|
||||
cu.create_tab(kSandbox);
|
||||
cu.activate_tab(kSandbox, "t1");
|
||||
cu.close_tab(kSandbox, "t1");
|
||||
ASSERT_INVALID_ARG(cu.activate_tab(kSandbox, "../x"));
|
||||
ASSERT_INVALID_ARG(cu.command(kSandbox, "nope", Json::object()));
|
||||
ASSERT_EQ(server.requests().size(), 5u);
|
||||
|
||||
// A sandbox that is not running never reaches the data plane.
|
||||
auto paused = std::make_shared<LoopbackConnector>(bearer_client(), opt, std::make_shared<FakeTokens>(),
|
||||
"STATE_PAUSED");
|
||||
ComputerUse cu2(bearer_client(), paused);
|
||||
ASSERT_TRUE(throws_status([&] { cu2.health(kSandbox); }, 409, "FAILED_PRECONDITION"));
|
||||
ASSERT_EQ(server.requests().size(), 5u);
|
||||
}
|
||||
|
||||
TEST(sandbox_computer, no_credential_and_no_signer_are_503_before_traffic) {
|
||||
auto t = std::make_unique<molo::clients::transports::MockTransport>();
|
||||
auto* mock = t.get();
|
||||
auto bare = std::make_shared<GoogleClient>(std::move(t), nullptr);
|
||||
ComputerUse cu(bare, std::make_shared<DataPlaneConnector>(bare, DataPlaneOptions{}));
|
||||
ASSERT_TRUE(throws_status([&] { cu.navigate(kSandbox, "https://example.com"); }, 503, "NOT_CONFIGURED"));
|
||||
ASSERT_TRUE(throws_status([&] { cu.list_tabs(kSandbox); }, 503, "NOT_CONFIGURED"));
|
||||
ASSERT_TRUE(throws_status([&] { cu.open_cdp(kSandbox); }, 503, "NOT_CONFIGURED"));
|
||||
ASSERT_INVALID_ARG(cu.navigate("x", "https://example.com"));
|
||||
ASSERT_EQ(mock->send_count(), 0u);
|
||||
// A credential but no service account to sign with: 503 too, still no traffic.
|
||||
auto t2 = std::make_unique<molo::clients::transports::MockTransport>();
|
||||
auto* mock2 = t2.get();
|
||||
auto cred = std::make_shared<GoogleClient>(std::move(t2), Credentials::bearer("tok"));
|
||||
ComputerUse cu2(cred, std::make_shared<DataPlaneConnector>(cred, DataPlaneOptions{}));
|
||||
ASSERT_TRUE(throws_status([&] { cu2.screenshot(kSandbox); }, 503, "NOT_CONFIGURED"));
|
||||
ASSERT_EQ(mock2->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(sandbox_bidi, streamed_reply_over_the_real_transport) {
|
||||
// Mechanics of the streamed POST: path, version, bearer header, array
|
||||
// stream decoding, event cap. The reply text is the test's own.
|
||||
Server server([](const Req&) {
|
||||
Res res;
|
||||
Chunk a;
|
||||
a.mime_type = "application/json";
|
||||
a.data = R"({"stdout":"one\n","returncode":0})";
|
||||
Chunk f;
|
||||
f.mime_type = "text/plain";
|
||||
f.data = "file body";
|
||||
f.attributes["file_name"] = "out.txt";
|
||||
res.body = "[{\"outputs\":[" + a.to_json().dump() + "]},{\"outputs\":[" + f.to_json().dump() + "]}]";
|
||||
return res;
|
||||
});
|
||||
auto client = bearer_client();
|
||||
StreamExecutor stream(client);
|
||||
stream.use_base_url(server.base_url());
|
||||
int events = 0;
|
||||
ExecutionResult r = stream.run(kSandbox, ChunkCodec::shell_inputs("echo one", "", 5),
|
||||
[&](const std::vector<Chunk>&) {
|
||||
++events;
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(events, 2);
|
||||
ASSERT_EQ(r.std_out, std::string("one\n"));
|
||||
ASSERT_EQ(r.files.size(), 1u);
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen[0].method, std::string("POST"));
|
||||
ASSERT_EQ(seen[0].target, "/v1beta1/" + kSandbox + ":bidiExecute");
|
||||
ASSERT_EQ(seen[0].header("authorization"), std::string("Bearer tok"));
|
||||
Json body = Json::parse(seen[0].body);
|
||||
ASSERT_EQ(body.find("inputs")->as_array().size(), 3u);
|
||||
ASSERT_INVALID_ARG(stream.use_base_url("ftp://x"));
|
||||
}
|
||||
|
||||
TEST(sandbox_operations, paths_and_validation) {
|
||||
Server server([](const Req& r) {
|
||||
Res res;
|
||||
if (r.target.find("/operations?") != std::string::npos)
|
||||
res.body = R"({"operations":[{"name":"x/operations/1","done":true}]})";
|
||||
else
|
||||
res.body = R"({"name":"n","done":true})";
|
||||
return res;
|
||||
});
|
||||
Operations ops(bearer_client());
|
||||
ops.use_base_url(server.base_url());
|
||||
const std::string snap =
|
||||
"projects/p-1/locations/us-central1/reasoningEngines/123/sandboxEnvironmentSnapshots/8";
|
||||
const std::string tmpl =
|
||||
"projects/p-1/locations/us-central1/reasoningEngines/123/sandboxEnvironmentTemplates/7";
|
||||
ASSERT_TRUE(ops.get(kSandbox + "/operations/99").done);
|
||||
ASSERT_TRUE(ops.wait(snap + "/operations/5", 30).done);
|
||||
ops.cancel(tmpl + "/operations/6");
|
||||
ops.remove(kSandbox + "/operations/7");
|
||||
ASSERT_EQ(ops.list(kSandbox, 10).size(), 1u);
|
||||
auto seen = server.requests();
|
||||
ASSERT_EQ(seen.size(), 5u);
|
||||
ASSERT_EQ(seen[0].target, "/v1/" + kSandbox + "/operations/99");
|
||||
ASSERT_EQ(seen[1].method, std::string("POST"));
|
||||
ASSERT_EQ(seen[1].target, "/v1/" + snap + "/operations/5:wait?timeout=30s");
|
||||
ASSERT_EQ(seen[2].target, "/v1/" + tmpl + "/operations/6:cancel");
|
||||
ASSERT_EQ(seen[3].method, std::string("DELETE"));
|
||||
ASSERT_EQ(seen[4].target, "/v1beta1/" + kSandbox + "/operations?pageSize=10");
|
||||
ASSERT_INVALID_ARG(ops.get(kSandbox));
|
||||
ASSERT_INVALID_ARG(ops.get(kSandbox + "/operations/"));
|
||||
ASSERT_INVALID_ARG(ops.get("projects/p/operations/1"));
|
||||
ASSERT_INVALID_ARG(ops.wait(kSandbox + "/operations/1", 0));
|
||||
ASSERT_INVALID_ARG(ops.wait(kSandbox + "/operations/1", SandboxCaps::kMaxOperationWaitSeconds + 1));
|
||||
ASSERT_INVALID_ARG(ops.list("projects/p-1/locations/us-central1/reasoningEngines/123"));
|
||||
ASSERT_INVALID_ARG(ops.list(kSandbox, 101));
|
||||
ASSERT_EQ(server.requests().size(), 5u);
|
||||
auto names = SandboxNames::parse_operation(snap + "/operations/5");
|
||||
ASSERT_EQ(names.family, std::string("sandboxEnvironmentSnapshots"));
|
||||
ASSERT_EQ(names.id, std::string("5"));
|
||||
}
|
||||
|
||||
namespace {
|
||||
bool live_ready() {
|
||||
return std::getenv("MOLO_LIVE_NET") &&
|
||||
(!Credentials::env().empty() || !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() ||
|
||||
!Credentials::env("GOOGLE_APPLICATION_CREDENTIALS").empty()) &&
|
||||
!CloudContext::from_env().project.empty();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(google_live_sandbox, operations_read_only) {
|
||||
// Read-only: operations of resources that cannot exist. Observed 2026-10-24:
|
||||
// list answers 200 {} (empty), get answers 404 NOT_FOUND "Operation ... not found."
|
||||
if (!live_ready()) {
|
||||
std::cout << "SKIP google_live_sandbox.operations_read_only (set MOLO_LIVE_NET=1, "
|
||||
"a Google credential and GOOGLE_CLOUD_PROJECT)\n";
|
||||
return;
|
||||
}
|
||||
CloudContext ctx = CloudContext::from_env();
|
||||
std::string engine = ctx.child("reasoningEngines", "1234567890123456789");
|
||||
Operations ops(GoogleClient::from_env());
|
||||
ASSERT_TRUE(ops.list(engine + "/sandboxEnvironments/1234567890123456789").empty());
|
||||
for (const char* family : {"sandboxEnvironments", "sandboxEnvironmentTemplates",
|
||||
"sandboxEnvironmentSnapshots"}) {
|
||||
try {
|
||||
ops.get(engine + "/" + family + "/1234567890123456789/operations/1");
|
||||
FAIL("an operation that cannot exist must not be found");
|
||||
} catch (const GoogleError& e) {
|
||||
std::cout << " live: " << family << " " << e.http_status() << " " << e.status() << "\n";
|
||||
ASSERT_EQ(e.http_status(), 404);
|
||||
ASSERT_EQ(e.status(), std::string("NOT_FOUND"));
|
||||
ASSERT_TRUE(e.message().find("Operation") != std::string::npos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(google_live_sandbox, sign_jwt_probe) {
|
||||
// signJwt creates no cloud resource; it signs a small claim set with the
|
||||
// service account. Observed 2026-10-24: 403 because the IAM Credentials API
|
||||
// is not enabled on the project (route, path and body shape were accepted
|
||||
// far enough to reach that check). With the API enabled and
|
||||
// roles/iam.serviceAccountTokenCreator this returns a three part JWT.
|
||||
if (!live_ready()) {
|
||||
std::cout << "SKIP google_live_sandbox.sign_jwt_probe (set MOLO_LIVE_NET=1, "
|
||||
"a Google credential and GOOGLE_CLOUD_PROJECT)\n";
|
||||
return;
|
||||
}
|
||||
DataPlaneOptions o = DataPlaneOptions::from_env();
|
||||
if (o.service_account_email.empty()) {
|
||||
std::cout << "SKIP google_live_sandbox.sign_jwt_probe (no service account email)\n";
|
||||
return;
|
||||
}
|
||||
IamJwtSource src(GoogleClient::from_env(), o.service_account_email);
|
||||
try {
|
||||
std::string jwt = src.token();
|
||||
ASSERT_EQ(std::count(jwt.begin(), jwt.end(), '.'), 2);
|
||||
std::cout << " live: signJwt returned a JWT\n";
|
||||
} catch (const GoogleError& e) {
|
||||
std::cout << " live: signJwt " << e.http_status() << " " << e.status() << "\n";
|
||||
ASSERT_TRUE(e.http_status() == 403 || e.http_status() == 400);
|
||||
}
|
||||
}
|
||||
@@ -373,27 +373,89 @@ TEST(sandbox_bodies, snapshot_and_template) {
|
||||
}
|
||||
|
||||
TEST(sandbox_bodies, execute_inputs) {
|
||||
Json req = ChunkCodec::request({ChunkCodec::code_input("print(1)", {{"a.txt", "hi"}})});
|
||||
// Code: one JSON chunk {"code"}, then one chunk per file (SDK shape).
|
||||
std::vector<Chunk> code = ChunkCodec::code_inputs(
|
||||
"print(1)", {{"a.txt", "hi", ""}, {"b.bin", "x", "application/x-test"}});
|
||||
ASSERT_EQ(code.size(), 3u);
|
||||
Json req = ChunkCodec::request(code);
|
||||
expect_keys_declared(req, "sandbox.execute");
|
||||
const Json& chunk = req.find("inputs")->as_array().front();
|
||||
ASSERT_EQ(chunk.find("mimeType")->as_string(), std::string("application/json"));
|
||||
// data is base64 on the wire: it must not be the plain JSON text.
|
||||
ASSERT_TRUE(chunk.find("data")->as_string().find('{') == std::string::npos);
|
||||
Chunk back = Chunk::from_json(chunk);
|
||||
Json payload = Json::parse(back.data);
|
||||
Json payload = Json::parse(Chunk::from_json(chunk).data);
|
||||
ASSERT_EQ(payload.find("code")->as_string(), std::string("print(1)"));
|
||||
ASSERT_EQ(payload.find("files")->as_array().size(), 1u);
|
||||
Json sh = Json::parse(ChunkCodec::shell_input("ls", "/workspace", 5).data);
|
||||
ASSERT_EQ(sh.find("command")->as_string(), std::string("ls"));
|
||||
ASSERT_EQ(sh.find("timeout")->as_int(), 5);
|
||||
Json cdp = Json::parse(ChunkCodec::cdp_input("Page.navigate", ComputerUse::navigate_params("https://a.b")).data);
|
||||
ASSERT_EQ(cdp.find("command")->as_string(), std::string("Page.navigate"));
|
||||
ASSERT_EQ(cdp.find("params")->find("url")->as_string(), std::string("https://a.b"));
|
||||
ASSERT_TRUE(payload.find("files") == nullptr);
|
||||
ASSERT_EQ(code[1].mime_type, std::string("text/plain"));
|
||||
ASSERT_EQ(code[1].data, std::string("hi"));
|
||||
ASSERT_EQ(code[1].attributes.at("file_name"), std::string("a.txt"));
|
||||
ASSERT_EQ(code[2].mime_type, std::string("application/x-test"));
|
||||
// The file name attribute is base64 on the wire too (Discovery: format byte).
|
||||
Json wire = req.find("inputs")->as_array()[1];
|
||||
ASSERT_EQ(wire.find("metadata")->find("attributes")->find("file_name")->as_string(),
|
||||
std::string("YS50eHQ="));
|
||||
|
||||
// Shell: request URI, port, then the JSON command (SDK execute_bash).
|
||||
std::vector<Chunk> sh = ChunkCodec::shell_inputs("ls", "/workspace", 5);
|
||||
ASSERT_EQ(sh.size(), 3u);
|
||||
ASSERT_EQ(sh[0].mime_type, std::string("application/x.sandbox-request-uri"));
|
||||
ASSERT_EQ(sh[0].data, std::string("/exec"));
|
||||
ASSERT_EQ(sh[1].mime_type, std::string("application/x.sandbox-request-port"));
|
||||
ASSERT_EQ(sh[1].data, std::string("8080"));
|
||||
Json cmd = Json::parse(sh[2].data);
|
||||
ASSERT_EQ(cmd.find("command")->as_string(), std::string("ls"));
|
||||
ASSERT_EQ(cmd.find("cwd")->as_string(), std::string("/workspace"));
|
||||
ASSERT_EQ(cmd.find("timeout")->as_int(), 5);
|
||||
|
||||
ASSERT_INVALID(ChunkCodec::request({}));
|
||||
std::vector<Chunk> many(SandboxCaps::kMaxInputChunks + 1);
|
||||
ASSERT_INVALID(ChunkCodec::request(many));
|
||||
|
||||
Json m = ComputerUse::mouse_params("mousePressed", 3, 4);
|
||||
ASSERT_EQ(m.find("type")->as_string(), std::string("mousePressed"));
|
||||
ASSERT_EQ(m.find("x")->as_int(), 3);
|
||||
}
|
||||
|
||||
TEST(sandbox_codec, shell_and_code_outputs_decode) {
|
||||
// Output shapes from the docs and SDK (msg_out/msg_err, stdout/stderr/returncode),
|
||||
// built here with our own encoder: this proves the decoder, not Google.
|
||||
auto reply_of = [](const Json& payload) {
|
||||
Json outs = Json::array();
|
||||
outs.push(Chunk::json_payload(payload).to_json());
|
||||
Json r = Json::object();
|
||||
r.set("outputs", outs);
|
||||
return r;
|
||||
};
|
||||
Json sh = Json::parse(R"({"stdout":"hi\n","stderr":"e","returncode":2,"duration_ms":8})");
|
||||
ExecutionResult a = ChunkCodec::decode(reply_of(sh));
|
||||
ASSERT_EQ(a.std_out, std::string("hi\n"));
|
||||
ASSERT_EQ(a.std_err, std::string("e"));
|
||||
ASSERT_EQ(*a.exit_status, 2);
|
||||
ASSERT_EQ(a.duration_ms, 8);
|
||||
ASSERT_TRUE(!a.ok());
|
||||
Json code = Json::parse(R"({"msg_out":"o","msg_err":"","output_files":[{"name":"f.txt","content":"aGk="}]})");
|
||||
ExecutionResult b = ChunkCodec::decode(reply_of(code));
|
||||
ASSERT_EQ(b.std_out, std::string("o"));
|
||||
ASSERT_EQ(b.files.size(), 1u);
|
||||
ASSERT_EQ(b.files[0].content, std::string("hi"));
|
||||
// A non-base64 attribute value is skipped loudly, never guessed.
|
||||
Json bad = Json::object();
|
||||
bad.set("mimeType", Json("text/plain"));
|
||||
bad.set("data", Json("aGk="));
|
||||
Json attrs = Json::object();
|
||||
attrs.set("file_name", Json("not base64!"));
|
||||
Json meta = Json::object();
|
||||
meta.set("attributes", attrs);
|
||||
bad.set("metadata", meta);
|
||||
Json outs = Json::array();
|
||||
outs.push(bad);
|
||||
Json r = Json::object();
|
||||
r.set("outputs", outs);
|
||||
ExecutionResult c = ChunkCodec::decode(r);
|
||||
ASSERT_TRUE(c.files.empty());
|
||||
ASSERT_EQ(c.skipped.size(), 1u);
|
||||
}
|
||||
|
||||
TEST(sandbox_codec, chunk_round_trip_with_attributes) {
|
||||
Chunk c;
|
||||
c.mime_type = "text/plain";
|
||||
@@ -507,7 +569,7 @@ TEST(sandbox_requests, shapes_over_a_failing_transport) {
|
||||
seen = probe([](const auto& c) { Sandboxes(c).pause(kSandbox); });
|
||||
ASSERT_EQ(seen[0].target, "/v1/" + kSandbox + ":pause");
|
||||
|
||||
seen = probe([](const auto& c) { StreamExecutor(c).run(kSandbox, {ChunkCodec::shell_input("ls", "", 5)}); });
|
||||
seen = probe([](const auto& c) { StreamExecutor(c).run(kSandbox, ChunkCodec::shell_inputs("ls", "", 5)); });
|
||||
ASSERT_TRUE(!seen.empty());
|
||||
ASSERT_EQ(seen[0].target, "/v1beta1/" + kSandbox + ":bidiExecute");
|
||||
}
|
||||
@@ -521,9 +583,10 @@ TEST(sandbox_calls, table_is_well_formed) {
|
||||
ASSERT_TRUE(std::string(c.path).rfind("{+", 0) == 0);
|
||||
std::string v = c.version;
|
||||
ASSERT_TRUE(v == "v1" || v == "v1beta1");
|
||||
ASSERT_TRUE(std::string(c.rpc).rfind("aiplatform.projects.locations.reasoningEngines.", 0) == 0);
|
||||
ASSERT_TRUE(std::string(c.rpc).rfind("aiplatform.projects.locations.reasoningEngines.", 0) == 0 ||
|
||||
std::string(c.rpc) == "iamcredentials.projects.serviceAccounts.signJwt");
|
||||
}
|
||||
ASSERT_EQ(sizeof(kDeclaredCalls) / sizeof(kDeclaredCalls[0]), 18u);
|
||||
ASSERT_EQ(sizeof(kDeclaredCalls) / sizeof(kDeclaredCalls[0]), 34u);
|
||||
}
|
||||
|
||||
TEST(google_live_sandbox, read_only_probe) {
|
||||
|
||||
Reference in New Issue
Block a user