1use super::*;
4
5#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum Blocker {
8 SenderCannotEnumerate,
9 RecipientCannotEnumerate,
10 SenderSendNotAllowed,
11 RecipientReceiveNotAllowed,
12 SenderReceiveDenied,
13 RecipientReceiveDenied,
14 SameAgent,
15}
16#[derive(Debug, Serialize)]
17pub struct Gate {
18 pub allowed: bool,
19 pub blocked_by: Vec<Blocker>,
20}
21impl Gate {
22 fn new(checks: impl IntoIterator<Item = (Blocker, bool)>, same: bool) -> Self {
23 let mut blocked_by: Vec<_> = checks
24 .into_iter()
25 .filter_map(|(reason, allowed)| (!allowed).then_some(reason))
26 .collect();
27 if same {
28 blocked_by.push(Blocker::SameAgent);
29 }
30 Self {
31 allowed: blocked_by.is_empty(),
32 blocked_by,
33 }
34 }
35}
36#[derive(Debug, Serialize)]
37pub struct Relationship {
38 pub enumerate: bool,
39 pub send_unsolicited: Gate,
40 pub receive_unsolicited: Gate,
41 pub reply_policy: Gate,
44}
45#[derive(Debug, Serialize)]
46#[serde(rename_all = "snake_case")]
47pub enum Decision {
48 Allow,
49 ExplicitDeny,
50 NoMatchingRule,
51 Revoked,
52}
53#[derive(Debug, Serialize)]
54pub struct Trace {
55 pub permission: Permission,
56 pub decision: Decision,
57 pub matching_rules: Vec<usize>,
59}
60impl Policy {
61 pub fn cluster_default() -> Self {
65 Self {
66 rules: [Permission::Enumerate, Permission::Receive, Permission::Send]
67 .into_iter()
68 .map(|permission| Rule {
69 actor: Selector::All,
70 peer: Selector::All,
71 permission,
72 effect: Effect::Allow,
73 })
74 .collect(),
75 ..Self::default()
76 }
77 }
78
79 pub(super) fn matching_rules<'a>(
80 &'a self,
81 actor: &'a Agent,
82 peer: &'a Agent,
83 permission: Permission,
84 ) -> impl Iterator<Item = (usize, &'a Rule)> {
85 self.rules.iter().enumerate().filter(move |(_, rule)| {
86 rule.permission == permission
87 && self.matches(&rule.actor, actor, peer)
88 && self.matches(&rule.peer, peer, actor)
89 })
90 }
91 fn checks<'a>(
92 &'a self,
93 sender: &'a Agent,
94 recipient: &'a Agent,
95 kinds: &'static [Blocker],
96 ) -> impl Iterator<Item = (Blocker, bool)> + 'a {
97 kinds.iter().copied().map(move |reason| {
98 let allowed = match reason {
99 Blocker::SenderCannotEnumerate => self.visible(sender, recipient),
100 Blocker::RecipientCannotEnumerate => self.visible(recipient, sender),
101 Blocker::SenderSendNotAllowed => {
102 self.decision(sender, recipient, Permission::Send) == Some(Effect::Allow)
103 }
104 Blocker::RecipientReceiveNotAllowed => {
105 self.decision(recipient, sender, Permission::Receive) == Some(Effect::Allow)
106 }
107 Blocker::SenderReceiveDenied => {
108 self.decision(sender, recipient, Permission::Receive) != Some(Effect::Deny)
109 }
110 Blocker::RecipientReceiveDenied => {
111 self.decision(recipient, sender, Permission::Receive) != Some(Effect::Deny)
112 }
113 Blocker::SameAgent => sender.id != recipient.id,
114 };
115 (reason, allowed)
116 })
117 }
118 pub(super) fn open_checks<'a>(
119 &'a self,
120 sender: &'a Agent,
121 recipient: &'a Agent,
122 ) -> impl Iterator<Item = (Blocker, bool)> + 'a {
123 self.checks(
124 sender,
125 recipient,
126 &[
127 Blocker::SenderCannotEnumerate,
128 Blocker::RecipientCannotEnumerate,
129 Blocker::SenderSendNotAllowed,
130 Blocker::RecipientReceiveNotAllowed,
131 Blocker::SenderReceiveDenied,
132 ],
133 )
134 }
135 pub(super) fn reply_checks<'a>(
136 &'a self,
137 sender: &'a Agent,
138 recipient: &'a Agent,
139 ) -> impl Iterator<Item = (Blocker, bool)> + 'a {
140 self.checks(
141 sender,
142 recipient,
143 &[
144 Blocker::SenderCannotEnumerate,
145 Blocker::RecipientCannotEnumerate,
146 Blocker::SenderReceiveDenied,
147 Blocker::RecipientReceiveDenied,
148 ],
149 )
150 }
151 pub fn relationship(&self, actor: &Agent, peer: &Agent) -> Relationship {
152 let same = actor.id == peer.id;
153 Relationship {
154 enumerate: self.visible(actor, peer),
155 send_unsolicited: Gate::new(self.open_checks(actor, peer), same),
156 receive_unsolicited: Gate::new(self.open_checks(peer, actor), same),
157 reply_policy: Gate::new(self.reply_checks(actor, peer), same),
158 }
159 }
160 pub fn trace(&self, actor: &Agent, peer: &Agent) -> Vec<Trace> {
161 [Permission::Enumerate, Permission::Receive, Permission::Send]
162 .into_iter()
163 .map(|permission| Trace {
164 permission,
165 decision: if actor.revoked || peer.revoked {
166 Decision::Revoked
167 } else {
168 match self.decision(actor, peer, permission) {
169 Some(Effect::Allow) => Decision::Allow,
170 Some(Effect::Deny) => Decision::ExplicitDeny,
171 None => Decision::NoMatchingRule,
172 }
173 },
174 matching_rules: self
175 .matching_rules(actor, peer, permission)
176 .map(|(index, _)| index)
177 .collect(),
178 })
179 .collect()
180 }
181}
182
183#[cfg(test)]
184mod tests;