use crate::{derive_vault_id, procedures::Runner, Client, ClientError, Location};
use engine::vault::VaultId;
pub const DEFAULT_RANDOM_HINT_SIZE: usize = 24;
pub struct ClientVault {
pub(crate) client: Client,
pub(crate) vault_path: Vec<u8>,
}
impl ClientVault {
pub fn write_secret(&self, location: Location, payload: Vec<u8>) -> Result<(), ClientError> {
self.client.write_to_vault(&location, payload)?;
Ok(())
}
pub fn delete_secret<P>(&self, record_path: P) -> Result<bool, ClientError>
where
P: AsRef<[u8]>,
{
self.revoke_secret(record_path)?;
self.cleanup()
}
pub fn revoke_secret<P>(&self, record_path: P) -> Result<(), ClientError>
where
P: AsRef<[u8]>,
{
let location = Location::Generic {
record_path: record_path.as_ref().to_vec(),
vault_path: self.vault_path.clone(),
};
self.client.revoke_data(&location)?;
Ok(())
}
pub fn cleanup(&self) -> Result<bool, ClientError> {
let result = self.client.garbage_collect(self.id())?;
Ok(result)
}
pub fn id(&self) -> VaultId {
derive_vault_id(self.vault_path.clone())
}
#[cfg(test)]
pub fn read_secret<P>(&self, record_path: P) -> Result<Vec<u8>, ClientError>
where
P: AsRef<[u8]>,
{
let location = Location::generic(self.vault_path.clone(), record_path.as_ref().to_vec());
let mut data = Vec::new();
self.client.get_guard(&location, |guarded_data| {
let guarded_data = guarded_data.borrow();
data.extend_from_slice(&guarded_data);
Ok(())
})?;
Ok(data)
}
}