Skip to content

Commit

Permalink
Use archive feature flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Nov 27, 2023
1 parent d8b0b45 commit ffeaa6f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 111 deletions.
3 changes: 2 additions & 1 deletion workspace/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repository = "https://github.com/saveoursecrets/sdk"
crate-type = ["cdylib", "rlib"]

[features]
default = ["recovery"]
default = ["archive", "recovery"]
archive = []
mem-fs = ["sos-vfs/mem-fs"]
test-utils = ["dep:anyhow"]
recovery = ["dep:vsss-rs"]
Expand Down
110 changes: 0 additions & 110 deletions workspace/sdk/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use std::{

use crate::{
account::{
archive::{
AccountBackup, ExtractFilesLocation, Inventory, RestoreOptions,
},
login::Login,
password::DelegatedPassword,
search::{AccountStatistics, DocumentCount, SearchIndex},
Expand Down Expand Up @@ -38,7 +35,6 @@ use tracing::{span, Level};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use tokio::{
io::{AsyncRead, AsyncSeek},
sync::{mpsc, RwLock},
};

Expand Down Expand Up @@ -1670,104 +1666,6 @@ impl<D> Account<D> {
}
*/

/// Create a backup archive containing the
/// encrypted data for the account.
pub async fn export_backup_archive<P: AsRef<Path>>(
&self,
path: P,
) -> Result<()> {
AccountBackup::export_archive_file(path, self.address(), &self.paths)
.await?;

let audit_event = AuditEvent::new(
EventKind::ExportBackupArchive,
self.address().clone(),
None,
);
self.append_audit_logs(vec![audit_event]).await?;

Ok(())
}

/// Read the inventory from an archive.
pub async fn restore_archive_inventory<
R: AsyncRead + AsyncSeek + Unpin,
>(
buffer: R,
) -> Result<Inventory> {
let mut inventory =
AccountBackup::restore_archive_inventory(buffer).await?;
let accounts = AccountsList::list_accounts(None).await?;
let exists_local = accounts
.iter()
.any(|account| account.address() == &inventory.manifest.address);
inventory.exists_local = exists_local;
Ok(inventory)
}

/// Restore from a backup archive file.
pub async fn restore_backup_archive<P: AsRef<Path>>(
owner: Option<&mut Account<D>>,
path: P,
options: RestoreOptions,
data_dir: Option<PathBuf>,
) -> Result<AccountInfo> {
let file = File::open(path).await?;
let (account, owner) =
Self::restore_archive_reader(owner, file, options, data_dir)
.await?;

if let Some(owner) = owner {
let audit_event = AuditEvent::new(
EventKind::ImportBackupArchive,
owner.address().clone(),
None,
);
owner.append_audit_logs(vec![audit_event]).await?;
}

Ok(account)
}

/// Import from an archive reader.
async fn restore_archive_reader<R: AsyncRead + AsyncSeek + Unpin>(
mut owner: Option<&mut Account<D>>,
buffer: R,
mut options: RestoreOptions,
data_dir: Option<PathBuf>,
) -> Result<(AccountInfo, Option<&mut Account<D>>)> {
let files_dir = if let Some(owner) = owner.as_ref() {
ExtractFilesLocation::Path(owner.paths().files_dir().clone())
} else {
ExtractFilesLocation::Builder(Box::new(|address| {
let data_dir = UserPaths::data_dir().unwrap();
let paths = UserPaths::new(data_dir, address);
Some(paths.files_dir().to_owned())
}))
};

options.files_dir = Some(files_dir);

let (targets, account) = AccountBackup::restore_archive_buffer(
buffer,
options,
owner.is_some(),
data_dir,
)
.await?;

if let Some(owner) = owner.as_mut() {
{
let storage = owner.storage()?;
let mut writer = storage.write().await;
writer.restore_archive(&targets).await?;
}
owner.build_search_index().await?;
}

Ok((account, owner))
}

/// Create a detached view of an event log until a
/// particular commit.
///
Expand Down Expand Up @@ -1857,11 +1755,3 @@ impl<D> Account<D> {
DelegatedPassword::find_folder_password(identity, vault_id).await
}
}

/*
impl<D> From<Account<D>> for Arc<RwLock<FolderStorage>> {
fn from(value: Account<D>) -> Self {
value.storage
}
}
*/
109 changes: 109 additions & 0 deletions workspace/sdk/src/account/archive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,112 @@ pub use backup::{
RestoreOptions, RestoreTargets,
};
pub use zip::*;

use crate::{Result, events::{EventKind, AuditEvent}, vfs::File};
use std::path::{Path, PathBuf};
use super::{Account, AccountInfo, UserPaths, AccountsList};
use tokio::{
io::{AsyncRead, AsyncSeek},
sync::{mpsc, RwLock},
};

impl<D> Account<D> {
/// Create a backup archive containing the
/// encrypted data for the account.
pub async fn export_backup_archive<P: AsRef<Path>>(
&self,
path: P,
) -> Result<()> {
AccountBackup::export_archive_file(path, self.address(), &self.paths)
.await?;

let audit_event = AuditEvent::new(
EventKind::ExportBackupArchive,
self.address().clone(),
None,
);
self.append_audit_logs(vec![audit_event]).await?;

Ok(())
}

/// Read the inventory from an archive.
pub async fn restore_archive_inventory<
R: AsyncRead + AsyncSeek + Unpin,
>(
buffer: R,
) -> Result<Inventory> {
let mut inventory =
AccountBackup::restore_archive_inventory(buffer).await?;
let accounts = AccountsList::list_accounts(None).await?;
let exists_local = accounts
.iter()
.any(|account| account.address() == &inventory.manifest.address);
inventory.exists_local = exists_local;
Ok(inventory)
}

/// Restore from a backup archive file.
pub async fn restore_backup_archive<P: AsRef<Path>>(
owner: Option<&mut Account<D>>,
path: P,
options: RestoreOptions,
data_dir: Option<PathBuf>,
) -> Result<AccountInfo> {
let file = File::open(path).await?;
let (account, owner) =
Self::restore_archive_reader(owner, file, options, data_dir)
.await?;

if let Some(owner) = owner {
let audit_event = AuditEvent::new(
EventKind::ImportBackupArchive,
owner.address().clone(),
None,
);
owner.append_audit_logs(vec![audit_event]).await?;
}

Ok(account)
}

/// Import from an archive reader.
async fn restore_archive_reader<R: AsyncRead + AsyncSeek + Unpin>(
mut owner: Option<&mut Account<D>>,
buffer: R,
mut options: RestoreOptions,
data_dir: Option<PathBuf>,
) -> Result<(AccountInfo, Option<&mut Account<D>>)> {
let files_dir = if let Some(owner) = owner.as_ref() {
ExtractFilesLocation::Path(owner.paths().files_dir().clone())
} else {
ExtractFilesLocation::Builder(Box::new(|address| {
let data_dir = UserPaths::data_dir().unwrap();
let paths = UserPaths::new(data_dir, address);
Some(paths.files_dir().to_owned())
}))
};

options.files_dir = Some(files_dir);

let (targets, account) = AccountBackup::restore_archive_buffer(
buffer,
options,
owner.is_some(),
data_dir,
)
.await?;

if let Some(owner) = owner.as_mut() {
{
let storage = owner.storage()?;
let mut writer = storage.write().await;
writer.restore_archive(&targets).await?;
}
owner.build_search_index().await?;
}

Ok((account, owner))
}

}
1 change: 1 addition & 0 deletions workspace/sdk/src/account/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Create and manage local accounts.
mod account;
#[cfg(feature = "archive")]
pub mod archive;
mod builder;
#[cfg(feature = "contacts")]
Expand Down

0 comments on commit ffeaa6f

Please sign in to comment.