use super::grants::{Grant, GrantSet, ReplyShape, veto_grant_for_class};
use super::ids::HookId;
use super::registry::{HookReply, ReplyErrCode};
use super::views::Origin;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cause {
Deadline,
Crash,
Quarantine,
Busy,
HandshakeIncomplete,
EofWithoutReply,
}
impl Cause {
pub fn as_str(self) -> &'static str {
match self {
Cause::Deadline => "deadline",
Cause::Crash => "crash",
Cause::Quarantine => "quarantine",
Cause::Busy => "busy",
Cause::HandshakeIncomplete => "handshake_incomplete",
Cause::EofWithoutReply => "eof_without_reply",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpeechCode {
Unauthorized,
Malformed,
UnsupportedHook,
ProtocolViolation,
Unparseable,
OriginObserveOnly,
}
impl SpeechCode {
pub fn as_str(self) -> &'static str {
match self {
SpeechCode::Unauthorized => "unauthorized",
SpeechCode::Malformed => "malformed",
SpeechCode::UnsupportedHook => "unsupported_hook",
SpeechCode::ProtocolViolation => "protocol_violation",
SpeechCode::Unparseable => "unparseable",
SpeechCode::OriginObserveOnly => "origin_observe_only",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailureClass {
Unreachable(Cause),
Speech(SpeechCode),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClassDefault {
Deny { cause: Cause },
RefuseAdmission { cause: Cause },
Abstain,
Lapse,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
AsIfAbsent,
Denied { reason: String },
Default(ClassDefault),
Discarded {
code: SpeechCode,
grant: Option<Grant>,
},
}
pub fn resolve_outcome(
reply: &HookReply,
grants: &GrantSet,
hook: &HookId,
origin: Origin,
) -> Outcome {
if let HookReply::Err { code, .. } = reply {
return match code {
ReplyErrCode::Deadline => {
Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Deadline))
}
ReplyErrCode::Crashed => {
Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Crash))
}
ReplyErrCode::Malformed => Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None,
},
ReplyErrCode::Unauthorized => Outcome::Discarded {
code: SpeechCode::Unauthorized,
grant: None,
},
ReplyErrCode::UnsupportedHook => Outcome::Discarded {
code: SpeechCode::UnsupportedHook,
grant: None,
},
ReplyErrCode::ProtocolViolation => Outcome::Discarded {
code: SpeechCode::ProtocolViolation,
grant: None,
},
};
}
let shape = reply_shape(reply);
let mode = hook.mode();
if !shape.legal_at(mode) {
return Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None,
};
}
match shape {
ReplyShape::Ack | ReplyShape::FlowNext | ReplyShape::BailNone => Outcome::AsIfAbsent,
ReplyShape::FlowStop => {
match hook {
HookId::AdmitPre if origin != Origin::Channel => {
Outcome::Discarded {
code: SpeechCode::OriginObserveOnly,
grant: Some(Grant::VetoAdmit),
}
}
HookId::AdmitPre if !grants.contains(Grant::VetoAdmit) => Outcome::Discarded {
code: SpeechCode::Unauthorized,
grant: Some(Grant::VetoAdmit),
},
HookId::AdmitPre => Outcome::Denied {
reason: match reply {
HookReply::FlowStop { reason } => reason.clone(),
_ => unreachable!("shape checked above"),
},
},
_ => Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None,
},
}
}
ReplyShape::BailDeny => {
let required = veto_grant_for_class(hook.class());
match required {
Some(grant) if grants.contains(grant) => Outcome::Denied {
reason: match reply {
HookReply::BailDeny { reason } => reason.clone(),
_ => unreachable!("shape checked above"),
},
},
Some(grant) => Outcome::Discarded {
code: SpeechCode::Unauthorized,
grant: Some(grant),
},
None => Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None,
},
}
}
}
}
fn reply_shape(reply: &HookReply) -> ReplyShape {
match reply {
HookReply::Ack { .. } => ReplyShape::Ack,
HookReply::FlowNext => ReplyShape::FlowNext,
HookReply::FlowStop { .. } => ReplyShape::FlowStop,
HookReply::BailNone => ReplyShape::BailNone,
HookReply::BailDeny { .. } => ReplyShape::BailDeny,
HookReply::Err { .. } => unreachable!("err handled before shaping"),
}
}
pub(crate) fn grant_keyed_default(
grants: &GrantSet,
hook: &HookId,
origin: Origin,
cause: Cause,
) -> ClassDefault {
match hook {
HookId::ToolPre if grants.contains(Grant::VetoTool) => ClassDefault::Deny { cause },
HookId::AdmitPre if origin == Origin::Channel && grants.contains(Grant::VetoAdmit) => {
ClassDefault::RefuseAdmission { cause }
}
HookId::SettlePre if grants.contains(Grant::VetoSettle) => ClassDefault::Abstain,
_ => ClassDefault::Lapse,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn grants(names: &[&str]) -> GrantSet {
GrantSet::from_names(names).unwrap()
}
#[test]
fn class_default_is_keyed_by_grant() {
let tool_pre = HookId::parse("tool/pre").unwrap();
let admit_pre = HookId::parse("admit/pre").unwrap();
let settle_pre = HookId::parse("settle/pre").unwrap();
let unreachable = HookReply::Err {
code: ReplyErrCode::Crashed,
message: "gone".to_string(),
};
assert_eq!(
resolve_outcome(
&unreachable,
&grants(&["observe:tool", "veto:tool"]),
&tool_pre,
Origin::Api
),
Outcome::Default(ClassDefault::Deny {
cause: Cause::Crash
})
);
assert_eq!(
resolve_outcome(
&unreachable,
&grants(&["observe:admit", "veto:admit"]),
&admit_pre,
Origin::Channel
),
Outcome::Default(ClassDefault::RefuseAdmission {
cause: Cause::Crash
})
);
assert_eq!(
resolve_outcome(
&unreachable,
&grants(&["observe:admit", "veto:admit"]),
&admit_pre,
Origin::Api
),
Outcome::Default(ClassDefault::Lapse)
);
assert_eq!(
resolve_outcome(
&unreachable,
&grants(&["observe:settle", "veto:settle"]),
&settle_pre,
Origin::Api
),
Outcome::Default(ClassDefault::Abstain)
);
assert_eq!(
resolve_outcome(
&unreachable,
&grants(&["observe:tool"]),
&tool_pre,
Origin::Api
),
Outcome::Default(ClassDefault::Lapse)
);
let overdue = HookReply::Err {
code: ReplyErrCode::Deadline,
message: "slow".to_string(),
};
assert_eq!(
resolve_outcome(
&overdue,
&grants(&["observe:tool", "veto:tool"]),
&tool_pre,
Origin::Api
),
Outcome::Default(ClassDefault::Deny {
cause: Cause::Deadline
})
);
}
#[test]
fn unreachable_veto_denies_at_any_order_position() {
let tool_pre = HookId::parse("tool/pre").unwrap();
let abstain = HookReply::BailNone;
let unreachable = HookReply::Err {
code: ReplyErrCode::Deadline,
message: "over deadline".to_string(),
};
let veto_holder = grants(&["observe:tool", "veto:tool"]);
assert_eq!(
resolve_outcome(&abstain, &veto_holder, &tool_pre, Origin::Api),
Outcome::AsIfAbsent
);
assert_eq!(
resolve_outcome(&unreachable, &veto_holder, &tool_pre, Origin::Api),
Outcome::Default(ClassDefault::Deny {
cause: Cause::Deadline
})
);
}
#[test]
fn speech_defects_are_discarded_never_defaulted() {
let tool_pre = HookId::parse("tool/pre").unwrap();
let observe_only = grants(&["observe:tool"]);
assert_eq!(
resolve_outcome(
&HookReply::BailDeny {
reason: "no".to_string()
},
&observe_only,
&tool_pre,
Origin::Api
),
Outcome::Discarded {
code: SpeechCode::Unauthorized,
grant: Some(Grant::VetoTool)
}
);
assert_eq!(
resolve_outcome(
&HookReply::Ack { note: None },
&observe_only,
&tool_pre,
Origin::Api
),
Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None
}
);
assert_eq!(
resolve_outcome(
&HookReply::FlowStop {
reason: "no".to_string()
},
&observe_only,
&tool_pre,
Origin::Api
),
Outcome::Discarded {
code: SpeechCode::Malformed,
grant: None
}
);
let admit_pre = HookId::parse("admit/pre").unwrap();
assert_eq!(
resolve_outcome(
&HookReply::FlowStop {
reason: "no".to_string()
},
&grants(&["observe:admit", "veto:admit"]),
&admit_pre,
Origin::Api
),
Outcome::Discarded {
code: SpeechCode::OriginObserveOnly,
grant: Some(Grant::VetoAdmit)
}
);
assert_eq!(
resolve_outcome(
&HookReply::FlowStop {
reason: "off-hours".to_string()
},
&grants(&["observe:admit", "veto:admit"]),
&admit_pre,
Origin::Channel
),
Outcome::Denied {
reason: "off-hours".to_string()
}
);
assert_eq!(
resolve_outcome(
&HookReply::BailDeny {
reason: "blocked".to_string()
},
&grants(&["observe:tool", "veto:tool"]),
&tool_pre,
Origin::Api
),
Outcome::Denied {
reason: "blocked".to_string()
}
);
assert_eq!(
resolve_outcome(
&HookReply::BailNone,
&grants(&["observe:settle", "veto:settle"]),
&HookId::parse("settle/pre").unwrap(),
Origin::Api
),
Outcome::AsIfAbsent
);
}
}