Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

feat(cli): cua sb info --connection prints a Space's spacesd URL and token (CUA-1902) (#5551)

ddupontb8c764e34e2f
diff --git a/docs/content/docs/cua-cli/reference/cli/sandboxes.mdx b/docs/content/docs/cua-cli/reference/cli/sandboxes.mdx
index 29a761a547..94003822b5 100644
--- a/docs/content/docs/cua-cli/reference/cli/sandboxes.mdx
+++ b/docs/content/docs/cua-cli/reference/cli/sandboxes.mdx
@@ -210,12 +210,16 @@ Alias: `cua sandbox get`.
| Flag | Type | Default | Description |
| --- | --- | --- | --- |
| <span id="cua-sandbox-info--local"></span>`--local` | boolean | `false` | Look NAME up among local sandboxes only (same as `local:NAME`). |
+| <span id="cua-sandbox-info--connection"></span>`--connection` | boolean | `false` | Also print how to call the sandbox's cua-spacesd from your own code: its gRPC URL and bearer token (`authorization: Bearer <token>`). The token is a secret: anyone with it controls the sandbox. |
**Examples**
```bash test="cli-shape" id="ref-cua-sandbox-info"
cua sb info dev
cua sb info local:dev --json
+
+# The gRPC URL and token of its cua-spacesd, for your own client
+cua sb info dev --connection --json
```
### `cua sandbox suspend`
diff --git a/libs/cua/crates/cua-cli/src/sandbox.rs b/libs/cua/crates/cua-cli/src/sandbox.rs
index 778c984b54..5fb9b243ff 100644
--- a/libs/cua/crates/cua-cli/src/sandbox.rs
+++ b/libs/cua/crates/cua-cli/src/sandbox.rs
@@ -359,13 +359,21 @@ pub enum SandboxCmd {
visible_alias = "get",
after_help = "Examples:
cua sb info dev
- cua sb info local:dev --json"
+ cua sb info local:dev --json
+ # The gRPC URL and token of its cua-spacesd, for your own client
+ cua sb info dev --connection --json"
)]
Info {
/// Sandbox name or ref (`local:NAME`).
name: String,
#[command(flatten)]
at: Narrow,
+ /// Also print how to call the sandbox's cua-spacesd from your own
+ /// code: its gRPC URL and bearer token (`authorization: Bearer
+ /// <token>`). The token is a secret: anyone with it controls the
+ /// sandbox.
+ #[arg(long)]
+ connection: bool,
},
/// Suspend (pause/snapshot).
#[command(after_help = "Examples:
@@ -1714,13 +1722,40 @@ pub async fn run(
let name = at.apply(&name)?;
return port_forward(cua, &name, &spec, no_wait, json_global, out).await;
}
- SandboxCmd::Info { name, at } => {
+ SandboxCmd::Info {
+ name,
+ at,
+ connection,
+ } => {
let name = at.apply(&name)?;
let json = json_global;
match sbx.get(name.clone()).await {
Ok(i) => {
+ // The sandbox's own spacesd: URL and bearer token.
+ let spacesd = if connection {
+ // Through `cua daemon` the client gets the daemon's
+ // proxy route (a path under the daemon's URL) and the
+ // daemon's token, which only cua clients can use. A
+ // gRPC client of your own needs spacesd itself: open
+ // the sandbox from this process instead.
+ let env = if cua.mode() == cua_sdk::CuaMode::Daemon
+ && relay_space_id(&name).is_none()
+ {
+ let direct = Cua::embedded(cua_sdk::CuaConfig::default())?;
+ env_of(&direct, &name).await?
+ } else {
+ env_of(cua, &name).await?
+ };
+ Some((env.endpoint(), env.inner().token().map(str::to_owned)))
+ } else {
+ None
+ };
if json {
- line(out, info_json(&i).to_string());
+ let mut v = info_json(&i);
+ if let Some((url, token)) = &spacesd {
+ v["spacesd"] = serde_json::json!({ "url": url, "token": token });
+ }
+ line(out, v.to_string());
} else {
line(out, format!("Name: {}", i.name));
line(out, format!("Id: {}", i.id));
@@ -1735,6 +1770,13 @@ pub async fn run(
for (k, p) in s {
line(out, format!("Service: {k} ({p})"));
}
+ if let Some((url, token)) = &spacesd {
+ line(out, format!("Spacesd: {url}"));
+ line(
+ out,
+ format!("Token: {}", token.as_deref().unwrap_or("(none)")),
+ );
+ }
}
}
Err(e) => return Err(e),
diff --git a/libs/cua/crates/cua-cli/tests/sandbox_do.rs b/libs/cua/crates/cua-cli/tests/sandbox_do.rs
index 0d462f3a6b..e11ec327fe 100644
--- a/libs/cua/crates/cua-cli/tests/sandbox_do.rs
+++ b/libs/cua/crates/cua-cli/tests/sandbox_do.rs
@@ -965,3 +965,41 @@ async fn do_drives_a_relay_space() {
let state = read_json(&h.cua_home().join("do_target.json"));
assert_eq!(state["name"], id);
}
+
+/// `sb info --connection` prints the spacesd URL and token a harness of
+/// your own needs (and plain `sb info` never prints the token).
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn sandbox_info_connection_prints_the_spacesd_url_and_token() {
+ let env = fixtures::start_env(Some("s3cret-token"), None).await;
+ let h = Home::new();
+ direct_with(&h, &env.url, "dev", "s3cret-token").await;
+
+ let o = h
+ .run(&["--embedded", "--json", "sb", "info", "dev", "--connection"])
+ .await;
+ o.ok();
+ let spacesd = &o.json()["spacesd"];
+ assert_eq!(spacesd["token"], "s3cret-token", "{o:?}");
+ let url = spacesd["url"].as_str().unwrap_or_default();
+ assert_eq!(
+ url.trim_end_matches('/'),
+ env.url.trim_end_matches('/'),
+ "{o:?}"
+ );
+
+ let o = h
+ .run(&["--embedded", "sb", "info", "dev", "--connection"])
+ .await;
+ o.ok();
+ assert!(o.stdout.contains("Token: s3cret-token"), "{o:?}");
+ assert!(o.stdout.contains("Spacesd: http://"), "{o:?}");
+
+ for args in [
+ &["--embedded", "--json", "sb", "info", "dev"][..],
+ &["--embedded", "sb", "info", "dev"][..],
+ ] {
+ let o = h.run(args).await;
+ o.ok();
+ assert!(!o.stdout.contains("s3cret-token"), "{o:?}");
+ }
+}
diff --git a/scripts/docs-generators/cli-specs/cua.json b/scripts/docs-generators/cli-specs/cua.json
index 19e326b3e4..ccfc112ee6 100644
--- a/scripts/docs-generators/cli-specs/cua.json
+++ b/scripts/docs-generators/cli-specs/cua.json
@@ -3086,7 +3086,7 @@
},
{
"abstract": "Show one sandbox",
- "after_help": "Examples:\n cua sb info dev\n cua sb info local:dev --json",
+ "after_help": "Examples:\n cua sb info dev\n cua sb info local:dev --json\n # The gRPC URL and token of its cua-spacesd, for your own client\n cua sb info dev --connection --json",
"aliases": [
"get"
],
@@ -3124,6 +3124,17 @@
"repeatable": false,
"short_name": null,
"takes_value": false
+ },
+ {
+ "aliases": [],
+ "default_value": false,
+ "env": null,
+ "help": "Also print how to call the sandbox's cua-spacesd from your own code: its gRPC URL and bearer token (`authorization: Bearer <token>`). The token is a secret: anyone with it controls the sandbox",
+ "hidden": false,
+ "name": "connection",
+ "repeatable": false,
+ "short_name": null,
+ "takes_value": false
}
],
"hidden": false,