1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
//! 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<Arc<dyn Sandbox>> {
// 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<Vec<SandboxInfo>> {
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<Exit> {
if cmd.argv.is_empty() {
return Err(Error::invalid("sandbox command has no argv[0]"));
}
// Expand `blob:<digest>` argv elements to the staged blob's path —
// the loopback sandbox's blob dir is "inside the sandbox" as much as
// anything here is. An unstaged blob is a caller bug and fails closed.
let mut argv: Vec<String> = Vec::with_capacity(cmd.argv.len());
for a in &cmd.argv {
match a.strip_prefix("blob:") {
Some(digest) => {
let path = self.blob_path(digest)?;
if !path.exists() {
return Err(Error::fail_closed(format!(
"blob {digest} was never staged in sandbox {}",
self.id
)));
}
argv.push(path.display().to_string());
}
None => argv.push(a.clone()),
}
}
let mut c = std::process::Command::new(&argv[0]);
c.args(&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<Box<dyn Read>> {
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<PathBuf> {
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::<String>()
.trim_matches('-')
.chars()
.take(64)
.collect()
}
|