pub struct PolicyEngine<M: ValidationMode = SchemaFree> { /* private fields */ }Implementations§
Source§impl PolicyEngine<SchemaFree>
impl PolicyEngine<SchemaFree>
pub fn new_from_str(policy_text: &str) -> Result<Self, PolicyError>
Sourcepub fn new_from_str_with_policy_stores(
policy_text: &str,
layout: PolicyStoreLayout,
) -> Result<Self, PolicyError>
pub fn new_from_str_with_policy_stores( policy_text: &str, layout: PolicyStoreLayout, ) -> Result<Self, PolicyError>
Create an engine that partitions policies into namespace-owned stores.
Existing monolithic constructors remain unchanged. Store assignment is validated before the engine is returned, and each request must resolve to exactly one declared store or evaluation fails closed.
Sourcepub fn new_from_str_with_schema(
policy_text: &str,
schema: Schema,
) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
pub fn new_from_str_with_schema( policy_text: &str, schema: Schema, ) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
Create a new policy engine with schema-based policy and request validation.
Sourcepub fn new_from_str_with_schema_and_policy_stores(
policy_text: &str,
schema: Schema,
layout: PolicyStoreLayout,
) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
pub fn new_from_str_with_schema_and_policy_stores( policy_text: &str, schema: Schema, layout: PolicyStoreLayout, ) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
Create a namespace-partitioned engine with schema validation.
Sourcepub fn new_from_str_with_cedarschema(
policy_text: &str,
schema_text: &str,
) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
pub fn new_from_str_with_cedarschema( policy_text: &str, schema_text: &str, ) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
Create a new policy engine from policy text and Cedar schema text.
Sourcepub fn new_from_str_with_cedarschema_and_policy_stores(
policy_text: &str,
schema_text: &str,
layout: PolicyStoreLayout,
) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
pub fn new_from_str_with_cedarschema_and_policy_stores( policy_text: &str, schema_text: &str, layout: PolicyStoreLayout, ) -> Result<PolicyEngine<SchemaEnforcing>, PolicyError>
Create a namespace-partitioned engine from policy and Cedar schema text.
Source§impl<M: ValidationMode> PolicyEngine<M>
impl<M: ValidationMode> PolicyEngine<M>
Sourcepub fn with_label_registry(self, registry: LabelRegistry) -> Self
pub fn with_label_registry(self, registry: LabelRegistry) -> Self
Create a new policy engine with a label registry.
This is a convenience method that combines new_from_str and with_label_registry.
Sourcepub fn set_label_registry(&self, registry: LabelRegistry)
pub fn set_label_registry(&self, registry: LabelRegistry)
Set or replace the label registry for this engine.
This allows updating the labelers after the engine has been created.
Sourcepub fn label_registry(&self) -> Option<LabelRegistry>
pub fn label_registry(&self) -> Option<LabelRegistry>
Clone the current immutable label registry, if one is configured.
pub fn reload_from_str(&self, policy_text: &str) -> Result<(), PolicyError>
Sourcepub fn current_version(&self) -> PolicyVersion
pub fn current_version(&self) -> PolicyVersion
Get the complete current authorization-state version.
The policy hash, policy load time, label-set version, and engine generation all come from the same atomic state load.
Sourcepub fn session(&self) -> EvaluationSession<M>
pub fn session(&self) -> EvaluationSession<M>
Capture one coherent authorization-state generation for batch work.
Sourcepub fn policy_store_ids(&self) -> Option<Vec<PolicyStoreId>>
pub fn policy_store_ids(&self) -> Option<Vec<PolicyStoreId>>
Return configured policy-store IDs, or None for a monolithic engine.
Sourcepub fn evaluate(&self, request: &Request) -> Result<Decision, PolicyError>
pub fn evaluate(&self, request: &Request) -> Result<Decision, PolicyError>
Evaluate a policy request against the currently loaded policy set.
This method performs a complete Cedar policy evaluation:
- Applies any registered labelers to augment resource attributes
- Constructs Cedar entities for the principal (including groups), action, and resource
- Executes the Cedar authorization decision
- Returns either
Allow(with the matching policy) orDeny, both including version metadata
§Arguments
request- The authorization request containing the principal, action, and resource
§Returns
Ok(decision)withDecision::is_allowedreturningtrueif at least one permit policy matches and no forbid policies match, orfalseotherwiseErr(PolicyError)- If there’s an error constructing entities, parsing the request, or during evaluation
§Examples
use treetop_core::{PolicyEngine, Request, Principal, User, Action, Resource};
let policies = r#"
permit (
principal == User::"alice",
action == Action::"read",
resource == Document::"doc1"
);
"#;
let engine = PolicyEngine::new_from_str(policies).unwrap();
let request = Request {
principal: Principal::User(User::new("alice", None, None).unwrap()),
action: Action::new("read", None).unwrap(),
resource: Resource::new("Document", "doc1").unwrap(),
};
let decision = engine.evaluate(&request).unwrap();
assert!(decision.is_allowed());
// Access version information
println!("Allowed by policy version: {}", decision.version().hash);§Thread Safety
This method is thread-safe and lock-free. Multiple threads can evaluate requests concurrently without blocking each other.
Sourcepub fn evaluate_with_context(
&self,
request: &Request,
request_context: &RequestContext,
) -> Result<Decision, PolicyError>
pub fn evaluate_with_context( &self, request: &Request, request_context: &RequestContext, ) -> Result<Decision, PolicyError>
Evaluate a request with explicit Cedar request context.
Sourcepub fn evaluate_with_diagnostics(
&self,
request: &Request,
) -> Result<DecisionDiagnostics, PolicyError>
pub fn evaluate_with_diagnostics( &self, request: &Request, ) -> Result<DecisionDiagnostics, PolicyError>
Evaluate a request and include deny-side forbid diagnostics.
Sourcepub fn evaluate_with_context_and_diagnostics(
&self,
request: &Request,
request_context: &RequestContext,
) -> Result<DecisionDiagnostics, PolicyError>
pub fn evaluate_with_context_and_diagnostics( &self, request: &Request, request_context: &RequestContext, ) -> Result<DecisionDiagnostics, PolicyError>
Evaluate a request with explicit context and include deny diagnostics.
Sourcepub fn list_policies_for_user(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_user( &self, user: &str, groups: &[&str], namespace: &[&str], ) -> Result<PolicyCandidates, PolicyError>
List permit-policy candidates whose scope matches a user.
This mirrors PolicyEngine::evaluate input shape for principal identity:
user id + groups + shared namespace.
Matching includes all Cedar principal-constraint forms:
principal == User::"..."principal in Group::"..."principalprincipal is Userprincipal is User in Group::"..."
Cedar when and unless clauses are not evaluated. The result is not
an authorization decision; use PolicyEngine::evaluate to authorize.
Resource constraints are not applied in this method. To additionally
filter by policy resource constraints, use
PolicyEngine::list_policies_for_user_with_resource.
Output is deterministic: policies are sorted by Cedar policy ID.
Each returned policy includes match reasons via PolicyCandidates::matches().
§Arguments
user- User IDgroups- Group IDs the user belongs tonamespace- Optional shared namespace path for both user and groups
§Returns
Ok(PolicyCandidates)- Matching policies and match metadataErr(PolicyError)- If entity UID construction fails
§Examples
use treetop_core::PolicyEngine;
let policies = r#"
permit (principal == User::"alice", action, resource);
permit (principal in Group::"admins", action, resource);
"#;
let engine = PolicyEngine::new_from_str(policies).unwrap();
let candidates = engine.list_policies_for_user("alice", &["admins"], &[]).unwrap();
assert_eq!(candidates.policies().len(), 2);
assert!(!candidates.matches().is_empty());Sourcepub fn list_policies(
&self,
request: &Request,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies( &self, request: &Request, ) -> Result<PolicyCandidates, PolicyError>
List permit-policy candidates whose scope matches a concrete request.
This mirrors PolicyEngine::evaluate by accepting &Request and uses:
- the request principal (including user group membership, if any)
- the request action
- the request resource
Cedar when and unless clauses are not evaluated. The result is not
an authorization decision. This method defaults to permit policies; use
PolicyEngine::list_policies_with_effect for an explicit effect.
Sourcepub fn list_policies_with_effect(
&self,
request: &Request,
effect_filter: PolicyEffectFilter,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_with_effect( &self, request: &Request, effect_filter: PolicyEffectFilter, ) -> Result<PolicyCandidates, PolicyError>
List policy candidates for a request, with explicit effect filtering.
Cedar when and unless clauses are not evaluated.
Sourcepub fn list_policies_for_user_with_resource(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
resource: Option<&Resource>,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_user_with_resource( &self, user: &str, groups: &[&str], namespace: &[&str], resource: Option<&Resource>, ) -> Result<PolicyCandidates, PolicyError>
List all policies applicable to a user, optionally filtering by resource constraints.
This variant applies both principal and resource constraints:
- principal constraints as described in
PolicyEngine::list_policies_for_user - resource constraints (
==,in,is,is in,any) whenresourceis provided
When resource is None, behavior is equivalent to
PolicyEngine::list_policies_for_user.
Returned PolicyCandidates includes match reasons for principal and, when
applicable, resource matches.
Sourcepub fn list_policies_for_user_with_resource_and_effect(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
resource: Option<&Resource>,
effect_filter: PolicyEffectFilter,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_user_with_resource_and_effect( &self, user: &str, groups: &[&str], namespace: &[&str], resource: Option<&Resource>, effect_filter: PolicyEffectFilter, ) -> Result<PolicyCandidates, PolicyError>
List all policies applicable to a user with optional resource and effect filtering.
Sourcepub fn list_policies_for_group(
&self,
group: &str,
namespace: &[&str],
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_group( &self, group: &str, namespace: &[&str], ) -> Result<PolicyCandidates, PolicyError>
List all policies applicable to a group principal.
Useful when callers model group identities directly as principals
(mirroring Principal::Group in PolicyEngine::evaluate).
Resource constraints are not applied in this method. To also filter by
resource constraints, use
PolicyEngine::list_policies_for_group_with_resource.
Sourcepub fn list_policies_for_group_with_resource(
&self,
group: &str,
namespace: &[&str],
resource: Option<&Resource>,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_group_with_resource( &self, group: &str, namespace: &[&str], resource: Option<&Resource>, ) -> Result<PolicyCandidates, PolicyError>
List all policies applicable to a group principal, optionally filtering by resource constraints.
This applies principal constraints for a group principal and, when
resource is provided, resource constraints as well.
Sourcepub fn list_policies_for_group_with_resource_and_effect(
&self,
group: &str,
namespace: &[&str],
resource: Option<&Resource>,
effect_filter: PolicyEffectFilter,
) -> Result<PolicyCandidates, PolicyError>
pub fn list_policies_for_group_with_resource_and_effect( &self, group: &str, namespace: &[&str], resource: Option<&Resource>, effect_filter: PolicyEffectFilter, ) -> Result<PolicyCandidates, PolicyError>
List all policies applicable to a group principal with optional resource and effect filtering.
Source§impl PolicyEngine<SchemaEnforcing>
impl PolicyEngine<SchemaEnforcing>
Sourcepub fn reload_from_str_with_schema(
&self,
policy_text: &str,
schema: Schema,
) -> Result<(), PolicyError>
pub fn reload_from_str_with_schema( &self, policy_text: &str, schema: Schema, ) -> Result<(), PolicyError>
Reload policies and replace the enforced schema in one atomic update.
Sourcepub fn reload_from_str_with_cedarschema(
&self,
policy_text: &str,
schema_text: &str,
) -> Result<(), PolicyError>
pub fn reload_from_str_with_cedarschema( &self, policy_text: &str, schema_text: &str, ) -> Result<(), PolicyError>
Reload policies and replace the enforced schema from Cedar schema text.
Trait Implementations§
Source§impl<M: Clone + ValidationMode> Clone for PolicyEngine<M>
impl<M: Clone + ValidationMode> Clone for PolicyEngine<M>
Source§fn clone(&self) -> PolicyEngine<M>
fn clone(&self) -> PolicyEngine<M>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<M: ValidationMode> From<&PolicyEngine<M>> for PolicyVersion
impl<M: ValidationMode> From<&PolicyEngine<M>> for PolicyVersion
Source§fn from(engine: &PolicyEngine<M>) -> Self
fn from(engine: &PolicyEngine<M>) -> Self
Source§impl<M: ValidationMode> From<PolicyEngine<M>> for PolicyVersion
impl<M: ValidationMode> From<PolicyEngine<M>> for PolicyVersion
Source§fn from(engine: PolicyEngine<M>) -> Self
fn from(engine: PolicyEngine<M>) -> Self
Auto Trait Implementations§
impl<M = SchemaFree> !RefUnwindSafe for PolicyEngine<M>
impl<M = SchemaFree> !UnwindSafe for PolicyEngine<M>
impl<M> Freeze for PolicyEngine<M>
impl<M> Send for PolicyEngine<M>
impl<M> Sync for PolicyEngine<M>
impl<M> Unpin for PolicyEngine<M>
impl<M> UnsafeUnpin for PolicyEngine<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more