use spiffe::TrustDomain;
use std::collections::BTreeSet;
#[derive(Debug, Clone, Default)]
pub enum TrustDomainPolicy {
#[default]
AnyInBundleSet,
AllowList(BTreeSet<TrustDomain>),
LocalOnly(TrustDomain),
}
impl TrustDomainPolicy {
pub fn allows(&self, trust_domain: &TrustDomain) -> bool {
match self {
Self::AnyInBundleSet => true,
Self::AllowList(allowed) => allowed.contains(trust_domain),
Self::LocalOnly(local) => trust_domain == local,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_any_in_bundle_set() {
let policy = TrustDomainPolicy::AnyInBundleSet;
let td1 = TrustDomain::new("example.org").unwrap();
let td2 = TrustDomain::new("other.org").unwrap();
assert!(policy.allows(&td1));
assert!(policy.allows(&td2));
}
#[test]
fn test_allow_list() {
let td1 = TrustDomain::new("example.org").unwrap();
let td2 = TrustDomain::new("other.org").unwrap();
let td3 = TrustDomain::new("third.org").unwrap();
let mut allowed = BTreeSet::new();
allowed.insert(td1.clone());
allowed.insert(td2.clone());
let policy = TrustDomainPolicy::AllowList(allowed);
assert!(policy.allows(&td1));
assert!(policy.allows(&td2));
assert!(!policy.allows(&td3));
}
#[test]
fn test_local_only() {
let local = TrustDomain::new("example.org").unwrap();
let other = TrustDomain::new("other.org").unwrap();
let policy = TrustDomainPolicy::LocalOnly(local.clone());
assert!(policy.allows(&local));
assert!(!policy.allows(&other));
}
}