Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

7.9 KB
Raw Blame
//! The local storage-model migrations: where the vault's schema stands, the rebuild of a stale one, and the operations
//! behind the upgrade gate.

use std::collections::HashMap;

use secrecy::ExposeSecret;

use super::db::{self, SchemaState};
use super::errors::{Failure, SyncError, SyncResult};
use super::push::{self, PushStatus, WriteKind};
use super::state::{self, Ctx};
use super::types::{FullSyncResult, MigrateManifestResult, MigrationKind, MigrationStatusResult};
use super::{http, keys, legacy, pull};

/// Where the local vault's schema stands against the current one.
pub(crate) async fn schema_state(ctx: &mut Ctx) -> SyncResult<SchemaState> {
    let schema = ctx.schema().await?;
    db::schema_state(&ctx.host, &schema.migration_id).await
}

/// Whether the local vault is still on a schema the codec cannot canonicalize.
pub(crate) async fn vault_predates_current_schema(ctx: &mut Ctx) -> SyncResult<bool> {
    Ok(schema_state(ctx).await? != SchemaState::Current)
}

/// Whether the vault still has to run the manifest migration: a stale schema, or a missing account key hierarchy.
pub(crate) async fn vault_requires_manifest_migration(ctx: &mut Ctx) -> SyncResult<bool> {
    Ok(schema_state(ctx).await? == SchemaState::Stale || !keys::has_cached_key_chain(&ctx.host).await?)
}

/// The sync outcome for a vault that still has a migration pending, `None` when it has none.
pub(crate) async fn pending_migration_result(ctx: &mut Ctx) -> SyncResult<Option<FullSyncResult>> {
    let checked: SyncResult<Option<FullSyncResult>> = async {
        Ok(match schema_state(ctx).await? {
            SchemaState::LegacyChain => Some(FullSyncResult { success: true, sqlite_blob_upgrade_required: true, ..Default::default() }),
            SchemaState::Stale => Some(FullSyncResult { success: true, manifest_migration_required: true, ..Default::default() }),
            SchemaState::Current if !keys::has_cached_key_chain(&ctx.host).await? => Some(FullSyncResult { success: true, manifest_migration_required: true, ..Default::default() }),
            SchemaState::Current => None,
        })
    }
    .await;
    match checked {
        Ok(result) => Ok(result),
        Err(error @ SyncError::VaultVersionIncompatible(_)) => Err(error),
        // A failed local database command can mean the stored vault does not decrypt; the sync diagnoses it.
        Err(error @ SyncError::Host { command: "dbQuery" | "dbExec" | "dbExport", .. }) => Err(error),
        Err(error) => {
            ctx.warn(format!("[Migration] Ignoring failed pending migration check: {}", error)).await;
            Ok(None)
        }
    }
}

/// Classify the pending migration so the upgrade gate knows whether it may run on its own.
pub(crate) async fn migration_status(ctx: &mut Ctx) -> MigrationStatusResult {
    let classified: SyncResult<MigrationKind> = async {
        if schema_state(ctx).await? == SchemaState::LegacyChain {
            return Ok(MigrationKind::None);
        }
        // LEGACY: an account without a key chain is still on sqlite-blob. TODO: remove with `legacy.rs`.
        if !keys::has_cached_key_chain(&ctx.host).await? && !legacy::accept_upgrade_done_elsewhere(ctx).await? {
            return Ok(MigrationKind::StorageFormatUpgrade);
        }
        // Read again: accepting an upgrade done elsewhere replaced the local vault with the server's.
        Ok(if schema_state(ctx).await? == SchemaState::Stale { MigrationKind::SchemaRebuild } else { MigrationKind::None })
    }
    .await;
    let kind = match classified {
        Ok(kind) => kind,
        Err(error) => {
            ctx.warn(format!("[Migration] Could not classify the pending migration, assuming it crosses the storage format: {}", error)).await;
            MigrationKind::StorageFormatUpgrade
        }
    };
    MigrationStatusResult { kind }
}

