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:
@@ -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
|
||||
Reference in New Issue
Block a user