fix: harden the vault before release
Require a private passphrase and loopback-or-token admin auth. New vaults use Argon2id; existing PBKDF2 vaults still open, and new snapshot metadata is encrypted. Wormhole tokens survive restart, offset uploads seal into the vault, and client-certificate uploads are real. Reject the command provider and keep Postgres passwords off the process arguments. Keep the interruptible canvas motion.
This commit is contained in:
@@ -28,11 +28,17 @@ pub struct Vault {
|
||||
|
||||
impl Vault {
|
||||
pub fn init(root: impl AsRef<Path>, passphrase: &str) -> Result<Self> {
|
||||
if passphrase == "dev-vault-key" {
|
||||
anyhow::bail!(
|
||||
"refusing to initialize a vault with the published dev passphrase; choose a private passphrase"
|
||||
);
|
||||
}
|
||||
let root = root.as_ref().to_path_buf();
|
||||
fs::create_dir_all(root.join("data"))?;
|
||||
fs::create_dir_all(root.join("snapshots"))?;
|
||||
fs::create_dir_all(root.join("index"))?;
|
||||
fs::create_dir_all(root.join("keys"))?;
|
||||
restrict_dir(&root.join("keys"))?;
|
||||
|
||||
let config_path = root.join("config");
|
||||
if !config_path.exists() {
|
||||
@@ -56,6 +62,7 @@ impl Vault {
|
||||
if !root.join("config").exists() {
|
||||
anyhow::bail!("vault not initialized");
|
||||
}
|
||||
restrict_dir(&root.join("keys"))?;
|
||||
let salt = root.join("keys/tessera.salt");
|
||||
let key = VaultKey::derive_from_passphrase(passphrase, &salt)?;
|
||||
let index = Self::load_index(&root)?;
|
||||
@@ -150,14 +157,28 @@ impl Vault {
|
||||
tree_root: chunk_id,
|
||||
};
|
||||
|
||||
let plain = serde_json::to_vec(&snap)?;
|
||||
let enc = self.key.encrypt(&plain)?;
|
||||
let path = self
|
||||
.root
|
||||
.join("snapshots")
|
||||
.join(format!("{}.json", snap.id));
|
||||
fs::write(&path, serde_json::to_string_pretty(&snap)?)?;
|
||||
.join(format!("{}.json.enc", snap.id));
|
||||
fs::write(&path, &enc)?;
|
||||
Ok(snap)
|
||||
}
|
||||
|
||||
fn read_snapshot_file(&self, path: &Path) -> Result<StoredSnapshot> {
|
||||
let name = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
|
||||
if name.ends_with(".json.enc") {
|
||||
let plain = self.key.decrypt(&fs::read(path)?)?;
|
||||
Ok(serde_json::from_slice(&plain)?)
|
||||
} else if name.ends_with(".json") {
|
||||
Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
|
||||
} else {
|
||||
anyhow::bail!("not a snapshot file");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_snapshots(&self) -> Result<Vec<StoredSnapshot>> {
|
||||
let dir = self.root.join("snapshots");
|
||||
if !dir.exists() {
|
||||
@@ -166,21 +187,21 @@ impl Vault {
|
||||
let mut out = Vec::new();
|
||||
for entry in fs::read_dir(&dir)? {
|
||||
let path = entry?.path();
|
||||
if path.extension().and_then(|e| e.to_str()) != Some("json") {
|
||||
let name = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
|
||||
if !(name.ends_with(".json") || name.ends_with(".json.enc")) {
|
||||
continue;
|
||||
}
|
||||
let data = fs::read_to_string(&path)?;
|
||||
let snap: StoredSnapshot = serde_json::from_str(&data)?;
|
||||
out.push(snap);
|
||||
out.push(self.read_snapshot_file(&path)?);
|
||||
}
|
||||
out.sort_by(|a, b| b.taken_at.cmp(&a.taken_at));
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn restore_snapshot(&self, id: Uuid, dest: &Path) -> Result<u64> {
|
||||
let path = self.root.join("snapshots").join(format!("{id}.json"));
|
||||
let data = fs::read_to_string(&path).context("snapshot metadata")?;
|
||||
let snap: StoredSnapshot = serde_json::from_str(&data)?;
|
||||
let enc = self.root.join("snapshots").join(format!("{id}.json.enc"));
|
||||
let plain = self.root.join("snapshots").join(format!("{id}.json"));
|
||||
let path = if enc.exists() { enc } else { plain };
|
||||
let snap = self.read_snapshot_file(&path).context("snapshot metadata")?;
|
||||
let bytes = self.read_chunk(&snap.tree_root)?;
|
||||
if let Some(parent) = dest.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
@@ -192,9 +213,15 @@ impl Vault {
|
||||
pub fn forget(&mut self, ids: &[Uuid]) -> Result<usize> {
|
||||
let mut removed = 0;
|
||||
for id in ids {
|
||||
let path = self.root.join("snapshots").join(format!("{id}.json"));
|
||||
if path.exists() {
|
||||
fs::remove_file(&path)?;
|
||||
let mut gone = false;
|
||||
for suffix in [".json.enc", ".json"] {
|
||||
let path = self.root.join("snapshots").join(format!("{id}{suffix}"));
|
||||
if path.exists() {
|
||||
fs::remove_file(&path)?;
|
||||
gone = true;
|
||||
}
|
||||
}
|
||||
if gone {
|
||||
removed += 1;
|
||||
}
|
||||
}
|
||||
@@ -236,6 +263,19 @@ impl Vault {
|
||||
}
|
||||
}
|
||||
|
||||
fn restrict_dir(path: &Path) -> Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
if path.exists() {
|
||||
fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = path;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -315,4 +355,69 @@ mod tests {
|
||||
.iter()
|
||||
.any(|s| s.manifest.orbit_id == other_id));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_snapshots_are_encrypted_and_legacy_json_still_reads() {
|
||||
let dir = tempdir().unwrap();
|
||||
let mut vault = Vault::init(dir.path(), "secret").unwrap();
|
||||
let legacy_id = Uuid::new_v4();
|
||||
let legacy = StoredSnapshot {
|
||||
id: legacy_id,
|
||||
taken_at: DateTime::from_timestamp(1_700_000_000, 0).unwrap(),
|
||||
manifest: SnapshotManifest {
|
||||
snapshot_id: legacy_id,
|
||||
orbit_id: Uuid::new_v4(),
|
||||
orbit_name: "legacy".into(),
|
||||
labels: vec![],
|
||||
provider: "filesystem".into(),
|
||||
paths: vec![],
|
||||
restore_hints: serde_json::json!({}),
|
||||
},
|
||||
size_bytes: 4,
|
||||
tree_root: "missing".into(),
|
||||
};
|
||||
fs::write(
|
||||
dir.path().join("snapshots").join(format!("{legacy_id}.json")),
|
||||
serde_json::to_string(&legacy).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let manifest = SnapshotManifest {
|
||||
snapshot_id: Uuid::new_v4(),
|
||||
orbit_id: Uuid::new_v4(),
|
||||
orbit_name: "new".into(),
|
||||
labels: vec![],
|
||||
provider: "filesystem".into(),
|
||||
paths: vec![],
|
||||
restore_hints: serde_json::json!({}),
|
||||
};
|
||||
let snap = vault.snapshot(manifest, b"cipher", false).unwrap();
|
||||
assert!(dir
|
||||
.path()
|
||||
.join("snapshots")
|
||||
.join(format!("{}.json.enc", snap.id))
|
||||
.exists());
|
||||
assert!(!dir
|
||||
.path()
|
||||
.join("snapshots")
|
||||
.join(format!("{}.json", snap.id))
|
||||
.exists());
|
||||
let names: Vec<_> = vault
|
||||
.list_snapshots()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|s| s.manifest.orbit_name)
|
||||
.collect();
|
||||
assert!(names.contains(&"legacy".to_string()));
|
||||
assert!(names.contains(&"new".to_string()));
|
||||
let out = dir.path().join("restored.bin");
|
||||
assert_eq!(vault.restore_snapshot(snap.id, &out).unwrap(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_rejects_published_dev_passphrase() {
|
||||
let dir = tempdir().unwrap();
|
||||
assert!(Vault::init(dir.path(), "dev-vault-key").is_err());
|
||||
assert!(Vault::init(dir.path(), "").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user