use crate::policy::Action;
use crate::scope::Environment;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum VaultError {
#[error("denied: {0}")]
Denied(String),
#[error("a {caller} caller may not {action} a {secret} secret")]
EnvironmentMismatch {
caller: Environment,
secret: Environment,
action: Action,
},
#[error("not found: {0}")]
NotFound(String),
#[error("conflict: {0}")]
Conflict(String),
#[error("invalid {what}: {msg}")]
Invalid {
what: &'static str,
msg: String,
},
#[error("could not unlock: {0}")]
Unlock(String),
#[error("crypto: {0}")]
Crypto(String),
#[error("storage: {0}")]
Storage(String),
#[error("key store: {0}")]
KeyStore(String),
#[error("audit log does not verify at record {0}")]
AuditBroken(u64),
#[error(transparent)]
Io(#[from] std::io::Error),
}
impl VaultError {
pub(crate) fn invalid(what: &'static str, msg: impl Into<String>) -> Self {
VaultError::Invalid { what, msg: msg.into() }
}
pub fn is_refusal(&self) -> bool {
matches!(self, VaultError::Denied(_) | VaultError::EnvironmentMismatch { .. })
}
}
impl From<weirdnet_keys::KeysError> for VaultError {
fn from(e: weirdnet_keys::KeysError) -> Self {
use weirdnet_keys::KeysError as K;
match e {
K::Unlock(m) => VaultError::Unlock(m),
K::NotInitialized(_) => VaultError::NotFound("the vault's keys are not initialized".into()),
K::Io(e) => VaultError::Io(e),
K::Invalid { what, msg } => VaultError::Invalid { what, msg },
other => VaultError::Crypto(other.to_string()),
}
}
}
impl From<weirdnet_storage::StorageError> for VaultError {
fn from(e: weirdnet_storage::StorageError) -> Self {
use weirdnet_storage::StorageError as S;
match e {
S::WrongKey { .. } => VaultError::Unlock(e.to_string()),
S::Io(e) => VaultError::Io(e),
other => VaultError::Storage(other.to_string()),
}
}
}
impl From<weirdnet_storage::rusqlite::Error> for VaultError {
fn from(e: weirdnet_storage::rusqlite::Error) -> Self {
VaultError::Storage(e.to_string())
}
}
pub type Result<T, E = VaultError> = std::result::Result<T, E>;