use id_effect::succeed;
use crate::policy::{Decision, Policy, PolicyEffect};
use crate::principal::{CurrentPrincipal, Principal};
use crate::rbac::permission_matches;
pub trait Owned {
fn owner_id(&self) -> &str;
}
pub trait Tenanted {
fn tenant_id(&self) -> &str;
}
pub fn owner() -> OwnerPolicy {
OwnerPolicy
}
#[derive(Clone, Copy, Debug)]
pub struct OwnerPolicy;
impl<Res: Owned + ?Sized> Policy<Res> for OwnerPolicy {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let owns = principal.is_authenticated() && principal.id() == resource.owner_id();
succeed(Decision::allow_if(owns, "not the owner"))
}
}
pub fn same_tenant() -> SameTenant {
SameTenant
}
#[derive(Clone, Copy, Debug)]
pub struct SameTenant;
impl<Res: Tenanted + ?Sized> Policy<Res> for SameTenant {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let same = principal.tenant() == Some(resource.tenant_id());
succeed(Decision::allow_if(
same,
"resource belongs to another tenant",
))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Grantee {
User(String),
Role(String),
Authenticated,
Anyone,
}
impl Grantee {
pub fn covers(&self, principal: &dyn Principal) -> bool {
match self {
Self::User(id) => principal.is_authenticated() && principal.id() == id,
Self::Role(role) => principal.has_role(role),
Self::Authenticated => principal.is_authenticated(),
Self::Anyone => true,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Acl {
entries: Vec<(Grantee, String)>,
}
impl Acl {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn grant(mut self, grantee: Grantee, permission: impl Into<String>) -> Self {
self.entries.push((grantee, permission.into()));
self
}
#[must_use]
pub fn allow_user(self, id: impl Into<String>, permission: impl Into<String>) -> Self {
self.grant(Grantee::User(id.into()), permission)
}
#[must_use]
pub fn allow_role(self, role: impl Into<String>, permission: impl Into<String>) -> Self {
self.grant(Grantee::Role(role.into()), permission)
}
pub fn entries(&self) -> &[(Grantee, String)] {
&self.entries
}
pub fn permits(&self, principal: &dyn Principal, permission: &str) -> bool {
self
.entries
.iter()
.any(|(who, granted)| who.covers(principal) && permission_matches(granted, permission))
}
}
pub trait HasAcl {
fn acl(&self) -> &Acl;
}
impl HasAcl for Acl {
fn acl(&self) -> &Acl {
self
}
}
pub fn acl_permits(permission: impl Into<String>) -> AclPolicy {
AclPolicy(permission.into())
}
#[derive(Clone, Debug)]
pub struct AclPolicy(String);
impl<Res: HasAcl + ?Sized> Policy<Res> for AclPolicy {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let allowed = resource.acl().permits(&**principal, &self.0);
succeed(Decision::allow_if(
allowed,
format!("no access-list entry grants {}", self.0),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::principal::{Anonymous, BasicPrincipal};
use id_effect::Exit;
struct Account {
owner: String,
org: String,
}
impl Owned for Account {
fn owner_id(&self) -> &str {
&self.owner
}
}
impl Tenanted for Account {
fn tenant_id(&self) -> &str {
&self.org
}
}
fn decide<Res, P: Policy<Res>>(policy: &P, who: impl Principal, res: &Res) -> Decision {
match policy.evaluate(&CurrentPrincipal::new(who), res).block_on() {
Exit::Success(d) => d,
other => panic!("unexpected exit: {other:?}"),
}
}
fn account() -> Account {
Account {
owner: "alice".into(),
org: "acme".into(),
}
}
#[test]
fn ownership() {
assert!(decide(&owner(), BasicPrincipal::new("alice"), &account()).is_allowed());
assert_eq!(
decide(&owner(), BasicPrincipal::new("bob"), &account()),
Decision::deny("not the owner")
);
let unowned = Account {
owner: String::new(),
org: "acme".into(),
};
assert!(decide(&owner(), Anonymous, &unowned).is_denied());
}
#[test]
fn tenant_match() {
let acme = BasicPrincipal::new("bob").with_tenant("acme");
assert!(decide(&same_tenant(), acme, &account()).is_allowed());
let other = BasicPrincipal::new("bob").with_tenant("globex");
assert!(decide(&same_tenant(), other, &account()).is_denied());
assert!(decide(&same_tenant(), BasicPrincipal::new("bob"), &account()).is_denied());
}
#[test]
fn grantees_cover_the_right_principals() {
let alice = BasicPrincipal::new("alice").with_role("auditor");
assert!(Grantee::User("alice".into()).covers(&alice));
assert!(!Grantee::User(String::new()).covers(&Anonymous));
assert!(Grantee::Role("auditor".into()).covers(&alice));
assert!(Grantee::Authenticated.covers(&alice));
assert!(!Grantee::Authenticated.covers(&Anonymous));
assert!(Grantee::Anyone.covers(&Anonymous));
}
#[test]
fn acl_entries_and_policy() {
let acl = Acl::new()
.allow_user("bob", "account:read")
.allow_role("auditor", "account:*")
.grant(Grantee::Anyone, "account:list");
assert_eq!(acl.entries().len(), 3);
let read = acl_permits("account:read");
assert!(decide(&read, BasicPrincipal::new("bob"), &acl).is_allowed());
assert!(decide(&read, BasicPrincipal::new("eve").with_role("auditor"), &acl).is_allowed());
assert_eq!(
decide(&read, BasicPrincipal::new("eve"), &acl),
Decision::deny("no access-list entry grants account:read")
);
assert!(decide(&acl_permits("account:list"), Anonymous, &acl).is_allowed());
assert!(!Acl::new().permits(&BasicPrincipal::new("bob"), "x"));
}
}