/// Bring the local vault onto the current storage model and push it: a schema rebuild, or the one-time account upgrade
/// for an account without a key chain yet.
pub(crate) async fn migrate_manifest(ctx: &mut Ctx) -> MigrateManifestResult {
    let migrated: SyncResult<bool> = async {
        // LEGACY: an account without a key chain is still on sqlite-blob. TODO: remove with `legacy.rs`.
        if !keys::has_cached_key_chain(&ctx.host).await? {
            ensure_server_ready(ctx).await?;
            return legacy::upgrade_account_to_manifest_v1(ctx).await;
        }
        migrate_schema(ctx).await
    }
    .await;
    match migrated {
        Ok(pushed) => MigrateManifestResult { success: true, pushed, ..Default::default() },
        Err(error) => {
            ctx.warn(format!("[Migration] Migration failed: {}", error)).await;
            MigrateManifestResult { failure: (&error).into(), ..Default::default() }
        }
    }
}

/// Refuse the account upgrade before it touches the local vault unless the server is reachable.
async fn ensure_server_ready(ctx: &Ctx) -> SyncResult<()> {
    if http::get_status(&ctx.host).await?.server_version == http::SERVER_UNREACHABLE_VERSION {
        return Err(SyncError::Network("the server is unreachable; the migration needs it".to_string()));
    }
    Ok(())
}

/// Rebuild a stale local schema onto the current one and push it; returns whether the push reached the server, with a
/// vault already on the current schema counting as pushed.
pub(crate) async fn migrate_schema(ctx: &mut Ctx) -> SyncResult<bool> {
    match schema_state(ctx).await? {
        SchemaState::Current => Ok(true),
        SchemaState::LegacyChain => Err(SyncError::LegacyUpgradePending),
        SchemaState::Stale => {
            rebuild_local_schema(ctx, None).await?;
            push_migrated_vault(ctx, WriteKind::Changes { buckets_only: false }).await
        }
    }
}

/// Rebuild the local vault onto the current schema and store it as a pending change; `stamp_unstamped_into` stamps
/// rows that carry no manifest yet.
pub(crate) async fn rebuild_local_schema(ctx: &mut Ctx, stamp_unstamped_into: Option<String>) -> SyncResult<()> {
    ctx.log("[Migration] Migrating local vault onto the current schema (local round-trip, no server involved)...").await;
    let set = push::canonicalize_vault(ctx, stamp_unstamped_into).await?;
    let mut blob_map = HashMap::new();
    for entry in &set.canonicalized.manifests {
        for (hash, blob) in &entry.blobs {
            blob_map.insert(hash.clone(), blob.bytes.clone());
        }
    }
    let manifests: Vec<_> = set.canonicalized.manifests.iter().map(|m| m.manifest.clone()).collect();
    let bytes = pull::materialize_to_sqlite(ctx, &manifests, &set.canonicalized.data_buckets, &blob_map, &HashMap::new()).await?;
    ctx.log(format!("[Migration] Migration complete: {} blobs re-embedded, {} bytes.", blob_map.len(), bytes.len())).await;

    let stored = ctx.store_vault(&state::encrypt_vault_blob(&bytes, (ctx.encryption_key()?).expose_secret())?, true, None, None).await?;
    ctx.mutation_sequence = stored.mutation_sequence;
    ctx.is_dirty = true;
    ctx.vault_changed = true;
    Ok(())
}

/// Push the migrated vault and clear the dirty flag; returns whether the push reached the server, a refused or failed push
/// leaving the vault dirty for the next sync unless the failure ends the session.
pub(crate) async fn push_migrated_vault(ctx: &mut Ctx, kind: WriteKind) -> SyncResult<bool> {
    match push::upload_vault(ctx, None, kind).await {
        Ok(upload) if upload.status == PushStatus::Ok => {
            state::mark_clean(&ctx.host, upload.mutation_seq_at_start).await?;
            Ok(true)
        }
        Ok(upload) => {
            ctx.warn(format!("[Migration] Migration push did not succeed ({:?}), vault stays dirty for the next sync.", upload.status)).await;
            Ok(false)
        }
        Err(error) if matches!(error.failure(), Failure::Logout(_)) => Err(error),
        Err(error) => {
            ctx.warn(format!("[Migration] Migration push failed, vault stays dirty for the next sync: {}", error)).await;
            Ok(false)
        }
    }
}