use crate::policy::{AccessRequest, Action, Authorize, Decision};
use weirdnet_access::{Catalog, Facts, RoleSet, Settings};
pub const LIST: &str = "vault.list";
pub const READ_META: &str = "vault.read-meta";
pub const CREATE: &str = "vault.create";
pub const ROTATE: &str = "vault.rotate";
pub const DELETE: &str = "vault.delete";
pub const USE: &str = "vault.use";
pub const REVEAL: &str = "vault.reveal";
pub const GRANT: &str = "vault.grant";
pub const AUDIT: &str = "vault.audit";
pub const BACKUP: &str = "vault.backup";
pub const ADMIN: &str = "vault.admin";
pub fn permission(action: Action) -> &'static str {
match action {
Action::List => LIST,
Action::ReadMeta => READ_META,
Action::Create => CREATE,
Action::Rotate => ROTATE,
Action::Delete => DELETE,
Action::Use => USE,
Action::Reveal => REVEAL,
Action::Grant => GRANT,
Action::Audit => AUDIT,
Action::Backup => BACKUP,
}
}
pub fn add_vault_permissions(catalog: &mut Catalog) -> weirdnet_access::Result<()> {
for (name, what, managing) in [
(LIST, "list secret metadata", false),
(READ_META, "read a secret's metadata, versions and grants", false),
(CREATE, "create secrets", true),
(ROTATE, "stage, promote and roll back secret versions", true),
(DELETE, "delete secrets", true),
(USE, "use secrets inside the server", false),
(REVEAL, "retrieve secret values", true),
(GRANT, "approve identities for secrets", true),
(AUDIT, "read the audit log", false),
(BACKUP, "take vault snapshots", true),
(ADMIN, "everything in the vault", true),
] {
catalog.add(name, what, managing)?;
}
catalog.imply(READ_META, &[LIST])?;
catalog.imply(USE, &[READ_META])?;
catalog.imply(REVEAL, &[READ_META])?;
catalog.imply(ADMIN, &[CREATE, ROTATE, DELETE, USE, REVEAL, GRANT, AUDIT, BACKUP])?;
Ok(())
}
pub fn vault_catalog() -> weirdnet_access::Result<Catalog> {
let mut c = Catalog::core();
add_vault_permissions(&mut c)?;
Ok(c)
}
pub struct AccessAuthorizer<F> {
catalog: Catalog,
roles: RoleSet,
settings: Settings,
facts: F,
}
impl<F> std::fmt::Debug for AccessAuthorizer<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AccessAuthorizer").field("roles", &self.roles).field("settings", &self.settings).finish_non_exhaustive()
}
}
impl<F> AccessAuthorizer<F>
where
F: Fn(&AccessRequest<'_>) -> Facts + Send + Sync,
{
pub fn new(catalog: Catalog, roles: RoleSet, settings: Settings, facts: F) -> Self {
AccessAuthorizer { catalog, roles, settings, facts }
}
}
impl<F> Authorize for AccessAuthorizer<F>
where
F: Fn(&AccessRequest<'_>) -> Facts + Send + Sync,
{
fn authorize(&self, request: &AccessRequest<'_>) -> Decision {
let facts = (self.facts)(request);
match weirdnet_access::decide(&self.catalog, &self.roles, self.settings, &facts, permission(request.action)) {
weirdnet_access::Decision::Allow { .. } => Decision::Allow,
weirdnet_access::Decision::NotFound => Decision::deny("not found"),
weirdnet_access::Decision::JoinFirst => Decision::deny("membership required"),
weirdnet_access::Decision::Denied(why) => Decision::deny(why),
}
}
}