use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::ops::Not;
use thiserror::Error;
#[derive(Debug, Clone)]
pub struct ValidationError {
pub field: String,
pub error_type: ValidationErrorType,
pub message: String,
pub suggestion: Option<String>,
}
impl std::fmt::Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.field, self.message)?;
if let Some(suggestion) = &self.suggestion {
write!(f, " (suggestion: {})", suggestion)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationErrorType {
Required,
InvalidFormat,
OutOfRange,
FqnParseError,
NamespaceNotRegistered,
InvalidUuid,
EmptyList,
}
#[derive(Debug, Error)]
pub enum PolicyError {
#[error("Invalid attribute: {fqn} - {reason}")]
InvalidAttribute { fqn: String, reason: String },
#[error("Invalid operator '{operator}' in context: {context}")]
InvalidOperator { operator: String, context: String },
#[error("Invalid value type for attribute '{attribute}': expected {expected}, got {actual}")]
InvalidValueType {
attribute: String,
expected: String,
actual: String,
},
#[error("Policy evaluation error: {reason}")]
EvaluationError {
reason: String,
attribute: Option<String>,
},
#[error("Policy validation failed with {} error(s)", .0.len())]
ValidationFailed(Vec<ValidationError>),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("FQN parse error: {0}")]
FqnError(#[from] FqnError),
}
#[derive(Debug, Error, Clone)]
pub enum FqnError {
#[error("Invalid URL scheme: expected '{expected}', found '{found}'")]
InvalidScheme {
expected: &'static str,
found: String,
},
#[error("URL must use HTTPS scheme: {url}")]
NotHttps { url: String },
#[error("FQN missing required /attr/ structure: {url}")]
MissingAttrStructure { url: String },
#[error("Invalid namespace '{namespace}': {reason}")]
InvalidNamespace { namespace: String, reason: String },
#[error("Namespace not registered: {namespace}")]
NamespaceNotRegistered { namespace: String },
#[error("Malformed URL: {0}")]
MalformedUrl(String),
#[error("Missing required component: {component}")]
MissingComponent { component: &'static str },
}
impl FqnError {
pub fn kind(&self) -> FqnErrorKind {
match self {
FqnError::InvalidScheme { .. } => FqnErrorKind::InvalidScheme,
FqnError::NotHttps { .. } => FqnErrorKind::NotHttps,
FqnError::MissingAttrStructure { .. } => FqnErrorKind::MissingAttrStructure,
FqnError::InvalidNamespace { .. } => FqnErrorKind::InvalidNamespace,
FqnError::NamespaceNotRegistered { .. } => FqnErrorKind::NamespaceNotRegistered,
FqnError::MalformedUrl(_) => FqnErrorKind::MalformedUrl,
FqnError::MissingComponent { .. } => FqnErrorKind::MissingComponent,
}
}
pub fn hint(&self) -> &'static str {
match self {
FqnError::InvalidScheme { expected, .. } => {
if *expected == "https" {
"Use HTTPS scheme: https://example.com/attr/name/value/val"
} else {
"Use correct URL scheme"
}
}
FqnError::NotHttps { .. } => {
"FQNs must use HTTPS for security. Example: https://example.com/attr/name/value/val"
}
FqnError::MissingAttrStructure { .. } => {
"FQN must follow format: https://<namespace>/attr/<name>/value/<value>"
}
FqnError::InvalidNamespace { .. } => {
"Namespace must be a valid domain-like identifier (lowercase, no special chars)"
}
FqnError::NamespaceNotRegistered { .. } => {
"Register the namespace using NamespaceRegistry::register() before use"
}
FqnError::MalformedUrl(_) => "Ensure URL is properly formatted with scheme://host/path",
FqnError::MissingComponent { component } => match *component {
"namespace" => "Provide a namespace: https://namespace.com/...",
"name" => "Provide an attribute name: .../attr/name/...",
_ => "Ensure all required FQN components are present",
},
}
}
pub fn error_code(&self) -> &'static str {
match self {
FqnError::InvalidScheme { .. } => "OPENTDF_E_FQN_SCHEME_INVALID",
FqnError::NotHttps { .. } => "OPENTDF_E_FQN_NOT_HTTPS",
FqnError::MissingAttrStructure { .. } => "OPENTDF_E_FQN_STRUCTURE",
FqnError::InvalidNamespace { .. } => "OPENTDF_E_FQN_NAMESPACE_INVALID",
FqnError::NamespaceNotRegistered { .. } => "OPENTDF_E_FQN_NAMESPACE_UNREGISTERED",
FqnError::MalformedUrl(_) => "OPENTDF_E_FQN_MALFORMED",
FqnError::MissingComponent { .. } => "OPENTDF_E_FQN_COMPONENT_MISSING",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FqnErrorKind {
InvalidScheme,
NotHttps,
MissingAttrStructure,
InvalidNamespace,
NamespaceNotRegistered,
MalformedUrl,
MissingComponent,
}
impl PolicyError {
pub fn is_retryable(&self) -> bool {
matches!(self, PolicyError::SerializationError(_))
}
pub fn suggestion(&self) -> Option<&str> {
match self {
PolicyError::InvalidAttribute { .. } => {
Some("Use AttributeFqn::parse() to validate FQN format")
}
PolicyError::ValidationFailed(errors) if !errors.is_empty() => {
errors[0].suggestion.as_deref()
}
PolicyError::FqnError(FqnError::NotHttps { .. }) => Some(
"Use HTTPS URLs for attribute FQNs (e.g., https://example.com/attr/name/value/val)",
),
PolicyError::FqnError(FqnError::MissingAttrStructure { .. }) => {
Some("FQN must follow format: https://<namespace>/attr/<name>/value/<value>")
}
_ => None,
}
}
pub fn error_code(&self) -> &'static str {
match self {
PolicyError::InvalidAttribute { .. } => "OPENTDF_E_POLICY_ATTRIBUTE_INVALID",
PolicyError::InvalidOperator { .. } => "OPENTDF_E_POLICY_OPERATOR_INVALID",
PolicyError::InvalidValueType { .. } => "OPENTDF_E_POLICY_VALUE_TYPE",
PolicyError::EvaluationError { .. } => "OPENTDF_E_POLICY_EVALUATION",
PolicyError::ValidationFailed(_) => "OPENTDF_E_POLICY_VALIDATION",
PolicyError::SerializationError(_) => "OPENTDF_E_POLICY_SERIALIZATION",
PolicyError::FqnError(_) => "OPENTDF_E_FQN",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AttributeIdentifier {
pub namespace: String,
pub name: String,
}
impl AttributeIdentifier {
pub fn new(namespace: impl Into<String>, name: impl Into<String>) -> Self {
Self {
namespace: namespace.into(),
name: name.into(),
}
}
pub fn from_string(s: &str) -> Result<Self, PolicyError> {
let parts: Vec<&str> = s.split(':').collect();
if parts.len() != 2 {
return Err(PolicyError::InvalidAttribute {
fqn: s.to_string(),
reason: "Attribute must be in format 'namespace:name'".to_string(),
});
}
Ok(Self::new(parts[0], parts[1]))
}
pub fn as_string(&self) -> String {
format!("{}:{}", self.namespace, self.name)
}
}
impl fmt::Display for AttributeIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.namespace, self.name)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AttributeValue {
String(String),
Number(f64),
Boolean(bool),
DateTime(DateTime<Utc>),
StringArray(Vec<String>),
NumberArray(Vec<f64>),
}
impl AttributeValue {
pub fn as_string(&self) -> Option<&str> {
match self {
AttributeValue::String(s) => Some(s),
_ => None,
}
}
pub fn as_number(&self) -> Option<f64> {
match self {
AttributeValue::Number(n) => Some(*n),
_ => None,
}
}
pub fn as_boolean(&self) -> Option<bool> {
match self {
AttributeValue::Boolean(b) => Some(*b),
_ => None,
}
}
pub fn as_datetime(&self) -> Option<&DateTime<Utc>> {
match self {
AttributeValue::DateTime(dt) => Some(dt),
_ => None,
}
}
pub fn as_string_array(&self) -> Option<&[String]> {
match self {
AttributeValue::StringArray(arr) => Some(arr),
_ => None,
}
}
pub fn as_number_array(&self) -> Option<&[f64]> {
match self {
AttributeValue::NumberArray(arr) => Some(arr),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Operator {
Equals,
NotEquals,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual,
Contains,
In,
AllOf,
AnyOf,
NotIn,
MinimumOf, MaximumOf, Present, NotPresent, }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AttributeCondition {
pub attribute: AttributeIdentifier,
pub operator: Operator,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<AttributeValue>,
}
impl AttributeCondition {
pub fn new(
attribute: AttributeIdentifier,
operator: Operator,
value: Option<AttributeValue>,
) -> Self {
Self {
attribute,
operator,
value,
}
}
pub fn present(attribute: AttributeIdentifier) -> Self {
Self::new(attribute, Operator::Present, None)
}
pub fn equals(attribute: AttributeIdentifier, value: AttributeValue) -> Self {
Self::new(attribute, Operator::Equals, Some(value))
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE", tag = "type")]
pub enum LogicalOperator {
AND { conditions: Vec<AttributePolicy> },
OR { conditions: Vec<AttributePolicy> },
NOT { condition: Box<AttributePolicy> },
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AttributePolicy {
Condition(AttributeCondition),
Logical(LogicalOperator),
}
impl AttributePolicy {
pub fn condition(
attribute: AttributeIdentifier,
operator: Operator,
value: AttributeValue,
) -> Self {
AttributePolicy::Condition(AttributeCondition::new(attribute, operator, Some(value)))
}
pub fn and(conditions: Vec<AttributePolicy>) -> Self {
AttributePolicy::Logical(LogicalOperator::AND { conditions })
}
pub fn or(conditions: Vec<AttributePolicy>) -> Self {
AttributePolicy::Logical(LogicalOperator::OR { conditions })
}
pub fn create_not(condition: AttributePolicy) -> Self {
AttributePolicy::Logical(LogicalOperator::NOT {
condition: Box::new(condition),
})
}
}
impl Not for AttributePolicy {
type Output = Self;
fn not(self) -> Self::Output {
AttributePolicy::Logical(LogicalOperator::NOT {
condition: Box::new(self),
})
}
}
impl AttributePolicy {
pub fn evaluate(
&self,
attributes: &HashMap<AttributeIdentifier, AttributeValue>,
) -> Result<bool, PolicyError> {
if self.depth() > 50 {
self.evaluate_iterative(attributes)
} else {
self.evaluate_recursive(attributes)
}
}
pub fn depth(&self) -> usize {
match self {
AttributePolicy::Condition(_) => 1,
AttributePolicy::Logical(operator) => match operator {
LogicalOperator::AND { conditions } | LogicalOperator::OR { conditions } => {
1 + conditions.iter().map(|c| c.depth()).max().unwrap_or(0)
}
LogicalOperator::NOT { condition } => 1 + condition.depth(),
},
}
}
fn evaluate_recursive(
&self,
attributes: &HashMap<AttributeIdentifier, AttributeValue>,
) -> Result<bool, PolicyError> {
match self {
AttributePolicy::Condition(condition) => evaluate_condition(condition, attributes),
AttributePolicy::Logical(operator) => match operator {
LogicalOperator::AND { conditions } => {
for condition in conditions {
if !condition.evaluate_recursive(attributes)? {
return Ok(false);
}
}
Ok(true)
}
LogicalOperator::OR { conditions } => {
for condition in conditions {
if condition.evaluate_recursive(attributes)? {
return Ok(true);
}
}
Ok(false)
}
LogicalOperator::NOT { condition } => {
Ok(!condition.evaluate_recursive(attributes)?)
}
},
}
}
fn evaluate_iterative(
&self,
attributes: &HashMap<AttributeIdentifier, AttributeValue>,
) -> Result<bool, PolicyError> {
enum EvalTask<'a> {
Evaluate(&'a AttributePolicy),
EvaluateAnd(&'a [AttributePolicy], usize), EvaluateOr(&'a [AttributePolicy], usize), ApplyNot,
}
let mut results = Vec::new();
let mut tasks = vec![EvalTask::Evaluate(self)];
while let Some(task) = tasks.pop() {
match task {
EvalTask::Evaluate(policy) => match policy {
AttributePolicy::Condition(condition) => {
let result = evaluate_condition(condition, attributes)?;
results.push(result);
}
AttributePolicy::Logical(LogicalOperator::AND { conditions }) => {
if conditions.is_empty() {
results.push(true); } else {
tasks.push(EvalTask::EvaluateAnd(conditions, 0));
}
}
AttributePolicy::Logical(LogicalOperator::OR { conditions }) => {
if conditions.is_empty() {
results.push(false); } else {
tasks.push(EvalTask::EvaluateOr(conditions, 0));
}
}
AttributePolicy::Logical(LogicalOperator::NOT { condition }) => {
tasks.push(EvalTask::ApplyNot);
tasks.push(EvalTask::Evaluate(condition));
}
},
EvalTask::EvaluateAnd(conditions, idx) => {
if idx == 0 {
results.push(true);
}
let current_result = *results.last().unwrap();
if !current_result {
continue;
}
if idx < conditions.len() {
tasks.push(EvalTask::EvaluateAnd(conditions, idx + 1));
tasks.push(EvalTask::Evaluate(&conditions[idx]));
results.pop();
}
}
EvalTask::EvaluateOr(conditions, idx) => {
if idx == 0 {
results.push(false);
}
let current_result = *results.last().unwrap();
if current_result {
continue;
}
if idx < conditions.len() {
tasks.push(EvalTask::EvaluateOr(conditions, idx + 1));
tasks.push(EvalTask::Evaluate(&conditions[idx]));
results.pop();
}
}
EvalTask::ApplyNot => {
let result = results.pop().unwrap();
results.push(!result);
}
}
}
if results.len() != 1 {
return Err(PolicyError::EvaluationError {
reason: "Invalid policy evaluation state".to_string(),
attribute: None,
});
}
Ok(results[0])
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Policy {
pub uuid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub valid_from: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub valid_to: Option<DateTime<Utc>>,
pub body: PolicyBody,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PolicyBody {
#[serde(
rename = "dataAttributes",
default,
serialize_with = "serialize_empty_vec_as_null",
deserialize_with = "deserialize_null_as_empty_vec"
)]
pub attributes: Vec<AttributePolicy>,
#[serde(
default,
serialize_with = "serialize_empty_vec_as_null",
deserialize_with = "deserialize_null_as_empty_vec"
)]
pub dissem: Vec<String>, }
fn serialize_empty_vec_as_null<T, S>(vec: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>
where
T: serde::Serialize,
S: serde::Serializer,
{
if vec.is_empty() {
serializer.serialize_none()
} else {
vec.serialize(serializer)
}
}
fn deserialize_null_as_empty_vec<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
T: serde::Deserialize<'de>,
D: serde::Deserializer<'de>,
{
let opt = Option::<Vec<T>>::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}
impl Policy {
pub fn new(uuid: String, attributes: Vec<AttributePolicy>, dissem: Vec<String>) -> Self {
Self {
uuid,
valid_from: None,
valid_to: None,
body: PolicyBody { attributes, dissem },
}
}
pub fn with_time_window(
mut self,
valid_from: Option<DateTime<Utc>>,
valid_to: Option<DateTime<Utc>>,
) -> Self {
self.valid_from = valid_from;
self.valid_to = valid_to;
self
}
pub fn to_json(&self) -> Result<String, PolicyError> {
serde_json::to_string(self).map_err(PolicyError::from)
}
pub fn from_json(json: &str) -> Result<Self, PolicyError> {
serde_json::from_str(json).map_err(PolicyError::from)
}
pub fn is_valid_at(&self, time: DateTime<Utc>) -> bool {
let after_start = self.valid_from.is_none_or(|from| time >= from);
let before_end = self.valid_to.is_none_or(|to| time <= to);
after_start && before_end
}
pub fn validate(&self) -> Result<(), PolicyError> {
let mut errors = Vec::new();
if let Err(e) = uuid::Uuid::parse_str(&self.uuid) {
errors.push(ValidationError {
field: "uuid".to_string(),
error_type: ValidationErrorType::InvalidFormat,
message: format!("Invalid UUID format: {}", e),
suggestion: Some(
"Use PolicyBuilder::id_auto() to generate a valid UUID".to_string(),
),
});
}
if let (Some(from), Some(to)) = (self.valid_from, self.valid_to)
&& from >= to
{
errors.push(ValidationError {
field: "time_window".to_string(),
error_type: ValidationErrorType::OutOfRange,
message: format!("valid_from ({}) must be before valid_to ({})", from, to),
suggestion: Some("Ensure valid_from is earlier than valid_to".to_string()),
});
}
for (idx, attr_policy) in self.body.attributes.iter().enumerate() {
if let Err(e) = validate_policy(attr_policy) {
errors.push(ValidationError {
field: format!("attributes[{}]", idx),
error_type: ValidationErrorType::InvalidFormat,
message: format!("Invalid attribute policy: {}", e),
suggestion: None,
});
}
}
if self.body.dissem.is_empty() && !self.body.attributes.is_empty() {
errors.push(ValidationError {
field: "dissem".to_string(),
error_type: ValidationErrorType::EmptyList,
message: "Dissemination list is empty but attributes are defined".to_string(),
suggestion: Some(
"Add at least one dissemination entity or remove all attributes".to_string(),
),
});
}
let mut seen = std::collections::HashSet::new();
for (idx, entity) in self.body.dissem.iter().enumerate() {
if entity.trim().is_empty() {
errors.push(ValidationError {
field: format!("dissem[{}]", idx),
error_type: ValidationErrorType::InvalidFormat,
message: "Empty dissemination entity".to_string(),
suggestion: Some("Remove empty entries from dissemination list".to_string()),
});
} else if !seen.insert(entity) {
errors.push(ValidationError {
field: format!("dissem[{}]", idx),
error_type: ValidationErrorType::InvalidFormat,
message: format!("Duplicate dissemination entity: {}", entity),
suggestion: Some(
"Remove duplicate entries from dissemination list".to_string(),
),
});
}
}
if errors.is_empty() {
Ok(())
} else {
Err(PolicyError::ValidationFailed(errors))
}
}
pub fn evaluate(
&self,
attributes: &HashMap<AttributeIdentifier, AttributeValue>,
) -> Result<bool, PolicyError> {
for policy in &self.body.attributes {
validate_policy(policy)?;
}
if !self.is_valid_at(Utc::now()) {
return Ok(false);
}
if self.body.attributes.is_empty() {
return Ok(true);
}
for policy in &self.body.attributes {
if !policy.evaluate(attributes)? {
return Ok(false);
}
}
Ok(true)
}
pub fn has_attribute(&self, fqn: &crate::fqn::AttributeFqn) -> bool {
self.find_attribute(fqn).is_some()
}
pub fn find_attribute(&self, fqn: &crate::fqn::AttributeFqn) -> Option<&AttributeCondition> {
let mut pending: Vec<&AttributePolicy> = self.body.attributes.iter().rev().collect();
while let Some(node) = pending.pop() {
match node {
AttributePolicy::Condition(condition) => {
if condition_matches_fqn(condition, fqn) {
return Some(condition);
}
}
AttributePolicy::Logical(
LogicalOperator::AND { conditions } | LogicalOperator::OR { conditions },
) => pending.extend(conditions.iter().rev()),
AttributePolicy::Logical(LogicalOperator::NOT { condition }) => {
pending.push(condition);
}
}
}
None
}
}
fn condition_matches_fqn(condition: &AttributeCondition, fqn: &crate::fqn::AttributeFqn) -> bool {
if condition.attribute.namespace.to_lowercase() != fqn.get_namespace() {
return false;
}
let wanted_value = fqn.get_value();
if fqn.get_name().is_empty() && wanted_value.is_none() {
return true;
}
if condition.attribute.name != fqn.get_name() {
return false;
}
match wanted_value {
None => true,
Some(wanted) => {
condition.operator == Operator::Equals
&& matches!(&condition.value, Some(AttributeValue::String(value)) if value == wanted)
}
}
}
#[derive(Debug, Default)]
pub struct PolicyBuilder {
uuid: Option<String>,
valid_from: Option<DateTime<Utc>>,
valid_to: Option<DateTime<Utc>>,
attributes: Vec<AttributePolicy>,
dissem: Vec<String>,
}
impl PolicyBuilder {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn id(mut self, uuid: impl Into<String>) -> Self {
self.uuid = Some(uuid.into());
self
}
#[must_use]
pub fn id_auto(mut self) -> Self {
self.uuid = Some(uuid::Uuid::new_v4().to_string());
self
}
#[must_use]
pub fn attribute(mut self, policy: AttributePolicy) -> Self {
self.attributes.push(policy);
self
}
pub fn attribute_fqn(mut self, fqn: impl AsRef<str>) -> Result<Self, PolicyError> {
use crate::fqn::AttributeFqn;
let parsed = AttributeFqn::parse(fqn.as_ref())?;
let identifier = parsed.to_identifier();
if let Some(value) = parsed.get_value() {
self.attributes
.push(AttributePolicy::Condition(AttributeCondition {
attribute: identifier,
operator: Operator::Equals,
value: Some(AttributeValue::String(value.to_string())),
}));
} else {
self.attributes
.push(AttributePolicy::Condition(AttributeCondition {
attribute: identifier,
operator: Operator::Present,
value: None,
}));
}
Ok(self)
}
#[must_use]
pub fn attributes(mut self, policies: impl IntoIterator<Item = AttributePolicy>) -> Self {
self.attributes.extend(policies);
self
}
#[must_use]
pub fn dissem(mut self, target: impl Into<String>) -> Self {
self.dissem.push(target.into());
self
}
#[must_use]
pub fn dissemination(mut self, targets: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.dissem.extend(targets.into_iter().map(|t| t.into()));
self
}
#[must_use]
pub fn valid_from(mut self, time: DateTime<Utc>) -> Self {
self.valid_from = Some(time);
self
}
#[must_use]
pub fn valid_to(mut self, time: DateTime<Utc>) -> Self {
self.valid_to = Some(time);
self
}
#[must_use]
pub fn valid_window(mut self, from: DateTime<Utc>, to: DateTime<Utc>) -> Self {
self.valid_from = Some(from);
self.valid_to = Some(to);
self
}
#[must_use]
pub fn valid_for(mut self, duration: chrono::Duration) -> Self {
let now = Utc::now();
self.valid_from = Some(now);
self.valid_to = Some(now + duration);
self
}
#[must_use]
pub fn valid_for_days(self, days: i64) -> Self {
self.valid_for(chrono::Duration::days(days))
}
pub fn build(self) -> Result<Policy, PolicyError> {
let uuid = self
.uuid
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
Ok(Policy {
uuid,
valid_from: self.valid_from,
valid_to: self.valid_to,
body: PolicyBody {
attributes: self.attributes,
dissem: self.dissem,
},
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ClearanceHierarchy {
pub levels: HashMap<String, i32>,
}
impl Default for ClearanceHierarchy {
fn default() -> Self {
let mut levels = HashMap::new();
levels.insert("TOP_SECRET".to_string(), 4);
levels.insert("SECRET".to_string(), 3);
levels.insert("CONFIDENTIAL".to_string(), 2);
levels.insert("PUBLIC".to_string(), 1);
Self { levels }
}
}
pub static CLEARANCE_HIERARCHY: std::sync::OnceLock<ClearanceHierarchy> =
std::sync::OnceLock::new();
fn get_clearance_hierarchy() -> &'static ClearanceHierarchy {
CLEARANCE_HIERARCHY.get_or_init(|| {
ClearanceHierarchy::default()
})
}
pub fn validate_policy(policy: &AttributePolicy) -> Result<(), PolicyError> {
match policy {
AttributePolicy::Condition(condition) => validate_condition(condition),
AttributePolicy::Logical(operator) => match operator {
LogicalOperator::AND { conditions } | LogicalOperator::OR { conditions } => {
for condition in conditions {
validate_policy(condition)?;
}
Ok(())
}
LogicalOperator::NOT { condition } => validate_policy(condition),
},
}
}
fn validate_condition(condition: &AttributeCondition) -> Result<(), PolicyError> {
match condition.operator {
Operator::Present | Operator::NotPresent => return Ok(()),
_ => {}
}
if condition.value.is_none() {
return Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "a value".to_string(),
actual: "None".to_string(),
});
}
if let Some(value) = &condition.value {
match condition.operator {
Operator::AllOf | Operator::AnyOf => {
if !matches!(value, AttributeValue::StringArray(_)) {
return Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string array".to_string(),
actual: format!("{:?}", value),
});
}
}
Operator::In | Operator::NotIn => {
if !matches!(
value,
AttributeValue::StringArray(_) | AttributeValue::NumberArray(_)
) {
return Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "array (string or number)".to_string(),
actual: format!("{:?}", value),
});
}
}
Operator::Contains => {
if !matches!(value, AttributeValue::String(_)) {
return Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string".to_string(),
actual: format!("{:?}", value),
});
}
}
Operator::GreaterThan
| Operator::GreaterThanOrEqual
| Operator::LessThan
| Operator::LessThanOrEqual => {
if !matches!(
value,
AttributeValue::Number(_) | AttributeValue::DateTime(_)
) {
return Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "number or datetime".to_string(),
actual: format!("{:?}", value),
});
}
}
Operator::Equals
| Operator::NotEquals
| Operator::MinimumOf
| Operator::MaximumOf
| Operator::Present
| Operator::NotPresent => {}
}
}
Ok(())
}
fn evaluate_condition(
condition: &AttributeCondition,
attributes: &HashMap<AttributeIdentifier, AttributeValue>,
) -> Result<bool, PolicyError> {
validate_condition(condition)?;
match condition.operator {
Operator::Present => return Ok(attributes.contains_key(&condition.attribute)),
Operator::NotPresent => return Ok(!attributes.contains_key(&condition.attribute)),
_ => {}
}
let attr_value = match attributes.get(&condition.attribute) {
Some(value) => value,
None => return Ok(false), };
let expected_value = match &condition.value {
Some(value) => value,
None => {
return Err(PolicyError::EvaluationError {
reason: "Value required for this operator".to_string(),
attribute: Some(condition.attribute.as_string()),
});
}
};
match condition.operator {
Operator::Equals => Ok(compare_values(attr_value, expected_value, |a, b| a == b)?),
Operator::NotEquals => Ok(compare_values(attr_value, expected_value, |a, b| a != b)?),
Operator::GreaterThan => Ok(compare_numeric(attr_value, expected_value, |a, b| a > b)?),
Operator::GreaterThanOrEqual => {
Ok(compare_numeric(attr_value, expected_value, |a, b| a >= b)?)
}
Operator::LessThan => Ok(compare_numeric(attr_value, expected_value, |a, b| a < b)?),
Operator::LessThanOrEqual => {
Ok(compare_numeric(attr_value, expected_value, |a, b| a <= b)?)
}
Operator::Contains => match (attr_value, expected_value) {
(AttributeValue::String(haystack), AttributeValue::String(needle)) => {
Ok(haystack.contains(needle.as_str()))
}
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string values".to_string(),
actual: format!("attr={:?}, value={:?}", attr_value, expected_value),
}),
},
Operator::In => match expected_value {
AttributeValue::StringArray(values) => match attr_value {
AttributeValue::String(s) => Ok(values.contains(s)),
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string".to_string(),
actual: format!("{:?}", attr_value),
}),
},
AttributeValue::NumberArray(values) => match attr_value {
AttributeValue::Number(n) => Ok(values.contains(n)),
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "number".to_string(),
actual: format!("{:?}", attr_value),
}),
},
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "array value".to_string(),
actual: format!("{:?}", expected_value),
}),
},
Operator::NotIn => match expected_value {
AttributeValue::StringArray(values) => match attr_value {
AttributeValue::String(s) => Ok(!values.contains(s)),
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string".to_string(),
actual: format!("{:?}", attr_value),
}),
},
AttributeValue::NumberArray(values) => match attr_value {
AttributeValue::Number(n) => Ok(!values.contains(n)),
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "number".to_string(),
actual: format!("{:?}", attr_value),
}),
},
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "array value".to_string(),
actual: format!("{:?}", expected_value),
}),
},
Operator::MinimumOf => {
match (attr_value, expected_value) {
(AttributeValue::String(a), AttributeValue::String(b)) => {
if a == b {
return Ok(true);
}
let a_upper = a.to_uppercase();
let b_upper = b.to_uppercase();
let hierarchy = get_clearance_hierarchy();
let level_a = hierarchy.levels.get(&a_upper).copied().unwrap_or(0);
let level_b = hierarchy.levels.get(&b_upper).copied().unwrap_or(0);
Ok(level_a >= level_b)
}
_ => compare_numeric(attr_value, expected_value, |a, b| a >= b),
}
}
Operator::MaximumOf => {
match (attr_value, expected_value) {
(AttributeValue::String(a), AttributeValue::String(b)) => {
if a == b {
return Ok(true);
}
let a_upper = a.to_uppercase();
let b_upper = b.to_uppercase();
let hierarchy = get_clearance_hierarchy();
let level_a = hierarchy.levels.get(&a_upper).copied().unwrap_or(0);
let level_b = hierarchy.levels.get(&b_upper).copied().unwrap_or(0);
Ok(level_a <= level_b)
}
_ => compare_numeric(attr_value, expected_value, |a, b| a <= b),
}
}
Operator::AllOf => match (expected_value, attr_value) {
(AttributeValue::StringArray(required), AttributeValue::StringArray(actual)) => {
Ok(required.iter().all(|r| actual.contains(r)))
}
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string arrays".to_string(),
actual: format!("expected={:?}, actual={:?}", expected_value, attr_value),
}),
},
Operator::AnyOf => match (expected_value, attr_value) {
(AttributeValue::StringArray(required), AttributeValue::StringArray(actual)) => {
Ok(required.iter().any(|r| actual.contains(r)))
}
_ => Err(PolicyError::InvalidValueType {
attribute: condition.attribute.as_string(),
expected: "string arrays".to_string(),
actual: format!("expected={:?}, actual={:?}", expected_value, attr_value),
}),
},
Operator::Present | Operator::NotPresent => {
unreachable!("These operators should be handled earlier")
}
}
}
fn compare_values<F>(
actual: &AttributeValue,
expected: &AttributeValue,
compare: F,
) -> Result<bool, PolicyError>
where
F: Fn(&AttributeValue, &AttributeValue) -> bool,
{
Ok(compare(actual, expected))
}
fn compare_numeric<F>(
actual: &AttributeValue,
expected: &AttributeValue,
compare: F,
) -> Result<bool, PolicyError>
where
F: Fn(f64, f64) -> bool,
{
match (actual, expected) {
(AttributeValue::Number(a), AttributeValue::Number(b)) => Ok(compare(*a, *b)),
(AttributeValue::DateTime(a), AttributeValue::DateTime(b)) => {
Ok(compare(a.timestamp() as f64, b.timestamp() as f64))
}
_ => Err(PolicyError::InvalidValueType {
attribute: "unknown".to_string(),
expected: "number or datetime".to_string(),
actual: format!("actual={:?}, expected={:?}", actual, expected),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Duration, TimeZone};
#[test]
fn test_attribute_identifier_parsing() {
let attr = AttributeIdentifier::from_string("clearance:SECRET").unwrap();
assert_eq!(attr.namespace, "clearance");
assert_eq!(attr.name, "SECRET");
let result = AttributeIdentifier::from_string("invalid_format");
assert!(result.is_err());
}
#[test]
fn test_simple_equals_condition() {
let attr_id = AttributeIdentifier::new("clearance", "level");
let condition = AttributeCondition::equals(
attr_id.clone(),
AttributeValue::String("SECRET".to_string()),
);
let mut attributes = HashMap::new();
attributes.insert(
attr_id.clone(),
AttributeValue::String("SECRET".to_string()),
);
let policy = AttributePolicy::Condition(condition);
assert!(policy.evaluate(&attributes).unwrap());
attributes.insert(attr_id, AttributeValue::String("CONFIDENTIAL".to_string()));
assert!(!policy.evaluate(&attributes).unwrap());
}
#[test]
fn test_logical_operators() {
let dept_attr = AttributeIdentifier::new("department", "name");
let level_attr = AttributeIdentifier::new("clearance", "level");
let dept_condition = AttributePolicy::condition(
dept_attr.clone(),
Operator::Equals,
AttributeValue::String("FINANCE".to_string()),
);
let level_condition = AttributePolicy::condition(
level_attr.clone(),
Operator::MinimumOf,
AttributeValue::String("CONFIDENTIAL".to_string()),
);
let and_policy =
AttributePolicy::and(vec![dept_condition.clone(), level_condition.clone()]);
let or_policy = AttributePolicy::or(vec![dept_condition.clone(), level_condition.clone()]);
let mut attributes = HashMap::new();
attributes.insert(
dept_attr.clone(),
AttributeValue::String("FINANCE".to_string()),
);
attributes.insert(
level_attr.clone(),
AttributeValue::String("SECRET".to_string()),
);
assert!(and_policy.evaluate(&attributes).unwrap());
assert!(or_policy.evaluate(&attributes).unwrap());
attributes.insert(
dept_attr.clone(),
AttributeValue::String("ENGINEERING".to_string()),
);
assert!(!and_policy.evaluate(&attributes).unwrap());
assert!(or_policy.evaluate(&attributes).unwrap());
let level_exact_match = AttributePolicy::condition(
level_attr.clone(),
Operator::Equals, AttributeValue::String("CONFIDENTIAL".to_string()),
);
let and_policy_exact =
AttributePolicy::and(vec![dept_condition.clone(), level_exact_match.clone()]);
let or_policy_exact =
AttributePolicy::or(vec![dept_condition.clone(), level_exact_match.clone()]);
attributes.insert(level_attr, AttributeValue::String("PUBLIC".to_string()));
assert!(!and_policy_exact.evaluate(&attributes).unwrap());
assert!(!or_policy_exact.evaluate(&attributes).unwrap());
let not_policy = !dept_condition.clone();
assert!(not_policy.evaluate(&attributes).unwrap());
attributes.insert(dept_attr, AttributeValue::String("FINANCE".to_string()));
assert!(!not_policy.evaluate(&attributes).unwrap());
}
#[test]
fn test_array_operators() {
let locations = AttributeIdentifier::new("location", "allowed");
let in_condition = AttributePolicy::condition(
locations.clone(),
Operator::In,
AttributeValue::StringArray(vec![
"USA".to_string(),
"CANADA".to_string(),
"UK".to_string(),
]),
);
let mut attributes = HashMap::new();
attributes.insert(locations.clone(), AttributeValue::String("USA".to_string()));
assert!(in_condition.evaluate(&attributes).unwrap());
attributes.insert(
locations.clone(),
AttributeValue::String("FRANCE".to_string()),
);
assert!(!in_condition.evaluate(&attributes).unwrap());
let not_in_condition = AttributePolicy::condition(
locations.clone(),
Operator::NotIn,
AttributeValue::StringArray(vec!["CHINA".to_string(), "RUSSIA".to_string()]),
);
attributes.insert(locations, AttributeValue::String("USA".to_string()));
assert!(not_in_condition.evaluate(&attributes).unwrap());
}
#[test]
fn test_time_based_policy() {
let now = Utc::now();
let tomorrow = now + Duration::days(1);
let yesterday = now - Duration::days(1);
let mut policy = Policy::new(
"test-uuid".to_string(),
vec![],
vec!["user@example.com".to_string()],
);
assert!(policy.is_valid_at(now));
policy = policy.with_time_window(Some(tomorrow), None);
assert!(!policy.is_valid_at(now));
policy = policy.with_time_window(Some(yesterday), None);
assert!(policy.is_valid_at(now));
policy = policy.with_time_window(Some(yesterday), Some(tomorrow));
assert!(policy.is_valid_at(now));
policy = policy.with_time_window(None, Some(yesterday));
assert!(!policy.is_valid_at(now));
}
#[test]
fn test_policy_serialization() {
let dept_attr = AttributeIdentifier::new("department", "name");
let level_attr = AttributeIdentifier::new("clearance", "level");
let dept_condition = AttributePolicy::condition(
dept_attr,
Operator::Equals,
AttributeValue::String("FINANCE".to_string()),
);
let level_condition = AttributePolicy::condition(
level_attr,
Operator::MinimumOf,
AttributeValue::String("CONFIDENTIAL".to_string()),
);
let policy = Policy::new(
"test-uuid".to_string(),
vec![dept_condition, level_condition],
vec!["user@example.com".to_string()],
);
let json = policy.to_json().unwrap();
let deserialized = Policy::from_json(&json).unwrap();
assert_eq!(policy.uuid, deserialized.uuid);
assert_eq!(policy.body.dissem, deserialized.body.dissem);
assert_eq!(
policy.body.attributes.len(),
deserialized.body.attributes.len()
);
}
#[test]
fn test_complex_policy_evaluation() {
let dept_attr = AttributeIdentifier::new("department", "name");
let level_attr = AttributeIdentifier::new("clearance", "level");
let region_attr = AttributeIdentifier::new("region", "code");
let finance_condition = AttributePolicy::condition(
dept_attr.clone(),
Operator::Equals,
AttributeValue::String("FINANCE".to_string()),
);
let legal_condition = AttributePolicy::condition(
dept_attr.clone(),
Operator::Equals,
AttributeValue::String("LEGAL".to_string()),
);
let clearance_condition = AttributePolicy::condition(
level_attr.clone(),
Operator::Equals,
AttributeValue::String("SECRET".to_string()),
);
let apac_condition = AttributePolicy::condition(
region_attr.clone(),
Operator::Equals,
AttributeValue::String("APAC".to_string()),
);
let dept_policy = AttributePolicy::or(vec![finance_condition, legal_condition]);
let not_apac = AttributePolicy::not(apac_condition);
let complex_policy = AttributePolicy::and(vec![dept_policy, clearance_condition, not_apac]);
let mut attributes = HashMap::new();
attributes.insert(
dept_attr.clone(),
AttributeValue::String("FINANCE".to_string()),
);
attributes.insert(
level_attr.clone(),
AttributeValue::String("SECRET".to_string()),
);
attributes.insert(
region_attr.clone(),
AttributeValue::String("EMEA".to_string()),
);
assert!(complex_policy.evaluate(&attributes).unwrap());
attributes.insert(
dept_attr.clone(),
AttributeValue::String("LEGAL".to_string()),
);
assert!(complex_policy.evaluate(&attributes).unwrap());
attributes.insert(
dept_attr.clone(),
AttributeValue::String("FINANCE".to_string()),
);
attributes.insert(
region_attr.clone(),
AttributeValue::String("APAC".to_string()),
);
assert!(!complex_policy.evaluate(&attributes).unwrap());
attributes.insert(dept_attr, AttributeValue::String("MARKETING".to_string()));
attributes.insert(region_attr, AttributeValue::String("EMEA".to_string()));
assert!(!complex_policy.evaluate(&attributes).unwrap());
attributes.insert(
level_attr,
AttributeValue::String("CONFIDENTIAL".to_string()),
);
assert!(!complex_policy.evaluate(&attributes).unwrap());
}
#[test]
fn test_datetime_comparison() {
let time_attr = AttributeIdentifier::new("access", "time");
let base_time = Utc.with_ymd_and_hms(2023, 4, 1, 9, 0, 0).unwrap();
let condition = AttributePolicy::condition(
time_attr.clone(),
Operator::GreaterThan,
AttributeValue::DateTime(base_time),
);
let mut attributes = HashMap::new();
let later = base_time + Duration::hours(1);
attributes.insert(time_attr.clone(), AttributeValue::DateTime(later));
assert!(condition.evaluate(&attributes).unwrap());
let earlier = base_time - Duration::hours(1);
attributes.insert(time_attr, AttributeValue::DateTime(earlier));
assert!(!condition.evaluate(&attributes).unwrap());
}
}
#[test]
fn test_empty_policy_serialization_compatibility() {
let policy = Policy::new("test-uuid".to_string(), vec![], vec![]);
let json_str = policy.to_json().unwrap();
println!("Serialized empty policy: {}", json_str);
assert!(
json_str.contains(r#""dataAttributes":null"#),
"Empty dataAttributes should serialize as null, got: {}",
json_str
);
assert!(
json_str.contains(r#""dissem":null"#),
"Empty dissem should serialize as null, got: {}",
json_str
);
let parsed = Policy::from_json(&json_str).unwrap();
assert_eq!(parsed.body.attributes.len(), 0);
assert_eq!(parsed.body.dissem.len(), 0);
}
#[test]
fn test_policy_validate_valid_policy() {
let policy = PolicyBuilder::new()
.id_auto()
.attribute_fqn("https://example.com/attr/clearance/value/secret")
.unwrap()
.dissemination(["user@example.com"])
.build()
.unwrap();
assert!(policy.validate().is_ok());
}
#[test]
fn test_policy_validate_invalid_uuid() {
let policy = Policy::new("not-a-valid-uuid".to_string(), vec![], vec![]);
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].field, "uuid");
assert_eq!(errors[0].error_type, ValidationErrorType::InvalidFormat);
assert!(errors[0].suggestion.is_some());
} else {
panic!("Expected ValidationFailed error");
}
}
#[test]
fn test_policy_validate_invalid_time_window() {
use chrono::Duration;
let now = Utc::now();
let future = now + Duration::days(7);
let past = now - Duration::days(7);
let mut policy = PolicyBuilder::new()
.id_auto()
.dissemination(["user@example.com"])
.build()
.unwrap();
policy.valid_from = Some(future);
policy.valid_to = Some(past);
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert!(errors.iter().any(|e| e.field == "time_window"));
assert!(
errors
.iter()
.any(|e| e.error_type == ValidationErrorType::OutOfRange)
);
} else {
panic!("Expected ValidationFailed error");
}
}
#[test]
fn test_policy_validate_empty_dissem_with_attributes() {
let attr_policy = AttributePolicy::condition(
AttributeIdentifier::new("example.com", "clearance"),
Operator::Equals,
AttributeValue::String("secret".to_string()),
);
let policy = Policy::new(uuid::Uuid::new_v4().to_string(), vec![attr_policy], vec![]);
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert!(errors.iter().any(|e| e.field == "dissem"));
assert!(
errors
.iter()
.any(|e| e.error_type == ValidationErrorType::EmptyList)
);
} else {
panic!("Expected ValidationFailed error");
}
}
#[test]
fn test_policy_validate_duplicate_dissem_entities() {
let policy = PolicyBuilder::new()
.id_auto()
.attribute_fqn("https://example.com/attr/clearance/value/secret")
.unwrap()
.dissemination(["user@example.com", "admin@example.com", "user@example.com"])
.build()
.unwrap();
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert!(errors.iter().any(|e| e.field.starts_with("dissem[")));
assert!(
errors
.iter()
.any(|e| e.message.contains("Duplicate dissemination entity"))
);
} else {
panic!("Expected ValidationFailed error");
}
}
#[test]
fn test_policy_validate_empty_dissem_entry() {
let mut policy = PolicyBuilder::new()
.id_auto()
.attribute_fqn("https://example.com/attr/clearance/value/secret")
.unwrap()
.dissemination(["user@example.com"])
.build()
.unwrap();
policy.body.dissem.push("".to_string());
policy.body.dissem.push(" ".to_string());
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert!(errors.len() >= 2); assert!(
errors
.iter()
.any(|e| e.message.contains("Empty dissemination entity"))
);
} else {
panic!("Expected ValidationFailed error");
}
}
#[test]
fn test_policy_validate_multiple_errors() {
let mut policy = Policy::new("invalid-uuid".to_string(), vec![], vec![]);
let now = Utc::now();
policy.valid_from = Some(now);
policy.valid_to = Some(now - chrono::Duration::days(1));
let result = policy.validate();
assert!(result.is_err());
if let Err(PolicyError::ValidationFailed(errors)) = result {
assert!(errors.len() >= 2);
assert!(errors.iter().any(|e| e.field == "uuid"));
assert!(errors.iter().any(|e| e.field == "time_window"));
} else {
panic!("Expected ValidationFailed error");
}
}
#[cfg(test)]
mod attribute_lookup_tests {
use super::*;
use crate::fqn::AttributeFqn;
const NS: &str = "example.com";
fn string(value: &str) -> AttributeValue {
AttributeValue::String(value.to_string())
}
fn equals(name: &str, value: &str) -> AttributeCondition {
AttributeCondition::equals(AttributeIdentifier::new(NS, name), string(value))
}
fn policy(attributes: Vec<AttributePolicy>) -> Policy {
Policy::new(
"00000000-0000-0000-0000-000000000000".to_string(),
attributes,
vec![],
)
}
fn fqn(url: &str) -> AttributeFqn {
AttributeFqn::parse(url).unwrap()
}
#[test]
fn finds_condition_built_from_the_same_fqn() {
let url = "https://example.com/attr/storage/value/permanent";
let policy = PolicyBuilder::new()
.id_auto()
.dissem("user@example.com")
.attribute_fqn(url)
.unwrap()
.build()
.unwrap();
assert!(policy.has_attribute(&fqn(url)));
assert_eq!(
policy.find_attribute(&fqn(url)),
Some(&equals("storage", "permanent"))
);
for miss in [
"https://example.com/attr/storage/value/temporary",
"https://example.com/attr/retention/value/permanent",
"https://other.example.com/attr/storage/value/permanent",
"https://example.com/attr/Storage/value/permanent",
"https://example.com/attr/storage/value/Permanent",
] {
assert!(!policy.has_attribute(&fqn(miss)), "{miss}");
assert_eq!(policy.find_attribute(&fqn(miss)), None, "{miss}");
}
}
#[test]
fn empty_policy_has_no_attributes() {
let policy = policy(vec![]);
assert!(!policy.has_attribute(&fqn("https://example.com")));
assert_eq!(
policy.find_attribute(&fqn("https://example.com/attr/storage")),
None
);
}
#[test]
fn attribute_fqn_without_value_matches_any_operator() {
let id = AttributeIdentifier::new(NS, "storage");
let conditions = [
AttributeCondition::present(id.clone()),
AttributeCondition::new(id.clone(), Operator::NotPresent, None),
AttributeCondition::new(id.clone(), Operator::NotEquals, Some(string("permanent"))),
AttributeCondition::new(
id.clone(),
Operator::In,
Some(AttributeValue::StringArray(vec!["permanent".to_string()])),
),
AttributeCondition::new(id, Operator::GreaterThan, Some(AttributeValue::Number(1.0))),
];
for condition in conditions {
let policy = policy(vec![AttributePolicy::Condition(condition.clone())]);
assert_eq!(
policy.find_attribute(&fqn("https://example.com/attr/storage")),
Some(&condition),
"{condition:?}"
);
assert!(!policy.has_attribute(&fqn("https://example.com/attr/retention")));
}
}
#[test]
fn value_fqn_matches_only_string_equality() {
let id = AttributeIdentifier::new(NS, "storage");
let wanted = fqn("https://example.com/attr/storage/value/permanent");
let mentions_but_does_not_require = [
AttributeCondition::present(id.clone()),
AttributeCondition::new(id.clone(), Operator::NotEquals, Some(string("permanent"))),
AttributeCondition::new(id.clone(), Operator::MinimumOf, Some(string("permanent"))),
AttributeCondition::new(id.clone(), Operator::Contains, Some(string("permanent"))),
AttributeCondition::new(
id.clone(),
Operator::In,
Some(AttributeValue::StringArray(vec!["permanent".to_string()])),
),
AttributeCondition::new(
id.clone(),
Operator::AnyOf,
Some(AttributeValue::StringArray(vec!["permanent".to_string()])),
),
AttributeCondition::new(
id.clone(),
Operator::Equals,
Some(AttributeValue::StringArray(vec!["permanent".to_string()])),
),
AttributeCondition::new(id.clone(), Operator::Equals, None),
];
for condition in mentions_but_does_not_require {
let policy = policy(vec![AttributePolicy::Condition(condition.clone())]);
assert!(!policy.has_attribute(&wanted), "{condition:?}");
}
let numeric = policy(vec![AttributePolicy::Condition(
AttributeCondition::equals(id, AttributeValue::Number(5.0)),
)]);
assert!(!numeric.has_attribute(&fqn("https://example.com/attr/storage/value/5")));
}
#[test]
fn namespace_fqn_matches_any_attribute_in_the_namespace() {
let policy = policy(vec![
AttributePolicy::Condition(equals("storage", "permanent")),
AttributePolicy::Condition(equals("retention", "7y")),
]);
assert_eq!(
policy.find_attribute(&fqn("https://example.com")),
Some(&equals("storage", "permanent"))
);
assert_eq!(
policy.find_attribute(&AttributeFqn::namespace(NS)),
Some(&equals("storage", "permanent"))
);
assert!(!policy.has_attribute(&fqn("https://other.example.com")));
assert!(!policy.has_attribute(&fqn("https://example.co")));
assert!(!policy.has_attribute(&fqn("https://sub.example.com")));
assert!(!policy.has_attribute(&AttributeFqn::with_value(NS, "", "permanent")));
}
#[test]
fn namespace_is_compared_case_insensitively() {
let condition = AttributeCondition::equals(
AttributeIdentifier::new("Example.COM", "storage"),
string("permanent"),
);
let policy = policy(vec![AttributePolicy::Condition(condition.clone())]);
for url in [
"https://example.com",
"https://EXAMPLE.com/attr/storage",
"https://example.com/attr/storage/value/permanent",
] {
assert_eq!(policy.find_attribute(&fqn(url)), Some(&condition), "{url}");
}
}
#[test]
fn finds_conditions_nested_in_logical_operators() {
let policy = policy(vec![
AttributePolicy::Condition(equals("top", "1")),
AttributePolicy::and(vec![
AttributePolicy::or(vec![
AttributePolicy::Condition(equals("in-or", "2")),
AttributePolicy::create_not(AttributePolicy::Condition(equals("in-not", "3"))),
]),
AttributePolicy::Condition(equals("in-and", "4")),
]),
]);
for (name, value) in [
("top", "1"),
("in-or", "2"),
("in-not", "3"),
("in-and", "4"),
] {
let url = format!("https://example.com/attr/{name}/value/{value}");
assert_eq!(
policy.find_attribute(&fqn(&url)),
Some(&equals(name, value)),
"{url}"
);
}
assert!(!policy.has_attribute(&fqn("https://example.com/attr/absent")));
}
#[test]
fn returns_the_first_match_in_document_order() {
let policy = policy(vec![
AttributePolicy::or(vec![
AttributePolicy::Condition(equals("other", "x")),
AttributePolicy::and(vec![AttributePolicy::Condition(equals(
"storage", "nested",
))]),
AttributePolicy::Condition(equals("storage", "sibling")),
]),
AttributePolicy::Condition(equals("storage", "top-level")),
]);
assert_eq!(
policy.find_attribute(&fqn("https://example.com/attr/storage")),
Some(&equals("storage", "nested"))
);
assert_eq!(
policy.find_attribute(&fqn("https://example.com/attr/storage/value/top-level")),
Some(&equals("storage", "top-level"))
);
}
#[test]
fn finds_attribute_in_policy_parsed_from_manifest_json() {
let json = r#"{
"uuid": "00000000-0000-0000-0000-000000000000",
"body": {
"dataAttributes": [{
"type": "AND",
"conditions": [{
"attribute": {"namespace": "example.com", "name": "storage"},
"operator": "equals",
"value": "permanent"
}]
}],
"dissem": null
}
}"#;
let policy = Policy::from_json(json).unwrap();
assert!(policy.has_attribute(&fqn("https://example.com/attr/storage/value/permanent")));
assert!(!policy.has_attribute(&fqn("https://example.com/attr/storage/value/other")));
}
#[test]
fn deeply_nested_policy_is_searched_without_recursion() {
let mut nested = AttributePolicy::Condition(equals("storage", "permanent"));
for _ in 0..2_000 {
nested = AttributePolicy::create_not(nested);
}
let policy = policy(vec![nested]);
assert!(policy.has_attribute(&fqn("https://example.com/attr/storage/value/permanent")));
assert!(!policy.has_attribute(&fqn("https://example.com/attr/absent")));
}
}