diff --git a/CHANGELOG.md b/CHANGELOG.md index 38747b5b..2617b15a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Dev: Integration tests now use `@holochain-open-dev/tryorama` 0.20.0 (the holochain-0.7-compatible tryorama line) and run test files sequentially — each file boots multiple conductors and concurrent files starve each other into flaky timeouts. - Fix: In some group chats, every message from a particular member was invisible to everyone, that member included — their messages seemed to vanish right after sending. The messages were delivered all along and now show. Members whose name the chat does not know yet appear as "Unknown member", and the app now repairs your own entry in any chat where it was missing. - Fix: Scrolling up in a conversation now loads its whole history. It used to stop at the first stretch of three or more days without messages, even though older messages were stored on the device. +- Fix: Moving from 1.x keeps your identity even when the first start of 2.0 goes wrong. The app used to switch to a brand new identity after a single failed attempt, which locked you out of your private conversations for good; it now tries again on the next start, and only starts a new identity when the old key is confirmed to be gone. +- Fix: When the app cannot finish starting, it now says so instead of staying on the "Starting up..." screen forever. +- Fix: An app closed during its very first start, or while it applied an update, could stay unusable on every later start. It now recovers by itself. ## [1.0.3] - 2026-09-12 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 496059d4..c5633d0d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -12173,7 +12173,10 @@ dependencies = [ "hex", "holochain_client", "holochain_conductor_api 0.7.0", + "holochain_keystore 0.7.0", "holochain_types 0.7.0", + "lair_keystore 0.6.3", + "lair_keystore_api 0.7.1", "log", "mr_bundle 0.7.0", "serde_json", @@ -12193,6 +12196,7 @@ dependencies = [ "tauri-plugin-sharesheet", "tauri-plugin-shell", "tempdir", + "tokio", "url2", "uuid 1.18.1", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a56110bd..7d4f2da4 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -26,6 +26,7 @@ tauri-plugin-holochain-service-client = { git = "https://github.com/HelloVolla/v holochain_types = "0.7.0" holochain_conductor_api = "0.7.0" +lair_keystore_api = { version = "0.7.1", optional = true } holochain_client = "0.9.0" mr_bundle = "0.7.0" sha2 = "0.10" @@ -50,8 +51,15 @@ serde_json = "1" uuid = { version = "1.12.0", features = ["v4", "fast-rng"] } base64ct = "=1.7.3" +[dev-dependencies] +# A real lair, to pin the keystore behaviour the identity carry-over relies on. +holochain_keystore = "0.7.0" +tokio = { version = "1", features = ["macros", "rt-multi-thread"] } +# The lair 1.x ran, to write a keystore exactly as 1.x left it. +lair_keystore_06 = { package = "lair_keystore", version = "=0.6.3" } + [features] default = [] -holochain_bundled = ["dep:tauri-plugin-holochain"] +holochain_bundled = ["dep:tauri-plugin-holochain", "dep:lair_keystore_api"] holochain_service = ["dep:tauri-plugin-holochain-service-client"] diff --git a/src-tauri/src/builder/holochain_bundled.rs b/src-tauri/src/builder/holochain_bundled.rs index 9b5fcc57..aab35dcf 100644 --- a/src-tauri/src/builder/holochain_bundled.rs +++ b/src-tauri/src/builder/holochain_bundled.rs @@ -2,11 +2,12 @@ use crate::config::{APP_ID, HAPP_BUNDLE_BYTES}; #[cfg(target_os = "android")] use crate::android_barcode_scanner; use crate::happ_update; -use holochain_types::prelude::{AppBundleSource, InstallAppPayload}; +use holochain_types::prelude::{AppBundleSource, AppStatus, InstallAppPayload}; use std::collections::HashMap; use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use tauri::{AppHandle, Builder, Listener, Manager, Runtime}; +use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; use tauri_plugin_holochain::{ vec_to_locked, HolochainExt, HolochainPluginConfig, NetworkConfig, WindowOptions, EVENT_READY, EVENT_SETUP_FAILED, @@ -19,8 +20,15 @@ pub const IROH_RELAY_URL: &'static str = "https://iroh-relay.volla.tech/"; pub fn setup_builder(builder: Builder) -> Builder { // Before the plugin starts lair, so the conductor opens the carried 1.x - // keystore instead of creating an empty one. - crate::migration_identity::carry_keystore_forward(&holochain_dir()); + // keystore instead of creating an empty one. If carrying fails, the + // conductor must not start at all: it would create that empty keystore, + // and the carry would never be tried again. + if let Err(e) = crate::migration_identity::carry_keystore_forward(&holochain_dir()) { + return builder.setup(move |app| { + report_setup_failure(app.handle().clone(), &e); + Ok(()) + }); + } builder .plugin(tauri_plugin_holochain::init( @@ -33,8 +41,8 @@ pub fn setup_builder(builder: Builder) -> Builder { let handle = app.handle().clone(); let handle_fail = app.handle().clone(); app.handle().listen(EVENT_SETUP_FAILED, move |event| { - log::error!("holochain setup failed: {}", event.payload()); - handle_fail.exit(1); + let error = anyhow::anyhow!("holochain setup failed: {}", event.payload()); + report_setup_failure(handle_fail.clone(), &error); }); let handle_event = handle.clone(); @@ -84,7 +92,10 @@ fn open_main_window_once(handle: AppHandle) { } tauri::async_runtime::spawn(async move { - setup(handle.clone()).await.expect("Failed to setup"); + if let Err(e) = setup(handle.clone()).await { + report_setup_failure(handle, &e); + return; + } let mut window_options = WindowOptions::default(); #[cfg(desktop)] @@ -153,35 +164,65 @@ async fn setup(handle: AppHandle) -> anyhow::Result<()> { // Install under the 1.x agent when its keystore was carried over, so // membrane proofs naming that agent still admit it to its private - // conversations. - match crate::migration_identity::carried_agent_key(&holochain_dir()) { - Some(agent_key) => { - log::info!("installing under the carried 1.x agent {agent_key}"); - if let Err(e) = runtime - .setup_app(install_payload(Some(agent_key)), true) - .await - { - // Falling back costs the user their old identity, but - // failing here would leave the app unable to start at all, - // on every launch. - log::error!( - "installing under the carried agent failed, starting with a new agent: {e:?}" - ); - runtime.setup_app(install_payload(None), true).await?; - } - } - None => { - runtime.setup_app(install_payload(None), true).await?; - } - } + // conversations. A failure here is never answered with a new agent: + // that would lose the old identity for good, so the next launch tries + // again instead. + let agent_key = + crate::migration_identity::agent_to_install_under(&runtime, &holochain_dir()).await?; + runtime.setup_app(install_payload(agent_key), true).await?; happ_update::record_installed_bundle_hash(&holochain_dir(), APP_ID, HAPP_BUNDLE_BYTES)?; } else { happ_update::update_app_if_necessary(&runtime, &holochain_dir(), APP_ID, HAPP_BUNDLE_BYTES) .await?; } + + ensure_app_enabled(&runtime).await +} + +/// Enable the app if an earlier launch left it disabled. +/// +/// `setup_app` installs and then enables in two steps, and only enables an app +/// it has just installed; a coordinator update disables and re-enables it. A +/// launch cut short between either pair leaves the app disabled, and nothing +/// else would ever enable it again. This app has no way for the user to +/// disable it, so a disabled app is always one of those. +async fn ensure_app_enabled(runtime: &tauri_plugin_holochain::Runtime) -> anyhow::Result<()> { + let app = runtime + .list_apps() + .await? + .into_iter() + .find(|app| app.installed_app_id == APP_ID) + .ok_or_else(|| anyhow::anyhow!("{APP_ID} is not installed"))?; + + if let AppStatus::Disabled(reason) = app.status { + log::warn!("{APP_ID} was left disabled ({reason:?}); enabling it"); + // Not fatal: refusing to start would gain nothing, while an open app + // still shows the user what they have, and the next launch tries again. + if let Err(e) = runtime.enable_app(APP_ID.into()).await { + log::error!("could not enable {APP_ID}: {e:?}"); + } + } Ok(()) } +/// Tell the user startup failed, then quit. Without this a failed setup +/// leaves the splashscreen up forever with no explanation. +fn report_setup_failure(handle: AppHandle, error: &anyhow::Error) { + log::error!("setup failed: {error:?}"); + + let message = format!( + "Volla Messages could not finish starting. Your account and messages are kept \ + as they are.\n\nPlease open the app again. If this keeps happening, please \ + contact support.\n\nDetails: {error:#}" + ); + handle + .dialog() + .message(message) + .title("Volla Messages") + .kind(MessageDialogKind::Error) + .show(move |_| handle.exit(1)); +} + fn network_config() -> NetworkConfig { let mut config = NetworkConfig::default(); config.bootstrap_url = url2::url2!("{}", BOOTSTRAP_URL); diff --git a/src-tauri/src/migration_identity.rs b/src-tauri/src/migration_identity.rs index 62d984f7..921dc07e 100644 --- a/src-tauri/src/migration_identity.rs +++ b/src-tauri/src/migration_identity.rs @@ -19,7 +19,14 @@ use std::fs; use std::path::{Path, PathBuf}; use std::sync::OnceLock; -use holochain_types::prelude::AgentPubKey; +use anyhow::Context; + +use holochain_types::prelude::{ + AgentPubKey, CellId, DnaHash, ExternIO, Timestamp, ZomeCallParams, +}; +use lair_keystore_api::config::LairServerConfigInner; +use tauri_plugin_holochain::Runtime; +use uuid::Uuid; /// Data directory of the last `1.x` release, under `/holochain/`. const LEGACY_BUCKET: &str = "1"; @@ -35,6 +42,9 @@ const CARRIED_MARKER: &str = "keystore-carried-from-1"; /// here and reported by `report_carry_outcome` once logging works. static CARRY_OUTCOME: OnceLock = OnceLock::new(); +/// Holds the secrets that decrypt `store_file`; a store without it is unreadable. +const LAIR_CONFIG: &str = "lair-keystore-config.yaml"; + /// A running lair leaves these behind. Copying them would point the new /// keystore at the old process. const SKIPPED_FILES: [&str; 2] = ["socket", "pid_file"]; @@ -50,13 +60,15 @@ const SKIPPED_FILES: [&str; 2] = ["socket", "pid_file"]; /// /// Skipped in dev builds: they run a throwaway conductor, and giving one the /// user's real keystore would let a dev run act as the real agent. -pub fn carry_keystore_forward(holochain_dir: &Path) { - let outcome = match try_carry_keystore_forward(holochain_dir) { - Ok(outcome) => outcome, - // Not fatal: the app still starts, just under a new agent. - Err(e) => format!("could not carry the 1.x keystore forward: {e:?}"), - }; +/// +/// An error means the conductor must not start: it would create a fresh +/// keystore in `holochain_dir`, and once that exists the carry is never tried +/// again, so the 1.x identity would be lost for good. +pub fn carry_keystore_forward(holochain_dir: &Path) -> anyhow::Result<()> { + let outcome = try_carry_keystore_forward(holochain_dir) + .context("could not carry the 1.x keystore forward")?; let _ = CARRY_OUTCOME.set(outcome); + Ok(()) } /// Log what `carry_keystore_forward` did. Call once the log plugin is up. @@ -84,6 +96,11 @@ fn try_carry_keystore_forward(holochain_dir: &Path) -> anyhow::Result { CarryOutcome::NoLegacyKeystore => { format!("no 1.x keystore at {}; starting fresh", source.display()) } + CarryOutcome::UnreadableLegacyKeystore => format!( + "the 1.x keystore at {} has no {LAIR_CONFIG} lair can read, so its keys \ + cannot be read; starting fresh", + source.display() + ), CarryOutcome::Carried => format!( "carried the 1.x keystore from {} into {}", source.display(), @@ -98,6 +115,9 @@ enum CarryOutcome { AlreadyPresent, /// No 1.x keystore to carry; the conductor starts fresh. NoLegacyKeystore, + /// A 1.x store without a readable lair config to decrypt it; the + /// conductor starts fresh. + UnreadableLegacyKeystore, Carried, } @@ -110,6 +130,17 @@ fn carry_keystore(source: &Path, destination: &Path) -> anyhow::Result LairServerConfigInner::from_bytes(&bytes).ok(), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, + Err(e) => return Err(e.into()), + }; + let Some(config) = config else { + // Without a config lair can read, no one can ever decrypt this store + // (1.x could not either), so it holds no identity to keep. Carried + // along, it would stop the new lair from starting at all. + return Ok(CarryOutcome::UnreadableLegacyKeystore); + }; let staging = staging_dir(destination)?; if staging.exists() { @@ -117,48 +148,161 @@ fn carry_keystore(source: &Path, destination: &Path) -> anyhow::Result/keystore/`; the 0.7 plugin runs it from // `/` directly. Copying into a `keystore/` subdirectory is silently // ignored and a fresh keystore is generated instead. + // + // A 1.x app still running at the same time (two installs side by side) + // cannot be detected: its lair runs inside the app with no socket. The + // copy is still safe: the agent key was written long before, and SQLite + // falls back to the last consistent state when the WAL does not match. copy_dir(source, &staging)?; - rewrite_lair_config(source, &staging, destination)?; + rewrite_lair_config(config, source, &staging, destination)?; fs::write(staging.join(CARRIED_MARKER), b"")?; fs::rename(&staging, destination)?; Ok(CarryOutcome::Carried) } -/// The agent key to install the app under, or `None` for a new agent. +/// The agent to install the app under, or `None` for a new agent. /// -/// Only returns a key when the keystore was carried from 1.x, since installing -/// under a key whose private half lair does not hold cannot succeed. -pub fn carried_agent_key(holochain_dir: &Path) -> Option { +/// A new agent costs the user their 1.x identity for good, so it is only +/// chosen when there is no 1.x identity to keep, or lair is confirmed not to +/// hold its key. Any doubt is an error instead: the app does not start, and +/// the next launch tries again. +pub async fn agent_to_install_under( + runtime: &Runtime, + holochain_dir: &Path, +) -> anyhow::Result> { + let Some(agent_key) = carried_agent_key(holochain_dir)? else { + return Ok(None); + }; + + if keystore_holds_key(runtime, &agent_key).await? { + log::info!("installing under the carried 1.x agent {agent_key}"); + Ok(Some(agent_key)) + } else { + log::error!( + "the carried keystore does not hold the key of the 1.x agent {agent_key}; \ + starting with a new agent" + ); + Ok(None) + } +} + +/// The 1.x agent named by the export, when the keystore was carried from 1.x. +/// +/// `None` when there is no 1.x identity to keep. An export that cannot be read +/// right now is an error, since the next launch may well read it. +fn carried_agent_key(holochain_dir: &Path) -> anyhow::Result> { if !holochain_dir.join(CARRIED_MARKER).exists() { - return None; + return Ok(None); } let contents = match crate::migration_export::read_export(None) { Ok(Some(contents)) => contents, + // Updated straight from a release older than 1.0.3, which wrote no + // export: nothing names the agent to keep. Ok(None) => { log::warn!("keystore was carried from 1.x but there is no export naming its agent"); - return None; - } - Err(e) => { - log::error!("could not read the migration export: {e}"); - return None; + return Ok(None); } + Err(e) => anyhow::bail!("could not read the migration export: {e}"), }; + // Only 1.x writes the export, so a broken one stays broken: waiting for + // the next launch would keep the app from ever starting. match agent_key_from_export(&contents) { - Ok(key) => Some(key), + Ok(key) => Ok(Some(key)), Err(e) => { log::error!("migration export names no usable agent key: {e}"); - None + Ok(None) } } } +/// Whether lair holds the private key of `agent_key`. +/// +/// The runtime offers no way to list lair's keys, so this asks lair to sign +/// with it. Errors unless lair answers for sure. +async fn keystore_holds_key(runtime: &Runtime, agent_key: &AgentPubKey) -> anyhow::Result { + let sign = |key: AgentPubKey| async move { + runtime + .sign_zome_call(probe_call(key)) + .await + .map(|_| ()) + .map_err(|e| format!("{e:?}")) + }; + + classify_key_probe( + sign(runtime.device_agent_key()).await, + sign(random_agent_key()).await, + sign(agent_key.clone()).await, + ) +} + +/// Decide from three signing attempts whether lair holds a key: +/// +/// - `device`: the runtime's own device key, which lair always holds; a +/// failure means lair is not working (locked, say), so it proves nothing. +/// - `unheld`: a random key lair cannot hold; it must fail with lair's "no +/// such key" answer, the one a missing carried key gets too. +/// - `carried`: the key in question. +fn classify_key_probe( + device: Result<(), String>, + unheld: Result<(), String>, + carried: Result<(), String>, +) -> anyhow::Result { + if let Err(e) = device { + anyhow::bail!("lair cannot sign with the device key: {e}"); + } + let Err(not_held) = unheld else { + anyhow::bail!("lair signed with a random key, so its answers cannot be trusted"); + }; + + // The device key is cached in memory, so it signs even when lair cannot + // read its database. Then every other key fails alike, with that database + // error: only lair's own "no such key" answer proves anything. + anyhow::ensure!( + not_held.contains(LAIR_KEY_NOT_FOUND), + "lair could not look up a random key: {not_held}" + ); + + match carried { + Ok(()) => Ok(true), + Err(e) if e == not_held => Ok(false), + Err(e) => anyhow::bail!("lair could not sign with the carried key: {e}"), + } +} + +/// What lair's SQLite store answers for a public key it holds no entry for. +/// Pinned by `real_lair_answers_every_unheld_key_alike`. +const LAIR_KEY_NOT_FOUND: &str = "Query returned no rows"; + +/// A zome call that is only ever signed, never made: signing looks at nothing +/// but its provenance. +fn probe_call(provenance: AgentPubKey) -> ZomeCallParams { + ZomeCallParams { + cell_id: CellId::new(DnaHash::from_raw_32(vec![0; 32]), provenance.clone()), + provenance, + zome_name: "probe".into(), + fn_name: "probe".into(), + cap_secret: None, + payload: ExternIO::encode(()).expect("unit always serialises"), + nonce: [0; 32].into(), + expires_at: Timestamp::now(), + } +} + +fn random_agent_key() -> AgentPubKey { + let mut bytes = Uuid::new_v4().as_bytes().to_vec(); + bytes.extend_from_slice(Uuid::new_v4().as_bytes()); + AgentPubKey::from_raw_32(bytes) +} + /// The agent key named by a migration export's JSON. fn agent_key_from_export(contents: &str) -> Result { let export: serde_json::Value = @@ -170,6 +314,18 @@ fn agent_key_from_export(contents: &str) -> Result { AgentPubKey::try_from(encoded.to_string()).map_err(|e| format!("invalid key {encoded}: {e:?}")) } +/// The unix socket lair listens on, or `None` for a named pipe (Windows), +/// which names no directory. Lair's own `get_connection_path` panics on a URL +/// it cannot read; a broken config must not crash startup. +fn unix_socket(config: &LairServerConfigInner) -> anyhow::Result> { + if config.connection_url.scheme() != "unix" { + return Ok(None); + } + config.connection_url.to_file_path().map(Some).map_err(|()| { + anyhow::anyhow!("connectionUrl {} names no absolute path", config.connection_url) + }) +} + /// Sibling of `holochain_dir`, so the final rename stays on one filesystem. fn staging_dir(holochain_dir: &Path) -> anyhow::Result { let parent = holochain_dir @@ -196,7 +352,9 @@ fn copy_dir(source: &Path, destination: &Path) -> anyhow::Result<()> { let from = entry.path(); let to = destination.join(&name); - if entry.file_type()?.is_dir() { + // Follows symlinks, so a linked directory is copied rather than + // failing the carry on every launch. + if fs::metadata(&from)?.is_dir() { copy_dir(&from, &to)?; } else { fs::copy(&from, &to)?; @@ -206,21 +364,65 @@ fn copy_dir(source: &Path, destination: &Path) -> anyhow::Result<()> { Ok(()) } +/// Lair runs with an empty passphrase, so whoever can read the store and its +/// config can read the keys: only the user may enter the directory. +fn restrict_to_owner(dir: &Path) -> anyhow::Result<()> { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(dir, fs::Permissions::from_mode(0o700))?; + } + #[cfg(not(unix))] + let _ = dir; + Ok(()) +} + /// `lair-keystore-config.yaml` stores absolute paths, so a copied keystore /// still points at the old directory. Rewrite them to where the keystore will /// live after the rename — not the staging directory it is assembled in. -fn rewrite_lair_config(source: &Path, staging: &Path, final_dir: &Path) -> anyhow::Result<()> { - let config = staging.join("lair-keystore-config.yaml"); - if !config.exists() { - return Ok(()); +/// +/// Goes through lair's own config type rather than replacing text: the socket +/// path sits percent-encoded inside `connectionUrl` (macOS's "Application +/// Support" becomes `Application%20Support`), where a plain replace misses it +/// and the new lair would put its socket into the 1.x directory. +fn rewrite_lair_config( + mut config: LairServerConfigInner, + source: &Path, + staging: &Path, + final_dir: &Path, +) -> anyhow::Result<()> { + + let moved = |file: &Path| -> anyhow::Result { + let name = file + .file_name() + .ok_or_else(|| anyhow::anyhow!("{} has no file name", file.display()))?; + Ok(final_dir.join(name)) + }; + config.pid_file = moved(&config.pid_file)?; + config.store_file = moved(&config.store_file)?; + + if let Some(socket) = unix_socket(&config)? { + let socket = moved(&socket)?; + let socket_str = socket + .to_str() + .ok_or_else(|| anyhow::anyhow!("{} is not valid UTF-8", socket.display()))?; + // Keeps the `?k=` server key, which clients check. + config.connection_url.set_path(socket_str); + anyhow::ensure!( + unix_socket(&config)?.as_ref() == Some(&socket), + "the rewritten connectionUrl {} does not lead to {}", + config.connection_url, + socket.display() + ); } - let contents = fs::read_to_string(&config)?; - let rewritten = contents.replace( - source.to_string_lossy().as_ref(), - final_dir.to_string_lossy().as_ref(), + let rewritten = config.to_string(); + anyhow::ensure!( + !rewritten.contains(source.to_string_lossy().as_ref()), + "the rewritten lair config still names {}", + source.display() ); - fs::write(&config, rewritten)?; + fs::write(staging.join(LAIR_CONFIG), rewritten)?; Ok(()) } @@ -228,6 +430,7 @@ fn rewrite_lair_config(source: &Path, staging: &Path, final_dir: &Path) -> anyho #[cfg(test)] mod tests { use super::*; + use lair_keystore_api::config::get_connection_path; use std::collections::BTreeMap; use tempdir::TempDir; @@ -236,21 +439,35 @@ mod tests { fn legacy_keystore(root: &Path) -> PathBuf { let keystore = root.join("holochain").join("1").join("keystore"); fs::create_dir_all(&keystore).unwrap(); - let k = keystore.display(); fs::write(keystore.join("store_file"), b"encrypted store").unwrap(); fs::write(keystore.join("store_file-wal"), b"write-ahead log").unwrap(); - fs::write( - keystore.join("lair-keystore-config.yaml"), - format!( - "connectionUrl: unix://{k}/socket?k=abc\npidFile: {k}/pid_file\nstoreFile: {k}/store_file\n" - ), - ) - .unwrap(); + fs::write(keystore.join(LAIR_CONFIG), lair_config(&keystore).to_string()).unwrap(); fs::write(keystore.join("socket"), b"").unwrap(); fs::write(keystore.join("pid_file"), b"4242").unwrap(); keystore } + /// The config lair writes for a keystore in `dir`. Lair 0.6 and 0.7 share + /// the format. + fn lair_config(dir: &Path) -> LairServerConfigInner { + use lair_keystore_api::dependencies::hc_seed_bundle::PwHashLimits; + + let runtime = tokio::runtime::Runtime::new().unwrap(); + // Minimum limits only make the passphrase hashing fast. + PwHashLimits::Minimum + .with_exec(|| { + runtime.block_on(LairServerConfigInner::new( + dir, + tauri_plugin_holochain::vec_to_locked(vec![]), + )) + }) + .unwrap() + } + + fn read_config(dir: &Path) -> LairServerConfigInner { + LairServerConfigInner::from_bytes(&fs::read(dir.join(LAIR_CONFIG)).unwrap()).unwrap() + } + /// Every file under `dir` and its contents, to compare before and after. fn snapshot(dir: &Path) -> BTreeMap> { let mut files = BTreeMap::new(); @@ -284,18 +501,127 @@ mod tests { assert!(!destination.join("socket").exists()); assert!(!destination.join("pid_file").exists()); - // Config paths point at the final directory, never the source or staging. - let config = fs::read_to_string(destination.join("lair-keystore-config.yaml")).unwrap(); - let dest = destination.display().to_string(); - assert!(config.contains(&format!("unix://{dest}/socket?k=abc"))); - assert!(config.contains(&format!("pidFile: {dest}/pid_file"))); - assert!(config.contains(&format!("storeFile: {dest}/store_file"))); - assert!(!config.contains(&source.display().to_string())); - assert!(!config.contains("keystore-staging")); + assert_config_moved(&source, &destination); // No staging directory left behind, and the 1.x keystore is untouched. assert!(!staging_dir(&destination).unwrap().exists()); assert_eq!(snapshot(&source), before); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = fs::metadata(&destination).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o700); + } + } + + #[cfg(unix)] + #[test] + fn follows_a_symlinked_directory_in_the_keystore() { + let root = TempDir::new("carry").unwrap(); + let source = legacy_keystore(root.path()); + let elsewhere = root.path().join("elsewhere"); + fs::create_dir_all(&elsewhere).unwrap(); + fs::write(elsewhere.join("note"), b"kept").unwrap(); + std::os::unix::fs::symlink(&elsewhere, source.join("linked")).unwrap(); + let destination = root.path().join("holochain").join("2"); + + assert_eq!(carry_keystore(&source, &destination).unwrap(), CarryOutcome::Carried); + assert_eq!(fs::read(destination.join("linked").join("note")).unwrap(), b"kept"); + } + + /// Config paths point at the final directory, never the source or + /// staging, and everything else in the config is kept. + fn assert_config_moved(source: &Path, destination: &Path) { + let before = read_config(source); + let after = read_config(destination); + + assert_eq!(after.pid_file, destination.join("pid_file")); + assert_eq!(after.store_file, destination.join("store_file")); + assert_eq!(get_connection_path(&after.connection_url), destination.join("socket")); + assert_eq!(after.connection_url.query(), before.connection_url.query()); + assert_eq!(after.get_server_pub_key().unwrap(), before.get_server_pub_key().unwrap()); + assert_eq!(after.database_salt, before.database_salt); + assert_eq!(after.runtime_secrets_salt, before.runtime_secrets_salt); + + let text = fs::read_to_string(destination.join(LAIR_CONFIG)).unwrap(); + assert!(!text.contains(&source.display().to_string())); + assert!(!text.contains("keystore-staging")); + } + + /// macOS keeps app data under "Application Support". Inside + /// `connectionUrl` the space is percent-encoded, which a plain text + /// replace of the path never matched. + #[test] + fn moves_the_socket_out_of_a_directory_with_a_space() { + let root = TempDir::new("carry").unwrap(); + let app_root = root.path().join("Library").join("Application Support").join("app"); + let source = legacy_keystore(&app_root); + let destination = app_root.join("holochain").join("2"); + assert!(read_config(&source).connection_url.as_str().contains("Application%20Support")); + + assert_eq!(carry_keystore(&source, &destination).unwrap(), CarryOutcome::Carried); + + assert_config_moved(&source, &destination); + assert!(!read_config(&destination) + .connection_url + .as_str() + .contains("holochain/1")); + } + + /// Windows lair listens on a named pipe, which names no directory. + #[test] + fn keeps_a_named_pipe_connection_url_as_it_is() { + let root = TempDir::new("carry").unwrap(); + let source = legacy_keystore(root.path()); + let mut config = read_config(&source); + config.connection_url = format!( + "named-pipe:\\\\.\\pipe\\abc?{}", + config.connection_url.query().unwrap() + ) + .parse() + .unwrap(); + fs::write(source.join(LAIR_CONFIG), config.to_string()).unwrap(); + let destination = root.path().join("holochain").join("2"); + + assert_eq!(carry_keystore(&source, &destination).unwrap(), CarryOutcome::Carried); + + let after = read_config(&destination); + assert_eq!(after.connection_url, config.connection_url); + assert_eq!(after.store_file, destination.join("store_file")); + } + + /// The config holds the secrets that decrypt `store_file`. Without it the + /// store can never be read, and carried along it would keep the new lair + /// from starting. + #[test] + fn starts_fresh_when_the_legacy_keystore_has_no_config() { + let root = TempDir::new("carry").unwrap(); + let source = legacy_keystore(root.path()); + fs::remove_file(source.join(LAIR_CONFIG)).unwrap(); + let destination = root.path().join("holochain").join("2"); + + assert_eq!( + carry_keystore(&source, &destination).unwrap(), + CarryOutcome::UnreadableLegacyKeystore + ); + assert!(!destination.exists()); + } + + /// 1.x parsed it with the same type, so it could not open this store + /// either. + #[test] + fn starts_fresh_when_lair_cannot_parse_the_legacy_config() { + let root = TempDir::new("carry").unwrap(); + let source = legacy_keystore(root.path()); + fs::write(source.join(LAIR_CONFIG), b"not: [a lair config").unwrap(); + let destination = root.path().join("holochain").join("2"); + + assert_eq!( + carry_keystore(&source, &destination).unwrap(), + CarryOutcome::UnreadableLegacyKeystore + ); + assert!(!destination.exists()); } #[test] @@ -347,6 +673,33 @@ mod tests { assert!(!staging.exists()); } + /// A failed copy must leave no data directory behind: the conductor would + /// otherwise start on an empty keystore there, and no later launch would + /// try the carry again. + #[cfg(unix)] + #[test] + fn a_failed_copy_leaves_nothing_behind_and_the_next_launch_retries() { + use std::os::unix::fs::PermissionsExt; + + let root = TempDir::new("carry").unwrap(); + let source = legacy_keystore(root.path()); + let destination = root.path().join("holochain").join("2"); + let unreadable = source.join("store_file-wal"); + fs::set_permissions(&unreadable, fs::Permissions::from_mode(0o000)).unwrap(); + if fs::read(&unreadable).is_ok() { + // Running as root: permissions cannot make the copy fail. + return; + } + + assert!(carry_keystore(&source, &destination).is_err()); + assert!(!destination.exists()); + + fs::set_permissions(&unreadable, fs::Permissions::from_mode(0o600)).unwrap(); + assert_eq!(carry_keystore(&source, &destination).unwrap(), CarryOutcome::Carried); + assert_eq!(fs::read(destination.join("store_file-wal")).unwrap(), b"write-ahead log"); + assert!(!staging_dir(&destination).unwrap().exists()); + } + #[test] fn reads_the_agent_key_from_an_export() { // From 32 bytes, so the library computes the location bytes a real key has. @@ -371,6 +724,179 @@ mod tests { let root = TempDir::new("carry").unwrap(); // No marker: the conductor's keystore was not carried over, so it // cannot hold the exported agent's private key. - assert_eq!(carried_agent_key(root.path()), None); + assert_eq!(carried_agent_key(root.path()).unwrap(), None); + } + + fn random_key_bytes() -> [u8; 32] { + let mut bytes = [0; 32]; + bytes[..16].copy_from_slice(Uuid::new_v4().as_bytes()); + bytes[16..].copy_from_slice(Uuid::new_v4().as_bytes()); + bytes + } + + const NOT_HELD: &str = r#"Lair(OneErr { error: "Query returned no rows" })"#; + + fn not_held() -> Result<(), String> { + Err(NOT_HELD.to_string()) + } + + #[test] + fn the_carried_key_is_held_when_lair_signs_with_it() { + assert!(classify_key_probe(Ok(()), not_held(), Ok(())).unwrap()); + } + + #[test] + fn the_carried_key_is_missing_only_when_lair_says_so_as_for_an_unheld_key() { + assert!(!classify_key_probe(Ok(()), not_held(), not_held()).unwrap()); + } + + #[test] + fn any_other_signing_error_is_not_proof_the_key_is_missing() { + let other = Err("Lair(OneErr { error: \"database is locked\" })".to_string()); + assert!(classify_key_probe(Ok(()), not_held(), other).is_err()); + } + + #[test] + fn a_database_error_for_every_uncached_key_is_not_proof_the_key_is_missing() { + // The device key signs from lair's memory cache while the store + // itself cannot be read, so both other keys fail with the same error. + let broken = || Err("Lair(OneErr { error: \"database disk image is malformed\" })".into()); + assert!(classify_key_probe(Ok(()), broken(), broken()).is_err()); + } + + #[test] + fn a_locked_keystore_is_not_proof_the_key_is_missing() { + // Locked, lair gives every key the same error, so the carried key + // would look exactly like an unheld one. The device key exposes it. + let locked = || Err("Lair(OneErr { error: \"KeystoreLocked\" })".to_string()); + assert!(classify_key_probe(locked(), locked(), locked()).is_err()); + } + + #[test] + fn a_keystore_signing_with_any_key_is_not_trusted() { + assert!(classify_key_probe(Ok(()), Ok(()), not_held()).is_err()); + } + + /// Pins the lair behaviour `classify_key_probe` relies on, against the + /// real lair the app ships: a held key signs, and every key lair does not + /// hold gets the very same error. + #[tokio::test(flavor = "multi_thread")] + async fn real_lair_answers_every_unheld_key_alike() { + use holochain_keystore::lair_keystore::spawn_lair_keystore_in_proc; + use std::sync::Arc; + + let root = TempDir::new("lair").unwrap(); + let passphrase = tauri_plugin_holochain::vec_to_locked(vec![]); + let config = root.path().join("lair-keystore-config.yaml"); + let keystore = spawn_lair_keystore_in_proc(&config, passphrase) + .await + .unwrap(); + let lair = keystore.lair_client(); + let held = lair.new_seed("held".into(), None, false).await.unwrap(); + + let sign = |key: [u8; 32]| { + let lair = lair.clone(); + async move { + lair.sign_by_pub_key(key.into(), None, Arc::from(&b"probe"[..])) + .await + .map(|_| ()) + .map_err(|e| format!("{e:?}")) + } + }; + let random = random_key_bytes; + + // The held key stands in for the device key: lair holds both. + let held_key = *held.ed25519_pub_key.0; + let held_carried = sign(held_key).await; + let unheld_carried = sign(random()).await; + // Pins the answer `classify_key_probe` requires for an unheld key. + assert!(unheld_carried.as_ref().unwrap_err().contains(LAIR_KEY_NOT_FOUND)); + assert!(classify_key_probe(sign(held_key).await, sign(random()).await, held_carried) + .unwrap()); + assert!(!classify_key_probe(sign(held_key).await, sign(random()).await, unheld_carried) + .unwrap()); + } + + /// The whole carry against real lairs: a keystore written exactly as 1.x + /// wrote it (lair 0.6.3 in process, empty passphrase) still signs as the + /// 1.x agent once carried and opened by the lair 2.x ships. + #[tokio::test(flavor = "multi_thread")] + async fn lair_07_signs_as_the_agent_of_a_carried_06_keystore() { + use holochain_keystore::lair_keystore::spawn_lair_keystore_in_proc; + use lair_keystore_06::dependencies::lair_keystore_api as lair_06; + use lair_keystore_06::dependencies::sodoken as sodoken_06; + use std::sync::{Arc, Mutex}; + + let root = TempDir::new("carry").unwrap(); + let source = root.path().join("holochain").join("1").join("keystore"); + fs::create_dir_all(&source).unwrap(); + + // As the 1.x plugin's `spawn_lair_keystore_in_proc` did. + let agent_key = { + let passphrase = Arc::new(Mutex::new(sodoken_06::LockedArray::from(vec![]))); + let config = lair_06::prelude::PwHashLimits::Minimum + .with_exec(|| { + lair_06::config::LairServerConfigInner::new(&source, passphrase.clone()) + }) + .await + .unwrap(); + fs::write(source.join(LAIR_CONFIG), config.to_string()).unwrap(); + let store = lair_keystore_06::create_sql_pool_factory( + &config.store_file, + &config.database_salt, + ); + let keystore = lair_06::in_proc_keystore::InProcKeystore::new( + Arc::new(config), + store, + passphrase, + ) + .await + .unwrap(); + let lair = keystore.new_client().await.unwrap(); + let seed = lair.new_seed("agent".into(), None, false).await.unwrap(); + *seed.ed25519_pub_key.0 + }; + + let destination = root.path().join("holochain").join("2"); + assert_eq!(carry_keystore(&source, &destination).unwrap(), CarryOutcome::Carried); + + let keystore = spawn_lair_keystore_in_proc( + &destination.join(LAIR_CONFIG), + tauri_plugin_holochain::vec_to_locked(vec![]), + ) + .await + .unwrap(); + let lair = keystore.lair_client(); + let sign = |key: [u8; 32]| { + let lair = lair.clone(); + async move { + lair.sign_by_pub_key(key.into(), None, Arc::from(&b"probe"[..])) + .await + .map(|_| ()) + .map_err(|e| format!("{e:?}")) + } + }; + + let device = lair.new_seed("device".into(), None, false).await.unwrap(); + let device_key = *device.ed25519_pub_key.0; + assert!(classify_key_probe( + sign(device_key).await, + sign(random_key_bytes()).await, + sign(agent_key).await + ) + .unwrap()); + // The new lair runs from the new directory, not the 1.x one. + assert!(destination.join("socket").exists()); + assert!(!source.join("socket").exists()); + } + + #[test] + fn the_probe_call_is_signed_as_the_given_agent() { + let key = random_agent_key(); + let call = probe_call(key.clone()); + assert_eq!(call.provenance, key); + assert_eq!(call.cell_id.agent_pubkey(), &key); + assert!(call.serialize_and_hash().is_ok()); + assert_ne!(random_agent_key(), random_agent_key()); } }