use crate::{CanaryKind, TriggerSource};
use core::fmt;
use std::time::SystemTime;
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CanaryEvent {
pub token_id: String,
pub token_name: String,
pub kind: CanaryKind,
pub source: TriggerSource,
pub triggered_at: SystemTime,
pub correlation_id: Option<String>,
pub thread_id: Option<String>,
}
impl CanaryEvent {
#[must_use]
pub fn new(
token_id: impl Into<String>,
token_name: impl Into<String>,
kind: CanaryKind,
source: TriggerSource,
) -> Self {
Self {
token_id: token_id.into(),
token_name: token_name.into(),
kind,
source,
triggered_at: SystemTime::now(),
correlation_id: None,
thread_id: Some(format!("{:?}", std::thread::current().id())),
}
}
#[must_use]
pub fn with_triggered_at(mut self, triggered_at: SystemTime) -> Self {
self.triggered_at = triggered_at;
self
}
#[must_use]
pub fn with_correlation_id(mut self, correlation_id: impl Into<String>) -> Self {
self.correlation_id = Some(correlation_id.into());
self
}
#[must_use]
pub fn with_thread_id(mut self, thread_id: impl Into<String>) -> Self {
self.thread_id = Some(thread_id.into());
self
}
}
impl fmt::Debug for CanaryEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("CanaryEvent");
d.field("token_id", &self.token_id)
.field("token_name", &self.token_name)
.field("kind", &self.kind)
.field("source", &self.source)
.field("triggered_at", &self.triggered_at);
if let Some(ref cid) = self.correlation_id {
d.field("correlation_id", cid);
}
if let Some(ref tid) = self.thread_id {
d.field("thread_id", tid);
}
d.finish()
}
}
impl fmt::Display for CanaryEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"canary event: token={token} id={id} kind={kind} source={source}",
token = self.token_name,
id = self.token_id,
kind = self.kind,
source = self.source,
)?;
if let Some(ref cid) = self.correlation_id {
write!(f, " correlation_id={cid}")?;
}
if let Some(ref tid) = self.thread_id {
write!(f, " thread_id={tid}")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::CanaryEvent;
use crate::{CanaryKind, TriggerSource};
use std::time::SystemTime;
fn sample_event() -> CanaryEvent {
CanaryEvent::new(
"id-1",
"prod.db.password",
CanaryKind::Password,
TriggerSource::Reveal,
)
.with_triggered_at(SystemTime::UNIX_EPOCH)
}
#[test]
fn new_stores_metadata() {
let event = sample_event();
assert_eq!(event.token_id, "id-1");
assert_eq!(event.token_name, "prod.db.password");
assert_eq!(event.kind, CanaryKind::Password);
assert_eq!(event.source, TriggerSource::Reveal);
assert_eq!(event.triggered_at, SystemTime::UNIX_EPOCH);
}
#[test]
fn new_captures_thread_id() {
let event = CanaryEvent::new("id-1", "test", CanaryKind::Password, TriggerSource::Reveal);
assert!(event.thread_id.is_some());
let tid = event.thread_id.as_ref().unwrap();
assert!(!tid.is_empty());
}
#[test]
fn new_has_no_correlation_id_by_default() {
let event = CanaryEvent::new("id-1", "test", CanaryKind::Password, TriggerSource::Reveal);
assert!(event.correlation_id.is_none());
}
#[test]
fn with_triggered_at_replaces_timestamp() {
let event = CanaryEvent::new(
"id-1",
"prod.db.password",
CanaryKind::Password,
TriggerSource::Reveal,
)
.with_triggered_at(SystemTime::UNIX_EPOCH);
assert_eq!(event.triggered_at, SystemTime::UNIX_EPOCH);
}
#[test]
fn with_correlation_id_sets_value() {
let event = sample_event().with_correlation_id("req-abc-123");
assert_eq!(event.correlation_id.as_deref(), Some("req-abc-123"));
}
#[test]
fn with_thread_id_overrides_default() {
let event = sample_event().with_thread_id("tokio-task-42");
assert_eq!(event.thread_id.as_deref(), Some("tokio-task-42"));
}
#[test]
fn display_contains_metadata() {
let event = sample_event();
let display = event.to_string();
assert!(display.contains("token=prod.db.password"));
assert!(display.contains("id=id-1"));
assert!(display.contains("kind=password"));
assert!(display.contains("source=reveal"));
}
#[test]
fn display_includes_optional_fields_when_present() {
let event = sample_event()
.with_correlation_id("req-1")
.with_thread_id("thread-1");
let display = event.to_string();
assert!(display.contains("correlation_id=req-1"));
assert!(display.contains("thread_id=thread-1"));
}
#[test]
fn display_omits_optional_fields_when_absent() {
let event = CanaryEvent::new("id-1", "test", CanaryKind::Password, TriggerSource::Reveal)
.with_triggered_at(SystemTime::UNIX_EPOCH);
let event = CanaryEvent {
thread_id: None,
..event
};
let display = event.to_string();
assert!(!display.contains("correlation_id"));
assert!(!display.contains("thread_id"));
}
#[test]
fn display_does_not_include_canary_value() {
let canary_value = "canary-fake-password-9f8a7b";
let event = sample_event();
let display = event.to_string();
assert!(!display.contains(canary_value));
}
#[test]
fn debug_contains_metadata() {
let event = sample_event();
let debug = format!("{event:?}");
assert!(debug.contains("CanaryEvent"));
assert!(debug.contains("token_id"));
assert!(debug.contains("id-1"));
assert!(debug.contains("token_name"));
assert!(debug.contains("prod.db.password"));
assert!(debug.contains("kind"));
assert!(debug.contains("source"));
assert!(debug.contains("triggered_at"));
}
#[test]
fn debug_includes_optional_fields_when_present() {
let event = sample_event()
.with_correlation_id("req-1")
.with_thread_id("thread-1");
let debug = format!("{event:?}");
assert!(debug.contains("correlation_id"));
assert!(debug.contains("req-1"));
assert!(debug.contains("thread_id"));
assert!(debug.contains("thread-1"));
}
#[test]
fn debug_does_not_include_canary_value() {
let canary_value = "canary-fake-password-9f8a7b";
let event = sample_event();
let debug = format!("{event:?}");
assert!(!debug.contains(canary_value));
}
#[test]
fn clone_and_eq_work() {
let event = sample_event().with_correlation_id("req-1");
let cloned = event.clone();
assert_eq!(event, cloned);
}
#[test]
fn supports_custom_kind_and_source() {
let event = CanaryEvent::new(
"id-2",
"ldap.bind.password",
CanaryKind::custom("ldap"),
TriggerSource::custom("serde"),
)
.with_triggered_at(SystemTime::UNIX_EPOCH);
assert_eq!(event.kind.to_string(), "custom:ldap");
assert_eq!(event.source.to_string(), "custom:serde");
}
}