use std::collections::{HashMap, HashSet};
use thiserror::Error;
use super::types::{AccessDecision, Action, Attribute, AttributeRule, EntitlementFailure};
use super::validators::validate_attribute;
pub type Entitlements = HashMap<String, Vec<String>>;
#[derive(Debug, Error)]
pub enum PdpError {
#[error("invalid attribute definition: {0}")]
InvalidAttributeDefinition(String),
#[error("invalid action: {0}")]
InvalidAction(String),
#[error("invalid resource: {0}")]
InvalidResource(String),
#[error("attribute rule unspecified: {0}")]
UnspecifiedRule(String),
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PdpOptions {
#[doc(hidden)]
pub _reserved: (),
}
#[derive(Debug)]
pub struct AccessPdp {
attributes_by_def_fqn: HashMap<String, Attribute>,
parent_def_by_value_fqn: HashMap<String, String>,
}
impl AccessPdp {
pub fn new(attributes: Vec<Attribute>, _opts: PdpOptions) -> Result<Self, PdpError> {
let mut attributes_by_def_fqn = HashMap::new();
let mut parent_def_by_value_fqn = HashMap::new();
for mut attr in attributes {
attr.fqn = attr.fqn.to_ascii_lowercase();
for v in &mut attr.values {
v.fqn = v.fqn.to_ascii_lowercase();
}
validate_attribute(&attr)?;
for v in &attr.values {
parent_def_by_value_fqn.insert(v.fqn.clone(), attr.fqn.clone());
}
attributes_by_def_fqn.insert(attr.fqn.clone(), attr);
}
Ok(Self {
attributes_by_def_fqn,
parent_def_by_value_fqn,
})
}
pub fn check(
&self,
entitlements: &Entitlements,
action: &Action,
resource_attribute_value_fqns: &[String],
) -> Result<AccessDecision, PdpError> {
if action.name.is_empty() {
return Err(PdpError::InvalidAction("action name required".into()));
}
if resource_attribute_value_fqns.is_empty() {
return Err(PdpError::InvalidResource(
"at least one attribute value FQN required".into(),
));
}
let normalized_entitlements: HashMap<String, Vec<String>> = entitlements
.iter()
.map(|(k, v)| (k.to_ascii_lowercase(), v.clone()))
.collect();
let mut value_fqns: Vec<String> = Vec::with_capacity(resource_attribute_value_fqns.len());
let mut missing: Vec<String> = Vec::new();
for raw in resource_attribute_value_fqns {
let normalized = raw.to_ascii_lowercase();
if !self.parent_def_by_value_fqn.contains_key(&normalized) {
missing.push(normalized);
} else {
value_fqns.push(normalized);
}
}
if !missing.is_empty() {
return Ok(AccessDecision::Deny {
failures: missing
.into_iter()
.map(|fqn| EntitlementFailure {
attribute_value_fqn: fqn,
action_name: action.name.clone(),
})
.collect(),
});
}
let mut by_def: HashMap<String, Vec<String>> = HashMap::new();
for fqn in &value_fqns {
let parent_fqn = self
.parent_def_by_value_fqn
.get(fqn)
.expect("checked above")
.clone();
by_def.entry(parent_fqn).or_default().push(fqn.clone());
}
let mut all_failures: Vec<EntitlementFailure> = Vec::new();
for (def_fqn, fqns) in by_def {
let definition = self
.attributes_by_def_fqn
.get(&def_fqn)
.expect("populated alongside parent_def_by_value_fqn");
let failures = match definition.rule {
AttributeRule::AllOf => all_of_rule(&normalized_entitlements, action, &fqns),
AttributeRule::AnyOf => any_of_rule(&normalized_entitlements, action, &fqns),
AttributeRule::Hierarchy => {
hierarchy_rule(&normalized_entitlements, action, &fqns, definition)
}
AttributeRule::Unspecified => {
return Err(PdpError::UnspecifiedRule(def_fqn));
}
};
all_failures.extend(failures);
}
if all_failures.is_empty() {
Ok(AccessDecision::Allow)
} else {
Ok(AccessDecision::Deny {
failures: all_failures,
})
}
}
}
fn value_has_action(entitlements: &Entitlements, value_fqn: &str, action: &Action) -> bool {
entitlements
.get(value_fqn)
.map(|acts| acts.iter().any(|a| a.eq_ignore_ascii_case(&action.name)))
.unwrap_or(false)
}
fn all_of_rule(
entitlements: &Entitlements,
action: &Action,
resource_value_fqns: &[String],
) -> Vec<EntitlementFailure> {
let mut failures = Vec::new();
for fqn in resource_value_fqns {
if !value_has_action(entitlements, fqn, action) {
failures.push(EntitlementFailure {
attribute_value_fqn: fqn.clone(),
action_name: action.name.clone(),
});
}
}
failures
}
fn any_of_rule(
entitlements: &Entitlements,
action: &Action,
resource_value_fqns: &[String],
) -> Vec<EntitlementFailure> {
if resource_value_fqns.is_empty() {
return Vec::new();
}
let mut failures = Vec::new();
let mut any_match = false;
for fqn in resource_value_fqns {
if value_has_action(entitlements, fqn, action) {
any_match = true;
} else {
failures.push(EntitlementFailure {
attribute_value_fqn: fqn.clone(),
action_name: action.name.clone(),
});
}
}
if any_match { Vec::new() } else { failures }
}
fn hierarchy_rule(
entitlements: &Entitlements,
action: &Action,
resource_value_fqns: &[String],
definition: &Attribute,
) -> Vec<EntitlementFailure> {
if resource_value_fqns.is_empty() {
return Vec::new();
}
let value_index: HashMap<&str, usize> = definition
.values
.iter()
.enumerate()
.map(|(i, v)| (v.fqn.as_str(), i))
.collect();
let mut lowest_idx = definition.values.len();
for fqn in resource_value_fqns {
if let Some(&i) = value_index.get(fqn.as_str())
&& i < lowest_idx
{
lowest_idx = i;
}
}
let mut seen: HashSet<&str> = HashSet::new();
for entitled_fqn in entitlements.keys() {
if !seen.insert(entitled_fqn.as_str()) {
continue;
}
if let Some(&i) = value_index.get(entitled_fqn.as_str())
&& i <= lowest_idx
&& value_has_action(entitlements, entitled_fqn, action)
{
return Vec::new();
}
}
resource_value_fqns
.iter()
.map(|fqn| EntitlementFailure {
attribute_value_fqn: fqn.clone(),
action_name: action.name.clone(),
})
.collect()
}