1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
use cedar_policy::{
Authorizer, Entities, Entity, Policy, PolicyId, PolicySet, Request as CedarRequest, Schema,
};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant, SystemTime};
use std::vec;
use crate::labels::LabelRegistry;
use crate::policy_match::{
action_match_reason, matches_effect, principal_match_reason, resource_match_reason,
};
use crate::query::{ActionQuery, PrincipalQuery, ResourceQuery};
use crate::timers::PhaseTimer;
use crate::traits::CedarAtom;
use crate::types::{
Decision, DecisionDiagnostics, FromDecisionWithPolicy, PermitPolicies, PermitPolicy,
PolicyEffectFilter, PolicyMatchReason, PolicyVersion, Request, RequestContext, Resource,
UserPolicies,
};
use crate::{Groups, Principal};
use crate::{error::PolicyError, loader};
use arc_swap::ArcSwap;
use sha2::{Digest, Sha256};
use tracing::debug;
#[cfg(feature = "observability")]
use tracing::info_span;
#[cfg(feature = "observability")]
use crate::metrics::{
EvaluationPhases, EvaluationStats, record_evaluation, record_evaluation_phases, record_reload,
};
/// Static cached Authorizer instance (stateless, reusable across evaluations).
fn get_authorizer() -> &'static Authorizer {
static AUTHORIZER: OnceLock<Authorizer> = OnceLock::new();
AUTHORIZER.get_or_init(Authorizer::new)
}
/// Aggregates timing information for all evaluation phases.
#[derive(Debug)]
struct EvalTimers {
/// Total elapsed time from start of evaluation
total_start: Instant,
/// Time spent applying labels
labels: Duration,
/// Time spent constructing Cedar request
construct_req: Duration,
/// Time spent building Cedar entities
entities: Duration,
/// Time spent resolving groups
groups: Duration,
/// Time spent performing authorization
authz: Duration,
}
impl EvalTimers {
fn start() -> Self {
Self {
total_start: Instant::now(),
labels: Duration::ZERO,
construct_req: Duration::ZERO,
entities: Duration::ZERO,
groups: Duration::ZERO,
authz: Duration::ZERO,
}
}
fn total_elapsed(&self) -> Duration {
self.total_start.elapsed()
}
}
/// Result of preparing a request for authorization: Cedar request, entities, snapshot, and phase timings.
struct PreparedRequest {
cedar_req: CedarRequest,
entities: Entities,
snapshot: Snapshot,
timers: EvalTimers,
}
/// Immutable snapshot of a compiled policy set, along with metadata.
#[derive(Debug)]
struct PolicySnapshot {
set: PolicySet,
version: PolicyVersion,
permit_policies: HashMap<PolicyId, PermitPolicy>,
forbid_policy_ids: HashMap<PolicyId, String>,
schema: Option<Arc<Schema>>,
}
/// Convenience alias for a shared policy snapshot.
type Snapshot = Arc<PolicySnapshot>;
impl PolicySnapshot {
fn from_policy_text(policy_text: &str) -> Result<Self, PolicyError> {
Self::from_policy_text_with_schema(policy_text, None)
}
fn from_policy_text_with_schema(
policy_text: &str,
schema: Option<Arc<Schema>>,
) -> Result<Self, PolicyError> {
let set = match schema.as_deref() {
Some(schema) => loader::compile_policy_with_schema(policy_text, schema)?,
None => loader::compile_policy(policy_text)?,
};
let permit_policies = loader::precompute_permit_policies(&set);
let forbid_policy_ids = loader::precompute_forbid_policy_ids(&set);
let mut hasher = Sha256::new();
hasher.update(policy_text.as_bytes());
let hash = hasher
.finalize()
.iter()
.fold(String::with_capacity(64), |mut s, b| {
use std::fmt::Write;
write!(s, "{b:02x}").unwrap();
s
});
Ok(PolicySnapshot {
set,
version: PolicyVersion {
hash,
loaded_at: humantime::format_rfc3339(SystemTime::now()).to_string(),
},
permit_policies,
forbid_policy_ids,
schema,
})
}
fn policy_set(&self) -> &PolicySet {
&self.set
}
fn version(&self) -> PolicyVersion {
self.version.clone()
}
fn schema(&self) -> Option<&Schema> {
self.schema.as_deref()
}
}
/// Extract all permit policies from the Cedar authorization result.
#[inline]
fn extract_permit_policies(
snapshot: &PolicySnapshot,
result: &cedar_policy::Response,
) -> PermitPolicies {
if result.decision() != cedar_policy::Decision::Allow {
return PermitPolicies::empty();
}
result
.diagnostics()
.reason()
.filter_map(|reason| snapshot.permit_policies.get(reason))
.cloned()
.collect()
}
/// Extract matched forbid policy IDs from a Cedar authorization result.
#[inline]
fn extract_forbid_policy_ids(
snapshot: &PolicySnapshot,
result: &cedar_policy::Response,
) -> Vec<String> {
if result.decision() != cedar_policy::Decision::Deny {
return Vec::new();
}
let mut ids: Vec<String> = result
.diagnostics()
.reason()
.filter_map(|reason| snapshot.forbid_policy_ids.get(reason).cloned())
.collect();
ids.sort();
ids.dedup();
ids
}
/// Iterate over groups from a request principal.
#[inline]
fn request_groups(request: &Request) -> Option<&Groups> {
match &request.principal {
Principal::User(user) => Some(user.groups()),
Principal::Group(_) => None,
}
}
/// Apply label augmentations to a resource.
#[inline]
fn apply_labels(
registry: &Option<Arc<LabelRegistry>>,
resource: &mut crate::types::Resource,
timers: &mut EvalTimers,
) {
let _timer = PhaseTimer::new(&mut timers.labels);
#[cfg(feature = "observability")]
let _label_span = info_span!("apply_labels").entered();
if let Some(registry) = registry {
registry.apply(resource);
}
}
/// Build the effective Cedar request context by merging resource context and
/// optional request context.
#[inline]
fn build_effective_context(
resource: &crate::types::Resource,
request_context: Option<&RequestContext>,
) -> Result<cedar_policy::Context, PolicyError> {
let base_context = resource.cedar_ctx()?;
let Some(request_context) = request_context else {
return Ok(base_context);
};
if request_context.is_empty() {
return Ok(base_context);
}
Ok(base_context.merge(
request_context
.iter()
.map(|(k, v)| (k.clone(), v.to_re()))
.collect::<Vec<_>>(),
)?)
}
/// Build a Cedar request from the authorization request and resource.
/// UIDs should be pre-converted to avoid redundant conversions.
#[inline]
fn build_cedar_req(
principal_uid: &cedar_policy::EntityUid,
action_uid: &cedar_policy::EntityUid,
resource_uid: &cedar_policy::EntityUid,
context: &cedar_policy::Context,
schema: Option<&Schema>,
timers: &mut EvalTimers,
) -> Result<CedarRequest, PolicyError> {
let _timer = PhaseTimer::new(&mut timers.construct_req);
#[cfg(feature = "observability")]
let _req_span = info_span!("construct_cedar_req").entered();
Ok(CedarRequest::new(
principal_uid.clone(),
action_uid.clone(),
resource_uid.clone(),
context.clone(),
schema,
)?)
}
/// Build Cedar entities for the principal, resource, and groups.
/// UIDs should be pre-converted to avoid redundant conversions.
#[inline]
fn build_entities(
principal_uid: &cedar_policy::EntityUid,
resource_uid: &cedar_policy::EntityUid,
resource: &crate::types::Resource,
groups: Option<&Groups>,
schema: Option<&Schema>,
timers: &mut EvalTimers,
) -> Result<Entities, PolicyError> {
// Time and measure entity construction
let entities = {
let _timer = PhaseTimer::new(&mut timers.entities);
#[cfg(feature = "observability")]
let _entity_span = info_span!("construct_entities").entered();
// Collect group UIDs with pre-allocation to avoid over-allocations
let group_uids = {
let _timer = PhaseTimer::new(&mut timers.groups);
#[cfg(feature = "observability")]
let _groups_span = info_span!("resolve_groups").entered();
match groups {
Some(groups_slice) => {
let mut uids = Vec::with_capacity(groups_slice.len());
for g in groups_slice {
uids.push(g.cedar_entity_uid()?);
}
uids.into_iter().collect::<HashSet<_>>()
}
None => HashSet::new(),
}
};
// Construct resource entity
let resource_attrs = resource.cedar_attr()?;
let resource_entity =
cedar_policy::Entity::new(resource_uid.clone(), resource_attrs, Default::default())?;
// Construct principal entity with groups as parents
let principal_entity =
Entity::new(principal_uid.clone(), HashMap::new(), group_uids.clone())?;
// Construct group entities and batch all entities into a single call
let mut all_entities = vec![principal_entity, resource_entity];
all_entities.extend(group_uids.into_iter().map(Entity::with_uid));
// Combine all entities in a single call to reduce overhead
Entities::empty().add_entities(all_entities, schema)?
};
debug!(
event = "Request",
phase = "Entities",
time = timers.entities.as_micros(),
entities = entities
.iter()
.map(|e| format!("[{e}]"))
.collect::<Vec<_>>()
.join(", ")
.replace('\n', "")
);
Ok(entities)
}
/// The main engine handle. Thread-safe and cheaply cloneable.
///
/// Cloning is cheap (just increments Arc refcounts), but for multithreaded
/// applications, wrapping in `Arc<PolicyEngine>` and using `Arc::clone()`
/// is more idiomatic and makes ownership clearer.
///
/// For single-threaded use or when passing the engine to a single thread,
/// you can simply clone it directly.
#[derive(Clone)]
pub struct PolicyEngine {
/// Shared pointer to an `ArcSwap` holding the current `Snapshot`.
inner: Arc<ArcSwap<Snapshot>>,
/// Optional label registry for augmenting resources with derived attributes.
label_registry: Option<Arc<LabelRegistry>>,
}
impl From<PolicyEngine> for PolicyVersion {
fn from(engine: PolicyEngine) -> Self {
engine.current_version()
}
}
impl From<&PolicyEngine> for PolicyVersion {
fn from(engine: &PolicyEngine) -> Self {
engine.current_version()
}
}
impl PolicyEngine {
pub fn new_from_str(policy_text: &str) -> Result<Self, PolicyError> {
let snapshot: Snapshot = Arc::new(PolicySnapshot::from_policy_text(policy_text)?);
Ok(PolicyEngine {
inner: Arc::new(ArcSwap::from(Arc::new(snapshot))),
label_registry: None,
})
}
/// Create a new policy engine with schema-based policy and request validation.
pub fn new_from_str_with_schema(
policy_text: &str,
schema: Schema,
) -> Result<Self, PolicyError> {
let snapshot: Snapshot = Arc::new(PolicySnapshot::from_policy_text_with_schema(
policy_text,
Some(Arc::new(schema)),
)?);
Ok(PolicyEngine {
inner: Arc::new(ArcSwap::from(Arc::new(snapshot))),
label_registry: None,
})
}
/// Create a new policy engine from policy text and Cedar schema text.
pub fn new_from_str_with_cedarschema(
policy_text: &str,
schema_text: &str,
) -> Result<Self, PolicyError> {
let schema: Schema = schema_text
.parse()
.map_err(|e| PolicyError::ParseError(format!("failed to parse Cedar schema: {e}")))?;
Self::new_from_str_with_schema(policy_text, schema)
}
/// Create a new policy engine with a label registry.
///
/// This is a convenience method that combines `new_from_str` and `with_label_registry`.
pub fn with_label_registry(mut self, registry: LabelRegistry) -> Self {
self.label_registry = Some(Arc::new(registry));
self
}
/// Set or replace the label registry for this engine.
///
/// This allows updating the labelers after the engine has been created.
pub fn set_label_registry(&mut self, registry: LabelRegistry) {
self.label_registry = Some(Arc::new(registry));
}
/// Get a reference to the label registry, if one is configured.
pub fn label_registry(&self) -> Option<&LabelRegistry> {
self.label_registry.as_deref()
}
pub fn reload_from_str(&self, policy_text: &str) -> Result<(), PolicyError> {
let current_snapshot = self.current_snapshot();
let had_schema = current_snapshot.schema.is_some();
let schema = current_snapshot.schema.clone();
let new_snapshot: Snapshot = Arc::new(PolicySnapshot::from_policy_text_with_schema(
policy_text,
schema,
)?);
self.inner.store(Arc::new(new_snapshot));
debug!(
event = "PolicyReload",
schema_enabled = had_schema,
schema_reloaded = false
);
// Track reloads for metrics (no-op if feature disabled or no sink configured)
#[cfg(feature = "observability")]
record_reload();
Ok(())
}
/// Reload policies and replace the engine schema at the same time.
pub fn reload_from_str_with_schema(
&self,
policy_text: &str,
schema: Schema,
) -> Result<(), PolicyError> {
let had_schema = self.current_snapshot().schema.is_some();
let new_snapshot: Snapshot = Arc::new(PolicySnapshot::from_policy_text_with_schema(
policy_text,
Some(Arc::new(schema)),
)?);
self.inner.store(Arc::new(new_snapshot));
debug!(
event = "PolicyReload",
schema_enabled = true,
schema_reloaded = true,
schema_previously_enabled = had_schema
);
// Track reloads for metrics (no-op if feature disabled or no sink configured)
#[cfg(feature = "observability")]
record_reload();
Ok(())
}
/// Reload policies and replace the engine schema from Cedar schema text.
pub fn reload_from_str_with_cedarschema(
&self,
policy_text: &str,
schema_text: &str,
) -> Result<(), PolicyError> {
let schema: Schema = schema_text
.parse()
.map_err(|e| PolicyError::ParseError(format!("failed to parse Cedar schema: {e}")))?;
self.reload_from_str_with_schema(policy_text, schema)
}
/// Get the current snapshot using a short-lived read lock, then drop the lock.
fn current_snapshot(&self) -> Snapshot {
// `load_full` returns `Arc<Arc<PolicySnapshot>>`; deref one level.
let outer = self.inner.load_full();
Arc::clone(&outer)
}
/// Get the current policy version.
///
/// The `hash` is computed from the policy text, and `loaded_at` reflects
/// when this snapshot was installed.
pub fn current_version(&self) -> PolicyVersion {
self.current_snapshot().version()
}
/// Prepare a request for authorization: accumulate labels, build Cedar entities, resolve groups.
///
/// This separates request preparation from the authorization decision, making both
/// more testable and the main hot path more readable.
fn prepare(
&self,
request: &Request,
request_context: Option<&RequestContext>,
) -> Result<PreparedRequest, PolicyError> {
let snapshot = self.current_snapshot();
let schema = snapshot.schema();
let mut timers = EvalTimers::start();
let groups = request_groups(request);
debug!(
event = "Request",
phase = "Evaluation",
principal = request.principal.to_string(),
action = request.action.to_string(),
resource = request.resource.to_string(),
groups = %groups.map(ToString::to_string).unwrap_or_else(|| "[]".into())
);
// Convert UIDs once to avoid redundant conversions (on original resource)
let principal_uid = request.principal.cedar_entity_uid()?;
let action_uid = request.action.cedar_entity_uid()?;
let resource_uid_original = request.resource.cedar_entity_uid()?;
let context_original = build_effective_context(&request.resource, request_context)?;
// Only clone and apply labels if a label registry is configured
// We need to keep the potentially-modified resource for building entities
let (resource_uid, context, resource_for_entities) = if self.label_registry.is_some() {
let mut resource = request.resource.clone();
apply_labels(&self.label_registry, &mut resource, &mut timers);
debug!(
event = "Request",
phase = "LabelsApplied",
time = timers.labels.as_micros(),
resource_attrs = ?resource.attrs()
);
let uid = resource.cedar_entity_uid()?;
let ctx = build_effective_context(&resource, request_context)?;
(uid, ctx, resource)
} else {
debug!(
event = "Request",
phase = "LabelsApplied",
time = timers.labels.as_micros()
);
(
resource_uid_original,
context_original,
request.resource.clone(),
)
};
debug!(
event = "Request",
phase = "Parsed",
principal = principal_uid.to_string(),
action = action_uid.to_string(),
resource = resource_uid.to_string(),
context = context.to_string(),
groups = %groups.map(ToString::to_string).unwrap_or_else(|| "[]".into()),
attrs = ?resource_for_entities.cedar_attr()
);
// Build Cedar request with pre-converted UIDs
let cedar_req = build_cedar_req(
&principal_uid,
&action_uid,
&resource_uid,
&context,
schema,
&mut timers,
)?;
// Build entities with pre-converted UIDs and potentially-modified resource
let entities = build_entities(
&principal_uid,
&resource_uid,
&resource_for_entities,
groups,
schema,
&mut timers,
)?;
debug!(
event = "Request",
phase = "GroupsResolved",
time = timers.groups.as_micros(),
);
Ok(PreparedRequest {
cedar_req,
entities,
snapshot,
timers,
})
}
/// Evaluate a policy request against the currently loaded policy set.
///
/// This method performs a complete Cedar policy evaluation:
/// 1. Applies any registered labelers to augment resource attributes
/// 2. Constructs Cedar entities for the principal (including groups), action, and resource
/// 3. Executes the Cedar authorization decision
/// 4. Returns either `Allow` (with the matching policy) or `Deny`, both including version metadata
///
/// # Arguments
///
/// * `request` - The authorization request containing the principal, action, and resource
///
/// # Returns
///
/// * `Ok(Decision::Allow)` - If at least one permit policy matches and no forbid policies match
/// * `Ok(Decision::Deny)` - If no permit policies match or if a forbid policy matches
/// * `Err(PolicyError)` - If there's an error constructing entities, parsing the request, or during evaluation
///
/// # Examples
///
/// ```rust
/// use treetop_core::{PolicyEngine, Request, Principal, User, Action, Resource, Decision};
///
/// 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)),
/// action: Action::new("read", None),
/// resource: Resource::new("Document", "doc1"),
/// };
///
/// let decision = engine.evaluate(&request).unwrap();
/// assert!(matches!(decision, Decision::Allow { .. }));
///
/// // Access version information
/// if let Decision::Allow { version, .. } = decision {
/// println!("Allowed by policy version: {}", version.hash);
/// }
/// ```
///
/// # Thread Safety
///
/// This method is thread-safe and lock-free. Multiple threads can evaluate requests
/// concurrently without blocking each other.
#[cfg_attr(
feature = "observability",
tracing::instrument(
name = "policy_evaluation",
skip_all,
fields(
principal = %request.principal,
action = %request.action,
resource = %request.resource
)
)
)]
pub fn evaluate(&self, request: &Request) -> Result<Decision, PolicyError> {
Ok(self.evaluate_internal(request, None)?.decision)
}
/// Evaluate a request with explicit Cedar request context.
pub fn evaluate_with_context(
&self,
request: &Request,
request_context: &RequestContext,
) -> Result<Decision, PolicyError> {
Ok(self
.evaluate_internal(request, Some(request_context))?
.decision)
}
/// Evaluate a request and include deny-side forbid diagnostics.
pub fn evaluate_with_diagnostics(
&self,
request: &Request,
) -> Result<DecisionDiagnostics, PolicyError> {
self.evaluate_internal(request, None)
}
/// Evaluate a request with explicit context and include deny diagnostics.
pub fn evaluate_with_context_and_diagnostics(
&self,
request: &Request,
request_context: &RequestContext,
) -> Result<DecisionDiagnostics, PolicyError> {
self.evaluate_internal(request, Some(request_context))
}
fn evaluate_internal(
&self,
request: &Request,
request_context: Option<&RequestContext>,
) -> Result<DecisionDiagnostics, PolicyError> {
// Prepare the request: apply labels, build entities, resolve groups
let mut prepared = self.prepare(request, request_context)?;
// Perform authorization with RAII timing (using cached Authorizer)
let result = {
let _timer = PhaseTimer::new(&mut prepared.timers.authz);
#[cfg(feature = "observability")]
let _authz_span = info_span!("authorize").entered();
get_authorizer().is_authorized(
&prepared.cedar_req,
&prepared.snapshot.set,
&prepared.entities,
)
};
debug!(
event = "Request",
phase = "Authorized",
time = prepared.timers.authz.as_micros(),
decision = ?result.decision(),
);
let version = prepared.snapshot.version();
debug!(
event = "Request",
phase = "Result",
time = prepared.timers.total_elapsed().as_micros(),
result = ?result.decision(),
policy_hash = %version.hash,
policy_loaded_at = %version.loaded_at,
);
// Extract all permit policies from the authorization result
let permit_policies = extract_permit_policies(&prepared.snapshot, &result);
#[cfg(feature = "observability")]
let allow_policy_ids = permit_policies.ids();
let forbid_policy_ids = extract_forbid_policy_ids(&prepared.snapshot, &result);
let decision =
Decision::from_decision_with_policy(result.decision(), permit_policies, version);
// Record metrics (no-op when no sink is configured or feature disabled)
#[cfg(feature = "observability")]
{
let dur = prepared.timers.total_elapsed();
let allowed = result.decision() == cedar_policy::Decision::Allow;
let principal_id = request.principal.to_string();
let action_id = request.action.to_string();
let matched_policies = if allowed {
allow_policy_ids.clone()
} else {
forbid_policy_ids.clone()
};
let stats = EvaluationStats {
duration: dur,
allowed,
principal_id,
action_id,
matched_policies,
};
let phases = EvaluationPhases {
apply_labels_ms: prepared.timers.labels.as_secs_f64() * 1000.0,
construct_entities_ms: prepared.timers.entities.as_secs_f64() * 1000.0,
resolve_groups_ms: prepared.timers.groups.as_secs_f64() * 1000.0,
authorize_ms: prepared.timers.authz.as_secs_f64() * 1000.0,
total_ms: stats.duration.as_secs_f64() * 1000.0,
};
record_evaluation(&stats);
record_evaluation_phases(&stats, &phases);
}
Ok(DecisionDiagnostics {
decision,
matched_forbid_policy_ids: forbid_policy_ids,
})
}
/// List all policies applicable to 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::"..."`
/// - `principal`
/// - `principal is User`
/// - `principal is User in Group::"..."`
///
/// 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 `UserPolicies::matches()`.
///
/// # Arguments
///
/// * `user` - User ID
/// * `groups` - Group IDs the user belongs to
/// * `namespace` - Optional shared namespace path for both user and groups
///
/// # Returns
///
/// * `Ok(UserPolicies)` - Matching policies and match metadata
/// * `Err(PolicyError)` - If entity UID construction fails
///
/// # Examples
///
/// ```rust
/// 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 user_policies = engine.list_policies_for_user("alice", &["admins"], &[]).unwrap();
///
/// assert_eq!(user_policies.policies().len(), 2);
/// assert!(!user_policies.matches().is_empty());
/// ```
pub fn list_policies_for_user(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
) -> Result<UserPolicies, PolicyError> {
self.list_policies_for_user_with_resource_and_effect(
user,
groups,
namespace,
None,
PolicyEffectFilter::Any,
)
}
/// List all policies applicable to 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
///
/// Effect filter defaults to `Any`; to filter by permit/forbid, use
/// [`PolicyEngine::list_policies_with_effect`].
pub fn list_policies(&self, request: &Request) -> Result<UserPolicies, PolicyError> {
self.list_policies_with_effect(request, PolicyEffectFilter::Any)
}
/// List all policies applicable to a concrete request, with effect filtering.
pub fn list_policies_with_effect(
&self,
request: &Request,
effect_filter: PolicyEffectFilter,
) -> Result<UserPolicies, PolicyError> {
let principal = PrincipalQuery::from_principal(&request.principal)?;
let action = ActionQuery::from_action(&request.action)?;
self.list_policies_dispatch(
&request.principal.to_string(),
&principal,
Some(&action),
Some(&request.resource),
effect_filter,
)
}
/// 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`) when `resource` is provided
///
/// When `resource` is `None`, behavior is equivalent to
/// [`PolicyEngine::list_policies_for_user`].
///
/// Returned `UserPolicies` includes match reasons for principal and, when
/// applicable, resource matches.
pub fn list_policies_for_user_with_resource(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
resource: Option<&Resource>,
) -> Result<UserPolicies, PolicyError> {
self.list_policies_for_user_with_resource_and_effect(
user,
groups,
namespace,
resource,
PolicyEffectFilter::Any,
)
}
/// List all policies applicable to a user with optional resource and effect filtering.
pub fn list_policies_for_user_with_resource_and_effect(
&self,
user: &str,
groups: &[&str],
namespace: &[&str],
resource: Option<&Resource>,
effect_filter: PolicyEffectFilter,
) -> Result<UserPolicies, PolicyError> {
let principal = PrincipalQuery::for_user(user, groups, namespace)?;
self.list_policies_dispatch(user, &principal, None, resource, effect_filter)
}
/// 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`].
pub fn list_policies_for_group(
&self,
group: &str,
namespace: &[&str],
) -> Result<UserPolicies, PolicyError> {
self.list_policies_for_group_with_resource_and_effect(
group,
namespace,
None,
PolicyEffectFilter::Any,
)
}
/// 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.
pub fn list_policies_for_group_with_resource(
&self,
group: &str,
namespace: &[&str],
resource: Option<&Resource>,
) -> Result<UserPolicies, PolicyError> {
self.list_policies_for_group_with_resource_and_effect(
group,
namespace,
resource,
PolicyEffectFilter::Any,
)
}
/// List all policies applicable to a group principal with optional resource and effect filtering.
pub fn list_policies_for_group_with_resource_and_effect(
&self,
group: &str,
namespace: &[&str],
resource: Option<&Resource>,
effect_filter: PolicyEffectFilter,
) -> Result<UserPolicies, PolicyError> {
let principal = PrincipalQuery::for_group(group, namespace)?;
self.list_policies_dispatch(group, &principal, None, resource, effect_filter)
}
fn list_policies_dispatch(
&self,
principal_id: &str,
principal: &PrincipalQuery,
action: Option<&ActionQuery>,
resource: Option<&Resource>,
effect_filter: PolicyEffectFilter,
) -> Result<UserPolicies, PolicyError> {
let snapshot = self.current_snapshot();
let policies = snapshot.set.policies();
let resource_query = match resource {
Some(resource) => Some(ResourceQuery::from_resource(resource)?),
None => None,
};
let mut matching_policies: Vec<(Policy, Vec<PolicyMatchReason>)> = Vec::new();
for policy in policies {
if !matches_effect(policy.effect(), effect_filter) {
continue;
}
let Some(principal_reason) =
principal_match_reason(policy.principal_constraint(), principal)
else {
continue;
};
let Some(action_reason) = action_match_reason(policy.action_constraint(), action)
else {
continue;
};
let Some(resource_reason) =
resource_match_reason(policy.resource_constraint(), resource_query.as_ref())
else {
continue;
};
let mut reasons = vec![principal_reason];
if let Some(action_reason) = action_reason {
reasons.push(action_reason);
}
if let Some(resource_reason) = resource_reason {
reasons.push(resource_reason);
}
matching_policies.push((policy.clone(), reasons));
}
Ok(UserPolicies::new_with_matches(
principal_id,
matching_policies,
))
}
pub fn policies(&self) -> Result<Vec<Policy>, PolicyError> {
let snapshot = self.current_snapshot();
Ok(snapshot.policy_set().policies().cloned().collect())
}
}
#[cfg(test)]
mod tests;