pub mod events;
pub mod logger;
pub mod query;
pub mod storage;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogEntry {
pub id: String,
pub timestamp: DateTime<Utc>,
pub event_type: AuditEventType,
pub subject: Option<String>,
pub resource: Option<String>,
pub action: Option<String>,
pub result: AuditResult,
pub source_ip: Option<String>,
pub tenant_id: Option<String>,
pub metadata: HashMap<String, String>,
pub message: Option<String>,
}
impl AuditLogEntry {
pub fn new(event_type: AuditEventType, result: AuditResult) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
timestamp: Utc::now(),
event_type,
subject: None,
resource: None,
action: None,
result,
source_ip: None,
tenant_id: None,
metadata: HashMap::new(),
message: None,
}
}
pub fn with_subject(mut self, subject: String) -> Self {
self.subject = Some(subject);
self
}
pub fn with_resource(mut self, resource: String) -> Self {
self.resource = Some(resource);
self
}
pub fn with_action(mut self, action: String) -> Self {
self.action = Some(action);
self
}
pub fn with_source_ip(mut self, ip: String) -> Self {
self.source_ip = Some(ip);
self
}
pub fn with_tenant_id(mut self, tenant_id: String) -> Self {
self.tenant_id = Some(tenant_id);
self
}
pub fn with_metadata(mut self, key: String, value: String) -> Self {
self.metadata.insert(key, value);
self
}
pub fn with_message(mut self, message: String) -> Self {
self.message = Some(message);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AuditEventType {
Authentication,
Authorization,
DataAccess,
DataModification,
ConfigChange,
UserManagement,
RoleManagement,
KeyManagement,
Encryption,
Decryption,
Compliance,
SecurityScan,
System,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditResult {
Success,
Failure,
Denied,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AuditSeverity {
Info,
Warning,
Error,
Critical,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_audit_log_entry_creation() {
let entry = AuditLogEntry::new(AuditEventType::Authentication, AuditResult::Success)
.with_subject("user-123".to_string())
.with_action("login".to_string())
.with_source_ip("192.168.1.1".to_string());
assert_eq!(entry.event_type, AuditEventType::Authentication);
assert_eq!(entry.result, AuditResult::Success);
assert_eq!(entry.subject, Some("user-123".to_string()));
}
#[test]
fn test_audit_entry_serialization() {
let entry = AuditLogEntry::new(AuditEventType::DataAccess, AuditResult::Success);
let json = serde_json::to_string(&entry).expect("Serialization failed");
let deserialized: AuditLogEntry =
serde_json::from_str(&json).expect("Deserialization failed");
assert_eq!(deserialized.event_type, entry.event_type);
assert_eq!(deserialized.result, entry.result);
}
}