1use super::grants::{Grant, GrantSet, ReplyShape, veto_grant_for_class};
21use super::ids::HookId;
22use super::registry::{HookReply, ReplyErrCode};
23use super::views::Origin;
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum Cause {
28 Deadline,
29 Crash,
30 Quarantine,
31 Busy,
32 HandshakeIncomplete,
33 EofWithoutReply,
34}
35
36impl Cause {
37 pub fn as_str(self) -> &'static str {
38 match self {
39 Cause::Deadline => "deadline",
40 Cause::Crash => "crash",
41 Cause::Quarantine => "quarantine",
42 Cause::Busy => "busy",
43 Cause::HandshakeIncomplete => "handshake_incomplete",
44 Cause::EofWithoutReply => "eof_without_reply",
45 }
46 }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum SpeechCode {
57 Unauthorized,
58 Malformed,
59 UnsupportedHook,
60 ProtocolViolation,
61 Unparseable,
62 OriginObserveOnly,
63}
64
65impl SpeechCode {
66 pub fn as_str(self) -> &'static str {
67 match self {
68 SpeechCode::Unauthorized => "unauthorized",
69 SpeechCode::Malformed => "malformed",
70 SpeechCode::UnsupportedHook => "unsupported_hook",
71 SpeechCode::ProtocolViolation => "protocol_violation",
72 SpeechCode::Unparseable => "unparseable",
73 SpeechCode::OriginObserveOnly => "origin_observe_only",
74 }
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub enum FailureClass {
81 Unreachable(Cause),
82 Speech(SpeechCode),
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
96pub enum ClassDefault {
97 Deny { cause: Cause },
98 RefuseAdmission { cause: Cause },
99 Abstain,
100 Lapse,
101}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
105pub enum Outcome {
106 AsIfAbsent,
110 Denied { reason: String },
113 Default(ClassDefault),
115 Discarded {
118 code: SpeechCode,
119 grant: Option<Grant>,
120 },
121}
122
123pub fn resolve_outcome(
129 reply: &HookReply,
130 grants: &GrantSet,
131 hook: &HookId,
132 origin: Origin,
133) -> Outcome {
134 if let HookReply::Err { code, .. } = reply {
138 return match code {
139 ReplyErrCode::Deadline => {
140 Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Deadline))
141 }
142 ReplyErrCode::Crashed => {
143 Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Crash))
144 }
145 ReplyErrCode::Malformed => Outcome::Discarded {
146 code: SpeechCode::Malformed,
147 grant: None,
148 },
149 ReplyErrCode::Unauthorized => Outcome::Discarded {
150 code: SpeechCode::Unauthorized,
151 grant: None,
152 },
153 ReplyErrCode::UnsupportedHook => Outcome::Discarded {
154 code: SpeechCode::UnsupportedHook,
155 grant: None,
156 },
157 ReplyErrCode::ProtocolViolation => Outcome::Discarded {
158 code: SpeechCode::ProtocolViolation,
159 grant: None,
160 },
161 };
162 }
163
164 let shape = reply_shape(reply);
165 let mode = hook.mode();
166 if !shape.legal_at(mode) {
167 return Outcome::Discarded {
169 code: SpeechCode::Malformed,
170 grant: None,
171 };
172 }
173 match shape {
174 ReplyShape::Ack | ReplyShape::FlowNext | ReplyShape::BailNone => Outcome::AsIfAbsent,
175 ReplyShape::FlowStop => {
176 match hook {
179 HookId::AdmitPre if origin != Origin::Channel => {
180 Outcome::Discarded {
184 code: SpeechCode::OriginObserveOnly,
185 grant: Some(Grant::VetoAdmit),
186 }
187 }
188 HookId::AdmitPre if !grants.contains(Grant::VetoAdmit) => Outcome::Discarded {
189 code: SpeechCode::Unauthorized,
190 grant: Some(Grant::VetoAdmit),
191 },
192 HookId::AdmitPre => Outcome::Denied {
193 reason: match reply {
194 HookReply::FlowStop { reason } => reason.clone(),
195 _ => unreachable!("shape checked above"),
196 },
197 },
198 _ => Outcome::Discarded {
199 code: SpeechCode::Malformed,
200 grant: None,
201 },
202 }
203 }
204 ReplyShape::BailDeny => {
205 let required = veto_grant_for_class(hook.class());
206 match required {
207 Some(grant) if grants.contains(grant) => Outcome::Denied {
208 reason: match reply {
209 HookReply::BailDeny { reason } => reason.clone(),
210 _ => unreachable!("shape checked above"),
211 },
212 },
213 Some(grant) => Outcome::Discarded {
214 code: SpeechCode::Unauthorized,
215 grant: Some(grant),
216 },
217 None => Outcome::Discarded {
219 code: SpeechCode::Malformed,
220 grant: None,
221 },
222 }
223 }
224 }
225}
226
227fn reply_shape(reply: &HookReply) -> ReplyShape {
228 match reply {
229 HookReply::Ack { .. } => ReplyShape::Ack,
230 HookReply::FlowNext => ReplyShape::FlowNext,
231 HookReply::FlowStop { .. } => ReplyShape::FlowStop,
232 HookReply::BailNone => ReplyShape::BailNone,
233 HookReply::BailDeny { .. } => ReplyShape::BailDeny,
234 HookReply::Err { .. } => unreachable!("err handled before shaping"),
235 }
236}
237
238pub(crate) fn grant_keyed_default(
243 grants: &GrantSet,
244 hook: &HookId,
245 origin: Origin,
246 cause: Cause,
247) -> ClassDefault {
248 match hook {
249 HookId::ToolPre if grants.contains(Grant::VetoTool) => ClassDefault::Deny { cause },
250 HookId::AdmitPre if origin == Origin::Channel && grants.contains(Grant::VetoAdmit) => {
251 ClassDefault::RefuseAdmission { cause }
252 }
253 HookId::SettlePre if grants.contains(Grant::VetoSettle) => ClassDefault::Abstain,
254 _ => ClassDefault::Lapse,
258 }
259}
260
261#[cfg(test)]
262mod tests {
263 use super::*;
264
265 fn grants(names: &[&str]) -> GrantSet {
266 GrantSet::from_names(names).unwrap()
267 }
268
269 #[test]
270 fn class_default_is_keyed_by_grant() {
271 let tool_pre = HookId::parse("tool/pre").unwrap();
272 let admit_pre = HookId::parse("admit/pre").unwrap();
273 let settle_pre = HookId::parse("settle/pre").unwrap();
274
275 let unreachable = HookReply::Err {
276 code: ReplyErrCode::Crashed,
277 message: "gone".to_string(),
278 };
279
280 assert_eq!(
282 resolve_outcome(
283 &unreachable,
284 &grants(&["observe:tool", "veto:tool"]),
285 &tool_pre,
286 Origin::Api
287 ),
288 Outcome::Default(ClassDefault::Deny {
289 cause: Cause::Crash
290 })
291 );
292 assert_eq!(
294 resolve_outcome(
295 &unreachable,
296 &grants(&["observe:admit", "veto:admit"]),
297 &admit_pre,
298 Origin::Channel
299 ),
300 Outcome::Default(ClassDefault::RefuseAdmission {
301 cause: Cause::Crash
302 })
303 );
304 assert_eq!(
307 resolve_outcome(
308 &unreachable,
309 &grants(&["observe:admit", "veto:admit"]),
310 &admit_pre,
311 Origin::Api
312 ),
313 Outcome::Default(ClassDefault::Lapse)
314 );
315 assert_eq!(
317 resolve_outcome(
318 &unreachable,
319 &grants(&["observe:settle", "veto:settle"]),
320 &settle_pre,
321 Origin::Api
322 ),
323 Outcome::Default(ClassDefault::Abstain)
324 );
325 assert_eq!(
327 resolve_outcome(
328 &unreachable,
329 &grants(&["observe:tool"]),
330 &tool_pre,
331 Origin::Api
332 ),
333 Outcome::Default(ClassDefault::Lapse)
334 );
335 let overdue = HookReply::Err {
337 code: ReplyErrCode::Deadline,
338 message: "slow".to_string(),
339 };
340 assert_eq!(
341 resolve_outcome(
342 &overdue,
343 &grants(&["observe:tool", "veto:tool"]),
344 &tool_pre,
345 Origin::Api
346 ),
347 Outcome::Default(ClassDefault::Deny {
348 cause: Cause::Deadline
349 })
350 );
351 }
352
353 #[test]
354 fn unreachable_veto_denies_at_any_order_position() {
355 let tool_pre = HookId::parse("tool/pre").unwrap();
360 let abstain = HookReply::BailNone;
361 let unreachable = HookReply::Err {
362 code: ReplyErrCode::Deadline,
363 message: "over deadline".to_string(),
364 };
365 let veto_holder = grants(&["observe:tool", "veto:tool"]);
366
367 assert_eq!(
368 resolve_outcome(&abstain, &veto_holder, &tool_pre, Origin::Api),
369 Outcome::AsIfAbsent
370 );
371 assert_eq!(
372 resolve_outcome(&unreachable, &veto_holder, &tool_pre, Origin::Api),
373 Outcome::Default(ClassDefault::Deny {
374 cause: Cause::Deadline
375 })
376 );
377 }
378
379 #[test]
380 fn speech_defects_are_discarded_never_defaulted() {
381 let tool_pre = HookId::parse("tool/pre").unwrap();
384 let observe_only = grants(&["observe:tool"]);
385
386 assert_eq!(
388 resolve_outcome(
389 &HookReply::BailDeny {
390 reason: "no".to_string()
391 },
392 &observe_only,
393 &tool_pre,
394 Origin::Api
395 ),
396 Outcome::Discarded {
397 code: SpeechCode::Unauthorized,
398 grant: Some(Grant::VetoTool)
399 }
400 );
401 assert_eq!(
403 resolve_outcome(
404 &HookReply::Ack { note: None },
405 &observe_only,
406 &tool_pre,
407 Origin::Api
408 ),
409 Outcome::Discarded {
410 code: SpeechCode::Malformed,
411 grant: None
412 }
413 );
414 assert_eq!(
416 resolve_outcome(
417 &HookReply::FlowStop {
418 reason: "no".to_string()
419 },
420 &observe_only,
421 &tool_pre,
422 Origin::Api
423 ),
424 Outcome::Discarded {
425 code: SpeechCode::Malformed,
426 grant: None
427 }
428 );
429 let admit_pre = HookId::parse("admit/pre").unwrap();
432 assert_eq!(
433 resolve_outcome(
434 &HookReply::FlowStop {
435 reason: "no".to_string()
436 },
437 &grants(&["observe:admit", "veto:admit"]),
438 &admit_pre,
439 Origin::Api
440 ),
441 Outcome::Discarded {
442 code: SpeechCode::OriginObserveOnly,
443 grant: Some(Grant::VetoAdmit)
444 }
445 );
446 assert_eq!(
449 resolve_outcome(
450 &HookReply::FlowStop {
451 reason: "off-hours".to_string()
452 },
453 &grants(&["observe:admit", "veto:admit"]),
454 &admit_pre,
455 Origin::Channel
456 ),
457 Outcome::Denied {
458 reason: "off-hours".to_string()
459 }
460 );
461 assert_eq!(
463 resolve_outcome(
464 &HookReply::BailDeny {
465 reason: "blocked".to_string()
466 },
467 &grants(&["observe:tool", "veto:tool"]),
468 &tool_pre,
469 Origin::Api
470 ),
471 Outcome::Denied {
472 reason: "blocked".to_string()
473 }
474 );
475 assert_eq!(
476 resolve_outcome(
477 &HookReply::BailNone,
478 &grants(&["observe:settle", "veto:settle"]),
479 &HookId::parse("settle/pre").unwrap(),
480 Origin::Api
481 ),
482 Outcome::AsIfAbsent
483 );
484 }
485}