use crate::config::resources::ResourceKind;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct LockdownError {
failures: Vec<FailureKind>,
}
impl LockdownError {
#[must_use]
pub fn new(failures: Vec<FailureKind>) -> Self {
Self { failures }
}
#[must_use]
pub fn failures(&self) -> &[FailureKind] {
&self.failures
}
}
impl std::error::Error for LockdownError {}
impl std::fmt::Display for LockdownError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"lockdown configuration resulted in {} failures: {}",
self.failures.len(),
self
.failures
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join(", ")
)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum FailureKind {
General(String),
Failed(Box<LockdownError>),
NotExpresslyAllowed(String, ResourceKind),
Volume(String, String),
VolumeInvalid(String, String),
VolumeRestrictionInvalid(String),
Port(String, u16),
Address(String, String),
Url(String, String),
FileUrlInvalid(url::Url),
FileUrlNotFound(url::Url),
}
impl std::fmt::Display for FailureKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FailureKind::Failed(error) => write!(f, "{error}"),
FailureKind::General(s) => f.write_str(s),
FailureKind::VolumeInvalid(name, path) => write!(
f,
"the path {} for volume {} could not be reconciled on the file system",
path, name
),
FailureKind::Volume(id, path) => write!(f, "component {} is not allowed to access {}", id, path),
FailureKind::NotExpresslyAllowed(id,kind) => write!(f, "component {} is not expressly allowed to access a {} resource", id,kind),
FailureKind::VolumeRestrictionInvalid(path) => write!(
f,
"restricted volume '{}' is not valid, it can not be reconciled on the file system and can not be asserted against",
path
),
FailureKind::Port(id, port) => write!(f, "component {} is not allowed to access {}", id, port),
FailureKind::Address(id,address) => write!(f, "component {} is not allowed to access {}", id, address),
FailureKind::Url(id, url) => write!(f, "component {} is not allowed to access {}", id, url),
FailureKind::FileUrlInvalid(url) => write!(f, "could not create a file path out of {}", url),
FailureKind::FileUrlNotFound(url) => write!(f, "file URL '{}' does not point to a valid file", url),
}
}
}