use crate::control::security::blacklist::store::BlacklistStore;
use crate::control::security::catalog::SystemCatalog;
use super::super::divergence::{Divergence, DivergenceKind};
use super::diff::diff_sorted;
pub fn verify_blacklist(
store: &BlacklistStore,
catalog: &SystemCatalog,
) -> crate::Result<Vec<Divergence>> {
let mut expected: Vec<(String, String)> = catalog
.load_all_blacklist_entries()?
.into_iter()
.filter(|e| {
if e.expires_at == 0 {
return true;
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
now < e.expires_at
})
.map(|e| (e.key.clone(), e.kind.clone()))
.collect();
expected.sort_by(|a, b| a.0.cmp(&b.0));
let mut actual: Vec<(String, String)> = store
.list_all_entries()
.into_iter()
.filter(|e| !e.is_expired())
.map(|e| (e.key.clone(), e.kind.clone()))
.collect();
actual.sort_by(|a, b| a.0.cmp(&b.0));
let diff = diff_sorted(&expected, &actual, |a, b| a == b);
let mut out = Vec::new();
for (key, _) in &diff.only_in_expected {
out.push(Divergence::new(DivergenceKind::MissingInRegistry {
registry: "blacklist",
key: key.clone(),
}));
}
for (key, _) in &diff.only_in_actual {
out.push(Divergence::new(DivergenceKind::ExtraInRegistry {
registry: "blacklist",
key: key.clone(),
}));
}
Ok(out)
}
pub fn repair_blacklist(store: &BlacklistStore, catalog: &SystemCatalog) -> crate::Result<()> {
store.clear_and_reload(catalog)
}