Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/hub/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ fn scoped_route(
) => Some(CHAT),
("POST", "/api/agent-runs/{run_id}/answer") => Some(ANSWER),
("POST", "/api/agent-runs/{run_id}/message") => Some(MESSAGE),
// Files for the agent, saved on the run's machine (routes/agent_runs/files.rs)
(
"POST",
"/api/agent-runs/{run_id}/files/begin"
| "/api/agent-runs/{run_id}/files/chunk"
| "/api/agent-runs/{run_id}/files/end",
) => Some(MESSAGE),
("POST", "/api/agent-runs") => Some(START),
("GET", "/api/machines/{machine_id}/agents") => Some(START),
// Answering, messaging and starting take the machine's control lease, as the CLI does
Expand Down
52 changes: 51 additions & 1 deletion crates/hub/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tokio_util::sync::CancellationToken;

use crate::auth::AuthUser;
use crate::db::{self, integrations::{self, Integration}};
use crate::routes::agent_runs::files;
use crate::AppState;

/// The protocol version this hub speaks (`hello.protocol`).
Expand Down Expand Up @@ -429,6 +430,21 @@ enum ServerMessage {
limit: Option<u32>,
},
Cancel { command_id: String, run_id: String },
/// Starts saving a file for a run's agent on its machine (runs:message).
UploadBegin {
command_id: String,
run_id: String,
name: String,
size: u64,
},
/// The next chunk of an upload. `data` is never logged or kept.
UploadChunk {
command_id: String,
upload_id: String,
index: u32,
data: String,
},
UploadEnd { command_id: String, upload_id: String },
Pong,
#[serde(other)]
Other,
Expand Down Expand Up @@ -488,7 +504,10 @@ async fn handle(state: &AppState, bridge: &Integration, text: &str) -> Option<Va
ServerMessage::Answer { command_id, .. }
| ServerMessage::Start { command_id, .. }
| ServerMessage::Message { command_id, .. }
| ServerMessage::Cancel { command_id, .. } => command_id.clone(),
| ServerMessage::Cancel { command_id, .. }
| ServerMessage::UploadBegin { command_id, .. }
| ServerMessage::UploadChunk { command_id, .. }
| ServerMessage::UploadEnd { command_id, .. } => command_id.clone(),
};
// A command seen before gets the same result, not a second go
if let Some(previous) = state
Expand Down Expand Up @@ -558,6 +577,8 @@ fn bridge_caller(bridge: &Integration) -> AuthUser {
}
}

const UPLOAD_SCOPE: &str = "This bridge can't attach files; it needs runs:message";

