use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use serde::Serialize;
use super::{Finding, Kind, MigrateError, edit_line, file_hash, identity_findings, migrate_file};
use crate::config::{self, Config};
use crate::inbox::wire::ApiError;
use crate::state::State;
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigFindings {
pub file: PathBuf,
pub format: &'static str,
pub file_hash: String,
pub findings: Vec<IdentityFinding>,
}
#[derive(Debug, Serialize)]
pub struct IdentityFinding {
pub id: String,
pub table: String,
pub kind: &'static str,
pub key: String,
pub value: String,
pub edit: Option<String>,
pub sandboxes: Vec<String>,
pub acked: bool,
}
#[derive(Debug, Serialize)]
pub struct ConfigMigrated {
pub edits: usize,
pub drifted: Vec<String>,
}
#[derive(Debug, Serialize)]
pub struct ConfigAcked {
pub acked: Vec<String>,
}
fn kind_name(kind: Kind) -> &'static str {
match kind {
Kind::RootTarget => "root-target",
Kind::RootEnv => "root-env",
Kind::RootPath => "root-path",
Kind::RootUser => "root-user",
}
}
fn read(root: &Path) -> Result<(PathBuf, bool, String, Config), ApiError> {
if !root.is_absolute() {
return Err(ApiError::invalid(format!("`root` must be absolute, got `{}`", root.display())));
}
if !root.is_dir() {
return Err(ApiError::not_found(format!("no directory {}", root.display())));
}
let file = config::find(root)
.map_err(|e| ApiError::invalid(format!("{e:#}")))?
.ok_or_else(|| ApiError::not_found(format!("no config file in {}", root.display())))?;
let text = std::fs::read_to_string(&file)
.map_err(|e| ApiError::internal(anyhow::Error::new(e).context(format!("cannot read {}", file.display()))))?;
let yaml = matches!(file.extension().and_then(|e| e.to_str()), Some("yaml" | "yml"));
let parsed = if yaml { Config::parse_yaml(&text) } else { Config::parse(&text) };
let config = parsed.map_err(|e| ApiError::invalid(format!("in {}: {e:#}", file.display())))?;
Ok((file, yaml, text, config))
}
fn project(root: &Path) -> Result<String, ApiError> {
crate::commands::services::project_id(root).map_err(ApiError::internal)
}
pub fn findings(root: &Path, state: &Path, sandbox: Option<&str>) -> Result<ConfigFindings, ApiError> {
let (file, yaml, text, config) = read(root)?;
if let Some(name) = sandbox
&& !config.sandboxes.contains_key(name)
{
return Err(ApiError::not_found(format!("no sandbox `{name}` in {}", root.display())));
}
let state = State::load_at(state).map_err(ApiError::internal)?;
let acks = state.identity_acks.get(&project(root)?);
let findings = identity_findings(&config)
.into_iter()
.filter(|f| sandbox.is_none_or(|name| f.sandboxes.iter().any(|s| s == name)))
.map(|f| wire(f, acks))
.collect();
Ok(ConfigFindings { file, format: if yaml { "yaml" } else { "toml" }, file_hash: file_hash(&text), findings })
}
fn wire(f: Finding, acks: Option<&BTreeSet<String>>) -> IdentityFinding {
IdentityFinding {
acked: acks.is_some_and(|a| a.contains(&f.id)),
edit: edit_line(&f, false),
table: f.origin.table(),
kind: kind_name(f.kind),
id: f.id,
key: f.key,
value: f.value,
sandboxes: f.sandboxes,
}
}
pub fn migrate(
root: &Path,
state: &Path,
ids: Option<&[String]>,
keep: &[String],
expected_hash: &str,
) -> Result<ConfigMigrated, ApiError> {
#[cfg(unix)]
if let Some(data) = state.parent()
&& let Some((name, _)) = crate::serve::fleet::mesh::copy_of(data, root)
{
return Err(ApiError::denied(format!(
"{} is this member's copy of config root `{name}`, signed by the fleet's authority: edit the config on the authority",
root.display()
)));
}
let (file, _, _, before) = read(root)?;
let migrated = migrate_file(&file, ids, keep, expected_hash).map_err(|e| match e {
MigrateError::Conflict => ApiError::conflict(e.to_string()),
MigrateError::Unsupported { .. } => ApiError::unsupported(e.to_string()),
MigrateError::Invalid(why) => ApiError::invalid(why),
MigrateError::Io(e) => ApiError::internal(e),
})?;
if migrated.edits == 0 {
return Ok(ConfigMigrated { edits: 0, drifted: Vec::new() });
}
let (_, _, _, after) = read(root)?;
let state = State::load_at(state).map_err(ApiError::internal)?;
let drifted = drifted(&state, &project(root)?, &before, &after);
Ok(ConfigMigrated { edits: migrated.edits, drifted })
}
pub fn drifted(state: &State, project: &str, before: &Config, after: &Config) -> Vec<String> {
let hash = |config: &Config, name: &str| config.resolved_table(name).ok().map(|(_, h)| h);
state
.instances
.iter()
.filter(|(_, i)| i.project == project)
.filter(|(_, i)| hash(before, &i.sandbox) != hash(after, &i.sandbox))
.map(|(name, _)| name.clone())
.collect()
}
pub fn ack(root: &Path, state: &Path, ids: &[String]) -> Result<ConfigAcked, ApiError> {
let (_, _, _, config) = read(root)?;
let current: BTreeSet<String> = identity_findings(&config).into_iter().map(|f| f.id).collect();
if let Some(id) = ids.iter().find(|id| !current.contains(*id)) {
return Err(ApiError::invalid(format!("no finding {id} in the config (reread its findings)")));
}
let project = project(root)?;
let acked = State::update_at(state, |s| {
let mut set = s.identity_acks.remove(&project).unwrap_or_default();
set.extend(ids.iter().cloned());
set.retain(|id| current.contains(id));
let acked: Vec<String> = set.iter().cloned().collect();
if !set.is_empty() {
s.identity_acks.insert(project.clone(), set);
}
Ok(acked)
})
.map_err(ApiError::internal)?;
Ok(ConfigAcked { acked })
}
#[cfg(test)]
mod tests {
use super::*;
fn scratch(tag: &str) -> (PathBuf, PathBuf) {
let dir = std::env::temp_dir().join(format!("devsandbox-identity-ops-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("root")).unwrap();
(dir.join("root"), dir.join("state/state.toml"))
}
const CONFIG: &str = "[sandbox.web]\nimage = \"img\"\nremoteUser = \"root\"\nmounts = [\"source=/a,target=/root/.a\"]\n\
[sandbox.api]\nimage = \"img\"\nworkspaceFolder = \"/root/app\"\n[sandbox.ok]\nimage = \"img\"\n";
fn instance(sandbox: &str, project: &str) -> crate::state::Instance {
let toml = format!(
"sandbox = \"{sandbox}\"\nproject = \"{project}\"\ncontainer = \"c\"\nfolder = \"/w\"\nworkspace = \"/w\"\ncreated_unix = 1\n"
);
toml::from_str(&toml).unwrap()
}
#[test]
fn drifted_is_the_instances_whose_sandbox_hash_moved() {
let before = Config::parse(CONFIG).unwrap();
let after = Config::parse(&CONFIG.replace("target=/root/.a", "target=/opt/a")).unwrap();
let mut state = State::default();
for (name, sandbox, project) in
[("web", "web", "p1"), ("web-2", "web", "p1"), ("api", "api", "p1"), ("other", "web", "p2"), ("gone", "nope", "p1")]
{
state.instances.insert(name.into(), instance(sandbox, project));
}
assert_eq!(drifted(&state, "p1", &before, &after), vec!["web", "web-2"]);
assert_eq!(drifted(&state, "p1", &before, &before), Vec::<String>::new());
}
#[test]
fn ack_adds_prunes_and_drops_an_empty_root() {
let (root, state) = scratch("ack");
std::fs::write(root.join("devsandboxes.toml"), CONFIG).unwrap();
let all: Vec<String> = findings(&root, &state, None).unwrap().findings.into_iter().map(|f| f.id).collect();
assert_eq!(all.len(), 3);
let got = ack(&root, &state, &all[..2]).unwrap();
let mut want = all[..2].to_vec();
want.sort();
assert_eq!(got.acked, want);
let project = crate::commands::services::project_id(&root).unwrap();
State::update_at(&state, |s| {
s.identity_acks.get_mut(&project).unwrap().insert("stale".into());
Ok(())
})
.unwrap();
let got = ack(&root, &state, &[]).unwrap();
assert_eq!(got.acked, want);
assert_eq!(code(ack(&root, &state, &["nope".into()])), "invalid");
std::fs::write(root.join("devsandboxes.toml"), "[sandbox.ok]\nimage = \"img\"\n").unwrap();
assert_eq!(ack(&root, &state, &[]).unwrap().acked, Vec::<String>::new());
assert!(State::load_at(&state).unwrap().identity_acks.is_empty());
let _ = std::fs::remove_dir_all(root.parent().unwrap());
}
#[cfg(unix)]
#[test]
fn migrate_refuses_a_signed_configs_copy() {
let (root, state) = scratch("copy");
let data = state.parent().unwrap();
let copy = crate::serve::fleet::mesh::tree_path(data, "cfg");
std::fs::create_dir_all(copy.join("mcps")).unwrap();
std::fs::write(copy.join("devsandboxes.toml"), CONFIG).unwrap();
std::fs::write(root.join("devsandboxes.toml"), CONFIG).unwrap();
let hash = findings(©, &state, None).unwrap().file_hash;
for at in [copy.clone(), copy.join("mcps")] {
let err = migrate(&at, &state, None, &[], &hash).unwrap_err();
assert_eq!(err.code, "denied");
assert!(err.message.ends_with("copy of config root `cfg`, signed by the fleet's authority: edit the config on the authority"), "{}", err.message);
}
assert_eq!(std::fs::read_to_string(copy.join("devsandboxes.toml")).unwrap(), CONFIG, "nothing written");
assert_eq!(migrate(&root, &state, None, &[], &hash).unwrap().edits, 2);
let _ = std::fs::remove_dir_all(root.parent().unwrap());
}
fn code<T: std::fmt::Debug>(r: Result<T, ApiError>) -> &'static str {
r.unwrap_err().code
}
}