Skip to content

Commit

Permalink
feat: migration pre-upgrade script (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: blu3beri <blu3beri@proton.me>
  • Loading branch information
berendsliedrecht and andrewwhitehead authored Mar 8, 2023
1 parent 8954ebc commit ee4e5aa
Show file tree
Hide file tree
Showing 12 changed files with 679 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode
target
*.bak.db
*.db-shm
*.db-wal
Cargo.lock
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ no-default-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["all_backends", "ffi", "logger"]
default = ["all_backends", "ffi", "logger", "migration"]
all_backends = ["any", "postgres", "sqlite"]
any = []
ffi = ["any", "ffi-support", "logger"]
Expand All @@ -34,6 +34,7 @@ logger = ["env_logger", "log"]
postgres = ["sqlx", "sqlx/postgres", "sqlx/tls"]
sqlite = ["num_cpus", "sqlx", "sqlx/sqlite"]
pg_test = ["postgres"]
migration = ["rmp", "rmp-serde"]

[dev-dependencies]
hex-literal = "0.3"
Expand All @@ -57,6 +58,8 @@ num_cpus = { version = "1.0", optional = true }
once_cell = "1.5"
percent-encoding = "2.0"
rand = { version = "0.8", default-features = false }
rmp = { version = "0.8.11", optional = true }
rmp-serde = { version = "1.1.1", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
serde_cbor = "0.11"
Expand All @@ -75,7 +78,7 @@ features = ["all_keys", "any_key", "argon2", "crypto_box", "std"]
[dependencies.sqlx]
version = "0.6.2"
default-features = false
features = ["chrono", "runtime-tokio-rustls"]
features = ["chrono", "runtime-tokio-rustls", "macros"]
optional = true

[profile.release]
Expand Down
53 changes: 53 additions & 0 deletions src/ffi/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use ffi_support::FfiStr;

use crate::{future::spawn_ok, migration::IndySdkToAriesAskarMigration};

use super::{
error::{set_last_error, ErrorCode},
CallbackId, EnsureCallback,
};

/// Migrate an sqlite wallet from an indy-sdk structure to an aries-askar structure.
/// It is important to note that this does not do any post-processing. If the record values, tags,
/// names, etc. have changed, it must be processed manually afterwards. This script does the following:
///
/// 1. Create and rename the required tables
/// 2. Fetch the indy key from the wallet
/// 3. Create a new configuration
/// 4. Initialize a profile
/// 5. Update the items from the indy-sdk
/// 6. Clean up (drop tables and add a version of "1")
#[no_mangle]
pub extern "C" fn askar_migrate_indy_sdk(
spec_uri: FfiStr<'_>,
wallet_name: FfiStr<'_>,
wallet_key: FfiStr<'_>,
kdf_level: FfiStr<'_>,
cb: Option<extern "C" fn(cb_id: CallbackId, err: ErrorCode)>,
cb_id: CallbackId,
) -> ErrorCode {
catch_err!(
trace!("Migrate sqlite wallet from indy-sdk structure to aries-askar");
let cb = cb.ok_or_else(|| err_msg!("No callback provided"))?;
let spec_uri = spec_uri.into_opt_string().ok_or_else(|| err_msg!("No provision spec URI provided"))?;
let wallet_name = wallet_name.into_opt_string().ok_or_else(|| err_msg!("No wallet name provided"))?;
let wallet_key = wallet_key.into_opt_string().ok_or_else(|| err_msg!("No wallet key provided"))?;
let kdf_level = kdf_level.into_opt_string().ok_or_else(|| err_msg!("No KDF level provided"))?;

let cb = EnsureCallback::new(move |result|
match result {
Ok(_) => cb(cb_id, ErrorCode::Success),
Err(err) => cb(cb_id, set_last_error(Some(err))),
});

spawn_ok(async move {
let result = async {
let migrator = IndySdkToAriesAskarMigration::connect(&spec_uri, &wallet_name, &wallet_key, &kdf_level).await?;
migrator.migrate().await?;
Ok(())
}.await;
cb.resolve(result);
});
Ok(ErrorCode::Success)
)
}
3 changes: 3 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mod result_list;
mod secret;
mod store;

#[cfg(all(feature = "migration", feature = "sqlite"))]
mod migration;

use self::error::ErrorCode;
use crate::error::Error;

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ extern crate serde_json;
#[cfg(feature = "ffi")]
mod ffi;

#[cfg(all(feature = "migration", feature = "sqlite"))]
pub mod migration;

pub mod kms;

mod protect;
Expand Down
Loading

0 comments on commit ee4e5aa

Please sign in to comment.