1pub mod events;
4pub mod logger;
5pub mod query;
6pub mod storage;
7
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct AuditLogEntry {
15 pub id: String,
17 pub timestamp: DateTime<Utc>,
19 pub event_type: AuditEventType,
21 pub subject: Option<String>,
23 pub resource: Option<String>,
25 pub action: Option<String>,
27 pub result: AuditResult,
29 pub source_ip: Option<String>,
31 pub tenant_id: Option<String>,
33 pub metadata: HashMap<String, String>,
35 pub message: Option<String>,
37}
38
39impl AuditLogEntry {
40 pub fn new(event_type: AuditEventType, result: AuditResult) -> Self {
42 Self {
43 id: uuid::Uuid::new_v4().to_string(),
44 timestamp: Utc::now(),
45 event_type,
46 subject: None,
47 resource: None,
48 action: None,
49 result,
50 source_ip: None,
51 tenant_id: None,
52 metadata: HashMap::new(),
53 message: None,
54 }
55 }
56
57 pub fn with_subject(mut self, subject: String) -> Self {
59 self.subject = Some(subject);
60 self
61 }
62
63 pub fn with_resource(mut self, resource: String) -> Self {
65 self.resource = Some(resource);
66 self
67 }
68
69 pub fn with_action(mut self, action: String) -> Self {
71 self.action = Some(action);
72 self
73 }
74
75 pub fn with_source_ip(mut self, ip: String) -> Self {
77 self.source_ip = Some(ip);
78 self
79 }
80
81 pub fn with_tenant_id(mut self, tenant_id: String) -> Self {
83 self.tenant_id = Some(tenant_id);
84 self
85 }
86
87 pub fn with_metadata(mut self, key: String, value: String) -> Self {
89 self.metadata.insert(key, value);
90 self
91 }
92
93 pub fn with_message(mut self, message: String) -> Self {
95 self.message = Some(message);
96 self
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
102pub enum AuditEventType {
103 Authentication,
105 Authorization,
107 DataAccess,
109 DataModification,
111 ConfigChange,
113 UserManagement,
115 RoleManagement,
117 KeyManagement,
119 Encryption,
121 Decryption,
123 Compliance,
125 SecurityScan,
127 System,
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
133pub enum AuditResult {
134 Success,
136 Failure,
138 Denied,
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
144pub enum AuditSeverity {
145 Info,
147 Warning,
149 Error,
151 Critical,
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn test_audit_log_entry_creation() {
161 let entry = AuditLogEntry::new(AuditEventType::Authentication, AuditResult::Success)
162 .with_subject("user-123".to_string())
163 .with_action("login".to_string())
164 .with_source_ip("192.168.1.1".to_string());
165
166 assert_eq!(entry.event_type, AuditEventType::Authentication);
167 assert_eq!(entry.result, AuditResult::Success);
168 assert_eq!(entry.subject, Some("user-123".to_string()));
169 }
170
171 #[test]
172 fn test_audit_entry_serialization() {
173 let entry = AuditLogEntry::new(AuditEventType::DataAccess, AuditResult::Success);
174 let json = serde_json::to_string(&entry).expect("Serialization failed");
175 let deserialized: AuditLogEntry =
176 serde_json::from_str(&json).expect("Deserialization failed");
177
178 assert_eq!(deserialized.event_type, entry.event_type);
179 assert_eq!(deserialized.result, entry.result);
180 }
181}