use std::collections::BTreeSet;
use std::fs;
use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow, bail, ensure};
use super::aside::{RESTORE_MARKER, aside_path, carry_over, create_private_dir};
use super::device::sync_dir;
use super::hardware::{HardwareSource, SystemHardware};
use super::storage::create_device_keys_with;
use super::{
DEVICE_KEY_FILE, DEVICE_PUB_FILE, DeviceKeys, HARDWARE_BINDING_FILE, HeldEpoch, IdentityTag,
KeyStorage, NEXT_STORE_KEY_FILE, RecoveryPhrase, SEALED_DEVICE_KEY_FILE, STORE_KEY_FILE,
StoreKey, WRAPPED_STORE_KEY_FILE, found_identity, refresh_wrapped_store_key,
unwrap_store_key_with_epochs, unwrap_store_key_with_phrase, wrap, wrap_for_recovery,
write_store_key,
};
use crate::machine::MachineIdentity;
use crate::sqlite_store::SqliteStore;
use crate::sqlite_store::encrypt_migration::{
FileState, PLAINTEXT_COPY_PREFIX, RewriteTo, db_file_state,
};
use crate::store::StoreAdmin;
use crate::write_queue;
const DB_FILE: &str = "brain.db";
const MACHINE_FILE: &str = "machine.toml";
const CONFIG_FILE: &str = "config.toml";
const LEFT_BEHIND: &[&str] = &[
MACHINE_FILE,
DEVICE_KEY_FILE,
SEALED_DEVICE_KEY_FILE,
HARDWARE_BINDING_FILE,
DEVICE_PUB_FILE,
"sync.key",
"migrated-peers.json",
"api.token",
".api.token.lock",
"sync-peers",
"clock-reading",
"service.json",
"brain.db-shm",
STORE_KEY_FILE,
NEXT_STORE_KEY_FILE,
"brain.db.enc",
"brain.db.enc-journal",
"brain.db.enc-wal",
"brain.db.enc-shm",
];
const LEFT_BEHIND_PREFIXES: &[&str] = &[".device.key.", ".store.key.", PLAINTEXT_COPY_PREFIX];
const HOST_FILES: &[&str] = &["service.json"];
pub struct RestoreRequest<'a> {
pub backup: &'a Path,
pub data_dir: &'a Path,
pub phrase: &'a RecoveryPhrase,
pub storage: &'a KeyStorage,
pub now_micros: i64,
}
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum BackupKeys<'a> {
Phrase(&'a RecoveryPhrase),
Epochs(&'a [HeldEpoch]),
}
pub struct BackupRestore<'a> {
pub backup: &'a Path,
pub data_dir: &'a Path,
pub keys: BackupKeys<'a>,
pub storage: &'a KeyStorage,
pub now_micros: i64,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Restored {
pub machine: MachineIdentity,
pub identity: IdentityTag,
pub opened: Vec<IdentityTag>,
pub phrase: RecoveryPhrase,
pub moved_aside: Option<PathBuf>,
}
pub fn restore_backup(req: &RestoreRequest<'_>) -> Result<Restored> {
restore_backup_with(&BackupRestore {
backup: req.backup,
data_dir: req.data_dir,
keys: BackupKeys::Phrase(req.phrase),
storage: req.storage,
now_micros: req.now_micros,
})
}
pub fn restore_backup_with(req: &BackupRestore<'_>) -> Result<Restored> {
restore_with_hardware(req, &SystemHardware)
}
fn restore_with_hardware(
req: &BackupRestore<'_>,
hardware: &dyn HardwareSource,
) -> Result<Restored> {
let backup = check_backup(req.backup)?;
if let Ok(target) = fs::canonicalize(req.data_dir) {
ensure!(
target != backup,
"the backup is the data dir itself; restore from a copy"
);
}
let parent = parent_of(req.data_dir)?;
fs::create_dir_all(&parent).with_context(|| format!("creating {}", parent.display()))?;
let base = dir_name(req.data_dir)?;
let staging_prefix = format!(".{base}.restoring-");
remove_stale_staging(&parent, &staging_prefix, &backup);
let staging = parent.join(format!("{staging_prefix}{}", uuid::Uuid::new_v4().simple()));
let restored = match prepare(req, &backup, &staging, hardware) {
Ok(restored) => restored,
Err(e) => return Err(with_staging_removed(e, &staging)),
};
let moved_aside = match swap_in(req.data_dir, &staging, &base, req.now_micros, rename) {
Ok(moved) => moved,
Err(Swap::Untouched(e)) => return Err(with_staging_removed(e, &staging)),
Err(Swap::Stranded(e)) => return Err(e),
};
if let Err(e) = sync_dir(&parent) {
tracing::warn!("the restore is in place but not yet flushed to disk: {e:#}");
}
Ok(Restored {
moved_aside,
..restored
})
}
fn reseal_backup_queue(backup: &Path, staging: &Path, device: &DeviceKeys) -> Result<()> {
let queue = staging.join(write_queue::DIR_NAME);
if !queue.is_dir() {
return Ok(());
}
let old = match DeviceKeys::load(&backup.join(DEVICE_KEY_FILE)) {
Ok(Some(old)) => old,
Ok(None) => return Ok(()),
Err(e) => {
tracing::warn!("sealed queued writes in the backup stay unreadable: {e:#}");
return Ok(());
}
};
write_queue::reseal_dir(&queue, old.age_identity(), &[device.age_recipient()])?;
Ok(())
}
pub fn check_backup(backup: &Path) -> Result<PathBuf> {
let canonical = fs::canonicalize(backup)
.with_context(|| format!("reading the backup {}", backup.display()))?;
ensure!(
canonical.join(DB_FILE).is_file(),
"{} holds no {DB_FILE}; name a copy of a second-brain data dir",
canonical.display()
);
Ok(canonical)
}
fn prepare(
req: &BackupRestore<'_>,
backup: &Path,
staging: &Path,
hardware: &dyn HardwareSource,
) -> Result<Restored> {
if let BackupKeys::Epochs(held) = req.keys {
ensure!(
!held.is_empty(),
"no key epoch was given, so the backup cannot be opened"
);
}
create_private_dir(staging)?;
copy_tree(backup, staging, true)?;
let machine = MachineIdentity::load_or_create(staging)?;
let device = create_device_keys_with(staging, req.storage, hardware)?;
reseal_backup_queue(backup, staging, &device)?;
let store = open_backup_store(&staging.join(DB_FILE), staging, req.keys, &machine.id)
.context("opening the restored database")?;
store.register_machine(&machine)?;
let reopened;
let old_epochs: &[HeldEpoch] = match req.keys {
BackupKeys::Phrase(phrase) => {
reopened =
reopen_old_epochs(&store, phrase, &device, req.now_micros).with_context(|| {
format!("opening {} with the recovery phrase", backup.display())
})?;
&reopened
}
BackupKeys::Epochs(held) => {
keep_epochs(&store, held, &device, req.now_micros)?;
held
}
};
let phrase = RecoveryPhrase::generate()?;
let recipient = phrase.recipient()?;
let founded = found_identity(
&store,
&device,
&machine.name,
std::slice::from_ref(&recipient),
req.now_micros,
)?;
for held in old_epochs {
wrap_for_recovery(
&store,
&held.identity,
&[(held.epoch, &held.key)],
std::slice::from_ref(&recipient),
)?;
}
if cfg!(feature = "encryption") {
rekey_restored(&store, staging, &device)?;
}
drop(store);
let opened: BTreeSet<&str> = old_epochs.iter().map(|h| h.identity.as_str()).collect();
let opened = opened
.into_iter()
.map(|t| IdentityTag::from(t.to_string()))
.collect();
sync_tree(staging)?;
Ok(Restored {
machine,
identity: founded.tag,
opened,
phrase,
moved_aside: None,
})
}
fn open_backup_store(
db: &Path,
staging: &Path,
keys: BackupKeys<'_>,
machine_id: &str,
) -> Result<SqliteStore> {
match db_file_state(db)? {
FileState::Encrypted => {
let unwrapped = match keys {
BackupKeys::Phrase(phrase) => unwrap_store_key_with_phrase(staging, phrase)?,
BackupKeys::Epochs(held) => unwrap_store_key_with_epochs(staging, held)?,
};
let key = unwrapped.ok_or_else(|| {
anyhow!(
"the backup's {DB_FILE} is encrypted but it holds no \
{WRAPPED_STORE_KEY_FILE}; restore a copy of the whole data dir"
)
})?;
SqliteStore::open_with_key(db, machine_id.to_string(), &key)
}
_ => SqliteStore::open(db, machine_id.to_string()),
}
}
fn rekey_restored(store: &SqliteStore, staging: &Path, device: &DeviceKeys) -> Result<()> {
let key = StoreKey::generate()?;
write_store_key(staging, STORE_KEY_FILE, &key, device)?;
store
.rewrite_under_key(RewriteTo::Key(&key))
.context("rewriting the restored store under a new store key")?;
refresh_wrapped_store_key(store, staging, device, &key)
.context("wrapping the new store key to the new identity")?;
Ok(())
}
fn reopen_old_epochs(
store: &SqliteStore,
phrase: &RecoveryPhrase,
device: &DeviceKeys,
now_micros: i64,
) -> Result<Vec<HeldEpoch>> {
let identity = phrase.identity()?;
let own = store.machine_id().to_string();
let recipient = device.age_recipient();
let mut opened = Vec::new();
for envelope in store.recovery_envelopes(&phrase.recipient()?)? {
let key = match envelope.open(&identity) {
Ok(key) => key,
Err(e) => {
tracing::warn!("skipping a recovery envelope the phrase cannot open: {e:#}");
continue;
}
};
let rewrapped = wrap(
&key,
envelope.epoch.number,
&envelope.identity_tag,
&recipient,
Some(own.clone()),
)?;
store.put_keys_durably(
&envelope.identity_tag,
&[envelope.epoch],
&[rewrapped],
now_micros,
)?;
opened.push(HeldEpoch::new(envelope.identity_tag, envelope.epoch, key));
}
if opened.is_empty() {
bail!(
"this recovery phrase opens no key in the backup; check the phrase, or restore a \
backup of a device that trusted it"
);
}
Ok(opened)
}
fn keep_epochs(
store: &SqliteStore,
held: &[HeldEpoch],
device: &DeviceKeys,
now_micros: i64,
) -> Result<()> {
let own = store.machine_id().to_string();
let recipient = device.age_recipient();
for h in held {
let envelope = wrap(
&h.key,
h.epoch.number,
&h.identity,
&recipient,
Some(own.clone()),
)?;
store.put_keys_durably(&h.identity, &[h.epoch], &[envelope], now_micros)?;
}
Ok(())
}
enum Swap {
Untouched(anyhow::Error),
Stranded(anyhow::Error),
}
fn rename(from: &Path, to: &Path) -> io::Result<()> {
fs::rename(from, to)
}
fn swap_in(
data_dir: &Path,
staging: &Path,
base: &str,
now_micros: i64,
rename: impl Fn(&Path, &Path) -> io::Result<()>,
) -> Result<Option<PathBuf>, Swap> {
let parent = parent_of(data_dir).map_err(Swap::Untouched)?;
let moved_aside = match fs::symlink_metadata(data_dir) {
Ok(_) => {
let carried = HOST_FILES
.iter()
.try_for_each(|name| carry_over(&data_dir.join(name), &staging.join(name)))
.and_then(|()| {
if staging.join(CONFIG_FILE).exists() {
return Ok(());
}
carry_over(&data_dir.join(CONFIG_FILE), &staging.join(CONFIG_FILE))
})
.and_then(|()| sync_dir(staging));
carried.map_err(Swap::Untouched)?;
let aside = aside_path(&parent, base, RESTORE_MARKER, now_micros);
rename(data_dir, &aside)
.with_context(|| {
format!("moving {} aside to {}", data_dir.display(), aside.display())
})
.map_err(Swap::Untouched)?;
Some(aside)
}
Err(e) if e.kind() == ErrorKind::NotFound => None,
Err(e) => {
return Err(Swap::Untouched(
anyhow!(e).context(format!("reading {}", data_dir.display())),
));
}
};
let Err(e) = rename(staging, data_dir) else {
return Ok(moved_aside);
};
let error = anyhow!(e).context(format!(
"moving the restored copy {} into {}",
staging.display(),
data_dir.display()
));
let Some(aside) = moved_aside else {
return Err(Swap::Untouched(error));
};
match rename(&aside, data_dir) {
Ok(()) => Err(Swap::Untouched(error)),
Err(back) => Err(Swap::Stranded(error.context(format!(
"the previous data dir is at {} and could not be moved back ({back}); the restored \
copy is at {}",
aside.display(),
staging.display()
)))),
}
}
fn sync_tree(dir: &Path) -> Result<()> {
for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
let entry = entry?;
let path = entry.path();
let kind = entry.file_type()?;
if kind.is_dir() {
sync_tree(&path)?;
} else if kind.is_file() {
fs::File::open(&path)
.and_then(|f| f.sync_all())
.with_context(|| format!("syncing {}", path.display()))?;
}
}
sync_dir(dir)
}
fn copy_tree(from: &Path, to: &Path, top: bool) -> Result<()> {
for entry in fs::read_dir(from).with_context(|| format!("reading {}", from.display()))? {
let entry = entry?;
let name = entry.file_name();
if top
&& name.to_str().is_some_and(|n| {
LEFT_BEHIND.contains(&n) || LEFT_BEHIND_PREFIXES.iter().any(|p| n.starts_with(p))
})
{
continue;
}
let source = entry.path();
let target = to.join(&name);
let kind = entry.file_type()?;
if kind.is_dir() {
fs::create_dir(&target).with_context(|| format!("creating {}", target.display()))?;
copy_tree(&source, &target, false)?;
} else if kind.is_file() {
fs::copy(&source, &target)
.with_context(|| format!("copying {} to {}", source.display(), target.display()))?;
} else if kind.is_symlink() {
copy_symlink(&source, &target)?;
} else {
tracing::warn!("restore skips {}, which is not a file", source.display());
}
}
Ok(())
}
#[cfg(unix)]
fn copy_symlink(source: &Path, target: &Path) -> Result<()> {
let link = fs::read_link(source).with_context(|| format!("reading {}", source.display()))?;
std::os::unix::fs::symlink(&link, target)
.with_context(|| format!("linking {} to {}", target.display(), link.display()))
}
#[cfg(not(unix))]
fn copy_symlink(source: &Path, _target: &Path) -> Result<()> {
tracing::warn!("restore skips the link {}", source.display());
Ok(())
}
fn remove_stale_staging(parent: &Path, prefix: &str, backup: &Path) {
let Ok(entries) = fs::read_dir(parent) else {
return;
};
for entry in entries.flatten() {
if !entry.file_name().to_string_lossy().starts_with(prefix)
|| fs::canonicalize(entry.path()).is_ok_and(|p| p == backup)
{
continue;
}
if let Err(e) = fs::remove_dir_all(entry.path()) {
tracing::warn!(
"cannot remove the partial restore {}: {e}",
entry.path().display()
);
}
}
}
fn with_staging_removed(error: anyhow::Error, staging: &Path) -> anyhow::Error {
match fs::remove_dir_all(staging) {
Ok(()) => error,
Err(e) if e.kind() == ErrorKind::NotFound => error,
Err(e) => error.context(format!(
"also failed to remove the partial restore {}: {e}",
staging.display()
)),
}
}
fn parent_of(path: &Path) -> Result<PathBuf> {
match path.parent() {
Some(dir) if !dir.as_os_str().is_empty() => Ok(dir.to_path_buf()),
Some(_) => Ok(PathBuf::from(".")),
None => bail!("{} has no parent directory", path.display()),
}
}
fn dir_name(path: &Path) -> Result<String> {
path.file_name()
.map(|n| n.to_string_lossy().into_owned())
.ok_or_else(|| anyhow!("{} has no directory name", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys::{DeviceRecord, Epoch, IdentityKey, create_device_keys};
use crate::schema::{Memory, MemoryType};
use crate::store::Store;
const NOW: i64 = 1_793_600_000_000_000;
struct OldDevice {
dir: tempfile::TempDir,
tag: IdentityTag,
phrase: RecoveryPhrase,
memory: Memory,
}
fn open_restored(data_dir: &Path, machine_id: &str) -> SqliteStore {
let db = data_dir.join(DB_FILE);
if db_file_state(&db).unwrap() != FileState::Encrypted {
return SqliteStore::open(&db, machine_id.to_string()).unwrap();
}
let device = super::super::load_device_keys(data_dir).unwrap().unwrap();
let key = super::super::store_key_for(&db, &device).unwrap();
SqliteStore::open_with_key(&db, machine_id.to_string(), &key).unwrap()
}
#[cfg(feature = "encryption")]
#[test]
fn an_encrypted_backup_opens_with_the_phrase_and_comes_back_under_a_new_store_key() {
let old = old_device();
let old_machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let old_device_keys = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
{
let store =
SqliteStore::open(&old.dir.path().join(DB_FILE), old_machine.id.clone()).unwrap();
super::super::encrypt_store(&store, old.dir.path(), &old_device_keys).unwrap();
}
assert!(old.dir.path().join(WRAPPED_STORE_KEY_FILE).exists());
let old_store_key =
super::super::read_store_key(&old.dir.path().join(STORE_KEY_FILE), &old_device_keys)
.unwrap()
.unwrap();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
let restored = restore_backup(&RestoreRequest {
backup: old.dir.path(),
data_dir: &data_dir,
phrase: &old.phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
let db = data_dir.join(DB_FILE);
assert_eq!(db_file_state(&db).unwrap(), FileState::Encrypted);
assert!(
SqliteStore::open_with_key(&db, restored.machine.id.clone(), &old_store_key).is_err(),
"the backup's store key must not open the restored store"
);
let store = open_restored(&data_dir, &restored.machine.id);
assert_eq!(
store.get_memory(old.memory.id).unwrap().unwrap().content,
old.memory.content
);
let new_key = super::super::unwrap_store_key_with_phrase(&data_dir, &restored.phrase)
.unwrap()
.expect("the restored data dir wraps its store key to the new phrase");
assert!(SqliteStore::open_with_key(&db, restored.machine.id.clone(), &new_key).is_ok());
assert!(
super::super::unwrap_store_key_with_phrase(&data_dir, &old.phrase).is_err(),
"the old phrase opens no store key of the new identity"
);
}
const SEALED_ENTRY: &str = "0000000000000000001-0b5c2f9e-5d0e-4c1a-9a57-0c7f4f1f2a10.sealed";
fn queued_forget() -> write_queue::Entry {
write_queue::Entry {
tool: "forget".to_string(),
method: "DELETE".to_string(),
path: "/v1/memory/m-1".to_string(),
query: Some("hard=false&reason=wrong".to_string()),
body: None,
}
}
fn old_device() -> OldDevice {
let dir = tempfile::tempdir().unwrap();
let path = dir.path();
let machine = MachineIdentity::load_or_create(path).unwrap();
let device = create_device_keys(path, &KeyStorage::File).unwrap();
let store = SqliteStore::open(&path.join(DB_FILE), machine.id.clone()).unwrap();
store.register_machine(&machine).unwrap();
let epoch0 = IdentityKey::generate().unwrap();
let epoch1 = IdentityKey::generate().unwrap();
let tag = IdentityTag::migrated(&epoch0.key_id());
let epochs = [
Epoch {
number: 0,
key_id: epoch0.key_id(),
},
Epoch {
number: 1,
key_id: epoch1.key_id(),
},
];
let own = machine.id.clone();
let envelopes = [
wrap(&epoch0, 0, &tag, &device.age_recipient(), Some(own.clone())).unwrap(),
wrap(&epoch1, 1, &tag, &device.age_recipient(), Some(own.clone())).unwrap(),
];
let row = DeviceRecord {
machine_id: own.clone(),
identity_tag: tag.as_str().to_string(),
version: 1,
name: machine.name.clone(),
age_recipient: device.age_recipient().to_string(),
ssh_public_key: device.ssh_public_key(),
signing_public_key: device.verifying_key().to_bytes(),
recovery_fingerprints: Vec::new(),
ssh_login: None,
ssh_port: None,
ssh_host_key: None,
}
.sign(&own, device.signing_key());
store
.insert_first_device_record(&row, &own, &device.verifying_key())
.unwrap();
store
.put_keys_durably(&tag, &epochs, &envelopes, NOW)
.unwrap();
let phrase = RecoveryPhrase::generate().unwrap();
let held = [(epochs[0], &epoch0), (epochs[1], &epoch1)];
wrap_for_recovery(&store, &tag, &held, &[phrase.recipient().unwrap()]).unwrap();
let memory = Memory::new(
"the restored store keeps this".to_string(),
MemoryType::Semantic,
"test".to_string(),
String::new(),
);
store.store_memory(&memory).unwrap();
store.record_peer_ack("old-peer", 3, NOW).unwrap();
store.pin_peer("old-peer", 2).unwrap();
assert_eq!(store.peer_acks().unwrap().len(), 1);
drop(store);
fs::write(path.join("sync-peers"), "someone@old-peer\n").unwrap();
fs::write(path.join(".device.key.0123.tmp"), "half-written key").unwrap();
fs::write(path.join("api.token"), "old-token").unwrap();
fs::write(path.join(DEVICE_PUB_FILE), "age1old\n").unwrap();
fs::write(path.join("service.json"), "{\"from\":\"backup\"}").unwrap();
fs::create_dir(path.join("queue")).unwrap();
fs::write(path.join("queue").join("pending.json"), "{}").unwrap();
fs::write(
path.join("queue").join(SEALED_ENTRY),
queued_forget()
.to_sealed_file(&[device.age_recipient()])
.unwrap(),
)
.unwrap();
OldDevice {
dir,
tag,
phrase,
memory,
}
}
fn pins(store: &SqliteStore) -> i64 {
store
.conn()
.query_row("SELECT COUNT(*) FROM peer_pins", [], |r| r.get(0))
.unwrap()
}
#[test]
fn the_phrase_restores_a_backup_under_a_new_machine_id_and_identity() {
let old = old_device();
let old_machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let old_key = fs::read(old.dir.path().join(DEVICE_KEY_FILE)).unwrap();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
fs::create_dir(&data_dir).unwrap();
let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();
SqliteStore::open(&data_dir.join(DB_FILE), fresh.id.clone()).unwrap();
fs::write(data_dir.join("service.json"), "{\"from\":\"host\"}").unwrap();
let typed = RecoveryPhrase::parse(&old.phrase.expose_text()).unwrap();
let restored = restore_backup(&RestoreRequest {
backup: old.dir.path(),
data_dir: &data_dir,
phrase: &typed,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
let machine = MachineIdentity::load_or_create(&data_dir).unwrap();
assert_eq!(machine.id, restored.machine.id);
assert_ne!(machine.id, old_machine.id);
assert_ne!(machine.id, fresh.id);
assert_ne!(fs::read(data_dir.join(DEVICE_KEY_FILE)).unwrap(), old_key);
assert!(!data_dir.join("sync-peers").exists());
assert!(!data_dir.join(".device.key.0123.tmp").exists());
assert!(!data_dir.join("api.token").exists());
assert!(!data_dir.join(DEVICE_PUB_FILE).exists());
assert_eq!(
fs::read_to_string(data_dir.join("service.json")).unwrap(),
"{\"from\":\"host\"}"
);
assert!(data_dir.join("queue").join("pending.json").exists());
let sealed = fs::read(data_dir.join("queue").join(SEALED_ENTRY)).unwrap();
let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
let opened = write_queue::open(&sealed, Some(device.age_identity())).unwrap();
assert_eq!(opened, queued_forget());
let aside = restored.moved_aside.clone().unwrap();
assert!(aside.join(DB_FILE).exists());
assert_eq!(
MachineIdentity::load_or_create(&aside).unwrap().id,
fresh.id
);
assert!(
old.dir.path().join("sync-peers").exists(),
"the backup is untouched"
);
let expected = if cfg!(feature = "encryption") {
FileState::Encrypted
} else {
FileState::Plaintext
};
assert_eq!(db_file_state(&data_dir.join(DB_FILE)).unwrap(), expected);
let store = open_restored(&data_dir, &machine.id);
assert_eq!(
store.get_memory(old.memory.id).unwrap().unwrap().content,
old.memory.content
);
let row = store.device_record(&machine.id).unwrap().unwrap();
assert_eq!(row.current.record.identity_tag, restored.identity.as_str());
assert!(restored.identity.as_str().starts_with("f-"));
assert_ne!(restored.identity, old.tag);
assert_eq!(restored.opened, vec![old.tag.clone()]);
assert_eq!(
row.current.record.recovery_fingerprints,
vec![restored.phrase.fingerprint().unwrap()]
);
assert!(
store.peer_acks().unwrap().is_empty(),
"old neighbours are cleared"
);
assert_eq!(pins(&store), 0);
let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
let mine = store.envelopes_for_machine(&machine.id).unwrap();
let old_epochs: Vec<u32> = mine
.iter()
.filter(|e| e.identity_tag == old.tag)
.map(|e| e.epoch.number)
.collect();
assert_eq!(
old_epochs,
vec![0, 1],
"old epochs stay readable on the new device"
);
for envelope in &mine {
envelope.open(device.age_identity()).unwrap();
}
let new_epochs = store.key_epochs(&restored.identity).unwrap();
assert_eq!(new_epochs.len(), 1);
assert_eq!(new_epochs[0].epoch.number, 1);
let shown = RecoveryPhrase::parse(&restored.phrase.expose_text()).unwrap();
let recipients = store.recovery_recipients(&restored.identity).unwrap();
assert_eq!(recipients.len(), 1);
assert_eq!(
recipients[0].to_string(),
shown.recipient().unwrap().to_string()
);
let envelope = store
.recovery_envelopes(&shown.recipient().unwrap())
.unwrap();
assert_eq!(envelope.len(), 3, "the new epoch and both old ones");
envelope[0].open(&shown.identity().unwrap()).unwrap();
assert!(
store
.recovery_recipients(&restored.identity)
.unwrap()
.iter()
.all(|r| r.to_string() != old.phrase.recipient().unwrap().to_string()),
"the old phrase is not trusted by the new identity"
);
let leftovers: Vec<_> = fs::read_dir(home.path())
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().contains("restoring"))
.collect();
assert!(leftovers.is_empty());
}
#[test]
fn a_second_restore_with_the_new_phrase_opens_every_earlier_identity() {
let old = old_device();
let first_home = tempfile::tempdir().unwrap();
let first_dir = first_home.path().join(".second-brain");
let first = restore_backup(&RestoreRequest {
backup: old.dir.path(),
data_dir: &first_dir,
phrase: &old.phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
let second_home = tempfile::tempdir().unwrap();
let second_dir = second_home.path().join(".second-brain");
let second = restore_backup(&RestoreRequest {
backup: &first_dir,
data_dir: &second_dir,
phrase: &first.phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
let opened: BTreeSet<&str> = second.opened.iter().map(IdentityTag::as_str).collect();
assert_eq!(
opened,
BTreeSet::from([old.tag.as_str(), first.identity.as_str()])
);
let store = open_restored(&second_dir, &second.machine.id);
let device = super::super::load_device_keys(&second_dir)
.unwrap()
.unwrap();
let mine = store.envelopes_for_machine(&second.machine.id).unwrap();
let held = |tag: &IdentityTag| -> Vec<u32> {
mine.iter()
.filter(|e| &e.identity_tag == tag)
.map(|e| {
e.open(device.age_identity()).unwrap();
e.epoch.number
})
.collect()
};
assert_eq!(held(&old.tag), vec![0, 1]);
assert_eq!(held(&first.identity), vec![1]);
assert_eq!(held(&second.identity), vec![1]);
let last = store
.recovery_envelopes(&second.phrase.recipient().unwrap())
.unwrap();
assert_eq!(last.len(), 4, "every epoch reaches the newest phrase");
}
#[test]
fn a_restore_from_a_stranded_copy_keeps_that_copy() {
let old = old_device();
let home = tempfile::tempdir().unwrap();
let stranded = home.path().join("..second-brain.restoring-0123");
fs::create_dir(&stranded).unwrap();
copy_tree(old.dir.path(), &stranded, false).unwrap();
restore_backup(&RestoreRequest {
backup: &stranded,
data_dir: &home.path().join(".second-brain"),
phrase: &old.phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
assert!(stranded.join(DB_FILE).exists());
}
#[test]
fn a_restore_binds_the_new_device_keys_to_this_hardware_not_the_backups() {
use crate::keys::{
HardwareCheck, HardwareId, check_hardware_with, read_binding, write_binding,
};
struct Here;
impl HardwareSource for Here {
fn read(&self, _data_dir: &Path) -> HardwareId {
HardwareId::parse("disk:the-new-hardware")
}
}
let old = old_device();
write_binding(
old.dir.path(),
&HardwareId::parse("disk:the-backups-hardware"),
)
.unwrap();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
let restored = restore_with_hardware(
&BackupRestore {
backup: old.dir.path(),
data_dir: &data_dir,
keys: BackupKeys::Phrase(&old.phrase),
storage: &KeyStorage::File,
now_micros: NOW,
},
&Here,
)
.unwrap();
assert_eq!(
read_binding(&data_dir).unwrap(),
Some(HardwareId::parse("disk:the-new-hardware"))
);
assert_eq!(
check_hardware_with(&data_dir, &data_dir, &KeyStorage::File, &Here).unwrap(),
HardwareCheck::Bound
);
assert_eq!(
MachineIdentity::load_or_create(&data_dir).unwrap().id,
restored.machine.id
);
}
#[test]
fn a_restore_into_a_missing_data_dir_creates_it() {
let old = old_device();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
let stale = home.path().join("..second-brain.restoring-0123");
fs::create_dir(&stale).unwrap();
fs::write(stale.join(DB_FILE), "left by a crash").unwrap();
let restored = restore_backup(&RestoreRequest {
backup: old.dir.path(),
data_dir: &data_dir,
phrase: &old.phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
assert!(restored.moved_aside.is_none());
assert!(!stale.exists(), "a crashed restore's copy is removed");
assert!(data_dir.join(DB_FILE).exists());
assert!(!data_dir.join("service.json").exists());
}
fn handed_from(old: &OldDevice) -> Vec<HeldEpoch> {
let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let device = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
let held = super::super::every_held_epoch(&store, &device).unwrap();
let transit = DeviceKeys::generate().unwrap();
let envelopes = super::super::hand_over(&held, &transit.age_recipient()).unwrap();
super::super::open_handed(&envelopes, &transit).unwrap()
}
#[cfg(feature = "encryption")]
#[test]
fn epochs_a_surviving_device_hands_over_restore_an_encrypted_backup_as_a_new_identity() {
let old = old_device();
let backup = tempfile::tempdir().unwrap();
{
let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let device = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
}
copy_tree(old.dir.path(), backup.path(), false).unwrap();
let held = {
let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let device = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
let key = super::super::store_key_for(&old.dir.path().join(DB_FILE), &device).unwrap();
let store = SqliteStore::open_with_key(&old.dir.path().join(DB_FILE), machine.id, &key)
.unwrap();
let held = super::super::every_held_epoch(&store, &device).unwrap();
let transit = DeviceKeys::generate().unwrap();
let envelopes = super::super::hand_over(&held, &transit.age_recipient()).unwrap();
super::super::open_handed(&envelopes, &transit).unwrap()
};
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
let restored = restore_backup_with(&BackupRestore {
backup: backup.path(),
data_dir: &data_dir,
keys: BackupKeys::Epochs(&held),
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
assert_ne!(restored.identity, old.tag);
assert_eq!(restored.opened, vec![old.tag.clone()]);
let store = open_restored(&data_dir, &restored.machine.id);
assert_eq!(
store.get_memory(old.memory.id).unwrap().unwrap().content,
old.memory.content
);
let device = super::super::load_device_keys(&data_dir).unwrap().unwrap();
let kept: Vec<u32> = store
.envelopes_for_machine(&restored.machine.id)
.unwrap()
.iter()
.filter(|e| e.identity_tag == old.tag)
.map(|e| e.epoch.number)
.collect();
assert_eq!(kept, vec![0, 1], "the handed epochs stay decrypt-only");
let opened = super::super::every_held_epoch(&store, &device).unwrap();
assert_eq!(opened.len(), 3, "both old epochs and the new identity's");
let recovery = store
.recovery_envelopes(&restored.phrase.recipient().unwrap())
.unwrap();
assert_eq!(recovery.len(), 3, "the new phrase opens the old epochs too");
}
#[cfg(feature = "encryption")]
#[test]
fn a_surviving_device_restores_onto_itself_with_its_own_past_epochs() {
let old = old_device();
{
let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let device = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
}
let backup = tempfile::tempdir().unwrap();
copy_tree(old.dir.path(), backup.path(), false).unwrap();
let own = super::super::every_held_epoch_in(old.dir.path(), &KeyStorage::File).unwrap();
assert_eq!(own.len(), 2);
let restored = restore_backup_with(&BackupRestore {
backup: backup.path(),
data_dir: old.dir.path(),
keys: BackupKeys::Epochs(&own),
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap();
let aside = restored
.moved_aside
.clone()
.expect("its own store moves aside");
assert!(aside.to_string_lossy().contains(".before-restore-"));
assert!(aside.join(DB_FILE).exists());
assert!(aside.join(DEVICE_KEY_FILE).exists());
assert_eq!(restored.opened, vec![old.tag.clone()]);
assert_eq!(
db_file_state(&backup.path().join(DB_FILE)).unwrap(),
FileState::Encrypted,
"the backup opens only through the wrapped store key"
);
let store = open_restored(old.dir.path(), &restored.machine.id);
assert_eq!(
store.get_memory(old.memory.id).unwrap().unwrap().content,
old.memory.content
);
fs::remove_dir_all(&aside).unwrap();
}
#[cfg(feature = "encryption")]
#[test]
fn handed_epochs_that_do_not_seal_the_backup_change_nothing() {
let old = old_device();
{
let machine = MachineIdentity::load_or_create(old.dir.path()).unwrap();
let device = super::super::load_device_keys(old.dir.path())
.unwrap()
.unwrap();
let store = SqliteStore::open(&old.dir.path().join(DB_FILE), machine.id).unwrap();
super::super::encrypt_store(&store, old.dir.path(), &device).unwrap();
}
let held = handed_from(&old_device());
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
fs::create_dir(&data_dir).unwrap();
let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();
let err = restore_backup_with(&BackupRestore {
backup: old.dir.path(),
data_dir: &data_dir,
keys: BackupKeys::Epochs(&held),
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap_err();
assert!(
format!("{err:#}").contains("none of the keys at hand"),
"{err:#}"
);
assert_eq!(
MachineIdentity::load_or_create(&data_dir).unwrap().id,
fresh.id
);
let names: Vec<String> = fs::read_dir(home.path())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names, vec![".second-brain".to_string()]);
}
#[test]
fn no_handed_epochs_change_nothing() {
let old = old_device();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
fs::create_dir(&data_dir).unwrap();
let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();
let err = restore_backup_with(&BackupRestore {
backup: old.dir.path(),
data_dir: &data_dir,
keys: BackupKeys::Epochs(&[]),
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap_err();
assert!(format!("{err:#}").contains("no key"), "{err:#}");
assert_eq!(
MachineIdentity::load_or_create(&data_dir).unwrap().id,
fresh.id
);
}
#[test]
fn a_wrong_phrase_changes_nothing() {
let old = old_device();
let home = tempfile::tempdir().unwrap();
let data_dir = home.path().join(".second-brain");
fs::create_dir(&data_dir).unwrap();
let fresh = MachineIdentity::load_or_create(&data_dir).unwrap();
let other = RecoveryPhrase::generate().unwrap();
let err = restore_backup(&RestoreRequest {
backup: old.dir.path(),
data_dir: &data_dir,
phrase: &other,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap_err();
assert!(format!("{err:#}").contains("opens no key"), "{err:#}");
assert_eq!(
MachineIdentity::load_or_create(&data_dir).unwrap().id,
fresh.id
);
let names: Vec<String> = fs::read_dir(home.path())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names, vec![".second-brain".to_string()]);
}
#[test]
fn a_directory_without_a_store_is_not_a_backup() {
let empty = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
let phrase = RecoveryPhrase::generate().unwrap();
let err = restore_backup(&RestoreRequest {
backup: empty.path(),
data_dir: &home.path().join(".second-brain"),
phrase: &phrase,
storage: &KeyStorage::File,
now_micros: NOW,
})
.unwrap_err();
assert!(format!("{err:#}").contains("holds no brain.db"), "{err:#}");
}
fn failing(first: usize, last: usize) -> impl Fn(&Path, &Path) -> io::Result<()> {
let calls = std::cell::Cell::new(0);
move |from, to| {
calls.set(calls.get() + 1);
if (first..=last).contains(&calls.get()) {
return Err(io::Error::other("injected rename failure"));
}
fs::rename(from, to)
}
}
fn dirs_with_marker(home: &Path) -> (PathBuf, PathBuf) {
let data_dir = home.join(".second-brain");
let staging = home.join(".second-brain.restoring-test");
fs::create_dir(&data_dir).unwrap();
fs::write(data_dir.join("marker"), "old").unwrap();
fs::create_dir(&staging).unwrap();
fs::write(staging.join("marker"), "restored").unwrap();
(data_dir, staging)
}
#[test]
fn a_failed_swap_puts_the_old_data_dir_back() {
let home = tempfile::tempdir().unwrap();
let (data_dir, staging) = dirs_with_marker(home.path());
let Err(Swap::Untouched(err)) =
swap_in(&data_dir, &staging, ".second-brain", NOW, failing(2, 2))
else {
panic!("the swap must fail with the data dir back in place");
};
assert!(format!("{err:#}").contains("injected"), "{err:#}");
assert_eq!(fs::read_to_string(data_dir.join("marker")).unwrap(), "old");
assert!(staging.join("marker").exists());
}
#[test]
fn a_swap_that_cannot_move_back_names_both_copies_and_keeps_them() {
let home = tempfile::tempdir().unwrap();
let (data_dir, staging) = dirs_with_marker(home.path());
let Err(Swap::Stranded(err)) =
swap_in(&data_dir, &staging, ".second-brain", NOW, failing(2, 3))
else {
panic!("the swap must report both copies");
};
let shown = format!("{err:#}");
assert!(shown.contains(&staging.display().to_string()), "{shown}");
assert!(shown.contains("before-restore"), "{shown}");
assert!(staging.join("marker").exists());
}
}