Skip to main content

themis/
strategy.rs

1use crate::grant::{Effect, Grant};
2
3/// Grant applicable to the request, with the position of the step where it matched (0 = most specific).
4#[derive(Debug, Clone, PartialEq)]
5pub struct Candidate {
6    pub grant: Grant,
7    pub rank: usize,
8}
9
10/// Combines the applicable grants into a decision. Returns the index of the deciding candidate, or
11/// `None` to fall back to default-deny.
12pub trait Strategy: Send + Sync {
13    fn name(&self) -> &'static str;
14    fn combine(&self, candidates: &[Candidate]) -> Option<usize>;
15}
16
17fn first(candidates: &[Candidate], effect: Effect, filter: impl Fn(&Candidate) -> bool) -> Option<usize> {
18    candidates.iter().position(|c| c.grant.effect == effect && filter(c))
19}
20
21/// The most specific step with any grant decides; within it, `deny` beats `allow`. This is the
22/// engine's default.
23#[derive(Debug, Clone, Copy, Default)]
24pub struct MostSpecific;
25
26impl Strategy for MostSpecific {
27    fn name(&self) -> &'static str {
28        "most_specific"
29    }
30
31    fn combine(&self, candidates: &[Candidate]) -> Option<usize> {
32        let best = candidates.iter().map(|c| c.rank).min()?;
33        first(candidates, Effect::Deny, |c| c.rank == best)
34            .or_else(|| first(candidates, Effect::Allow, |c| c.rank == best))
35    }
36}
37
38/// Any `deny` wins, at any step.
39#[derive(Debug, Clone, Copy, Default)]
40pub struct DenyOverrides;
41
42impl Strategy for DenyOverrides {
43    fn name(&self) -> &'static str {
44        "deny_overrides"
45    }
46
47    fn combine(&self, candidates: &[Candidate]) -> Option<usize> {
48        first(candidates, Effect::Deny, |_| true).or_else(|| first(candidates, Effect::Allow, |_| true))
49    }
50}
51
52/// Any `allow` wins, at any step.
53#[derive(Debug, Clone, Copy, Default)]
54pub struct PermitOverrides;
55
56impl Strategy for PermitOverrides {
57    fn name(&self) -> &'static str {
58        "permit_overrides"
59    }
60
61    fn combine(&self, candidates: &[Candidate]) -> Option<usize> {
62        first(candidates, Effect::Allow, |_| true).or_else(|| first(candidates, Effect::Deny, |_| true))
63    }
64}
65
66/// The first applicable grant, in source order, decides.
67#[derive(Debug, Clone, Copy, Default)]
68pub struct FirstApplicable;
69
70impl Strategy for FirstApplicable {
71    fn name(&self) -> &'static str {
72        "first_applicable"
73    }
74
75    fn combine(&self, candidates: &[Candidate]) -> Option<usize> {
76        (!candidates.is_empty()).then_some(0)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use crate::grant::{SubjectMatcher, TargetSelector};
84
85    fn c(effect: Effect, rank: usize) -> Candidate {
86        Candidate {
87            grant: Grant::new("g", effect, SubjectMatcher::Any, "p", TargetSelector::global()),
88            rank,
89        }
90    }
91
92    #[test]
93    fn most_specific_prefers_lower_rank_then_deny() {
94        let set = [c(Effect::Deny, 3), c(Effect::Allow, 1), c(Effect::Deny, 1)];
95        assert_eq!(MostSpecific.combine(&set), Some(2));
96        let set = [c(Effect::Deny, 3), c(Effect::Allow, 1)];
97        assert_eq!(MostSpecific.combine(&set), Some(1));
98    }
99
100    #[test]
101    fn overrides_ignore_rank() {
102        let set = [c(Effect::Allow, 0), c(Effect::Deny, 4)];
103        assert_eq!(DenyOverrides.combine(&set), Some(1));
104        assert_eq!(PermitOverrides.combine(&set), Some(0));
105        assert_eq!(FirstApplicable.combine(&set), Some(0));
106        assert_eq!(MostSpecific.combine(&[]), None);
107    }
108}