A web app lists pending requests with full detail, leases and the audit log grouped by request, and sends Web Push notifications that can answer requests. Approvers sign in through an OpenID Connect provider; policy can require a fresh passkey (`confirm = "passkey"`) to approve, which only the web app can then give. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
282 lines
10 KiB
Rust
282 lines
10 KiB
Rust
//! The audit log gathered by request: what was asked, who approved it and how it ended.
|
|
use std::collections::HashMap;
|
|
|
|
use crate::audit::{Event, Record};
|
|
use crate::proto::ApprovedBy;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Outcome {
|
|
/// Waiting for an approver, or for its preparation.
|
|
Waiting,
|
|
/// Approved and not finished yet.
|
|
Running,
|
|
Succeeded,
|
|
/// Ran, and exited with a non-zero status.
|
|
ExitedWith(i32),
|
|
Failed,
|
|
Denied,
|
|
Expired,
|
|
Cancelled,
|
|
Invalid,
|
|
}
|
|
|
|
impl Outcome {
|
|
pub fn name(&self) -> String {
|
|
match self {
|
|
Outcome::Waiting => "Waiting".into(),
|
|
Outcome::Running => "Running".into(),
|
|
Outcome::Succeeded => "Succeeded".into(),
|
|
Outcome::ExitedWith(status) => format!("Exit status {status}"),
|
|
Outcome::Failed => "Failed".into(),
|
|
Outcome::Denied => "Denied".into(),
|
|
Outcome::Expired => "Expired".into(),
|
|
Outcome::Cancelled => "Cancelled".into(),
|
|
Outcome::Invalid => "Invalid".into(),
|
|
}
|
|
}
|
|
|
|
/// For styling: good, bad, or still going.
|
|
pub fn tone(&self) -> &'static str {
|
|
match self {
|
|
Outcome::Waiting | Outcome::Running => "active",
|
|
Outcome::Succeeded => "good",
|
|
Outcome::Expired | Outcome::Cancelled => "neutral",
|
|
_ => "bad",
|
|
}
|
|
}
|
|
|
|
/// The filter value for this outcome.
|
|
pub fn key(&self) -> &'static str {
|
|
match self {
|
|
Outcome::Waiting | Outcome::Running => "active",
|
|
Outcome::Succeeded => "succeeded",
|
|
Outcome::ExitedWith(_) | Outcome::Failed | Outcome::Invalid => "failed",
|
|
Outcome::Denied => "denied",
|
|
Outcome::Expired | Outcome::Cancelled => "expired",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Everything the log says about one request.
|
|
#[derive(Debug, Clone)]
|
|
pub struct History {
|
|
pub id: String,
|
|
pub time: String,
|
|
pub requester: String,
|
|
pub op: String,
|
|
pub summary: String,
|
|
pub label: Option<String>,
|
|
pub outcome: Outcome,
|
|
/// Who approved it (the request itself, not a step before it), with the answer they gave.
|
|
pub approved_by: Option<String>,
|
|
pub records: Vec<Record>,
|
|
}
|
|
|
|
fn id_of(event: &Event) -> Option<&str> {
|
|
match event {
|
|
Event::Unknown { .. } => None,
|
|
Event::Invalid { id, .. }
|
|
| Event::Request { id, .. }
|
|
| Event::Prepared { id, .. }
|
|
| Event::Approved { id, .. }
|
|
| Event::Decision { id, .. }
|
|
| Event::Denied { id, .. }
|
|
| Event::Expired { id }
|
|
| Event::Disconnected { id }
|
|
| Event::Finished { id, .. }
|
|
| Event::Failed { id, .. } => Some(id),
|
|
}
|
|
}
|
|
|
|
/// Groups records (oldest first) by request, newest request first. Connections from unknown
|
|
/// peers have no request and are returned separately.
|
|
pub fn gather(records: Vec<Record>) -> (Vec<History>, Vec<Record>) {
|
|
let mut order: Vec<String> = vec![];
|
|
let mut by_id: HashMap<String, Vec<Record>> = HashMap::new();
|
|
let mut unknown = vec![];
|
|
|
|
for record in records {
|
|
match id_of(&record.event) {
|
|
Some(id) => {
|
|
if !by_id.contains_key(id) {
|
|
order.push(id.to_owned());
|
|
}
|
|
by_id.entry(id.to_owned()).or_default().push(record);
|
|
}
|
|
None => unknown.push(record),
|
|
}
|
|
}
|
|
|
|
let histories = order.into_iter().rev().map(|id| summarize(id.clone(), by_id.remove(&id).unwrap())).collect();
|
|
unknown.reverse();
|
|
(histories, unknown)
|
|
}
|
|
|
|
fn summarize(id: String, records: Vec<Record>) -> History {
|
|
let mut history = History {
|
|
id,
|
|
time: records[0].time.clone(),
|
|
requester: String::new(),
|
|
op: String::new(),
|
|
summary: String::new(),
|
|
label: None,
|
|
outcome: Outcome::Waiting,
|
|
approved_by: None,
|
|
records: vec![],
|
|
};
|
|
let mut answer = None;
|
|
|
|
for record in &records {
|
|
match &record.event {
|
|
Event::Request { requester, op, summary, label, .. } => {
|
|
history.requester = requester.clone();
|
|
history.op = op.clone();
|
|
history.summary = summary.clone();
|
|
history.label = label.clone();
|
|
}
|
|
Event::Invalid { requester, error, .. } => {
|
|
history.requester = requester.clone();
|
|
history.summary = error.clone();
|
|
history.outcome = Outcome::Invalid;
|
|
}
|
|
Event::Decision { decision, approver, .. } => answer = Some((decision.clone(), approver.clone())),
|
|
Event::Approved { by, approver, step: None, .. } => {
|
|
history.outcome = Outcome::Running;
|
|
history.approved_by = Some(match (by, approver, &answer) {
|
|
(ApprovedBy::Policy, _, _) => "policy".into(),
|
|
(ApprovedBy::Lease, _, _) => "a lease".into(),
|
|
(ApprovedBy::Approver, Some(approver), Some((decision, _))) => format!("{approver} ({decision})"),
|
|
(ApprovedBy::Approver, Some(approver), None) => approver.clone(),
|
|
(ApprovedBy::Approver, None, _) => "an approver".into(),
|
|
});
|
|
}
|
|
Event::Denied { .. } => history.outcome = Outcome::Denied,
|
|
Event::Expired { .. } => history.outcome = Outcome::Expired,
|
|
Event::Disconnected { .. } => history.outcome = Outcome::Cancelled,
|
|
Event::Finished { exit: 0, .. } => history.outcome = Outcome::Succeeded,
|
|
Event::Finished { exit, .. } => history.outcome = Outcome::ExitedWith(*exit),
|
|
Event::Failed { .. } => history.outcome = Outcome::Failed,
|
|
Event::Unknown { .. } | Event::Prepared { .. } | Event::Approved { .. } => {}
|
|
}
|
|
}
|
|
|
|
history.records = records;
|
|
history
|
|
}
|
|
|
|
/// One event in a request's timeline: what happened, and any longer text to show with it.
|
|
pub fn describe(event: &Event) -> (String, Option<String>) {
|
|
match event {
|
|
Event::Unknown { peer } => (format!("Refused a connection from {peer}"), None),
|
|
Event::Invalid { error, .. } => (format!("Invalid request: {error}"), None),
|
|
Event::Request { summary, detail, label, .. } => {
|
|
let mut text = format!("Asked: {summary}");
|
|
if let Some(label) = label {
|
|
text += &format!(" (label: {label})");
|
|
}
|
|
(text, Some(detail.clone()).filter(|d| !d.is_empty()))
|
|
}
|
|
Event::Prepared { detail, .. } => {
|
|
("Prepared for approval".into(), Some(detail.clone()).filter(|d| !d.is_empty()))
|
|
}
|
|
Event::Approved { by, approver, step, .. } => {
|
|
let by = match (by, approver) {
|
|
(ApprovedBy::Policy, _) => "policy".to_owned(),
|
|
(ApprovedBy::Lease, _) => "a lease".to_owned(),
|
|
(ApprovedBy::Approver, Some(approver)) => approver.clone(),
|
|
(ApprovedBy::Approver, None) => "an approver".to_owned(),
|
|
};
|
|
match step {
|
|
Some(step) => (format!("Use of the {step} approved by {by}"), None),
|
|
None => (format!("Approved by {by}"), None),
|
|
}
|
|
}
|
|
Event::Decision { decision, approver, .. } => (format!("{approver} answered {decision}"), None),
|
|
Event::Denied { reason, .. } => (format!("Refused: {reason}"), None),
|
|
Event::Expired { .. } => ("Expired without an answer".into(), None),
|
|
Event::Disconnected { .. } => ("Cancelled: the requester disconnected".into(), None),
|
|
Event::Finished { exit, seconds, .. } => {
|
|
(format!("Finished with exit status {exit} after {seconds:.1}s"), None)
|
|
}
|
|
Event::Failed { error, .. } => (format!("Failed: {error}"), None),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn record(time: &str, event: Event) -> Record {
|
|
Record { time: time.into(), event }
|
|
}
|
|
|
|
fn request(id: &str) -> Event {
|
|
Event::Request {
|
|
id: id.into(),
|
|
requester: "slop-cannon".into(),
|
|
op: "ssh".into(),
|
|
summary: format!("ssh {id}"),
|
|
detail: "Command: uptime".into(),
|
|
label: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn gathers_requests_and_their_outcomes() {
|
|
let records = vec![
|
|
record("1", request("a")),
|
|
record("2", request("b")),
|
|
record("3", Event::Unknown { peer: "vsock CID 9".into() }),
|
|
record(
|
|
"4",
|
|
Event::Decision {
|
|
id: "a".into(),
|
|
requester: "slop-cannon".into(),
|
|
summary: "ssh a".into(),
|
|
decision: "5m".into(),
|
|
approver: "dev (web)".into(),
|
|
},
|
|
),
|
|
record(
|
|
"5",
|
|
Event::Approved {
|
|
id: "a".into(),
|
|
by: ApprovedBy::Approver,
|
|
approver: Some("dev (web)".into()),
|
|
step: None,
|
|
},
|
|
),
|
|
record("6", Event::Finished { id: "a".into(), exit: 1, seconds: 0.5 }),
|
|
record("7", Event::Expired { id: "b".into() }),
|
|
record("8", request("c")),
|
|
record("9", Event::Approved { id: "c".into(), by: ApprovedBy::Lease, approver: None, step: None }),
|
|
];
|
|
|
|
let (histories, unknown) = gather(records);
|
|
assert_eq!(unknown.len(), 1);
|
|
let ids: Vec<_> = histories.iter().map(|h| h.id.as_str()).collect();
|
|
assert_eq!(ids, ["c", "b", "a"]);
|
|
|
|
assert_eq!(histories[0].outcome, Outcome::Running);
|
|
assert_eq!(histories[0].approved_by.as_deref(), Some("a lease"));
|
|
assert_eq!(histories[1].outcome, Outcome::Expired);
|
|
assert_eq!(histories[2].outcome, Outcome::ExitedWith(1));
|
|
assert_eq!(histories[2].approved_by.as_deref(), Some("dev (web) (5m)"));
|
|
assert_eq!(histories[2].records.len(), 4);
|
|
}
|
|
|
|
#[test]
|
|
fn steps_before_the_request_dont_count_as_approving_it() {
|
|
let records = vec![
|
|
record("1", request("a")),
|
|
record(
|
|
"2",
|
|
Event::Approved { id: "a".into(), by: ApprovedBy::Lease, approver: None, step: Some("builder".into()) },
|
|
),
|
|
];
|
|
let (histories, _) = gather(records);
|
|
assert_eq!(histories[0].outcome, Outcome::Waiting);
|
|
assert_eq!(histories[0].approved_by, None);
|
|
}
|
|
}
|