From 632a08c8543f0db533efc7bfb6728b50a77ae3d8 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 22:34:16 +0000 Subject: sandbox: the seam between the pipeline and where build code runs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The step machine spawned its own subprocesses, which quietly made "build code runs on this machine, inside a devtools chroot, under §14's sudo rules" a pipeline fact rather than a deployment fact. Every build subprocess now goes through SandboxBackend: acquire a lease, run argv shaped commands (never a shell string), move content-addressed blobs, destroy. The loopback backend is today's behavior wearing the interface - same child_env shim protocol, same captured output - which is why the shim suite passes untouched: a step that cannot tell loopback from a remote sandbox is the property under test. --- src/sandbox/loopback.rs | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/sandbox/loopback.rs (limited to 'src/sandbox/loopback.rs') diff --git a/src/sandbox/loopback.rs b/src/sandbox/loopback.rs new file mode 100644 index 0000000..1b31b1a --- /dev/null +++ b/src/sandbox/loopback.rs @@ -0,0 +1,133 @@ +//! The loopback backend: run sandbox commands as local subprocesses. +//! +//! This is today's behavior wearing the new interface — same `Config::child_env` +//! shim protocol (§16a), same captured output, same process as the hub. It is +//! what `cargo test` exercises and what a single machine with devtools runs; +//! the shim suite passing unchanged through this file is what makes the seam a +//! pure refactor rather than a re-architecture. +//! +//! Isolation here is *none*: the "sandbox" is the hub's own user. That is +//! honest — the loopback backend is for development, tests, and the local +//! chroot deployment where §14's sudo rules are the boundary — and the Kata +//! backend is where the machine boundary lives. + +use std::fs; +use std::io::Read; +use std::path::PathBuf; +use std::sync::Arc; + +use crate::config::Config; +use crate::error::{Error, Result}; +use crate::sandbox::{Cmd, Exit, Sandbox, SandboxBackend, SandboxInfo, SandboxSpec}; + +pub struct LoopbackBackend { + cfg: Config, +} + +impl LoopbackBackend { + pub fn new(cfg: &Config) -> Self { + Self { cfg: cfg.clone() } + } +} + +impl SandboxBackend for LoopbackBackend { + fn acquire(&self, spec: &SandboxSpec) -> Result> { + // The "pool" is this machine: acquire always succeeds, instantly, and + // the TTL is a promise the loopback sandbox does not have to keep + // because there is nothing to leak — the process dies with the hub. + let blobs = blob_root(&self.cfg).join(safe_name(&spec.task)); + fs::create_dir_all(&blobs)?; + Ok(Arc::new(LoopbackSandbox { + id: format!("loopback:{}", spec.task), + blobs, + cfg: self.cfg.clone(), + })) + } + + fn status(&self) -> Result> { + Ok(vec![SandboxInfo { + id: "loopback".into(), + profile: self.cfg.build_profile.clone(), + warm: true, + }]) + } +} + +pub struct LoopbackSandbox { + id: String, + blobs: PathBuf, + cfg: Config, +} + +impl Sandbox for LoopbackSandbox { + fn id(&self) -> &str { + &self.id + } + + fn run(&self, cmd: &Cmd, on_log: &mut dyn FnMut(&[u8])) -> Result { + let argv0 = cmd + .argv + .first() + .ok_or_else(|| Error::invalid("sandbox command has no argv[0]"))?; + let mut c = std::process::Command::new(argv0); + c.args(&cmd.argv[1..]); + if let Some(dir) = &cmd.cwd { + c.current_dir(dir); + } + // The shim protocol lives here, not in the steps: a step that cannot + // tell loopback from a remote sandbox is the property under test. + self.cfg.child_env(&mut c); + let out = c.output()?; + on_log(&out.stdout); + on_log(&out.stderr); + Ok(Exit { code: out.status.code(), stdout: out.stdout, stderr: out.stderr }) + } + + fn put(&self, digest: &str, r: &mut dyn Read) -> Result<()> { + let path = self.blob_path(digest)?; + let mut f = fs::File::create(&path)?; + std::io::copy(r, &mut f)?; + Ok(()) + } + + fn get(&self, digest: &str) -> Result> { + let path = self.blob_path(digest)?; + Ok(Box::new(fs::File::open(&path)?)) + } + + fn destroy(&self) -> Result<()> { + // Nothing to reclaim: the sandbox is this process. Blob scratch is + // disposable staging and falls to `gc` like the rest of `staging/`. + Ok(()) + } +} + +impl LoopbackSandbox { + /// Digests double as blob filenames, so they must be path-safe. The digests + /// this codebase produces are hex or `hex-sig`; anything else is a caller + /// bug (or, later, a hostile sandbox) and fails closed. + fn blob_path(&self, digest: &str) -> Result { + if digest.is_empty() || digest.len() > 128 { + return Err(Error::invalid(format!("blob digest {digest:?} is not usable"))); + } + if !digest.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') { + return Err(Error::invalid(format!("blob digest {digest:?} is not path-safe"))); + } + Ok(self.blobs.join(digest)) + } +} + +fn blob_root(cfg: &Config) -> PathBuf { + cfg.staging_dir().join("blobs") +} + +/// Filesystem-safe name for a task id, same spirit as `config::slug`. +fn safe_name(s: &str) -> String { + s.chars() + .map(|c| if c.is_ascii_alphanumeric() { c.to_ascii_lowercase() } else { '-' }) + .collect::() + .trim_matches('-') + .chars() + .take(64) + .collect() +} -- cgit v1.2.3