summaryrefslogtreecommitdiffstats
path: root/src/sandbox/mod.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-19 15:45:38 +0000
committeraur-build <aur-build@localhost>2026-11-19 15:45:38 +0000
commit8dcdce879b51b19603d8163d8a9bfb5f61ff1558 (patch)
tree57ba7dee86414eaf24de114a1df8209149e7b5bd /src/sandbox/mod.rs
parentec80c312b80ba51271fff0548c6d1e293d373455 (diff)
downloadaur-build-8dcdce879b51b19603d8163d8a9bfb5f61ff1558.tar.gz
aur-build-8dcdce879b51b19603d8163d8a9bfb5f61ff1558.zip
sandbox: agent-sandbox is a backend now, and nerdctl is gone
`sandbox_backend` was a string and `backend_for` matched it with `_ => loopback`, so the documented Kata backend was unreachable from configuration: a hub told to use agent-sandbox would have quietly built untrusted PKGBUILDs as its own user. The kind now parses, validates, and produces a real `KataBackend` over `K8sSandboxProvisioner`, with the CR's `runtimeClass`, `activeDeadlineSeconds`, nonce, image and cluster coordinates all traced back to the config that produced them. Kata shares a *backend* per configuration rather than only a listener: it routes each dial-in by the nonce in its `Hello` into a map that belongs to it, so two backends on one address would each see half the dial-ins and neither could complete a rendezvous. The cache key is a digest of the distinguishing fields so the token and bearer are not copied into a second place. The containerd/`nerdctl` payload builder is deleted rather than left unused. It was a second deployment shape with no provisioner, no config path and no live tier — a builder that advertises a route the configuration refuses is worse than no builder, because someone will eventually assume the route exists. DESIGN.md now names one provisioning shape, which is the one that exists.
Diffstat (limited to 'src/sandbox/mod.rs')
-rw-r--r--src/sandbox/mod.rs66
1 files changed, 58 insertions, 8 deletions
diff --git a/src/sandbox/mod.rs b/src/sandbox/mod.rs
index cdbfa02..13cf282 100644
--- a/src/sandbox/mod.rs
+++ b/src/sandbox/mod.rs
@@ -24,11 +24,15 @@
//! there is no "path on the sandbox" concept in the interface at all, which
//! removes a class of hostile-write bugs that a path-based API would invite.
+use std::collections::HashMap;
use std::io::Read;
+use std::net::{TcpListener, TcpStream};
use std::path::PathBuf;
-use std::sync::Arc;
+use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
+use crossbeam_channel::{Receiver, Sender};
+
use crate::config::Config;
use crate::error::Result;
@@ -151,15 +155,61 @@ pub trait SandboxBackend: Send + Sync {
pub fn backend_for(cfg: &Config) -> Result<Arc<dyn SandboxBackend>> {
cfg.validate()?;
match &cfg.sandbox {
- crate::config::Sandbox::Loopback => {
- Ok(Arc::new(loopback::LoopbackBackend::new(cfg)))
- }
+ crate::config::Sandbox::Loopback => Ok(Arc::new(loopback::LoopbackBackend::new(cfg))),
crate::config::Sandbox::Remote { listen, token, .. } => {
Ok(Arc::new(remote::RemoteBackend::bind(cfg, listen, token)?))
}
- crate::config::Sandbox::AgentSandbox { .. } => Err(crate::error::Error::invalid(concat!(
- "sandbox kind \"agent-sandbox\" is not wired up yet; refusing to run this build ",
- "on a backend it was not configured for",
- ))),
+ crate::config::Sandbox::AgentSandbox { .. } => {
+ let backend: Arc<dyn SandboxBackend> = kata::KataBackend::shared_for(cfg)?;
+ Ok(backend)
+ }
}
}
+
+/// A listener shared by every backend built from the same `(addr, token)`.
+#[derive(Clone)]
+pub(crate) struct Bound {
+ pub local: String,
+ pub rx: Receiver<TcpStream>,
+}
+
+/// One bound listener per `(address, token)` per process.
+///
+/// The listener is a property of the *process*, not of a build. Binding it per
+/// build — which is what happened while `Ctx::new` constructed a backend for
+/// every build — meant a second concurrent build hit `EADDRINUSE` on a fixed
+/// `listen` and failed for a reason that had nothing to do with its package.
+/// Sharing the queue is also exactly the semantics the agent protocol has: an
+/// agent dials in when it is booted, and whichever build is waiting takes it.
+///
+/// The token is part of the key on purpose. Two different tokens cannot share a
+/// listener, and reusing the first one's listener for a second, different token
+/// would be a fail-open surprise; forcing a real bind makes the mistake loud.
+pub(crate) fn bound_listener(addr: &str, token: &str) -> Result<Bound> {
+ static REGISTRY: OnceLock<Mutex<HashMap<(String, String), Bound>>> = OnceLock::new();
+ let registry = REGISTRY.get_or_init(|| Mutex::new(HashMap::new()));
+ let key = (addr.to_string(), token.to_string());
+ // A poisoned registry means a panic in some other thread's bind; the map
+ // itself is still sound, so recover rather than wedge every later build —
+ // the same posture as the publish lock.
+ let mut reg = registry.lock().unwrap_or_else(|p| p.into_inner());
+ if let Some(b) = reg.get(&key) {
+ return Ok(b.clone());
+ }
+ let listener = TcpListener::bind(addr)?;
+ let local = listener.local_addr()?.to_string();
+ let (tx, rx): (Sender<TcpStream>, Receiver<TcpStream>) = crossbeam_channel::unbounded();
+ std::thread::spawn(move || {
+ for stream in listener.incoming() {
+ match stream {
+ Ok(s) => {
+ let _ = tx.send(s);
+ }
+ Err(_) => break,
+ }
+ }
+ });
+ let bound = Bound { local, rx };
+ reg.insert(key, bound.clone());
+ Ok(bound)
+}