mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
feat(vault): implement basics of dupe detection
Still needs a way to deterministically generate a key for each entry. Idea: a `hash() -> String` method on StorageContent so that new_record can call it, store said object with the newly-generated key. Since this is a deterministic operation, restoring a backup with dupes would not overwrite what's present in PDDB already.
This commit is contained in:
parent
473e1cf77b
commit
cdfae27afc
3 changed files with 69 additions and 17 deletions
|
|
@ -321,7 +321,7 @@ impl ActionManager {
|
||||||
count: 0,
|
count: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.storage.borrow_mut().new_record(&mut record, None) {
|
match self.storage.borrow_mut().new_record(&mut record, None, true) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log::error!("internal error");
|
log::error!("internal error");
|
||||||
|
|
@ -383,7 +383,7 @@ impl ActionManager {
|
||||||
notes: t!("vault.notes", xous::LANG).to_string(),
|
notes: t!("vault.notes", xous::LANG).to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.storage.borrow_mut().new_record(&mut totp, None) {
|
match self.storage.borrow_mut().new_record(&mut totp, None, true) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log::error!("internal error");
|
log::error!("internal error");
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ pub enum Error {
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
TotpSerError(TOTPSerializationError),
|
TotpSerError(TOTPSerializationError),
|
||||||
PasswordSerError(PasswordSerializationError),
|
PasswordSerError(PasswordSerializationError),
|
||||||
|
KeyExists,
|
||||||
|
DupesExist(Vec<usize>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
|
|
@ -85,6 +87,25 @@ impl Manager {
|
||||||
hex::encode(guid)
|
hex::encode(guid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pddb_exists(&self, dict: &str, key_name: &str, basis: Option<String>) -> bool {
|
||||||
|
match self.pddb.get(
|
||||||
|
dict,
|
||||||
|
&key_name,
|
||||||
|
basis.as_deref(),
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
Some(crate::basis_change),
|
||||||
|
) {
|
||||||
|
Ok(_) => return true,
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("error while trying to lookup if a key exists, {}", e);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn pddb_store(
|
fn pddb_store(
|
||||||
&self,
|
&self,
|
||||||
payload: &[u8],
|
payload: &[u8],
|
||||||
|
|
@ -93,12 +114,18 @@ impl Manager {
|
||||||
alloc_hint: Option<usize>,
|
alloc_hint: Option<usize>,
|
||||||
basis: Option<String>,
|
basis: Option<String>,
|
||||||
sync: bool,
|
sync: bool,
|
||||||
|
overwrite: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
|
if !overwrite && self.pddb_exists(dict, key_name, basis.clone()) {
|
||||||
|
return Err(Error::KeyExists);
|
||||||
|
}
|
||||||
|
|
||||||
match self.pddb.get(
|
match self.pddb.get(
|
||||||
dict,
|
dict,
|
||||||
&key_name,
|
&key_name,
|
||||||
basis.as_deref(),
|
basis.as_deref(),
|
||||||
true,
|
true, // if overwrite, we wanna create both the dict and the key if they don't
|
||||||
|
// exist
|
||||||
true,
|
true,
|
||||||
alloc_hint,
|
alloc_hint,
|
||||||
Some(crate::basis_change),
|
Some(crate::basis_change),
|
||||||
|
|
@ -155,6 +182,7 @@ impl Manager {
|
||||||
&mut self,
|
&mut self,
|
||||||
record: &mut dyn StorageContent,
|
record: &mut dyn StorageContent,
|
||||||
basis: Option<String>,
|
basis: Option<String>,
|
||||||
|
overwrite: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let record = record;
|
let record = record;
|
||||||
let settings = record.settings();
|
let settings = record.settings();
|
||||||
|
|
@ -169,6 +197,7 @@ impl Manager {
|
||||||
settings.alloc_hint,
|
settings.alloc_hint,
|
||||||
basis.clone(),
|
basis.clone(),
|
||||||
true,
|
true,
|
||||||
|
overwrite,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,9 +205,11 @@ impl Manager {
|
||||||
&mut self,
|
&mut self,
|
||||||
records: Vec<Box<dyn StorageContent>>,
|
records: Vec<Box<dyn StorageContent>>,
|
||||||
basis: Option<String>,
|
basis: Option<String>,
|
||||||
|
overwrite: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let mut precords = records.into_iter().peekable();
|
let mut precords = records.into_iter().peekable();
|
||||||
let mut current_idx = 0; // idk how to use peekable + enumerate
|
let mut current_idx = 0; // idk how to use peekable + enumerate
|
||||||
|
let mut dupes = vec![];
|
||||||
while let Some(record) = precords.next() {
|
while let Some(record) = precords.next() {
|
||||||
let mut record = record;
|
let mut record = record;
|
||||||
let settings = record.settings();
|
let settings = record.settings();
|
||||||
|
|
@ -187,9 +218,12 @@ impl Manager {
|
||||||
let guid = self.gen_guid();
|
let guid = self.gen_guid();
|
||||||
let should_sync = precords.peek().is_none() || current_idx % 10 == 0;
|
let should_sync = precords.peek().is_none() || current_idx % 10 == 0;
|
||||||
|
|
||||||
log::debug!("current_idx: {}, should_sync: {}, is_none: {}", current_idx, should_sync, precords.peek().is_none());
|
log::debug!(
|
||||||
|
"current_idx: {}, should_sync: {}, is_none: {}",
|
||||||
current_idx += 1;
|
current_idx,
|
||||||
|
should_sync,
|
||||||
|
precords.peek().is_none()
|
||||||
|
);
|
||||||
|
|
||||||
match self.pddb_store(
|
match self.pddb_store(
|
||||||
&serialized_record,
|
&serialized_record,
|
||||||
|
|
@ -198,10 +232,23 @@ impl Manager {
|
||||||
settings.alloc_hint,
|
settings.alloc_hint,
|
||||||
basis.clone(),
|
basis.clone(),
|
||||||
should_sync,
|
should_sync,
|
||||||
|
overwrite,
|
||||||
) {
|
) {
|
||||||
Ok(()) => continue,
|
Ok(()) => (),
|
||||||
Err(error) => return Err(error),
|
Err(error) => match error {
|
||||||
};
|
Error::KeyExists => {
|
||||||
|
dupes.push(current_idx);
|
||||||
|
()
|
||||||
|
}
|
||||||
|
_ => return Err(error),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
current_idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dupes.is_empty() {
|
||||||
|
return Err(Error::DupesExist(dupes));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -250,7 +297,7 @@ impl Manager {
|
||||||
self.pddb
|
self.pddb
|
||||||
.delete_key(&settings.dict, key_name, Some(&basis))?;
|
.delete_key(&settings.dict, key_name, Some(&basis))?;
|
||||||
|
|
||||||
self.new_record(&mut *record, Some(basis))
|
self.new_record(&mut *record, Some(basis), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&mut self, kind: ContentKind, key_name: &str) -> Result<(), Error> {
|
pub fn delete(&mut self, kind: ContentKind, key_name: &str) -> Result<(), Error> {
|
||||||
|
|
|
||||||
|
|
@ -44,11 +44,7 @@ impl VendorSession {
|
||||||
*self = VendorSession::default()
|
*self = VendorSession::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_from_wire(
|
fn read_from_wire(&mut self, w: backup::Wire, command: u8) -> Result<(), SessionError> {
|
||||||
&mut self,
|
|
||||||
w: backup::Wire,
|
|
||||||
command: u8,
|
|
||||||
) -> Result<(), SessionError> {
|
|
||||||
self.data.append(&mut w.data.clone());
|
self.data.append(&mut w.data.clone());
|
||||||
self.finished = !w.more_data;
|
self.finished = !w.more_data;
|
||||||
|
|
||||||
|
|
@ -298,8 +294,17 @@ fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
storage.new_records(entries, None)?;
|
match storage.new_records(entries, None, false) {
|
||||||
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
Ok(()) => Ok(vec![0xca, 0xfe, 0xba, 0xbe]),
|
||||||
|
Err(error) => match error {
|
||||||
|
crate::storage::Error::DupesExist(dupes) => {
|
||||||
|
// this is a non-fatal error, let's just print the dupes and continue
|
||||||
|
log::info!("dupes detected while restoring! {:?}", dupes);
|
||||||
|
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
||||||
|
}
|
||||||
|
_ => Err(error)?,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_backup(
|
fn handle_backup(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue