use std::collections::BTreeMap;
use std::io::Write;
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::path::{Component, Path, PathBuf};
use anyhow::{Context, Result, anyhow, bail};
use sha2::{Digest, Sha256};
use toml::Table;
use crate::config::{self, Build, Config, FeatureOptions, Mount, MountContext};
use crate::features::FeatureSource;
use crate::serve::fleet::code::{ROOT_SKIPS, ROOT_TAR_LIMIT, tar};
pub const HASH_DOMAIN: &str = "devsandbox/mesh-config/v1";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
pub exec: bool,
pub data: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct Bundle {
pub config_name: String,
pub config: Vec<u8>,
table: Table,
pub files: BTreeMap<String, File>,
pub dirs: std::collections::BTreeSet<String>,
}
fn put(out: &mut Sha256, field: &[u8]) {
out.update((field.len() as u64).to_be_bytes());
out.update(field);
}
fn hash_parts<'a>(table: &Table, files: impl IntoIterator<Item = (&'a str, bool, [u8; 32])>) -> [u8; 32] {
let mut h = Sha256::new();
put(&mut h, HASH_DOMAIN.as_bytes());
put(&mut h, toml::to_string(table).expect("a parsed table serializes").as_bytes());
for (path, exec, digest) in files {
put(&mut h, path.as_bytes());
put(&mut h, &[u8::from(exec)]);
put(&mut h, &digest);
}
h.finalize().into()
}
impl Bundle {
pub fn hash(&self) -> [u8; 32] {
hash_parts(&self.table, self.files.iter().map(|(path, f)| (path.as_str(), f.exec, Sha256::digest(&f.data).into())))
}
pub fn tar(&self) -> Result<Vec<u8>> {
let mut w = tar::Writer::default();
w.file(&self.config_name, false, &self.config)?;
for (path, f) in self.files.iter().filter(|(p, _)| **p != self.config_name) {
w.file(path, f.exec, &f.data)?;
}
Ok(w.finish())
}
}
pub fn bundle_path(data: &Path, hash: &[u8; 32]) -> PathBuf {
data.join("mesh").join("bundles").join(format!("{}.tar", hex::encode(hash)))
}
pub fn store(data: &Path, bundle: &Bundle) -> Result<PathBuf> {
let path = bundle_path(data, &bundle.hash());
let tar = bundle.tar()?;
if path.symlink_metadata().is_ok_and(|m| m.is_file()) && std::fs::read(&path).is_ok_and(|b| b == tar) {
return Ok(path);
}
let dir = path.parent().expect("a bundle path has a parent");
std::fs::create_dir_all(dir).with_context(|| format!("cannot create {}", dir.display()))?;
let leaf = path.file_name().and_then(|n| n.to_str()).unwrap_or_default();
let tmp = dir.join(format!(".{leaf}.{}-{}.tmp", std::process::id(), next_seq()));
let _ = std::fs::remove_file(&tmp);
let written = (|| {
let mut f = std::fs::File::options().write(true).create_new(true).mode(0o600).open(&tmp)?;
f.write_all(&tar)?;
f.sync_all()?;
drop(f);
std::fs::rename(&tmp, &path)?;
std::fs::File::open(dir)?.sync_all()
})();
if written.is_err() {
let _ = std::fs::remove_file(&tmp);
}
written.with_context(|| format!("cannot write {}", path.display()))?;
Ok(path)
}
pub fn roots_path(data: &Path) -> PathBuf {
data.join("mesh").join("roots.toml")
}
pub fn load_roots(path: &Path) -> Result<BTreeMap<String, PathBuf>> {
match std::fs::read_to_string(path) {
Ok(text) => toml::from_str(&text).with_context(|| format!("invalid {}", path.display())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeMap::new()),
Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
}
}
pub fn bound_root(data: &Path, name: &str) -> Result<Option<PathBuf>> {
Ok(load_roots(&roots_path(data))?.remove(name))
}
pub fn bind_in(roots: &mut BTreeMap<String, PathBuf>, name: &str, path: &Path) -> Result<(), PathBuf> {
match roots.get(name) {
Some(bound) if bound != path => Err(bound.clone()),
Some(_) => Ok(()),
None => {
roots.insert(name.to_string(), path.to_path_buf());
Ok(())
}
}
}
pub fn bind(data: &Path, name: &str, path: &Path) -> Result<Result<(), PathBuf>> {
crate::serve::locked_file::update(
&roots_path(data),
load_roots,
|r| toml::to_string(r).context("cannot serialize the config roots"),
|r| bind_in(r, name, path),
)
}
pub fn tree_path(data: &Path, name: &str) -> PathBuf {
data.join("mesh").join("tree").join(name)
}
pub fn installed_path(data: &Path) -> PathBuf {
data.join("mesh").join("installed.toml")
}
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
struct InstalledRoot {
hash: String,
config: String,
files: Vec<String>,
dirs: Vec<String>,
}
impl InstalledRoot {
fn listed(&self) -> Vec<String> {
let config = Some(&self.config).filter(|c| !c.is_empty() && !self.files.contains(c)).cloned();
config.into_iter().chain(self.files.iter().cloned()).collect()
}
}
fn load_installed(path: &Path) -> Result<BTreeMap<String, InstalledRoot>> {
match std::fs::read_to_string(path) {
Ok(text) => toml::from_str(&text).with_context(|| format!("invalid {}", path.display())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeMap::new()),
Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
}
}
pub fn installed(data: &Path) -> Result<BTreeMap<String, String>> {
Ok(load_installed(&installed_path(data))?.into_iter().map(|(name, r)| (name, r.hash)).collect())
}
pub fn copy_names(data: &Path) -> Result<std::collections::BTreeSet<String>> {
Ok(load_installed(&installed_path(data))?.into_keys().collect())
}
fn save_installed(path: &Path, roots: &BTreeMap<String, InstalledRoot>) -> Result<()> {
let text = toml::to_string(roots).context("cannot serialize the installed config roots")?;
crate::serve::locked_file::write_atomic_mode(path, &text, 0o644)
}
#[derive(Debug)]
struct BadBundle(String);
impl std::fmt::Display for BadBundle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for BadBundle {}
fn next_seq() -> u32 {
use std::sync::atomic::{AtomicU32, Ordering};
static SEQ: AtomicU32 = AtomicU32::new(0);
SEQ.fetch_add(1, Ordering::Relaxed)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Installed {
pub path: PathBuf,
pub removed: Vec<String>,
}
pub fn install(data: &Path, name: &str, tar: &[u8], head: &[u8; 32]) -> Result<Installed> {
super::membership::check_root_name(name).context("config root")?;
if let Some(root) = bound_root(data, name)? {
bail!("config root `{name}` is this host's own ({}): it gets no copy", root.display());
}
if tar.len() as u64 > ROOT_TAR_LIMIT {
return Err(BadBundle(format!("the config's bundle is over {ROOT_TAR_LIMIT} bytes")).into());
}
let mesh = data.join("mesh");
std::fs::create_dir_all(&mesh).with_context(|| format!("cannot create {}", mesh.display()))?;
let temp = mesh.join(format!(".in-{}-{}", std::process::id(), next_seq()));
let _ = std::fs::remove_dir_all(&temp);
std::fs::create_dir(&temp).with_context(|| format!("cannot create {}", temp.display()))?;
let unpacked = temp.join(name);
let bundle = tar::extract(tar, &unpacked).and_then(|()| collect(&unpacked));
let _ = std::fs::remove_dir_all(&temp);
let bundle = bundle.with_context(|| BadBundle(format!("the bundle of config root `{name}`")))?;
let got = bundle.hash();
if got != *head {
let why = format!("the bundle of config root `{name}` is {}, not the signed version ({})", hex::encode(got), hex::encode(head));
return Err(BadBundle(why).into());
}
store(data, &bundle)?;
let _lock = crate::serve::locked_file::lock_at(&installed_path(data).with_extension("lock"))?;
let mut installed = load_installed(&installed_path(data))?;
let previous = installed.get(name).cloned().unwrap_or_default();
let mut listed: Vec<(&str, bool, &[u8])> = vec![(bundle.config_name.as_str(), false, &bundle.config)];
listed.extend(bundle.files.iter().filter(|(p, _)| **p != bundle.config_name).map(|(p, f)| (p.as_str(), f.exec, f.data.as_slice())));
let entry = InstalledRoot {
hash: hex::encode(got),
config: bundle.config_name.clone(),
files: bundle.files.keys().cloned().collect(),
dirs: bundle.dirs.iter().cloned().collect(),
};
let (before, files) = (previous.listed(), entry.listed());
let mut union = before.clone();
union.extend(files.iter().filter(|f| !before.contains(f)).cloned());
installed.insert(
name.to_string(),
InstalledRoot { hash: previous.hash.clone(), config: previous.config.clone(), files: union, dirs: previous.dirs.clone() },
);
save_installed(&installed_path(data), &installed)?;
let tree = tree_path(data, name);
std::fs::create_dir_all(&tree).with_context(|| format!("cannot create {}", tree.display()))?;
for (rel, exec, bytes) in &listed {
write_in(&tree, rel, *exec, bytes)?;
}
let mut removed = Vec::new();
for rel in before.iter().filter(|f| !files.contains(f)) {
if tar::check_path(rel).is_err() || skipped(rel) {
continue;
}
if remove_in(&tree, rel)? {
removed.push(rel.clone());
}
}
for rel in strays(&tree, &entry, false)? {
remove_entry(&tree, &rel)?;
removed.push(rel);
}
installed.insert(name.to_string(), entry);
save_installed(&installed_path(data), &installed)?;
Ok(Installed { path: tree, removed })
}
pub fn roots(data: &Path) -> Vec<PathBuf> {
let names = load_installed(&installed_path(data)).map(|i| i.into_keys().collect()).unwrap_or_else(|_| Vec::new());
names.into_iter().filter_map(|name| std::fs::canonicalize(tree_path(data, &name)).ok()).collect()
}
pub fn copy_of(data: &Path, root: &Path) -> Option<(String, bool)> {
let tree = std::fs::canonicalize(data.join("mesh").join("tree")).ok()?;
let root = std::fs::canonicalize(root).ok()?;
let rel = root.strip_prefix(&tree).ok()?;
let mut parts = rel.components();
let name = parts.next()?.as_os_str().to_str()?.to_string();
Some((name, parts.next().is_none()))
}
pub fn root_id(bindings: &BTreeMap<String, PathBuf>, data: &Path, root: &Path) -> Option<String> {
let canonical = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
if let Some((name, _)) = bindings.iter().find(|(_, path)| **path == canonical) {
return Some(name.clone());
}
match copy_of(data, root) {
Some((name, true)) => Some(name),
_ => None,
}
}
fn in_mesh(data: &Path, root: &Path) -> bool {
let (Ok(mesh), Ok(root)) = (std::fs::canonicalize(data.join("mesh")), std::fs::canonicalize(root)) else {
return false;
};
root.starts_with(mesh)
}
pub fn verify(data: &Path, name: &str, head: Option<(u64, [u8; 32])>) -> Result<()> {
let installed = load_installed(&installed_path(data))?;
let (Some((version, head)), Some(copy)) = (head, installed.get(name)) else {
bail!("config root `{name}` has no signed version here yet");
};
if copy.hash != hex::encode(head) {
bail!("config root `{name}` v{version} isn't here yet");
}
let tree = tree_path(data, name);
let changed = || anyhow!("config root `{name}` isn't the signed config (v{version}): its copy was changed here");
match rehash(&tree, copy) {
Ok(h) if h == head => {}
_ => return Err(changed()),
}
match unlisted(&tree, copy) {
Ok(None) => Ok(()),
Ok(Some(rel)) => bail!("config root `{name}` isn't the signed config (v{version}): `{rel}` was added to its copy here"),
Err(_) => Err(changed()),
}
}
fn unlisted(tree: &Path, copy: &InstalledRoot) -> Result<Option<String>> {
Ok(strays(tree, copy, true)?.into_iter().next())
}
fn strays(tree: &Path, copy: &InstalledRoot, first: bool) -> Result<Vec<String>> {
fn walk(tree: &Path, rel: &str, listed: &std::collections::BTreeSet<String>, first: bool, out: &mut Vec<String>) -> Result<()> {
let path = tree.join(rel);
let meta = match path.symlink_metadata() {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(e).with_context(|| format!("cannot read {}", path.display())),
};
if !meta.is_dir() {
if !(meta.is_file() && listed.contains(rel)) {
out.push(rel.to_string());
}
return Ok(());
}
let mut names: Vec<String> = Vec::new();
for e in std::fs::read_dir(&path).with_context(|| format!("cannot list {}", path.display()))? {
let e = e.with_context(|| format!("cannot list {}", path.display()))?;
match e.file_name().into_string() {
Ok(n) => names.push(n),
Err(n) => {
out.push(format!("{rel}/{}", n.to_string_lossy()));
if first {
return Ok(());
}
}
}
}
names.sort();
for name in names {
if first && !out.is_empty() {
return Ok(());
}
let child = format!("{rel}/{name}");
if name == ".git" {
out.push(child);
continue;
}
if skipped(&child) {
continue;
}
walk(tree, &child, listed, first, out)?;
}
Ok(())
}
let listed: std::collections::BTreeSet<String> = copy.listed().into_iter().collect();
let mut out = Vec::new();
for rel in ©.dirs {
if first && !out.is_empty() {
break;
}
tar::check_path(rel)?;
let mut at = String::new();
let parts: Vec<&str> = rel.split('/').collect();
let mut reached = true;
for p in &parts[..parts.len() - 1] {
at = if at.is_empty() { p.to_string() } else { format!("{at}/{p}") };
match tree.join(&at).symlink_metadata() {
Ok(m) if m.is_dir() => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
reached = false;
break;
}
Ok(m) => {
if !(m.is_file() && listed.contains(&at)) && !out.contains(&at) {
out.push(at.clone());
}
reached = false;
break;
}
Err(e) => return Err(e).with_context(|| format!("cannot read {}", tree.join(&at).display())),
}
}
if reached {
walk(tree, rel, &listed, first, &mut out)?;
}
}
if first {
out.truncate(1);
}
Ok(out)
}
fn remove_entry(tree: &Path, rel: &str) -> Result<()> {
tar::check_path(rel)?;
let mut at = tree.to_path_buf();
let parts: Vec<&str> = rel.split('/').collect();
for p in &parts[..parts.len() - 1] {
at.push(p);
if !at.symlink_metadata().is_ok_and(|m| m.is_dir()) {
bail!("{} isn't a directory", at.display());
}
}
let path = tree.join(rel);
match path.symlink_metadata() {
Ok(m) if m.is_dir() => std::fs::remove_dir_all(&path),
Ok(_) => std::fs::remove_file(&path),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
.with_context(|| format!("cannot remove {}", path.display()))
}
pub fn check_root(root: &Path) -> Result<()> {
let data = crate::daemon_config::data_dir_from(std::env::var_os("XDG_DATA_HOME"), std::env::var_os("HOME"))?;
check_root_in(&data, &data.join("member.toml"), root)
}
pub fn check_root_in(data: &Path, member_toml: &Path, root: &Path) -> Result<()> {
let Some((name, exact)) = copy_of(data, root) else {
if in_mesh(data, root) {
bail!("`{}` is in the store of signed configs, not a config root: build from a copy, `mesh/tree/<name>`", root.display());
}
return Ok(());
};
if !exact {
bail!("`{}` is inside the copy of config root `{name}`, not a signed config", root.display());
}
let stored = super::membership_api::load(member_toml).map_err(|e| anyhow!("config root `{name}`: {}", e.message))?;
let head = stored.and_then(|s| s.log.membership().and_then(|m| m.config.get(&name).copied()));
verify(data, &name, head)
}
pub fn check_peer_root_in(data: &Path, member_toml: &Path, root: &Path, fleet_root: Option<&Path>) -> Result<()> {
check_root_in(data, member_toml, root)?;
if copy_of(data, root).is_some() {
return Ok(());
}
let canonical = |p: &Path| std::fs::canonicalize(p).ok();
if let (Some(fleet), Some(at)) = (fleet_root.and_then(canonical), canonical(root))
&& at.starts_with(fleet)
{
return Ok(());
}
let names: Vec<String> = [Some(root.to_path_buf()), std::fs::canonicalize(root).ok()]
.into_iter()
.flatten()
.filter_map(|p| p.file_name().and_then(|n| n.to_str()).map(str::to_string))
.collect();
if names.is_empty() {
return Ok(());
}
let stored = super::membership_api::load(member_toml).map_err(|e| anyhow!("config root `{}`: {}", root.display(), e.message))?;
let Some(m) = stored.as_ref().and_then(|s| s.log.membership()) else { return Ok(()) };
if let Some(name) = names.iter().find(|n| m.config.contains_key(n.as_str())) {
bail!(
"config root `{}` has the name of the signed config `{name}` but isn't its copy here ({}): run it from the copy",
root.display(),
tree_path(data, name).display()
);
}
Ok(())
}
fn rehash(tree: &Path, copy: &InstalledRoot) -> Result<[u8; 32]> {
let path = config::find(tree)?.with_context(|| format!("no config file in {}", tree.display()))?;
if path.file_name().and_then(|n| n.to_str()) != Some(copy.config.as_str()) {
bail!("{} isn't the installed config file", path.display());
}
let real = |rel: &str| -> Result<PathBuf> {
tar::check_path(rel)?;
let mut at = tree.to_path_buf();
let parts: Vec<&str> = rel.split('/').collect();
for p in &parts[..parts.len() - 1] {
at.push(p);
if !at.symlink_metadata().is_ok_and(|m| m.is_dir()) {
bail!("{} isn't a directory", at.display());
}
}
let path = tree.join(rel);
if !path.symlink_metadata().is_ok_and(|m| m.is_file()) {
bail!("{} isn't a file", path.display());
}
Ok(path)
};
let text = std::fs::read(real(©.config)?).with_context(|| format!("cannot read {}", path.display()))?;
let table = raw_table(&path, &text)?;
let mut files: Vec<(&str, bool, [u8; 32])> = Vec::new();
for rel in ©.files {
let path = real(rel)?;
let meta = std::fs::metadata(&path).with_context(|| format!("cannot read {}", path.display()))?;
let data = std::fs::read(&path).with_context(|| format!("cannot read {}", path.display()))?;
files.push((rel.as_str(), meta.permissions().mode() & 0o100 != 0, Sha256::digest(&data).into()));
}
files.sort_by(|a, b| a.0.cmp(b.0));
Ok(hash_parts(&table, files))
}
fn write_in(tree: &Path, rel: &str, exec: bool, bytes: &[u8]) -> Result<()> {
tar::check_path(rel)?;
let mut parts: Vec<&str> = rel.split('/').collect();
let leaf = parts.pop().expect("a checked path has a component");
let mut dir = tree.to_path_buf();
for p in parts {
dir.push(p);
match dir.symlink_metadata() {
Ok(m) if m.is_dir() => {}
Ok(_) => {
std::fs::remove_file(&dir).with_context(|| format!("cannot remove {}", dir.display()))?;
std::fs::create_dir(&dir).with_context(|| format!("cannot create {}", dir.display()))?
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
std::fs::create_dir(&dir).with_context(|| format!("cannot create {}", dir.display()))?
}
Err(e) => return Err(e).with_context(|| format!("cannot read {}", dir.display())),
}
}
let path = dir.join(leaf);
if path.symlink_metadata().is_ok_and(|m| m.is_dir()) {
std::fs::remove_dir_all(&path).with_context(|| format!("cannot remove {}", path.display()))?;
}
let tmp = dir.join(format!(".{leaf}.{}-{}.tmp", std::process::id(), next_seq()));
let _ = std::fs::remove_file(&tmp);
let mode = if exec { 0o755 } else { 0o644 };
let written = (|| {
let mut f = std::fs::File::options().write(true).create_new(true).mode(mode).open(&tmp)?;
f.write_all(bytes)?;
f.set_permissions(std::fs::Permissions::from_mode(mode))?;
drop(f);
std::fs::rename(&tmp, &path)
})();
if written.is_err() {
let _ = std::fs::remove_file(&tmp);
}
written.with_context(|| format!("cannot write {}", path.display()))
}
fn remove_in(tree: &Path, rel: &str) -> Result<bool> {
let mut at = tree.to_path_buf();
let parts: Vec<&str> = rel.split('/').collect();
for p in &parts[..parts.len() - 1] {
at.push(p);
if !at.symlink_metadata().is_ok_and(|m| m.is_dir()) {
return Ok(false);
}
}
let path = tree.join(rel);
match path.symlink_metadata() {
Ok(m) if !m.is_dir() => std::fs::remove_file(&path).with_context(|| format!("cannot remove {}", path.display()))?,
_ => return Ok(false),
}
let mut dir = path.parent().map(Path::to_path_buf);
while let Some(d) = dir.filter(|d| d != tree && d.starts_with(tree)) {
if std::fs::remove_dir(&d).is_err() {
break;
}
dir = d.parent().map(Path::to_path_buf);
}
Ok(true)
}
pub const BUNDLE_KIND: &str = "mesh-bundle";
fn is_bundle_name(name: &str) -> bool {
name.strip_suffix(".tar").is_some_and(|h| h.len() == 64 && h.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')))
}
pub fn serves(data: &Path, receivers: crate::serve::fleet::transfer::Receivers) -> crate::serve::fleet::transfer::Receivers {
let bundles = data.join("mesh").join("bundles");
receivers.serves(
BUNDLE_KIND,
move |name| {
if !is_bundle_name(name) {
bail!("`{name}` isn't a config bundle's name");
}
if !bundles.symlink_metadata().is_ok_and(|m| m.is_dir()) {
bail!("no {name} to pull here");
}
let path = bundles.join(name);
match path.symlink_metadata() {
Ok(m) if m.is_file() => Ok(path),
Ok(_) => bail!("{name} isn't a file"),
Err(_) => bail!("no {name} to pull here"),
}
},
|_| {},
)
}
pub fn pull_bundle(t: &dyn crate::serve::fleet::code::Transfer, data: &Path, hash: &[u8; 32]) -> Result<Vec<u8>> {
let mesh = data.join("mesh");
std::fs::create_dir_all(&mesh).with_context(|| format!("cannot create {}", mesh.display()))?;
let name = format!("{}.tar", hex::encode(hash));
let dest = mesh.join(format!(".pull-{}-{}.tar", std::process::id(), next_seq()));
let pulled = t.pull(BUNDLE_KIND, &name, ROOT_TAR_LIMIT, &dest).and_then(|_| std::fs::read(&dest).with_context(|| format!("cannot read {}", dest.display())));
let _ = std::fs::remove_file(&dest);
pulled.with_context(|| format!("cannot pull the config bundle {name}"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Due {
pub name: String,
pub version: u64,
pub hash: [u8; 32],
pub stored: bool,
pub dropped: Option<String>,
pub changed: Option<String>,
}
pub fn due(
heads: &BTreeMap<String, (u64, [u8; 32])>,
installed: &BTreeMap<String, String>,
bound: &BTreeMap<String, PathBuf>,
stored: &std::collections::BTreeSet<[u8; 32]>,
) -> Vec<Due> {
heads
.iter()
.filter(|(name, (_, hash))| !bound.contains_key(*name) && installed.get(*name) != Some(&hex::encode(hash)))
.map(|(name, (version, hash))| Due {
name: name.clone(),
version: *version,
hash: *hash,
stored: stored.contains(hash),
dropped: None,
changed: None,
})
.collect()
}
pub fn follow(
data: &Path,
heads: impl FnOnce() -> Result<BTreeMap<String, (u64, [u8; 32])>>,
from: &dyn crate::serve::fleet::code::Transfer,
) -> Result<Vec<(Due, Result<Installed>)>> {
static RUNS: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _one = RUNS.lock().unwrap_or_else(|e| e.into_inner());
let heads = heads()?;
let stored = heads.values().map(|(_, h)| *h).filter(|h| bundle_path(data, h).is_file()).collect();
let (installed, bound) = (installed(data)?, load_roots(&roots_path(data))?);
let mut due = due(&heads, &installed, &bound, &stored);
for (name, (version, hash)) in &heads {
if bound.contains_key(name) || installed.get(name) != Some(&hex::encode(hash)) {
continue;
}
if let Err(e) = verify(data, name, Some((*version, *hash))) {
let (stored, changed) = (stored.contains(hash), Some(format!("{e:#}")));
due.push(Due { name: name.clone(), version: *version, hash: *hash, stored, dropped: None, changed });
}
}
due.sort_by(|a, b| a.name.cmp(&b.name));
let checked = |d: &Due, r: Result<Installed>| {
r.and_then(|i| verify(data, &d.name, Some((d.version, d.hash))).map(|()| i).context("installed, but still not the signed config"))
};
Ok(due
.into_iter()
.map(|mut d| {
if d.stored {
let path = bundle_path(data, &d.hash);
let r = std::fs::read(&path)
.map_err(|e| anyhow::Error::new(e).context(BadBundle(format!("cannot read {}", path.display()))))
.and_then(|tar| install(data, &d.name, &tar, &d.hash));
match r {
Err(e) if e.downcast_ref::<BadBundle>().is_some() => {
let _ = std::fs::remove_file(&path);
d.stored = false;
d.dropped = Some(format!("{e:#}"));
}
r => {
let r = checked(&d, r);
return (d, r);
}
}
}
let r = pull_bundle(from, data, &d.hash).and_then(|tar| install(data, &d.name, &tar, &d.hash));
let r = checked(&d, r);
(d, r)
})
.collect())
}
pub fn collect(root: &Path) -> Result<Bundle> {
let config = Config::load(root)?;
let path = config::find(root)?.with_context(|| format!("no config file in {}", root.display()))?;
let config_name = path.file_name().and_then(|n| n.to_str()).context("the config file's name isn't UTF-8")?.to_string();
let text = std::fs::read(&path).with_context(|| format!("cannot read {}", path.display()))?;
let table = raw_table(&path, &text)?;
let canon = root.canonicalize().with_context(|| format!("cannot resolve {}", root.display()))?;
let mut refs: Vec<(PathBuf, bool)> = Vec::new();
let build_refs = |build: &Build, refs: &mut Vec<(PathBuf, bool)>| {
refs.extend(build.dockerfile.as_deref().map(|d| (PathBuf::from(d), false)));
refs.push((PathBuf::from(build.context.as_deref().unwrap_or(".")), true));
};
for name in config.sandboxes.keys() {
let (merged, _) = config.resolved_table(name).with_context(|| format!("sandbox `{name}`"))?;
let field = |key: &str| merged.get(key).cloned();
if let Some(build) = field("build") {
let build: Build = build.try_into().map_err(|e| anyhow!("sandbox `{name}`: build: {e}"))?;
build_refs(&build, &mut refs);
}
if let Some(mounts) = field("mounts") {
let mounts: Vec<Mount> = mounts.try_into().map_err(|e| anyhow!("sandbox `{name}`: mounts: {e}"))?;
refs.extend(mounts.iter().filter_map(|m| mount_ref(m, &canon)).map(|r| (r, false)));
}
if let Some(features) = field("features") {
let features: BTreeMap<String, FeatureOptions> =
features.try_into().map_err(|e| anyhow!("sandbox `{name}`: features: {e}"))?;
for (reference, options) in &features {
if matches!(options, FeatureOptions::Enabled(false)) {
continue;
}
if let Ok(FeatureSource::Local(p)) = FeatureSource::parse(reference) {
refs.push((p, true));
}
}
}
}
for (name, service) in &config.services {
if let Some(build) = service.get("build").cloned() {
let build: Build = build.try_into().map_err(|e| anyhow!("service `{name}`: build: {e}"))?;
build_refs(&build, &mut refs);
}
}
let mut files = BTreeMap::new();
let mut dirs = std::collections::BTreeSet::new();
let mut size = text.len() as u64;
for (r, whole) in refs {
let Some(rel) = relative_under(&canon, &r) else { continue };
add(&canon, &rel, &mut files, &mut size)?;
if whole {
dirs.insert(rel);
}
}
Ok(Bundle { config_name, config: text, table, files, dirs })
}
fn raw_table(path: &Path, text: &[u8]) -> Result<Table> {
let text = std::str::from_utf8(text).with_context(|| format!("{} isn't UTF-8", path.display()))?;
match path.extension().and_then(|e| e.to_str()) {
Some("yaml" | "yml") => {
let t: Option<Table> = serde_saphyr::from_str(text).map_err(|e| anyhow!("{e}")).with_context(|| format!("in {}", path.display()))?;
Ok(t.unwrap_or_default())
}
_ => toml::from_str(text).with_context(|| format!("in {}", path.display())),
}
}
fn mount_ref(mount: &Mount, canon: &Path) -> Option<PathBuf> {
let (kind, source) = match mount {
Mount::Shorthand(s) => {
let (kind, source, _, _) = config::parse_shorthand(s).ok()?;
(kind, source)
}
Mount::Object(o) => (o.kind.clone(), o.source.clone()),
};
if kind.as_deref().unwrap_or("bind") != "bind" {
return None;
}
let source = source?;
let rest = source.strip_prefix("${configDir}")?;
if rest.contains("${") {
return None;
}
let dir = canon.to_string_lossy();
let ctx = MountContext {
config_dir: &dir,
workspace_folder: "",
workspace_folder_basename: "",
shared_volumes: "",
instance: "",
container_env: None,
};
let resolved = PathBuf::from(config::substitute(&source, &ctx));
resolved.strip_prefix(canon).ok().map(Path::to_path_buf)
}
fn relative_under(canon: &Path, reference: &Path) -> Option<String> {
if reference.is_absolute() {
return None;
}
let mut at = canon.to_path_buf();
for c in reference.components() {
match c {
Component::CurDir => {}
Component::ParentDir => {
at.pop();
}
Component::Normal(n) => at.push(n),
Component::RootDir | Component::Prefix(_) => return None,
}
}
let rel = at.strip_prefix(canon).ok()?;
let rel = rel.to_str()?.to_string();
(!skipped(&rel)).then_some(rel)
}
fn skipped(rel: &str) -> bool {
rel.is_empty() || ROOT_SKIPS.iter().any(|s| rel.split('/').next() == Some(*s)) || rel.split('/').any(|c| c == ".git")
}
fn add(canon: &Path, rel: &str, files: &mut BTreeMap<String, File>, size: &mut u64) -> Result<()> {
let path = canon.join(rel);
let Ok(real) = path.canonicalize() else { return Ok(()) };
match real.strip_prefix(canon).ok().and_then(Path::to_str) {
Some(r) if !skipped(r) => {}
_ => return Ok(()),
}
let meta = std::fs::metadata(&real).with_context(|| format!("cannot read {}", path.display()))?;
if meta.is_file() {
*size += meta.len();
if *size > ROOT_TAR_LIMIT {
bail!("the config's bundle is over {ROOT_TAR_LIMIT} bytes");
}
let data = std::fs::read(&real).with_context(|| format!("cannot read {}", path.display()))?;
files.insert(rel.to_string(), File { exec: meta.permissions().mode() & 0o100 != 0, data });
} else if meta.is_dir() && !path.symlink_metadata().is_ok_and(|m| m.file_type().is_symlink()) {
let mut names: Vec<String> = Vec::new();
for e in std::fs::read_dir(&path).with_context(|| format!("cannot list {}", path.display()))? {
let e = e.with_context(|| format!("cannot list {}", path.display()))?;
let name = e.file_name();
names.push(name.to_str().with_context(|| format!("{} has a non-UTF-8 name", e.path().display()))?.to_string());
}
for name in names {
let child = format!("{rel}/{name}");
if !skipped(&child) {
add(canon, &child, files, size)?;
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn scratch(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("devsandbox-mesh-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn write(root: &Path, rel: &str, text: &str) {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
}
const CONFIG: &str = r#"
[template.base]
mounts = [
"source=${configDir}/mcps/common.json,target=/m.json,type=bind,readonly",
"source=${configDir}/missing.json,target=/x.json,type=bind",
"source=${sharedVolumes}/state,target=/s,type=bind",
"source=${configDir}/shared-volumes/other,target=/o,type=bind",
"source=${configDir},target=/self,type=bind",
]
[sandbox.web]
extends = "base"
folder = "../proj"
folders = { "/workspaces/root" = "../root" }
build = { dockerfile = "dockerfiles/web/Dockerfile", context = "dockerfiles/web" }
[sandbox.web.features]
"./features/hello" = {}
[sandbox.far]
folder = "../proj"
build = { dockerfile = "dockerfiles/far/Dockerfile", context = "../outside" }
mounts = [{ source = "${configDir}/mcps/far.json", target = "/far.json", type = "bind" }]
[services.db]
build = { dockerfile = "dockerfiles/db/Dockerfile" }
"#;
fn fixture(tag: &str) -> (PathBuf, PathBuf) {
let base = scratch(tag);
let root = base.join("root");
write(&root, "devsandboxes.toml", CONFIG);
write(&root, "mcps/common.json", "{}");
write(&root, "mcps/far.json", "{\"far\":1}");
write(&root, "dockerfiles/web/Dockerfile", "FROM alpine\n");
write(&root, "dockerfiles/web/files/a.txt", "a");
write(&root, "dockerfiles/far/Dockerfile", "FROM debian\n");
write(&root, "dockerfiles/db/Dockerfile", "FROM postgres\n");
write(&root, "features/hello/devcontainer-feature.json", "{\"id\":\"hello\"}");
write(&root, "features/hello/install.sh", "#!/bin/sh\n");
std::fs::set_permissions(root.join("features/hello/install.sh"), std::fs::Permissions::from_mode(0o755)).unwrap();
write(&root, "notes.md", "notes");
write(&root, "dispatcher/dispatch.log", "log");
write(&root, "shared-volumes/other/x", "state");
write(&root, "mcps/.git/HEAD", "ref");
write(&base, "outside/ctx.txt", "outside");
(base, root)
}
fn hash(root: &Path) -> [u8; 32] {
collect(root).unwrap().hash()
}
#[test]
fn the_bundle_is_the_config_and_what_it_references() {
let (base, root) = fixture("refs");
let b = collect(&root).unwrap();
let paths: Vec<&str> = b.files.keys().map(String::as_str).collect();
assert_eq!(
paths,
[
"dockerfiles/db/Dockerfile",
"dockerfiles/far/Dockerfile",
"dockerfiles/web/Dockerfile",
"dockerfiles/web/files/a.txt",
"features/hello/devcontainer-feature.json",
"features/hello/install.sh",
"mcps/common.json",
"mcps/far.json",
],
"no context outside the root, no root, no shared-volumes, no junk"
);
assert!(b.files["features/hello/install.sh"].exec);
assert_eq!(b.config_name, "devsandboxes.toml");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn the_hash_ignores_formatting_and_unreferenced_files() {
let (base, root) = fixture("same");
let h = hash(&root);
let reordered = CONFIG.replace("[services.db]\nbuild = { dockerfile = \"dockerfiles/db/Dockerfile\" }\n", "");
write(&root, "devsandboxes.toml", &format!("# a comment\n[services.db]\n build = {{ dockerfile = \"dockerfiles/db/Dockerfile\" }}\n\n{reordered}"));
assert_eq!(hash(&root), h, "formatting");
write(&root, "notes.md", "other notes");
write(&root, "dispatcher/dispatch.log", "more log");
write(&root, "__pycache__/x.pyc", "junk");
write(&root, "shared-volumes/other/x", "changed");
write(&root, "shared-volumes/state/y", "new");
write(&root, "mcps/.git/HEAD", "other");
assert_eq!(hash(&root), h, "unreferenced files");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn the_hash_follows_structure_and_referenced_files() {
let (base, root) = fixture("changes");
let mut seen = vec![hash(&root)];
let mut changed = |root: &Path, what: &str| {
let h = hash(root);
assert!(!seen.contains(&h), "{what}");
seen.push(h);
};
write(&root, "devsandboxes.toml", &format!("{CONFIG}\n[sandbox.more]\nimage = \"alpine\"\n"));
changed(&root, "a structural edit");
write(&root, "mcps/common.json", "{\"a\":1}");
changed(&root, "a ${configDir} mount");
write(&root, "dockerfiles/web/Dockerfile", "FROM alpine:3\n");
changed(&root, "a dockerfile");
write(&root, "dockerfiles/web/files/a.txt", "b");
changed(&root, "a file in a build context");
write(&root, "features/hello/install.sh", "#!/bin/sh\necho hi\n");
changed(&root, "a local feature");
std::fs::set_permissions(root.join("mcps/common.json"), std::fs::Permissions::from_mode(0o755)).unwrap();
changed(&root, "an exec bit");
write(&root, "outside/ignored", "x");
write(&base, "outside/ctx.txt", "changed");
assert_eq!(hash(&root), *seen.last().unwrap(), "a context outside the root");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_symlink_out_of_the_root_is_left_out() {
let (base, root) = fixture("links");
std::os::unix::fs::symlink(base.join("outside/ctx.txt"), root.join("dockerfiles/web/files/out.txt")).unwrap();
std::os::unix::fs::symlink("../../../notes.md", root.join("dockerfiles/web/files/notes.md")).unwrap();
let b = collect(&root).unwrap();
assert!(!b.files.contains_key("dockerfiles/web/files/out.txt"));
assert_eq!(b.files["dockerfiles/web/files/notes.md"].data, b"notes", "a link inside the root, followed");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn the_stored_tar_unpacks_to_the_same_hash() {
let (base, root) = fixture("tar");
let b = collect(&root).unwrap();
let data = base.join("data");
let path = store(&data, &b).unwrap();
assert_eq!(path, data.join(format!("mesh/bundles/{}.tar", hex::encode(b.hash()))));
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
let tar = std::fs::read(&path).unwrap();
assert_eq!(tar, b.tar().unwrap(), "deterministic");
let copy = base.join("copy/root");
std::fs::create_dir_all(copy.parent().unwrap()).unwrap();
tar::extract(&tar, ©).unwrap();
assert_eq!(hash(©), b.hash());
assert_eq!(std::fs::read_to_string(copy.join("devsandboxes.toml")).unwrap(), CONFIG, "the config as written");
assert!(!copy.join("notes.md").exists());
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_name_binds_to_its_first_path() {
let mut roots = BTreeMap::new();
assert_eq!(bind_in(&mut roots, "cfg", Path::new("/a/cfg")), Ok(()));
assert_eq!(bind_in(&mut roots, "cfg", Path::new("/a/cfg")), Ok(()));
assert_eq!(bind_in(&mut roots, "cfg", Path::new("/b/cfg")), Err(PathBuf::from("/a/cfg")));
assert_eq!(bind_in(&mut roots, "other", Path::new("/b/other")), Ok(()));
assert_eq!(roots.len(), 2);
}
#[test]
fn the_bindings_round_trip() {
let data = scratch("roots");
assert!(load_roots(&roots_path(&data)).unwrap().is_empty(), "a missing file is empty");
assert_eq!(bound_root(&data, "cfg").unwrap(), None);
assert_eq!(bind(&data, "cfg", Path::new("/a/cfg")).unwrap(), Ok(()));
assert_eq!(bind(&data, "x.y", Path::new("/a/x.y")).unwrap(), Ok(()));
let text = std::fs::read_to_string(roots_path(&data)).unwrap();
assert!(text.contains("cfg = \"/a/cfg\""), "{text}");
assert_eq!(bind(&data, "cfg", Path::new("/b/cfg")).unwrap(), Err(PathBuf::from("/a/cfg")));
assert_eq!(bound_root(&data, "cfg").unwrap(), Some(PathBuf::from("/a/cfg")));
assert_eq!(bound_root(&data, "x.y").unwrap(), Some(PathBuf::from("/a/x.y")));
let _ = std::fs::remove_dir_all(&data);
}
fn files_under(dir: &Path) -> Vec<String> {
fn walk(base: &Path, at: &Path, out: &mut Vec<String>) {
for e in std::fs::read_dir(at).unwrap().flatten() {
let p = e.path();
if p.symlink_metadata().unwrap().is_dir() {
walk(base, &p, out);
} else {
out.push(p.strip_prefix(base).unwrap().to_str().unwrap().to_string());
}
}
}
let mut out = Vec::new();
walk(dir, dir, &mut out);
out.sort();
out
}
#[test]
fn install_checks_the_head_then_updates_the_copy_in_place() {
let (base, root) = fixture("install");
let data = base.join("data");
let v1 = collect(&root).unwrap();
let tar1 = v1.tar().unwrap();
let got = install(&data, "cfg", &tar1, &v1.hash()).unwrap();
let tree = tree_path(&data, "cfg");
assert_eq!(got, Installed { path: tree.clone(), removed: vec![] });
assert_eq!(tree, data.join("mesh/tree/cfg"));
assert_eq!(hash(&tree), v1.hash(), "the copy re-collects to the head");
assert!(bundle_path(&data, &v1.hash()).is_file(), "kept to serve");
assert!(!tree.join("notes.md").exists());
assert!(std::fs::metadata(tree.join("features/hello/install.sh")).unwrap().permissions().mode() & 0o100 != 0);
let mesh_entries: Vec<_> = std::fs::read_dir(data.join("mesh")).unwrap().flatten().map(|e| e.file_name().into_string().unwrap()).collect();
assert!(mesh_entries.iter().all(|n| !n.starts_with('.') || n.ends_with(".lock")), "no temp left: {mesh_entries:?}");
write(&tree, ".leader", "me");
write(&tree, "shared-volumes/state/db", "rows");
write(&tree, "dispatcher/dispatch.log", "log");
write(&root, "devsandboxes.toml", &CONFIG.replace("[services.db]\nbuild = { dockerfile = \"dockerfiles/db/Dockerfile\" }\n", ""));
write(&root, "mcps/common.json", "{\"v\":2}");
let v2 = collect(&root).unwrap();
let before = files_under(&tree);
let err = format!("{:#}", install(&data, "cfg", &tar1, &v2.hash()).unwrap_err());
assert!(err.contains("not the signed version"), "{err}");
let mut tampered = v2.clone();
tampered.files.get_mut("mcps/common.json").unwrap().data = b"{\"evil\":1}".to_vec();
assert!(install(&data, "cfg", &tampered.tar().unwrap(), &v2.hash()).is_err());
assert_eq!(files_under(&tree), before);
assert_eq!(hash(&tree), v1.hash());
assert!(!bundle_path(&data, &tampered.hash()).exists(), "a refused bundle isn't kept");
let got = install(&data, "cfg", &v2.tar().unwrap(), &v2.hash()).unwrap();
assert_eq!(got.removed, ["dockerfiles/db/Dockerfile"]);
assert!(!tree.join("dockerfiles/db").exists(), "its emptied dir too");
assert_eq!(hash(&tree), v2.hash());
assert_eq!(std::fs::read_to_string(tree.join("mcps/common.json")).unwrap(), "{\"v\":2}");
for kept in [".leader", "shared-volumes/state/db", "dispatcher/dispatch.log"] {
assert!(tree.join(kept).is_file(), "{kept} stays");
}
assert_eq!(install(&data, "cfg", &v2.tar().unwrap(), &v2.hash()).unwrap().removed, Vec::<String>::new());
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn verify_rehashes_exactly_what_was_installed() {
let (base, root) = fixture("verify");
let data = base.join("data");
let v1 = collect(&root).unwrap();
let head = Some((3, v1.hash()));
let err = |head| format!("{:#}", verify(&data, "cfg", head).unwrap_err());
assert_eq!(err(head), "config root `cfg` has no signed version here yet", "not installed");
install(&data, "cfg", &v1.tar().unwrap(), &v1.hash()).unwrap();
verify(&data, "cfg", head).unwrap();
assert_eq!(err(None), "config root `cfg` has no signed version here yet", "no head");
assert_eq!(err(Some((4, [7; 32]))), "config root `cfg` v4 isn't here yet");
assert_eq!(copy_of(&data, &tree_path(&data, "cfg")), Some(("cfg".to_string(), true)));
assert_eq!(copy_of(&data, &tree_path(&data, "cfg").join("mcps")), Some(("cfg".to_string(), false)));
assert_eq!(copy_of(&data, &root), None);
let tree = tree_path(&data, "cfg");
let config = std::fs::read_to_string(tree.join("devsandboxes.toml")).unwrap();
write(&tree, "devsandboxes.toml", &format!("# a comment\n{config}"));
write(&tree, "missing.json", "{}");
write(&tree, "shared-volumes/x", "x");
write(&tree, "mcps/new.json", "{}");
write(&tree, ".leader", "me");
verify(&data, "cfg", head).unwrap();
let changed = "config root `cfg` isn't the signed config (v3): its copy was changed here";
write(&tree, "devsandboxes.toml", &format!("{config}\n[sandbox.more]\nimage = \"alpine\"\n"));
assert_eq!(err(head), changed, "a structural edit");
write(&tree, "devsandboxes.toml", &config);
verify(&data, "cfg", head).unwrap();
write(&tree, "mcps/common.json", "{\"evil\":1}");
assert_eq!(err(head), changed, "a listed file");
write(&tree, "mcps/common.json", "{}");
verify(&data, "cfg", head).unwrap();
std::fs::set_permissions(tree.join("dockerfiles/web/Dockerfile"), std::fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(err(head), changed, "an exec bit");
std::fs::set_permissions(tree.join("dockerfiles/web/Dockerfile"), std::fs::Permissions::from_mode(0o644)).unwrap();
std::fs::remove_file(tree.join("mcps/far.json")).unwrap();
assert_eq!(err(head), changed, "a listed file removed");
std::os::unix::fs::symlink(root.join("mcps/far.json"), tree.join("mcps/far.json")).unwrap();
assert_eq!(err(head), changed, "a listed file turned into a link, even to the same bytes");
std::fs::remove_file(tree.join("mcps/far.json")).unwrap();
write(&tree, "mcps/far.json", "{\"far\":1}");
verify(&data, "cfg", head).unwrap();
write(&tree, "devsandboxes.yaml", "sandbox: {}\n");
assert_eq!(err(head), changed, "a second config file");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_root_id_is_its_binding_or_its_exact_copy() {
let base = scratch("root-id");
let data = base.join("data");
let bound = base.join("cfg");
std::fs::create_dir_all(&bound).unwrap();
std::fs::create_dir_all(tree_path(&data, "lent").join("mcps")).unwrap();
let other = base.join("other");
std::fs::create_dir_all(&other).unwrap();
let bindings = BTreeMap::from([("cfg".to_string(), std::fs::canonicalize(&bound).unwrap())]);
assert_eq!(root_id(&bindings, &data, &bound), Some("cfg".to_string()));
assert_eq!(root_id(&bindings, &data, &bound.join("../cfg")), Some("cfg".to_string()), "canonicalized");
assert_eq!(root_id(&bindings, &data, &tree_path(&data, "lent")), Some("lent".to_string()));
assert_eq!(root_id(&bindings, &data, &tree_path(&data, "lent").join("mcps")), None, "inside a copy");
assert_eq!(root_id(&bindings, &data, &other), None);
let gone = BTreeMap::from([("gone".to_string(), base.join("gone"))]);
assert_eq!(root_id(&gone, &data, &base.join("gone")), Some("gone".to_string()), "as given when it doesn't resolve");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn verify_admits_nothing_unlisted_under_a_bundled_dir() {
let (base, root) = fixture("dirs");
let data = base.join("data");
let v1 = collect(&root).unwrap();
let dirs: Vec<&str> = v1.dirs.iter().map(String::as_str).collect();
assert_eq!(dirs, ["dockerfiles/web", "features/hello"], "contexts and local features under the root, not the root");
let head = Some((3, v1.hash()));
let err = |head| format!("{:#}", verify(&data, "cfg", head).unwrap_err());
install(&data, "cfg", &v1.tar().unwrap(), &v1.hash()).unwrap();
verify(&data, "cfg", head).unwrap();
let tree = tree_path(&data, "cfg");
write(&tree, "notes.md", "a note");
write(&tree, "dockerfiles/other.txt", "x");
verify(&data, "cfg", head).unwrap();
let added = |rel: &str| format!("config root `cfg` isn't the signed config (v3): `{rel}` was added to its copy here");
write(&tree, "dockerfiles/web/.git/HEAD", "ref");
assert_eq!(err(head), added("dockerfiles/web/.git"), "a .git in a build context");
std::fs::remove_dir_all(tree.join("dockerfiles/web/.git")).unwrap();
write(&tree, "dockerfiles/web/evil.sh", "#!/bin/sh\n");
assert_eq!(err(head), added("dockerfiles/web/evil.sh"), "a file in a build context");
std::fs::remove_file(tree.join("dockerfiles/web/evil.sh")).unwrap();
write(&tree, "dockerfiles/web/files/deep/evil", "x");
assert_eq!(err(head), added("dockerfiles/web/files/deep/evil"), "nested");
std::fs::remove_dir_all(tree.join("dockerfiles/web/files/deep")).unwrap();
std::os::unix::fs::symlink("/etc/passwd", tree.join("features/hello/passwd")).unwrap();
assert_eq!(err(head), added("features/hello/passwd"), "a symlink in a local feature");
std::fs::remove_file(tree.join("features/hello/passwd")).unwrap();
std::fs::create_dir(tree.join("dockerfiles/web/empty")).unwrap();
verify(&data, "cfg", head).unwrap();
std::fs::remove_file(root.join("dockerfiles/web/files/a.txt")).unwrap();
let v2 = collect(&root).unwrap();
assert_eq!(install(&data, "cfg", &v2.tar().unwrap(), &v2.hash()).unwrap().removed, ["dockerfiles/web/files/a.txt"]);
verify(&data, "cfg", Some((4, v2.hash()))).unwrap();
let _ = std::fs::remove_dir_all(&base);
}
fn log_with(data: &Path, heads: &[(u64, [u8; 32])]) -> PathBuf {
use crate::serve::fleet::identity::{Signer, SigningKey};
use crate::serve::fleet::membership::{self, Log, Op};
let key = SigningKey::from_seed([1; 32]);
let mut log = Log::default();
log.append(Op::genesis_with_id([1; 16], "lap", key.public_key(), "lap"), "lap", &key).unwrap();
for (version, hash) in heads {
log.append(Op::Config { root: "cfg".into(), version: *version, hash: *hash }, "lap", &key).unwrap();
}
let member = data.join("member.toml");
let text = membership::serialize(&membership::Stored { log, introductions: vec![] }).unwrap();
std::fs::write(membership::path(&member), text).unwrap();
member
}
#[test]
fn check_root_admits_only_the_copy_at_its_signed_head() {
let (base, root) = fixture("check");
let data = base.join("data");
let member = data.join("member.toml");
let err = |root: &Path| format!("{:#}", check_root_in(&data, &member, root).unwrap_err());
let tree = tree_path(&data, "cfg");
let v1 = collect(&root).unwrap();
check_root_in(&data, &member, &root).unwrap();
check_root_in(&data, &member, &base.join("nowhere")).unwrap();
install(&data, "cfg", &v1.tar().unwrap(), &v1.hash()).unwrap();
assert_eq!(err(&tree), "config root `cfg` has no signed version here yet", "no log");
log_with(&data, &[]);
assert_eq!(err(&tree), "config root `cfg` has no signed version here yet", "no head for the name");
log_with(&data, &[(1, v1.hash())]);
check_root_in(&data, &member, &tree).unwrap();
check_root_in(&data, &member, &tree.join("mcps/..")).unwrap();
check_root_in(&data, &member, &root).unwrap();
log_with(&data, &[(1, v1.hash()), (2, [9; 32])]);
assert_eq!(err(&tree), "config root `cfg` v2 isn't here yet");
log_with(&data, &[(1, v1.hash())]);
let inside = err(&tree.join("mcps"));
assert!(inside.ends_with("is inside the copy of config root `cfg`, not a signed config"), "{inside}");
for at in [data.join("mesh/tree"), data.join("mesh"), data.join("mesh/tree/.."), data.join("mesh/installed.toml")] {
let got = err(&at);
assert!(got.ends_with("is in the store of signed configs, not a config root: build from a copy, `mesh/tree/<name>`"), "{got}");
}
write(&tree, "mcps/common.json", "{\"evil\":1}");
assert_eq!(err(&tree), "config root `cfg` isn't the signed config (v1): its copy was changed here");
std::fs::write(crate::serve::fleet::membership::path(&member), "{").unwrap();
assert!(err(&tree).starts_with("config root `cfg`: "), "{}", err(&tree));
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_peer_cant_build_a_root_named_like_a_signed_config() {
let (base, root) = fixture("peer-name");
let data = base.join("data");
let member = data.join("member.toml");
let v1 = collect(&root).unwrap();
install(&data, "cfg", &v1.tar().unwrap(), &v1.hash()).unwrap();
let tree = tree_path(&data, "cfg");
let own = base.join("home/cfg");
write(&own, "devsandboxes.toml", CONFIG);
check_peer_root_in(&data, &member, &own, None).unwrap();
log_with(&data, &[]);
check_peer_root_in(&data, &member, &own, None).unwrap();
log_with(&data, &[(1, v1.hash())]);
let want = format!(
"config root `{}` has the name of the signed config `cfg` but isn't its copy here ({}): run it from the copy",
own.display(),
tree.display()
);
assert_eq!(format!("{:#}", check_peer_root_in(&data, &member, &own, None).unwrap_err()), want);
assert!(check_peer_root_in(&data, &member, &own, Some(&base.join("elsewhere"))).is_err(), "another fleet root");
let moved = data.join("fleet/lap/roots/x/cfg");
write(&moved, "devsandboxes.toml", CONFIG);
check_peer_root_in(&data, &member, &moved, Some(&data.join("fleet/lap"))).unwrap();
assert!(check_peer_root_in(&data, &member, &moved, None).is_err());
let link = base.join("home/other");
std::os::unix::fs::symlink(&own, &link).unwrap();
assert!(format!("{:#}", check_peer_root_in(&data, &member, &link, None).unwrap_err()).contains("signed config `cfg`"));
check_root_in(&data, &member, &own).unwrap();
check_peer_root_in(&data, &member, &tree, None).unwrap();
check_peer_root_in(&data, &member, &root, None).unwrap();
write(&tree, "mcps/common.json", "{\"evil\":1}");
assert!(check_peer_root_in(&data, &member, &tree, None).is_err());
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_stray_tar_member_isnt_installed() {
let (base, root) = fixture("stray");
let data = base.join("data");
let b = collect(&root).unwrap();
let mut w = tar::Writer::default();
w.file(&b.config_name, false, &b.config).unwrap();
for (p, f) in b.files.iter() {
w.file(p, f.exec, &f.data).unwrap();
}
w.file("dispatcher/evil.sh", true, b"#!/bin/sh\n").unwrap();
w.file("shared-volumes/x", false, b"x").unwrap();
install(&data, "cfg", &w.finish(), &b.hash()).unwrap();
let tree = tree_path(&data, "cfg");
assert!(!tree.join("dispatcher").exists() && !tree.join("shared-volumes").exists(), "only what the config references");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_bound_name_or_a_bad_name_gets_no_copy() {
let (base, root) = fixture("bound");
let data = base.join("data");
let b = collect(&root).unwrap();
bind(&data, "root", &root).unwrap().unwrap();
let err = format!("{:#}", install(&data, "root", &b.tar().unwrap(), &b.hash()).unwrap_err());
assert!(err.contains("this host's own"), "{err}");
assert!(!tree_path(&data, "root").exists());
for bad in ["..", "a/b", ""] {
assert!(install(&data, bad, &b.tar().unwrap(), &b.hash()).is_err(), "{bad:?}");
}
let _ = std::fs::remove_dir_all(&base);
}
fn patched(mut tar: Vec<u8>, from: &[u8], to: &[u8]) -> Vec<u8> {
let at = tar[..512].windows(from.len()).position(|w| w == from).unwrap();
tar[at..at + to.len()].copy_from_slice(to);
tar[148..156].copy_from_slice(b" ");
let sum: u32 = tar[..512].iter().map(|&b| u32::from(b)).sum();
tar[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes());
tar
}
#[test]
fn traversal_and_links_out_are_refused() {
let (base, root) = fixture("unsafe");
let data = base.join("data");
let b = collect(&root).unwrap();
let with = |first: Box<dyn Fn(&mut tar::Writer)>| {
let mut w = tar::Writer::default();
first(&mut w);
w.file(&b.config_name, false, &b.config).unwrap();
w.finish()
};
let cases = [
("unsafe path", patched(with(Box::new(|w| w.file("ab/x", false, b"x").unwrap())), b"ab/x", b"../x")),
("unsafe path", patched(with(Box::new(|w| w.file("_x", false, b"x").unwrap())), b"_x", b"/x")),
("points outside", patched(with(Box::new(|w| w.link("l", "xx/y").unwrap())), b"xx/y", b"../y")),
];
for (why, t) in cases {
let err = format!("{:#}", install(&data, "cfg", &t, &b.hash()).unwrap_err());
assert!(err.contains(why), "{why}: {err}");
}
assert!(!tree_path(&data, "cfg").exists());
assert!(!base.join("x").exists() && !base.join("data/mesh/x").exists());
let _ = std::fs::remove_dir_all(&base);
}
struct Served(PathBuf);
impl crate::serve::fleet::code::Transfer for Served {
fn send(&self, _: &crate::serve::fleet::transfer::Header, _: &mut dyn std::io::Read) -> Result<()> {
unreachable!()
}
fn pull(&self, kind: &str, name: &str, limit: u64, dest: &Path) -> Result<u64> {
let (a, b) = std::os::unix::net::UnixStream::pair().unwrap();
for s in [&a, &b] {
s.set_read_timeout(Some(std::time::Duration::from_secs(30))).unwrap();
s.set_write_timeout(Some(std::time::Duration::from_secs(30))).unwrap();
}
let r = serves(&self.0, crate::serve::fleet::transfer::Receivers::new(|_| {}));
let server = std::thread::spawn(move || r.receive(b).map(|_| ()));
let got = crate::serve::fleet::transfer::pull(a, kind, name, limit, dest);
let served = server.join().unwrap();
got.and_then(|n| served.map(|()| n))
}
}
#[test]
fn a_stored_bundle_is_served_by_its_hash() {
let (base, root) = fixture("serve");
let (authority, member) = (base.join("auth"), base.join("member"));
let b = collect(&root).unwrap();
store(&authority, &b).unwrap();
let tar = pull_bundle(&Served(authority.clone()), &member, &b.hash()).unwrap();
assert_eq!(tar, b.tar().unwrap());
assert!(bundle_path(&authority, &b.hash()).is_file(), "kept once sent");
let temps: Vec<_> = std::fs::read_dir(member.join("mesh")).unwrap().flatten().collect();
assert!(temps.is_empty(), "{temps:?}");
install(&member, "cfg", &tar, &b.hash()).unwrap();
let err = format!("{:#}", pull_bundle(&Served(authority.clone()), &member, &[1; 32]).unwrap_err());
assert!(err.contains("no ") && err.contains("to pull here"), "{err}");
let t = Served(authority.clone());
let dest = base.join("out.tar");
for bad in ["roots.toml", "x.tar", &format!("{}.tar", "A".repeat(64)), &format!("{}", hex::encode(b.hash()))] {
let err = format!("{:#}", crate::serve::fleet::code::Transfer::pull(&t, BUNDLE_KIND, bad, ROOT_TAR_LIMIT, &dest).unwrap_err());
assert!(err.contains("isn't a config bundle's name"), "{bad}: {err}");
}
assert!(!dest.exists());
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn due_names_are_unbound_heads_not_installed() {
let heads: BTreeMap<String, (u64, [u8; 32])> =
[("done", (2, [1; 32])), ("own", (1, [2; 32])), ("local", (3, [3; 32])), ("pull", (1, [4; 32])), ("behind", (5, [5; 32]))]
.into_iter()
.map(|(n, h)| (n.to_string(), h))
.collect();
let installed: BTreeMap<String, String> =
[("done", hex::encode([1; 32])), ("behind", hex::encode([6; 32]))].into_iter().map(|(n, h)| (n.to_string(), h)).collect();
let bound = BTreeMap::from([("own".to_string(), PathBuf::from("/a/own"))]);
let stored = std::collections::BTreeSet::from([[3; 32], [2; 32]]);
let due = |name: &str, version, h, stored| Due { name: name.into(), version, hash: [h; 32], stored, dropped: None, changed: None };
assert_eq!(
super::due(&heads, &installed, &bound, &stored),
[due("behind", 5, 5, false), due("local", 3, 3, true), due("pull", 1, 4, false)],
"installed already and bound: nothing"
);
assert!(super::due(&BTreeMap::new(), &installed, &bound, &stored).is_empty());
}
struct NoPull;
impl crate::serve::fleet::code::Transfer for NoPull {
fn send(&self, _: &crate::serve::fleet::transfer::Header, _: &mut dyn std::io::Read) -> Result<()> {
unreachable!()
}
fn pull(&self, _: &str, name: &str, _: u64, _: &Path) -> Result<u64> {
panic!("pulled {name}")
}
}
#[test]
fn follow_pulls_installs_and_then_does_nothing() {
let (base, root) = fixture("follow");
let (authority, member) = (base.join("auth"), base.join("member"));
let b = collect(&root).unwrap();
store(&authority, &b).unwrap();
let heads = || Ok(BTreeMap::from([("cfg".to_string(), (1, b.hash()))]));
let done = follow(&member, heads, &Served(authority.clone())).unwrap();
assert_eq!(done.len(), 1);
assert_eq!(done[0].0, Due { name: "cfg".into(), version: 1, hash: b.hash(), stored: false, dropped: None, changed: None });
assert_eq!(done[0].1.as_ref().unwrap().path, tree_path(&member, "cfg"));
assert_eq!(installed(&member).unwrap()["cfg"], hex::encode(b.hash()));
assert_eq!(hash(&tree_path(&member, "cfg")), b.hash());
assert!(follow(&member, heads, &NoPull).unwrap().is_empty(), "installed: nothing");
let other = base.join("other");
store(&other, &b).unwrap();
let done = follow(&other, heads, &NoPull).unwrap();
assert!(done[0].0.stored && done[0].1.is_ok(), "{done:?}");
let missing = base.join("missing");
let done = follow(&missing, heads, &Served(base.join("empty"))).unwrap();
assert!(format!("{:#}", done[0].1.as_ref().unwrap_err()).contains("to pull here"));
assert!(installed(&missing).unwrap().is_empty());
assert!(follow(&authority, || { bind(&authority, "cfg", &root).unwrap().unwrap(); heads() }, &NoPull).unwrap().is_empty());
let _ = std::fs::remove_dir_all(&base);
}
fn bundle_temps(data: &Path) -> Vec<PathBuf> {
let dir = data.join("mesh").join("bundles");
std::fs::read_dir(dir).unwrap().flatten().map(|e| e.path()).filter(|p| p.to_string_lossy().ends_with(".tmp")).collect()
}
#[test]
fn store_replaces_a_bad_stored_bundle() {
let (base, root) = fixture("restore");
let data = base.join("data");
let b = collect(&root).unwrap();
let tar = b.tar().unwrap();
let path = store(&data, &b).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), tar);
assert_eq!(path.metadata().unwrap().permissions().mode() & 0o777, 0o600);
for bad in [&tar[..tar.len() / 2], b"garbage", b""] {
std::fs::write(&path, bad).unwrap();
assert_eq!(store(&data, &b).unwrap(), path);
assert_eq!(std::fs::read(&path).unwrap(), tar, "replaced");
}
let ino = |p: &Path| std::os::unix::fs::MetadataExt::ino(&p.metadata().unwrap());
let before = ino(&path);
store(&data, &b).unwrap();
assert_eq!(ino(&path), before);
assert!(bundle_temps(&data).is_empty());
install(&data, "cfg", &std::fs::read(&path).unwrap(), &b.hash()).unwrap();
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn parallel_stores_of_one_bundle_leave_it_whole() {
let (base, root) = fixture("race");
let data = base.join("data");
let b = collect(&root).unwrap();
let path = bundle_path(&data, &b.hash());
for _ in 0..5 {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, b"garbage").unwrap();
let threads: Vec<_> = (0..8)
.map(|_| {
let (data, b) = (data.clone(), b.clone());
std::thread::spawn(move || store(&data, &b).map(|_| ()))
})
.collect();
for t in threads {
t.join().unwrap().unwrap();
}
assert_eq!(std::fs::read(&path).unwrap(), b.tar().unwrap());
assert!(bundle_temps(&data).is_empty());
}
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn follow_drops_a_bad_stored_bundle_and_pulls() {
let (base, root) = fixture("follow-bad");
let authority = base.join("auth");
let b = collect(&root).unwrap();
store(&authority, &b).unwrap();
let heads = || Ok(BTreeMap::from([("cfg".to_string(), (1, b.hash()))]));
let tar = b.tar().unwrap();
for (tag, bad) in [("cut", &tar[..tar.len() / 2]), ("garbage", b"garbage".as_slice())] {
let member = base.join(tag);
let path = bundle_path(&member, &b.hash());
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, bad).unwrap();
let done = follow(&member, heads, &Served(authority.clone())).unwrap();
assert_eq!(done.len(), 1);
assert!(!done[0].0.stored, "{tag}");
let why = done[0].0.dropped.as_deref().unwrap();
assert!(why.contains("the bundle of config root `cfg`"), "{tag}: {why}");
assert_eq!(done[0].1.as_ref().unwrap().path, tree_path(&member, "cfg"));
assert_eq!(installed(&member).unwrap()["cfg"], hex::encode(b.hash()));
assert_eq!(std::fs::read(&path).unwrap(), tar, "{tag}: the pulled one stored");
}
let member = base.join("unwritable");
store(&member, &b).unwrap();
write(&member, "mesh/tree/cfg", "a file, not a dir");
let done = follow(&member, heads, &NoPull).unwrap();
assert!(done[0].0.stored && done[0].0.dropped.is_none() && done[0].1.is_err(), "{done:?}");
assert_eq!(std::fs::read(bundle_path(&member, &b.hash())).unwrap(), tar);
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn follow_repairs_a_changed_copy_from_the_stored_bundle() {
let (base, root) = fixture("repair");
let data = base.join("data");
let b = collect(&root).unwrap();
install(&data, "cfg", &b.tar().unwrap(), &b.hash()).unwrap();
let heads = || Ok(BTreeMap::from([("cfg".to_string(), (1, b.hash()))]));
assert!(follow(&data, heads, &NoPull).unwrap().is_empty(), "intact: nothing");
let tree = tree_path(&data, "cfg");
write(&tree, "mcps/common.json", "{\"evil\":1}");
write(&tree, "dockerfiles/web/evil.sh", "#!/bin/sh\n");
write(&tree, "dispatcher/dispatch.log", "log");
write(&tree, ".leader", "me");
assert!(verify(&data, "cfg", Some((1, b.hash()))).is_err());
let done = follow(&data, heads, &NoPull).unwrap();
assert_eq!(done.len(), 1);
assert!(done[0].0.stored && done[0].0.changed.as_deref().unwrap().contains("its copy was changed here"), "{done:?}");
assert_eq!(done[0].1.as_ref().unwrap().removed, ["dockerfiles/web/evil.sh"]);
verify(&data, "cfg", Some((1, b.hash()))).unwrap();
assert_eq!(std::fs::read_to_string(tree.join("mcps/common.json")).unwrap(), "{}");
for kept in [".leader", "dispatcher/dispatch.log"] {
assert!(tree.join(kept).is_file(), "{kept} stays");
}
assert!(follow(&data, heads, &NoPull).unwrap().is_empty(), "repaired: nothing");
write(&tree, "devsandboxes.yaml", "sandbox: {}\n");
for _ in 0..2 {
let done = follow(&data, heads, &NoPull).unwrap();
let err = format!("{:#}", done[0].1.as_ref().unwrap_err());
assert!(err.starts_with("installed, but still not the signed config"), "{err}");
}
assert!(tree.join("devsandboxes.yaml").is_file(), "not a file the bundle's dirs hold: left");
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn install_removes_what_its_dirs_dont_list() {
let (base, root) = fixture("strays");
let data = base.join("data");
let v1 = collect(&root).unwrap();
install(&data, "cfg", &v1.tar().unwrap(), &v1.hash()).unwrap();
let tree = tree_path(&data, "cfg");
write(&tree, "dockerfiles/web/evil.sh", "#!/bin/sh\n");
write(&tree, "dockerfiles/web/files/deep/evil", "x");
write(&tree, "dockerfiles/web/.git/HEAD", "ref");
write(&base, "target/keep", "outside");
std::os::unix::fs::symlink(base.join("target"), tree.join("features/hello/out")).unwrap();
for local in ["notes.md", "dockerfiles/other.txt", "dispatcher/dispatch.log", "shared-volumes/x", ".worktrees/w/f"] {
write(&tree, local, "local");
}
assert!(verify(&data, "cfg", Some((1, v1.hash()))).is_err());
write(&root, "mcps/common.json", "{\"v\":2}");
let v2 = collect(&root).unwrap();
let got = install(&data, "cfg", &v2.tar().unwrap(), &v2.hash()).unwrap();
assert_eq!(got.removed, ["dockerfiles/web/.git", "dockerfiles/web/evil.sh", "dockerfiles/web/files/deep/evil", "features/hello/out"]);
verify(&data, "cfg", Some((2, v2.hash()))).unwrap();
assert_eq!(std::fs::read_to_string(base.join("target/keep")).unwrap(), "outside", "the link removed, not its target");
for local in ["notes.md", "dockerfiles/other.txt", "dispatcher/dispatch.log", "shared-volumes/x", ".worktrees/w/f"] {
assert!(tree.join(local).is_file(), "{local} stays");
}
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn install_replaces_what_is_in_the_way() {
let (base, root) = fixture("blocked");
let data = base.join("data");
let b = collect(&root).unwrap();
install(&data, "cfg", &b.tar().unwrap(), &b.hash()).unwrap();
let tree = tree_path(&data, "cfg");
write(&base, "target/Dockerfile", "FROM evil\n");
std::fs::remove_dir_all(tree.join("dockerfiles/web")).unwrap();
std::os::unix::fs::symlink(base.join("target"), tree.join("dockerfiles/web")).unwrap();
std::fs::remove_dir_all(tree.join("mcps")).unwrap();
write(&tree, "mcps", "a file");
std::fs::remove_file(tree.join("dockerfiles/db/Dockerfile")).unwrap();
write(&tree, "dockerfiles/db/Dockerfile/x", "x");
assert!(verify(&data, "cfg", Some((1, b.hash()))).is_err());
let done = follow(&data, || Ok(BTreeMap::from([("cfg".to_string(), (1, b.hash()))])), &NoPull).unwrap();
assert!(done[0].1.is_ok(), "{done:?}");
verify(&data, "cfg", Some((1, b.hash()))).unwrap();
assert!(tree.join("dockerfiles/web").symlink_metadata().unwrap().is_dir());
assert_eq!(std::fs::read_to_string(base.join("target/Dockerfile")).unwrap(), "FROM evil\n", "never written through");
assert_eq!(files_under(&base.join("target")), ["Dockerfile"]);
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_config_that_wont_load_has_no_bundle() {
let (base, root) = fixture("bad");
write(&root, "devsandboxes.toml", "[sandbox.a\n");
assert!(collect(&root).is_err());
write(&root, "devsandboxes.toml", "[sandbox.a]\nextends = \"nope\"\n");
assert!(format!("{:#}", collect(&root).unwrap_err()).contains("nope"));
let _ = std::fs::remove_dir_all(&base);
}
}