refactor all vault db retrieval to use new API

This refactors the remaining vault accessor routines to use
the new more efficient key pre-fetching routine. This also
fixes up an error in test case generation and hopefully resolves
an issue with concurrency errors using the older version of
the access routine.
This commit is contained in:
bunnie 2022-11-09 15:06:51 +08:00
parent c3ba30d55d
commit 474a02c2cb

View file

@ -752,33 +752,12 @@ impl<'a> ActionManager<'a> {
VaultMode::Fido => { VaultMode::Fido => {
// first assemble U2F records // first assemble U2F records
log::debug!("listing in {}", U2F_APP_DICT); log::debug!("listing in {}", U2F_APP_DICT);
let keylist = match self.pddb.borrow().list_keys(U2F_APP_DICT, None) { match self.pddb.borrow().read_dict(U2F_APP_DICT, None, Some(256 * 1024)) {
Ok(keylist) => keylist, Ok(keys) => {
Err(e) => { let mut oom_keys = 0;
match e.kind() { il.reserve(keys.len());
std::io::ErrorKind::NotFound => { for key in keys {
log::debug!("U2F dictionary not yet created"); if let Some(data) = key.data {
}
_ => self.report_err("Dictionary error accessing U2F database", Some(e)),
}
Vec::new()
}
};
log::debug!("list: {:?}", keylist);
for key in keylist {
match self.pddb.borrow().get(
U2F_APP_DICT,
&key,
None,
false, false, None,
Some(crate::basis_change)
) {
Ok(mut record) => {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
if let Some(ai) = deserialize_app_info(data) { if let Some(ai) = deserialize_app_info(data) {
let extra = format!("{}; {}{}", let extra = format!("{}; {}{}",
crate::ux::atime_to_str(ai.atime), crate::ux::atime_to_str(ai.atime),
@ -790,48 +769,44 @@ impl<'a> ActionManager<'a> {
name: desc, name: desc,
extra, extra,
dirty: true, dirty: true,
guid: key, guid: key.name,
}; };
il.push(li); il.push(li);
} else { } else {
self.report_err("Couldn't deserialize U2F:", Some(key)); let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, U2F_APP_DICT, key.name, key.len
);
self.report_err("Couldn't deserialize U2F key:", Some(err));
}
} else {
oom_keys += 1;
} }
} }
Err(e) => self.report_err("Couldn't access U2F key", Some(e)), if oom_keys != 0 {
log::warn!("Ran out of cache space handling U2F tokens. {} tokens are not loaded.", oom_keys);
self.report_err(&format!("Ran out of cache space handling U2F entries. {} tokens not loaded", oom_keys), None::<crate::storage::Error>);
} }
} }
Err(e) => self.report_err("Couldn't access U2F key", Some(e)),
}
}
log::debug!("listing in {}", FIDO_CRED_DICT);
let keylist = match self.pddb.borrow().list_keys(FIDO_CRED_DICT, None) {
Ok(keylist) => keylist,
Err(e) => { Err(e) => {
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => { ErrorKind::NotFound => {
log::debug!("FIDO2 dictionary not yet created"); // this is fine, it just means no entries have been entered yet
},
_ => {
log::error!("Error opening U2F dictionary");
self.report_err("Error opening U2F dictionary", Some(e))
} }
_ => self.report_err("Dictionary error accessing FIDO2 database", Some(e)),
} }
Vec::new() },
} }
};
log::debug!("keylist: {:?}", keylist); log::debug!("listing in {}", FIDO_CRED_DICT);
// now merge in the FIDO2 records match self.pddb.borrow().read_dict(FIDO_CRED_DICT, None, Some(256 * 1024)) {
for key in keylist { Ok(keys) => {
match self.pddb.borrow().get( let mut oom_keys = 0;
FIDO_CRED_DICT, il.reserve(keys.len());
&key, for key in keys {
None, if let Some(data) = key.data {
false, false, None,
Some(crate::basis_change)
) {
Ok(mut record) => {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
match crate::ctap::storage::deserialize_credential(&data) { match crate::ctap::storage::deserialize_credential(&data) {
Some(result) => { Some(result) => {
let name = if let Some(display_name) = result.user_display_name { let name = if let Some(display_name) = result.user_display_name {
@ -845,47 +820,46 @@ impl<'a> ActionManager<'a> {
name: desc, name: desc,
extra, extra,
dirty: true, dirty: true,
guid: key, guid: key.name,
}; };
il.push(li); il.push(li);
} }
None => self.report_err("Couldn't deserialize FIDO2:", Some(key)), None => {
let err = format!("{}:{}:{}: ({}){:x?}...",
key.basis, FIDO_CRED_DICT, key.name, key.len, &data[..16]
);
self.report_err("Couldn't deserialize FIDO2:", Some(err))
},
}
} else {
oom_keys += 1;
} }
} }
Err(e) => self.report_err("Couldn't access FIDO2 key", Some(e)), if oom_keys != 0 {
log::warn!("Ran out of cache space handling FIDO2 tokens. {} tokens are not loaded.", oom_keys);
self.report_err(&format!("Ran out of cache space handling FIDO2 entries. {} tokens not loaded", oom_keys), None::<crate::storage::Error>);
} }
} }
Err(e) => self.report_err("Couldn't access FIDO2 key", Some(e)), Err(e) => {
match e.kind() {
ErrorKind::NotFound => {
// this is fine, it just means no entries have been entered yet
},
_ => {
log::error!("Error opening FIDO2 dictionary");
self.report_err("Error opening FIDO2 dictionary", Some(e))
} }
} }
},
}
} }
VaultMode::Totp => { VaultMode::Totp => {
let keylist = match self.pddb.borrow().list_keys(VAULT_TOTP_DICT, None) { match self.pddb.borrow().read_dict(VAULT_TOTP_DICT, None, Some(256 * 1024)) {
Ok(keylist) => keylist, Ok(keys) => {
Err(e) => { let mut oom_keys = 0;
match e.kind() { il.reserve(keys.len());
std::io::ErrorKind::NotFound => { for key in keys {
log::debug!("TOTP dictionary not yet created"); if let Some(data) = key.data {
}
_ => self.report_err("Dictionary error accessing TOTP database", Some(e)),
}
Vec::new()
}
};
for key in keylist {
match self.pddb.borrow().get(
VAULT_TOTP_DICT,
&key,
None,
false, false, None,
Some(crate::basis_change)
) {
Ok(mut record) => {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
if let Some(totp) = storage::TotpRecord::try_from(data).ok() { if let Some(totp) = storage::TotpRecord::try_from(data).ok() {
let extra = format!("{}:{}:{}:{}:{}", let extra = format!("{}:{}:{}:{}:{}",
totp.secret, totp.secret,
@ -899,19 +873,36 @@ impl<'a> ActionManager<'a> {
name: desc, name: desc,
extra, extra,
dirty: true, dirty: true,
guid: key, guid: key.name,
}; };
il.push(li); il.push(li);
} else { } else {
self.report_err("Couldn't deserialize TOTP:", Some(key)); let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, VAULT_TOTP_DICT, key.name, key.len
);
self.report_err("Couldn't deserialize TOTP:", Some(err));
}
} else {
oom_keys += 1;
} }
} }
Err(e) => self.report_err("Couldn't access TOTP key", Some(e)), if oom_keys != 0 {
log::warn!("Ran out of cache space handling FIDO2 tokens. {} tokens are not loaded.", oom_keys);
self.report_err(&format!("Ran out of cache space handling FIDO2 entries. {} tokens not loaded", oom_keys), None::<crate::storage::Error>);
} }
} }
Err(e) => self.report_err("Couldn't access TOTP key", Some(e)), Err(e) => {
match e.kind() {
ErrorKind::NotFound => {
// this is fine, it just means no entries have been entered yet
},
_ => {
log::error!("Error opening FIDO2 dictionary");
self.report_err("Error opening FIDO2 dictionary", Some(e))
} }
} }
},
}
} }
} }
il.sort(); il.sort();
@ -1102,8 +1093,8 @@ impl<'a> ActionManager<'a> {
name, name,
id, id,
notes, notes,
ctime: 0, ctime: 1, // zero ctime is disallowed
atime: 0, atime: 1,
count: 0, count: 0,
}; };
let ser = serialize_app_info(&record); let ser = serialize_app_info(&record);