From 3374ffd34921e1de414e8b07f69753113f5ba5f9 Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 11:36:36 +0000 Subject: sandbox: ephemeral agent, wire protocol, and the hub's remote backend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The seam from the previous commits only had one body. This adds the other one: a sandbox-agent binary that dials the hub (outbound rendezvous — the hub never needs a route into an untrusted box), serves Run/Put/Get frames over length-prefixed JSON, streams the child's stdout/stderr as they fill, and dies — taking its child with it — when the connection drops or its TTL expires. The TTL watchdog is the backstop under every leak: hub crash, partition, a forgotten destroy. Hub side, RemoteBackend implements the same SandboxBackend: acquire waits for a dial-in and verifies the shared token and profile before a lease exists; a vanished sandbox fails the step, which is already the shape of an expired lease (I5), so the remote path adds no new failure vocabulary. Run frames carry the hub's child environment, so the §16a shim protocol reaches remote children exactly as local ones — which is what makes the end-to-end test honest: a real build, real frames, real log streaming into the SSE-tailed file, no root and no container. Workspace sync over the wire is deliberately not here: Run carries a hub-side cwd, honest for loopback-shaped deployments and this test tier, and the module says so rather than pretending. --- tests/remote_agent.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/remote_agent.rs (limited to 'tests') diff --git a/tests/remote_agent.rs b/tests/remote_agent.rs new file mode 100644 index 0000000..74a227b --- /dev/null +++ b/tests/remote_agent.rs @@ -0,0 +1,96 @@ +//! The remote-builder path, end to end: a real build driven over the real +//! agent protocol against the §16a shims. No root, no network beyond a +//! loopback socket, no container — but the frames, the rendezvous, the log +//! streaming and the blob transfer are the production ones. + +mod common; + +use std::io::Read; +use std::sync::Arc; +use std::time::Duration; + +use aur_build::pipeline::{self, Ctx}; +use aur_build::sandbox::remote::RemoteBackend; +use aur_build::sandbox::{SandboxBackend, SandboxSpec}; +use common::*; + +fn agent_bin() -> std::path::PathBuf { + shim_dir().join("sandbox-agent") +} + +fn approve_fixture(env: &Env, name: &str) -> aur_build::db::model::CandidateRow { + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); + let c = aur_build::review::candidates::pending_candidates(&env.db).unwrap().remove(0); + let dd = aur_build::review::diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + aur_build::review::approve::approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); + aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap() +} + +#[test] +fn a_full_build_runs_over_the_agent_protocol() { + let (env, _fx) = env_with_fixture("plain-make"); + let c = approve_fixture(&env, "plain-make"); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + + let mut ctx = Ctx::new(&env.cfg, &env.db, &id).unwrap(); + let backend = RemoteBackend::bind(&env.cfg, "127.0.0.1:0", "test-token").unwrap(); + let addr = backend.local_addr().to_string(); + ctx.backend = Arc::new(backend); + + // The sandbox dials the hub; the hub never dials it. + let mut agent = std::process::Command::new(agent_bin()) + .args(["--hub", &addr, "--token", "test-token", "--ttl", "30"]) + .spawn() + .expect("spawn sandbox-agent"); + + let out = pipeline::run_with(&ctx).unwrap(); + let _ = agent.kill(); + assert!(matches!(out, pipeline::Outcome::Succeeded { .. }), "{out:?}"); + + // The agent-streamed output landed in the same file the SSE tail reads. + let log = std::fs::read_to_string(env.cfg.build_log_path(&id)).unwrap(); + assert!( + log.contains("Making package"), + "agent-streamed output must reach the build log:\n{log}" + ); + + // The build really ran as the agent's child, with the shim protocol + // applied from the Run frame's environment. + let shimlog = env.cfg.hooks.shim_dir.clone().unwrap(); + assert_eq!(aur_build::shims::called_with(&shimlog, "makepkg", ""), 1); +} + +#[test] +fn blobs_round_trip_over_the_wire_and_a_bad_token_is_refused() { + let cfg = aur_build::config::Config::default(); + let backend = RemoteBackend::bind(&cfg, "127.0.0.1:0", "right-token").unwrap(); + let addr = backend.local_addr().to_string(); + + let mut good = std::process::Command::new(agent_bin()) + .args(["--hub", &addr, "--token", "right-token", "--ttl", "30"]) + .spawn() + .expect("spawn sandbox-agent"); + let sbx = backend + .acquire(&SandboxSpec::new("extra-x86_64", "blob-test", Duration::from_secs(10))) + .unwrap(); + let bytes = b"pinned artifact bytes".to_vec(); + let digest = aur_build::hash::hex(&bytes); + sbx.put(&digest, &mut std::io::Cursor::new(bytes.clone())).unwrap(); + let mut got = Vec::new(); + sbx.get(&digest).unwrap().read_to_end(&mut got).unwrap(); + assert_eq!(got, bytes, "blobs ride the wire by digest and come back byte-identical"); + sbx.destroy().unwrap(); + let _ = good.wait(); + + // A stranger's box is refused at the rendezvous, not leased and hoped about. + let mut bad = std::process::Command::new(agent_bin()) + .args(["--hub", &addr, "--token", "wrong-token", "--ttl", "30"]) + .spawn() + .expect("spawn sandbox-agent"); + let err = match backend.acquire(&SandboxSpec::new("extra-x86_64", "nope", Duration::from_secs(10))) { + Err(e) => e, + Ok(_) => panic!("a stranger's box must not be leased"), + }; + assert!(err.to_string().contains("token"), "{err}"); + let _ = bad.wait(); +} -- cgit v1.2.3