use crate::domain::auth::{ApiKeyScope, AuditDecision, AuditLogEntry};
#[derive(Clone, Debug)]
pub struct AuditLogBuilder {
pub api_key_id: Option<crate::domain::id::Id>,
pub team_id: Option<crate::domain::id::Id>,
pub requested_action: String,
pub decision: AuditDecision,
pub denial_reason: Option<String>,
pub scope_used: Option<ApiKeyScope>,
pub ip_address: Option<std::net::IpAddr>,
pub trace_id: Option<crate::domain::id::Id>,
pub user_agent: Option<String>,
pub request_path: Option<String>,
pub request_method: Option<String>,
pub metadata: serde_json::Value,
}
impl AuditLogBuilder {
pub fn new(action: impl Into<String>, decision: AuditDecision) -> Self {
Self {
api_key_id: None,
team_id: None,
requested_action: action.into(),
decision,
denial_reason: None,
scope_used: None,
ip_address: None,
trace_id: None,
user_agent: None,
request_path: None,
request_method: None,
metadata: serde_json::Value::Object(serde_json::Map::new()),
}
}
pub fn with_api_key_id(mut self, id: crate::domain::id::Id) -> Self {
self.api_key_id = Some(id);
self
}
pub fn with_team_id(mut self, id: crate::domain::id::Id) -> Self {
self.team_id = Some(id);
self
}
pub fn with_denial_reason(mut self, reason: impl Into<String>) -> Self {
self.denial_reason = Some(reason.into());
self
}
pub fn with_scope(mut self, scope: ApiKeyScope) -> Self {
self.scope_used = Some(scope);
self
}
pub fn maybe_with_api_key_id(mut self, id: Option<crate::domain::id::Id>) -> Self {
self.api_key_id = id.filter(|id| !id.is_nil());
self
}
pub fn maybe_with_team_id(mut self, id: Option<crate::domain::id::Id>) -> Self {
self.team_id = id.filter(|id| !id.is_nil());
self
}
pub fn maybe_with_scope(mut self, scope: Option<ApiKeyScope>) -> Self {
self.scope_used = scope;
self
}
pub fn with_ip_address(mut self, ip: impl Into<std::net::IpAddr>) -> Self {
self.ip_address = Some(ip.into());
self
}
pub fn with_trace_id(mut self, id: crate::domain::id::Id) -> Self {
self.trace_id = Some(id);
self
}
pub fn with_user_agent(mut self, ua: impl Into<String>) -> Self {
self.user_agent = Some(ua.into());
self
}
pub fn with_request_path(mut self, path: impl Into<String>) -> Self {
self.request_path = Some(path.into());
self
}
pub fn with_request_method(mut self, method: impl Into<String>) -> Self {
self.request_method = Some(method.into());
self
}
pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
if let serde_json::Value::Object(map) = &mut self.metadata {
map.insert(key.into(), value);
}
self
}
pub fn build(self) -> AuditLogEntry {
AuditLogEntry {
id: crate::domain::id::Id::generate(),
api_key_id: self.api_key_id,
team_id: self.team_id,
requested_action: self.requested_action,
decision: self.decision,
denial_reason: self.denial_reason,
scope_used: self.scope_used,
ip_address: self.ip_address,
trace_id: self.trace_id,
user_agent: self.user_agent,
request_path: self.request_path,
request_method: self.request_method,
metadata: self.metadata,
created_at: chrono::Utc::now(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use std::net::Ipv4Addr;
#[test]
fn test_audit_log_builder_new() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow);
assert_eq!(builder.requested_action, "test_action");
assert_eq!(builder.decision, AuditDecision::Allow);
}
#[test]
fn test_audit_log_builder_with_api_key_id() {
let api_key_id = crate::domain::id::Id::generate();
let builder =
AuditLogBuilder::new("test_action", AuditDecision::Allow).with_api_key_id(api_key_id);
assert_eq!(builder.api_key_id, Some(api_key_id));
}
#[test]
fn test_audit_log_builder_with_team_id() {
let team_id = crate::domain::id::Id::generate();
let builder =
AuditLogBuilder::new("test_action", AuditDecision::Allow).with_team_id(team_id);
assert_eq!(builder.team_id, Some(team_id));
}
#[test]
fn test_maybe_with_nil_ids_normalizes_to_none() {
let nil = crate::domain::id::Id::nil();
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.maybe_with_api_key_id(Some(nil))
.maybe_with_team_id(Some(nil));
assert_eq!(builder.api_key_id, None, "nil api key id must be dropped");
assert_eq!(builder.team_id, None, "nil team id must be dropped");
}
#[test]
fn test_maybe_with_non_nil_ids_keeps_values() {
let api_key_id = crate::domain::id::Id::generate();
let team_id = crate::domain::id::Id::generate();
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.maybe_with_api_key_id(Some(api_key_id))
.maybe_with_team_id(Some(team_id));
assert_eq!(builder.api_key_id, Some(api_key_id));
assert_eq!(builder.team_id, Some(team_id));
}
#[test]
fn test_audit_log_builder_with_denial_reason() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Deny)
.with_denial_reason("insufficient permissions");
assert_eq!(
builder.denial_reason,
Some("insufficient permissions".to_string())
);
}
#[test]
fn test_audit_log_builder_with_ip_address() {
let ip: std::net::IpAddr = Ipv4Addr::new(192, 168, 1, 1).into();
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow).with_ip_address(ip);
assert!(builder.ip_address.is_some());
}
#[test]
fn test_audit_log_builder_with_trace_id() {
let trace_id = crate::domain::id::Id::generate();
let builder =
AuditLogBuilder::new("test_action", AuditDecision::Allow).with_trace_id(trace_id);
assert_eq!(builder.trace_id, Some(trace_id));
}
#[test]
fn test_audit_log_builder_with_user_agent() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.with_user_agent("Test Agent/1.0");
assert_eq!(builder.user_agent, Some("Test Agent/1.0".to_string()));
}
#[test]
fn test_audit_log_builder_with_request_path() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.with_request_path("/api/v1/test");
assert_eq!(builder.request_path, Some("/api/v1/test".to_string()));
}
#[test]
fn test_audit_log_builder_with_request_method() {
let builder =
AuditLogBuilder::new("test_action", AuditDecision::Allow).with_request_method("POST");
assert_eq!(builder.request_method, Some("POST".to_string()));
}
#[test]
fn test_audit_log_builder_with_metadata() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.with_metadata("key1", json!("value1"))
.with_metadata("key2", json!(123));
match &builder.metadata {
serde_json::Value::Object(map) => {
assert_eq!(map.get("key1"), Some(&json!("value1")));
assert_eq!(map.get("key2"), Some(&json!(123)));
}
_ => panic!("Expected object metadata"),
}
}
#[test]
fn test_audit_log_builder_build_returns_entry() {
let api_key_id = crate::domain::id::Id::generate();
let team_id = crate::domain::id::Id::generate();
let trace_id = crate::domain::id::Id::generate();
let ip: std::net::IpAddr = Ipv4Addr::new(10, 0, 0, 1).into();
let entry = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.with_api_key_id(api_key_id)
.with_team_id(team_id)
.with_trace_id(trace_id)
.with_ip_address(ip)
.with_request_path("/api/test")
.with_request_method("GET")
.build();
assert_eq!(entry.requested_action, "test_action");
assert_eq!(entry.decision, AuditDecision::Allow);
assert_eq!(entry.api_key_id, Some(api_key_id));
assert_eq!(entry.team_id, Some(team_id));
assert_eq!(entry.trace_id, Some(trace_id));
assert_eq!(entry.request_path, Some("/api/test".to_string()));
assert_eq!(entry.request_method, Some("GET".to_string()));
assert!(entry.id != crate::domain::id::Id::nil());
assert!(entry.created_at <= chrono::Utc::now());
}
#[test]
fn test_audit_log_builder_with_scope() {
let scope = ApiKeyScope::default();
let builder =
AuditLogBuilder::new("test_action", AuditDecision::Allow).with_scope(scope.clone());
assert_eq!(builder.scope_used, Some(scope));
}
#[test]
fn test_maybe_with_api_key_id_none_preserves_none() {
let builder =
AuditLogBuilder::new("anon.deny", AuditDecision::Deny).maybe_with_api_key_id(None);
assert_eq!(builder.api_key_id, None);
}
#[test]
fn test_maybe_with_api_key_id_some_sets_value() {
let id = crate::domain::id::Id::generate();
let builder = AuditLogBuilder::new("authed.deny", AuditDecision::Deny)
.maybe_with_api_key_id(Some(id));
assert_eq!(builder.api_key_id, Some(id));
}
#[test]
fn test_maybe_with_team_id_none_preserves_none() {
let builder =
AuditLogBuilder::new("anon.deny", AuditDecision::Deny).maybe_with_team_id(None);
assert_eq!(builder.team_id, None);
}
#[test]
fn test_maybe_with_team_id_some_sets_value() {
let id = crate::domain::id::Id::generate();
let builder =
AuditLogBuilder::new("team.deny", AuditDecision::Deny).maybe_with_team_id(Some(id));
assert_eq!(builder.team_id, Some(id));
}
#[test]
fn test_maybe_with_scope_none_preserves_none() {
let builder = AuditLogBuilder::new("anon.deny", AuditDecision::Deny).maybe_with_scope(None);
assert_eq!(builder.scope_used, None);
}
#[test]
fn test_maybe_with_scope_some_sets_value() {
let scope = ApiKeyScope::full_access();
let builder = AuditLogBuilder::new("authed.deny", AuditDecision::Deny)
.maybe_with_scope(Some(scope.clone()));
assert_eq!(builder.scope_used, Some(scope));
}
#[test]
fn test_maybe_with_methods_build_entry_with_none_fields() {
let entry = AuditLogBuilder::new("anonymous.action", AuditDecision::Deny)
.maybe_with_api_key_id(None)
.maybe_with_team_id(None)
.with_denial_reason("auth required")
.maybe_with_scope(None)
.build();
assert_eq!(entry.decision, AuditDecision::Deny);
assert_eq!(entry.api_key_id, None);
assert_eq!(entry.team_id, None);
assert_eq!(entry.scope_used, None);
assert_eq!(entry.denial_reason.as_deref(), Some("auth required"));
assert_ne!(entry.id, crate::domain::id::Id::nil());
}
#[test]
fn test_maybe_with_methods_build_entry_with_some_fields() {
let api_key_id = crate::domain::id::Id::generate();
let team_id = crate::domain::id::Id::generate();
let scope = ApiKeyScope::read_only();
let entry = AuditLogBuilder::new("authed.action", AuditDecision::Deny)
.maybe_with_api_key_id(Some(api_key_id))
.maybe_with_team_id(Some(team_id))
.with_denial_reason("insufficient scope")
.maybe_with_scope(Some(scope.clone()))
.build();
assert_eq!(entry.api_key_id, Some(api_key_id));
assert_eq!(entry.team_id, Some(team_id));
assert_eq!(entry.scope_used, Some(scope));
}
#[test]
fn test_maybe_with_overrides_previous_with_value() {
let id = crate::domain::id::Id::generate();
let builder = AuditLogBuilder::new("action", AuditDecision::Allow)
.with_api_key_id(id)
.maybe_with_api_key_id(None);
assert_eq!(builder.api_key_id, None);
}
#[test]
fn test_audit_log_builder_build_includes_metadata_and_scope() {
let scope = ApiKeyScope::full_access();
let entry = AuditLogBuilder::new("delete_resource", AuditDecision::Deny)
.with_denial_reason("no permission")
.with_scope(scope.clone())
.with_metadata("resource_id", json!("res-123"))
.with_user_agent("AuditClient/2.0")
.build();
assert_eq!(entry.decision, AuditDecision::Deny);
assert_eq!(entry.denial_reason.as_deref(), Some("no permission"));
assert_eq!(entry.scope_used, Some(scope));
assert_eq!(entry.user_agent.as_deref(), Some("AuditClient/2.0"));
match &entry.metadata {
serde_json::Value::Object(map) => {
assert_eq!(map.get("resource_id"), Some(&json!("res-123")));
}
_ => panic!("Expected object metadata"),
}
}
#[test]
fn test_audit_log_builder_with_metadata_after_non_object_is_noop() {
let builder = AuditLogBuilder::new("test_action", AuditDecision::Allow)
.with_metadata("k1", json!("v1"));
match &builder.metadata {
serde_json::Value::Object(map) => {
assert_eq!(map.get("k1"), Some(&json!("v1")));
}
_ => panic!("Expected object metadata"),
}
}
}