pub struct PolicyRule {
pub peer_id: String,
pub action: Action,
pub resource: Resource,
pub expires_at: Option<u64>,
pub attributes: BTreeMap<String, String>,
pub context_expr: Option<ContextExpr>,
}Expand description
A single policy rule with optional ABAC (Attribute-Based Access Control) features
Fields§
§peer_id: StringPeer ID that this rule applies to
action: ActionAction allowed by this rule
resource: ResourceResource this rule applies to
expires_at: Option<u64>Optional expiration timestamp (Unix seconds) If set, the rule is only valid before this time
attributes: BTreeMap<String, String>Optional context attributes for conditional access (legacy - simple key-value matching) Examples: {“location”: “office”, “security_level”: “high”}
Uses BTreeMap for deterministic serialization (cryptographic safety)
Note: This is the legacy ABAC mechanism. For complex boolean logic,
use context_expr instead.
context_expr: Option<ContextExpr>Optional context expression for advanced ABAC (boolean logic)
This provides more powerful conditional logic than simple attribute matching:
- Boolean operators: AND, OR, NOT
- Comparison operators: ==, !=, <, <=, >, >=
- Attribute existence checks: HAS
Examples:
role == "admin" AND department == "IT"(role == "admin" OR role == "moderator") AND active == "true"NOT (status == "banned")
When both attributes and context_expr are present, both must match.
Implementations§
Source§impl PolicyRule
impl PolicyRule
Sourcepub fn new(peer_id: String, action: Action, resource: Resource) -> Self
pub fn new(peer_id: String, action: Action, resource: Resource) -> Self
Create a new policy rule with basic RBAC
Sourcepub fn with_expiration(
peer_id: String,
action: Action,
resource: Resource,
expires_at: u64,
) -> Self
pub fn with_expiration( peer_id: String, action: Action, resource: Resource, expires_at: u64, ) -> Self
Create a new policy rule with expiration (ABAC)
Sourcepub const fn with_attributes(
peer_id: String,
action: Action,
resource: Resource,
attributes: BTreeMap<String, String>,
) -> Self
pub const fn with_attributes( peer_id: String, action: Action, resource: Resource, attributes: BTreeMap<String, String>, ) -> Self
Create a new policy rule with attributes (ABAC)
Sourcepub const fn expires_at(self, timestamp: u64) -> Self
pub const fn expires_at(self, timestamp: u64) -> Self
Add an expiration time to this rule
Sourcepub fn with_attribute(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_attribute( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Add an attribute to this rule
Sourcepub fn with_context_expr(self, expr: ContextExpr) -> Self
pub fn with_context_expr(self, expr: ContextExpr) -> Self
Add a context expression to this rule (advanced ABAC)
§Example
use core_policy::{PolicyRule, Action, Resource, ContextExpr};
let rule = PolicyRule::new("alice".into(), Action::Read, Resource::All)
.with_context_expr(ContextExpr::parse("role == \"admin\"").unwrap());Sourcepub fn is_expired(&self, current_time: u64) -> bool
pub fn is_expired(&self, current_time: u64) -> bool
Check if this rule has expired
Sourcepub fn matches_context(&self, context: &BTreeMap<String, String>) -> bool
pub fn matches_context(&self, context: &BTreeMap<String, String>) -> bool
Check if this rule’s attributes match the given context
This method evaluates both legacy attribute matching and the new context expression:
- If
attributesis non-empty, all attributes must match (legacy behavior) - If
context_expris present, it must evaluate to true - Both conditions must be satisfied if both are present
Returns true if all context constraints match.
Trait Implementations§
Source§impl Clone for PolicyRule
impl Clone for PolicyRule
Source§fn clone(&self) -> PolicyRule
fn clone(&self) -> PolicyRule
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more