/// Carries out a command as the bridge: its access decides, and it's audited.
async fn command(state: &AppState, bridge: &Integration, message: ServerMessage) -> Value {
let caller = bridge_caller(bridge);
Expand Down Expand Up @@ -626,6 +647,35 @@ async fn command(state: &AppState, bridge: &Integration, message: ServerMessage)
Err(refused) => refusal(refused),
}
}
ServerMessage::UploadBegin { run_id, name, size, .. } => {
if !caller.allows(TokenScope::RunsMessage) {
let reason = UPLOAD_SCOPE;
caller.audit(&state.db, "upload", &run_id, reason);
return json!({ "ok": false, "status": 403, "error": reason });
}
match files::upload_begin_as(state, &caller, &run_id, &name, size).await {
Ok(begun) => json!({ "ok": true, "upload_id": begun.upload_id, "chunk_bytes": begun.chunk_bytes }),
Err(refused) => refusal(refused),
}
}
ServerMessage::UploadChunk { upload_id, index, data, .. } => {
if !caller.allows(TokenScope::RunsMessage) {
return json!({ "ok": false, "status": 403, "error": UPLOAD_SCOPE });
}
match files::upload_chunk_as(state, &caller, None, &upload_id, index, data).await {
Ok(received) => json!({ "ok": true, "received": received }),
Err(refused) => refusal(refused),
}
}
ServerMessage::UploadEnd { upload_id, .. } => {
if !caller.allows(TokenScope::RunsMessage) {
return json!({ "ok": false, "status": 403, "error": UPLOAD_SCOPE });
}
match files::upload_end_as(state, &caller, None, &upload_id).await {
Ok((path, size)) => json!({ "ok": true, "path": path, "size": size }),
Err(refused) => refusal(refused),
}
}
ServerMessage::Cancel { run_id, .. } => {
let reason = "This hub can't do that yet";
caller.audit(&state.db, "denied", &run_id, reason);
Expand Down
162 changes: 162 additions & 0 deletions crates/hub/src/bridge/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,165 @@ fn bridge_urls_are_wss_or_this_machine() {
assert!(new_key().starts_with("obk_") && new_key() != new_key());
assert!(backoff(0) >= Duration::from_millis(750) && backoff(20) <= Duration::from_secs(75));
}

/// A hub with one person, machine-a, an open run and an ended one.
fn upload_state() -> (AppState, std::path::PathBuf, String, String) {
let path = std::env::temp_dir().join(format!("offdesk-bridge-{}.db", uuid::Uuid::new_v4()));
let pool = db::create_pool(&path.to_string_lossy()).unwrap();
let (open, ended) = {
let conn = pool.get().unwrap();
db::init_db(&conn).unwrap();
db::users::create_user(&conn, "user-a", "test", "user-a", "user-a", None, "admin").unwrap();
db::machines::ensure_machine_for_user(&conn, "machine-a", "user-a", "studio", Some("linux"), Some("/tmp")).unwrap();
let open = db::agent_runs::insert(&conn, "machine-a", "t1", RunAgent::Claude, "/w", AgentRunStatus::Working).unwrap();
let ended = db::agent_runs::insert(&conn, "machine-a", "t2", RunAgent::Claude, "/w", AgentRunStatus::Working).unwrap();
db::agent_runs::set_status(&conn, &ended.id, AgentRunStatus::Done).unwrap();
(open.id, ended.id)
};
let state = AppState {
manager: Arc::new(MachineManager::new(pool.clone())),
router: Arc::new(HubRouter::new()),
web_previews: Arc::new(crate::web_preview::registry::Registry::default()),
db: pool,
jwt_secret: "test-secret".to_string(),
base_url: "http://localhost:4317".to_string(),
dev_mode: false,
github_client_id: None,
github_client_secret: None,
google_client_id: None,
google_client_secret: None,
};
(state, path, open, ended)
}

fn upload_bridge(id: &str, scopes: Vec<TokenScope>) -> Integration {
Integration {
id: id.into(),
user_id: "user-a".into(),
kind: "bridge".into(),
name: id.into(),
url: "ws://127.0.0.1:1/".into(),
secret: new_key(),
events: vec![],
access: TokenAccess { scopes, content: ContentLevel::Requests, ..Default::default() },
enabled: false,
created_at: 0,
last_delivery_at: None,
last_error: None,
pairing_code: None,
}
}

async fn send(state: &AppState, bridge: &Integration, command: Value) -> Value {
handle(state, bridge, &command.to_string()).await.expect("a result")
}

#[tokio::test]
async fn a_bridge_saves_files_for_a_run_on_its_machine() {
use base64::Engine;
use offdesk_protocol::{FsOp, FsOpErrorCode, HubToMachine, MachineInfo, MachineToHub, OnConflict};
let (state, path, open, ended) = upload_state();
let reader = upload_bridge("bridge-r", vec![TokenScope::RunsRead]);
let bridge = upload_bridge("bridge-m", vec![TokenScope::RunsRead, TokenScope::RunsMessage]);
let other = upload_bridge("bridge-o", vec![TokenScope::RunsRead, TokenScope::RunsMessage]);
for b in [&reader, &bridge, &other] {
integrations::insert(&state.db.get().unwrap(), b).unwrap();
}
let begin = |command_id: &str, run_id: &str, name: &str, size: u64| {
json!({ "type": "upload_begin", "command_id": command_id, "run_id": run_id, "name": name, "size": size })
};

// Refusals: scope, unknown run, ended run, too big, machine offline
let result = send(&state, &reader, begin("c1", &open, "a.png", 3)).await;
assert_eq!((result["type"].as_str(), result["command_id"].as_str(), result["status"].as_u64()), (Some("result"), Some("c1"), Some(403)));
assert_eq!(result["error"], "This bridge can't attach files; it needs runs:message");
assert_eq!(send(&state, &bridge, begin("c2", "nope", "a.png", 3)).await["status"], 404);
assert_eq!(send(&state, &bridge, begin("c3", &ended, "a.png", 3)).await["status"], 409);
assert_eq!(send(&state, &bridge, begin("c4", &open, "a.png", 100 * 1024 * 1024 + 1)).await["status"], 413);
let result = send(&state, &bridge, begin("c5", &open, "a.png", 3)).await;
assert_eq!((result["status"].as_u64(), result["error"].as_str()), (Some(503), Some("The machine is offline")));

// A node without file operations
let info = |id: &str| MachineInfo { id: id.into(), name: "studio".into(), os: "linux".into(), home_dir: "/home/me".into(), production: false };
let (_, _old) = state.manager.register_machine_with_capabilities(info("machine-a"), Some("user-a".into()), vec![]).await;
let result = send(&state, &bridge, begin("c6", &open, "a.png", 3)).await;
assert_eq!(result["status"], 503);
assert!(result["error"].as_str().unwrap().starts_with("Update Offdesk on that machine"));

// A node that can: folders one level at a time ("exists" is fine), then the upload
let (_, mut commands) = state
.manager
.register_machine_with_capabilities(info("machine-a"), Some("user-a".into()), vec![offdesk_protocol::FS_OPS_CAPABILITY.into()])
.await;
let dir = format!("~/.offdesk/attachments/{open}");
let saved = format!("/home/me/.offdesk/attachments/{open}/passwd");
let made = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
let machine_side = {
let (manager, dir, saved, made) = (state.manager.clone(), dir.clone(), saved.clone(), made.clone());
tokio::spawn(async move {
let mut received = 0u64;
while let Some(command) = commands.recv().await {
let reply = match command {
HubToMachine::FsOp { request_id, op: FsOp::Mkdir { path } } => {
let first = {
let mut made = made.lock().unwrap();
made.push(path.clone());
made.len() == 1
};
if first {
MachineToHub::FsOpError { request_id, code: FsOpErrorCode::Exists, message: "exists".into(), path: Some(path) }
} else {
MachineToHub::FsOpResult { request_id, paths: vec![path] }
}
}
HubToMachine::FsUploadBegin { request_id, dest_dir, filename, size, on_conflict, chunk_bytes, .. } => {
assert_eq!((dest_dir.as_str(), filename.as_str(), size, on_conflict, chunk_bytes), (dir.as_str(), "passwd", 5, OnConflict::KeepBoth, None));
MachineToHub::FsUploadAck { request_id, received: 0, parallel: false }
}
HubToMachine::FsUploadChunk { request_id, data, .. } => {
received += base64::engine::general_purpose::STANDARD.decode(data).unwrap().len() as u64;
MachineToHub::FsUploadAck { request_id, received, parallel: false }
}
HubToMachine::FsUploadEnd { request_id, .. } => {
MachineToHub::FsUploadDone { request_id, path: saved.clone(), size: received }
}
HubToMachine::FsUploadAbort { .. } => continue,
other => panic!("unexpected {other:?}"),
};
manager.handle_machine_message("machine-a", reply).await;
}
})
};
let result = send(&state, &bridge, begin("c7", &open, "../../etc/passwd", 5)).await;
assert_eq!((result["ok"].as_bool(), result["chunk_bytes"].as_u64()), (Some(true), Some(1024 * 1024)));
let upload_id = result["upload_id"].as_str().unwrap().to_string();
let chunk = |command_id: &str, index: u32, bytes: &[u8]| {
let data = base64::engine::general_purpose::STANDARD.encode(bytes);
json!({ "type": "upload_chunk", "command_id": command_id, "upload_id": upload_id, "index": index, "data": data })
};
// Only the bridge that began it goes on
assert_eq!(send(&state, &other, chunk("o1", 0, b"abc")).await["status"], 404);
let result = send(&state, &bridge, chunk("c8", 0, b"abc")).await;
assert_eq!((result["ok"].as_bool(), result["received"].as_u64()), (Some(true), Some(3)));
// A repeat gets the same result, not a second write
assert_eq!(send(&state, &bridge, chunk("c8", 0, b"abc")).await["received"], 3);
let kept = integrations::command_result(&state.db.get().unwrap(), "bridge-m", "c8").unwrap().unwrap();
assert!(!kept.contains("YWJj"), "a chunk's data isn't kept");
assert_eq!(send(&state, &bridge, chunk("c9", 1, b"de")).await["received"], 5);
let end = |command_id: &str| json!({ "type": "upload_end", "command_id": command_id, "upload_id": upload_id });
assert_eq!(send(&state, &other, end("o2")).await["status"], 404);
let result = send(&state, &bridge, end("c10")).await;
assert_eq!((result["ok"].as_bool(), result["path"].as_str(), result["size"].as_u64()), (Some(true), Some(saved.as_str()), Some(5)));
// Done: the upload is gone
assert_eq!(send(&state, &bridge, end("c11")).await["status"], 404);
assert_eq!(*made.lock().unwrap(), ["~/.offdesk".to_string(), "~/.offdesk/attachments".into(), dir.clone()]);

let audit = db::tokens::list_audit(&state.db.get().unwrap(), "user-a", Some("bridge-m"), 20).unwrap();
let actions: Vec<(&str, &str, &str)> = audit.iter().map(|a| (a.action.as_str(), a.target.as_str(), a.result.as_str())).collect();
assert!(actions.contains(&("upload_end", saved.as_str(), "ok, 5 bytes")), "{actions:?}");
assert!(actions.contains(&("upload", open.as_str(), "ok")), "{actions:?}");
drop(state);
machine_side.abort();
let _ = machine_side.await;
let _ = std::fs::remove_file(&path);
}
16 changes: 16 additions & 0 deletions crates/hub/src/routes/agent_runs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use crate::auth::AuthUser;
use crate::db::agent_runs;
use crate::AppState;

pub(crate) mod files;

/// The machines a scoped token may reach; None: any of the caller's (full access).
fn reachable_machines(
db: &rusqlite::Connection,
Expand Down Expand Up @@ -723,6 +725,7 @@ pub fn router() -> Router<AppState> {
"/api/machines/{machine_id}/sessions/{agent}/{session_id}/messages",
get(machine_session_conversation),
)
.merge(files::router())
}

#[cfg(test)]
Expand Down Expand Up @@ -974,11 +977,24 @@ mod tests {
("POST", "/api/auth/api-tokens"),
("POST", &format!("/api/agent-runs/{}/answer", waiting.id)),
("POST", "/api/agent-runs"),
("POST", &format!("/api/agent-runs/{}/files/begin", waiting.id)),
] {
let body = (method == "POST").then(|| serde_json::json!({}));
let (status, _) = call(&state, "odk_read", method, uri, body).await;
assert_eq!(status, StatusCode::FORBIDDEN, "{method} {uri}");
}
// Files for the agent take runs:message, and 100 MB at most
token(&state, "odk_message", Some(TokenAccess { scopes: vec![TokenScope::RunsMessage], ..Default::default() }));
let files = format!("/api/agent-runs/{}/files/begin", waiting.id);
let too_big = serde_json::json!({ "name": "a.mov", "size": 100 * 1024 * 1024 + 1 });
let (status, _) = call(&state, "odk_message", "POST", &files, Some(too_big)).await;
assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
let small = serde_json::json!({ "name": "a.png", "size": 3 });
let (status, body) = call(&state, "odk_message", "POST", &files, Some(small)).await;
assert_eq!((status, body.as_str()), (StatusCode::SERVICE_UNAVAILABLE, "Update Offdesk on that machine to attach files from here"));
let chunk = serde_json::json!({ "upload_id": "nope", "index": 0, "data": "" });
let (status, _) = call(&state, "odk_message", "POST", &format!("/api/agent-runs/{}/files/chunk", waiting.id), Some(chunk)).await;
assert_eq!(status, StatusCode::NOT_FOUND);
// ...and so are the WebSockets and the beacon, which take full access only
assert!(crate::auth::verify_bearer_token("odk_read", &state.db, &state.jwt_secret).is_err());
assert!(crate::auth::verify_bearer_token("odk_full", &state.db, &state.jwt_secret).is_ok());
Expand Down
Loading
Loading