use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt;
use core::time::Duration;
use std::thread::ThreadId;
mod no_audit;
pub use self::no_audit::NoAudit;
#[cfg(feature = "monitor-tracing")]
mod log_audit;
#[cfg(feature = "monitor-tracing")]
pub use self::log_audit::LogAudit;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AccessKind {
Register,
Unregister,
Read,
Rotate,
OneShotFragment,
OneShotDefragment,
MasterUnlockAttempt {
matched: bool,
},
}
impl fmt::Display for AccessKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Register => f.write_str("register"),
Self::Unregister => f.write_str("unregister"),
Self::Read => f.write_str("read"),
Self::Rotate => f.write_str("rotate"),
Self::OneShotFragment => f.write_str("one-shot-fragment"),
Self::OneShotDefragment => f.write_str("one-shot-defragment"),
Self::MasterUnlockAttempt { matched: true } => f.write_str("master-unlock-ok"),
Self::MasterUnlockAttempt { matched: false } => f.write_str("master-unlock-fail"),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct AuditEvent {
pub timestamp: Duration,
pub key_name: String,
pub kind: AccessKind,
pub thread_id: ThreadId,
pub note: Cow<'static, str>,
}
pub trait AuditSink: Send + Sync {
fn on_event(&self, event: &AuditEvent);
#[inline]
fn is_no_op(&self) -> bool {
false
}
}
impl AuditSink for alloc::sync::Arc<dyn AuditSink> {
fn on_event(&self, event: &AuditEvent) {
(**self).on_event(event);
}
#[inline]
fn is_no_op(&self) -> bool {
(**self).is_no_op()
}
}