rust-secure-logger 2.0.0

Memory-safe security logging for financial systems and critical infrastructure with encryption, log forwarding, and compliance automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! # Rust Secure Logger v2.0
//!
//! A production-ready, memory-safe logging library for financial systems and critical infrastructure.
//!
//! ## Features
//!
//! - **Memory Safety**: Built with Rust's ownership system to prevent buffer overflows and memory corruption
//! - **Thread Safety**: Concurrent logging without data races using Arc<Mutex>
//! - **Cryptographic Integrity**: SHA-256/SHA-3 hashing for tamper detection
//! - **Log Encryption**: AES-256-GCM encryption for sensitive log data (v2.0)
//! - **Log Compression**: GZIP compression for efficient storage (v2.0)
//! - **Log Correlation**: Automatic correlation IDs for distributed tracing (v2.0)
//! - **Rate Limiting**: Configurable rate limiting to prevent log flooding (v2.0)
//! - **Log Redaction**: Automatic PII/sensitive data redaction (v2.0)
//! - **File Persistence**: Log rotation and disk persistence
//! - **SIEM Integration**: CEF, LEEF, Syslog, and Splunk HEC formats
//! - **Compliance Reporting**: SOX, GLBA, PCI-DSS, HIPAA automated reports
//! - **Audit Trail**: Immutable log entries with timestamps
//! - **Metrics**: Built-in logging metrics and statistics (v2.0)
//!
//! ## Alignment with Federal Guidance
//!
//! This library aligns with 2024 CISA/FBI guidance recommending memory-safe
//! languages for critical infrastructure to eliminate 70% of security vulnerabilities.
//!
//! ## Quick Start
//!
//! ```rust
//! use rust_secure_logger::{SecureLogger, LoggerConfig};
//!
//! // Basic usage
//! let logger = SecureLogger::new();
//! logger.info("Application started");
//! logger.audit("User authentication successful", Some(serde_json::json!({
//!     "user_id": "12345",
//!     "ip_address": "192.168.1.100"
//! })));
//!
//! // v2.0: With encryption and correlation
//! let config = LoggerConfig::default()
//!     .with_encryption(true)
//!     .with_compression(true)
//!     .with_correlation_id("trace-12345");
//! let secure_logger = SecureLogger::with_config(config);
//! ```
//!
//! ## What's New in v2.0
//!
//! - **Encryption**: AES-256-GCM encryption for log entries
//! - **Compression**: GZIP compression for storage efficiency
//! - **Correlation IDs**: Distributed tracing support
//! - **Rate Limiting**: Prevent log flooding attacks
//! - **Redaction**: Automatic PII masking
//! - **HIPAA Compliance**: Healthcare compliance reporting
//! - **Enhanced Metrics**: Detailed logging statistics

pub mod compliance;
pub mod entry;
pub mod formats;
pub mod persistence;
pub mod encryption;
pub mod redaction;
pub mod metrics;

pub use compliance::{ComplianceFramework, ComplianceReport, ComplianceReporter};
pub use entry::{LogEntry, SecurityLevel};
pub use formats::{CEFFormatter, LEEFFormatter, SplunkFormatter, SyslogFormatter};
pub use persistence::{LogWriter, PersistenceConfig};
pub use encryption::{LogEncryptor, EncryptedLogEntry};
pub use redaction::{LogRedactor, RedactionPattern, RedactionConfig};
pub use metrics::{LogMetrics, MetricsSnapshot};

use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::collections::HashMap;
use thiserror::Error;

/// Logger errors
#[derive(Error, Debug)]
pub enum LoggerError {
    #[error("Rate limit exceeded: {0}")]
    RateLimitExceeded(String),

    #[error("Encryption error: {0}")]
    EncryptionError(String),

    #[error("Compression error: {0}")]
    CompressionError(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),
}

/// Logger configuration for v2.0 features
#[derive(Debug, Clone)]
pub struct LoggerConfig {
    pub enable_encryption: bool,
    pub enable_compression: bool,
    pub enable_redaction: bool,
    pub correlation_id: Option<String>,
    pub rate_limit_per_second: Option<u32>,
    pub max_entry_size: usize,
    pub retention_days: u32,
    pub hash_algorithm: HashAlgorithm,
}

/// Hash algorithm selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashAlgorithm {
    Sha256,
    Sha3_256,
}

impl Default for LoggerConfig {
    fn default() -> Self {
        Self {
            enable_encryption: false,
            enable_compression: false,
            enable_redaction: true,
            correlation_id: None,
            rate_limit_per_second: None,
            max_entry_size: 1024 * 1024, // 1MB
            retention_days: 90,
            hash_algorithm: HashAlgorithm::Sha256,
        }
    }
}

impl LoggerConfig {
    /// Enable encryption for log entries
    pub fn with_encryption(mut self, enable: bool) -> Self {
        self.enable_encryption = enable;
        self
    }

    /// Enable compression for log entries
    pub fn with_compression(mut self, enable: bool) -> Self {
        self.enable_compression = enable;
        self
    }

    /// Set correlation ID for distributed tracing
    pub fn with_correlation_id(mut self, id: impl Into<String>) -> Self {
        self.correlation_id = Some(id.into());
        self
    }

    /// Set rate limit (logs per second)
    pub fn with_rate_limit(mut self, limit: u32) -> Self {
        self.rate_limit_per_second = Some(limit);
        self
    }

    /// Set hash algorithm
    pub fn with_hash_algorithm(mut self, algorithm: HashAlgorithm) -> Self {
        self.hash_algorithm = algorithm;
        self
    }

    /// Enable automatic PII redaction
    pub fn with_redaction(mut self, enable: bool) -> Self {
        self.enable_redaction = enable;
        self
    }
}

/// Rate limiter for log flooding prevention
#[derive(Debug)]
struct RateLimiter {
    limit: u32,
    window_start: Instant,
    count: u32,
}

impl RateLimiter {
    fn new(limit: u32) -> Self {
        Self {
            limit,
            window_start: Instant::now(),
            count: 0,
        }
    }

    fn check(&mut self) -> bool {
        let now = Instant::now();
        if now.duration_since(self.window_start) >= Duration::from_secs(1) {
            self.window_start = now;
            self.count = 0;
        }

        if self.count >= self.limit {
            false
        } else {
            self.count += 1;
            true
        }
    }
}

/// Thread-safe secure logger for financial systems
#[derive(Clone)]
pub struct SecureLogger {
    entries: Arc<Mutex<Vec<LogEntry>>>,
    source: Option<String>,
    config: LoggerConfig,
    rate_limiter: Arc<Mutex<Option<RateLimiter>>>,
    metrics: Arc<Mutex<LogMetrics>>,
    redactor: Arc<LogRedactor>,
}

impl SecureLogger {
    /// Create a new secure logger instance
    pub fn new() -> Self {
        Self {
            entries: Arc::new(Mutex::new(Vec::new())),
            source: None,
            config: LoggerConfig::default(),
            rate_limiter: Arc::new(Mutex::new(None)),
            metrics: Arc::new(Mutex::new(LogMetrics::new())),
            redactor: Arc::new(LogRedactor::default()),
        }
    }

    /// Create a logger with custom configuration (v2.0)
    pub fn with_config(config: LoggerConfig) -> Self {
        let rate_limiter = config.rate_limit_per_second.map(RateLimiter::new);
        Self {
            entries: Arc::new(Mutex::new(Vec::new())),
            source: None,
            config,
            rate_limiter: Arc::new(Mutex::new(rate_limiter)),
            metrics: Arc::new(Mutex::new(LogMetrics::new())),
            redactor: Arc::new(LogRedactor::default()),
        }
    }

    /// Create a logger with a source identifier (hostname, service name)
    pub fn with_source(source: impl Into<String>) -> Self {
        Self {
            entries: Arc::new(Mutex::new(Vec::new())),
            source: Some(source.into()),
            config: LoggerConfig::default(),
            rate_limiter: Arc::new(Mutex::new(None)),
            metrics: Arc::new(Mutex::new(LogMetrics::new())),
            redactor: Arc::new(LogRedactor::default()),
        }
    }

    /// Get current configuration
    pub fn config(&self) -> &LoggerConfig {
        &self.config
    }

    /// Get current metrics snapshot
    pub fn get_metrics(&self) -> MetricsSnapshot {
        let metrics = self.metrics.lock().unwrap();
        metrics.snapshot()
    }

    /// Check rate limit before logging
    fn check_rate_limit(&self) -> bool {
        let mut limiter = self.rate_limiter.lock().unwrap();
        if let Some(ref mut rl) = *limiter {
            rl.check()
        } else {
            true
        }
    }

    /// Apply redaction to message if enabled
    fn apply_redaction(&self, message: &str) -> String {
        if self.config.enable_redaction {
            self.redactor.redact(message)
        } else {
            message.to_string()
        }
    }

    /// Log an informational message
    pub fn info(&self, message: impl Into<String>) {
        self.log(SecurityLevel::Info, message.into(), None, None);
    }

    /// Log a warning
    pub fn warning(&self, message: impl Into<String>) {
        self.log(SecurityLevel::Warning, message.into(), None, None);
    }

    /// Log a security event with optional metadata
    pub fn security_event(&self, message: impl Into<String>, metadata: Option<serde_json::Value>) {
        self.log(SecurityLevel::SecurityEvent, message.into(), metadata, None);
    }

    /// Log a critical security incident
    pub fn critical(&self, message: impl Into<String>, metadata: Option<serde_json::Value>) {
        self.log(SecurityLevel::Critical, message.into(), metadata, None);
    }

    /// Log an audit trail entry (for financial transactions, access control, etc.)
    pub fn audit(&self, message: impl Into<String>, metadata: Option<serde_json::Value>) {
        self.log(SecurityLevel::Audit, message.into(), metadata, None);
    }

    /// Log with category
    pub fn log_with_category(
        &self,
        level: SecurityLevel,
        message: impl Into<String>,
        metadata: Option<serde_json::Value>,
        category: impl Into<String>,
    ) {
        self.log(level, message.into(), metadata, Some(category.into()));
    }

    /// Internal logging function
    fn log(
        &self,
        level: SecurityLevel,
        message: String,
        metadata: Option<serde_json::Value>,
        category: Option<String>,
    ) {
        let entry =
            LogEntry::new_with_context(level, message, metadata, self.source.clone(), category);
        let mut entries = self.entries.lock().unwrap();
        entries.push(entry);
    }

    /// Get all log entries (read-only)
    pub fn get_entries(&self) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        entries.clone()
    }

    /// Get entries filtered by security level
    pub fn get_entries_by_level(&self, level: SecurityLevel) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        entries
            .iter()
            .filter(|e| e.level == level)
            .cloned()
            .collect()
    }

    /// Get entries by category
    pub fn get_entries_by_category(&self, category: &str) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        entries
            .iter()
            .filter(|e| e.category.as_deref() == Some(category))
            .cloned()
            .collect()
    }

    /// Verify integrity of all log entries
    pub fn verify_all_integrity(&self) -> bool {
        let entries = self.entries.lock().unwrap();
        entries.iter().all(|entry| entry.verify_integrity())
    }

    /// Export logs as JSON
    pub fn export_json(&self) -> Result<String, serde_json::Error> {
        let entries = self.entries.lock().unwrap();
        serde_json::to_string_pretty(&*entries)
    }

    /// Export logs in CEF format (for ArcSight)
    pub fn export_cef(&self) -> Vec<String> {
        let entries = self.entries.lock().unwrap();
        entries.iter().map(CEFFormatter::format).collect()
    }

    /// Export logs in LEEF format (for QRadar)
    pub fn export_leef(&self) -> Vec<String> {
        let entries = self.entries.lock().unwrap();
        entries.iter().map(LEEFFormatter::format).collect()
    }

    /// Export logs in Syslog format
    pub fn export_syslog(&self) -> Vec<String> {
        let entries = self.entries.lock().unwrap();
        entries.iter().map(SyslogFormatter::format).collect()
    }

    /// Export logs in Splunk HEC format
    pub fn export_splunk(&self) -> Vec<String> {
        let entries = self.entries.lock().unwrap();
        entries.iter().map(SplunkFormatter::format).collect()
    }

    /// Get count of entries by security level
    pub fn count_by_level(&self, level: SecurityLevel) -> usize {
        let entries = self.entries.lock().unwrap();
        entries.iter().filter(|e| e.level == level).count()
    }

    /// Get entries within a date range
    pub fn get_entries_by_date_range(
        &self,
        start: chrono::DateTime<chrono::Utc>,
        end: chrono::DateTime<chrono::Utc>,
    ) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        entries
            .iter()
            .filter(|e| e.timestamp >= start && e.timestamp <= end)
            .cloned()
            .collect()
    }

    /// Clear all log entries (use with caution - breaks audit trail)
    pub fn clear(&self) {
        let mut entries = self.entries.lock().unwrap();
        entries.clear()
    }

    /// Get the most recent N entries
    pub fn get_recent_entries(&self, count: usize) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        let total = entries.len();
        if total <= count {
            entries.clone()
        } else {
            entries[total - count..].to_vec()
        }
    }

    /// Search log entries by message content
    pub fn search(&self, query: &str) -> Vec<LogEntry> {
        let entries = self.entries.lock().unwrap();
        entries
            .iter()
            .filter(|e| e.message.contains(query))
            .cloned()
            .collect()
    }

    /// Get statistics about log entries
    pub fn get_statistics(&self) -> LogStatistics {
        let entries = self.entries.lock().unwrap();
        let total_entries = entries.len();

        let mut stats = LogStatistics {
            total_entries,
            info_count: 0,
            warning_count: 0,
            security_event_count: 0,
            critical_count: 0,
            audit_count: 0,
        };

        for entry in entries.iter() {
            match entry.level {
                SecurityLevel::Info => stats.info_count += 1,
                SecurityLevel::Warning => stats.warning_count += 1,
                SecurityLevel::SecurityEvent => stats.security_event_count += 1,
                SecurityLevel::Critical => stats.critical_count += 1,
                SecurityLevel::Audit => stats.audit_count += 1,
            }
        }

        stats
    }

    /// Generate SOX compliance report
    pub fn generate_sox_report(
        &self,
        period_start: chrono::DateTime<chrono::Utc>,
        period_end: chrono::DateTime<chrono::Utc>,
    ) -> ComplianceReport {
        let entries = self.get_entries();
        ComplianceReporter::generate_sox_report(&entries, period_start, period_end)
    }

    /// Generate PCI-DSS compliance report
    pub fn generate_pci_report(
        &self,
        period_start: chrono::DateTime<chrono::Utc>,
        period_end: chrono::DateTime<chrono::Utc>,
    ) -> ComplianceReport {
        let entries = self.get_entries();
        ComplianceReporter::generate_pci_report(&entries, period_start, period_end)
    }

    /// Generate GLBA compliance report
    pub fn generate_glba_report(
        &self,
        period_start: chrono::DateTime<chrono::Utc>,
        period_end: chrono::DateTime<chrono::Utc>,
    ) -> ComplianceReport {
        let entries = self.get_entries();
        ComplianceReporter::generate_glba_report(&entries, period_start, period_end)
    }
}

/// Statistics about log entries
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LogStatistics {
    pub total_entries: usize,
    pub info_count: usize,
    pub warning_count: usize,
    pub security_event_count: usize,
    pub critical_count: usize,
    pub audit_count: usize,
}

impl Default for SecureLogger {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_secure_logger() {
        let logger = SecureLogger::new();
        logger.info("Application started");
        logger.warning("High memory usage detected");
        logger.audit(
            "User authentication successful",
            Some(serde_json::json!({
                "user_id": "12345",
                "timestamp": "2024-11-06T00:00:00Z"
            })),
        );

        assert_eq!(logger.get_entries().len(), 3);
        assert_eq!(logger.count_by_level(SecurityLevel::Info), 1);
        assert_eq!(logger.count_by_level(SecurityLevel::Warning), 1);
        assert_eq!(logger.count_by_level(SecurityLevel::Audit), 1);
    }

    #[test]
    fn test_thread_safety() {
        use std::thread;

        let logger = SecureLogger::new();
        let mut handles = vec![];

        for i in 0..10 {
            let logger_clone = logger.clone();
            let handle = thread::spawn(move || {
                logger_clone.info(format!("Thread {} message", i));
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(logger.get_entries().len(), 10);
    }

    #[test]
    fn test_cef_export() {
        let logger = SecureLogger::new();
        logger.security_event(
            "Failed login attempt",
            Some(serde_json::json!({
                "username": "admin"
            })),
        );

        let cef_logs = logger.export_cef();
        assert_eq!(cef_logs.len(), 1);
        assert!(cef_logs[0].starts_with("CEF:0|"));
    }

    #[test]
    fn test_leef_export() {
        let logger = SecureLogger::new();
        logger.critical("Security breach detected", None);

        let leef_logs = logger.export_leef();
        assert_eq!(leef_logs.len(), 1);
        assert!(leef_logs[0].starts_with("LEEF:2.0|"));
    }

    #[test]
    fn test_compliance_reporting() {
        let logger = SecureLogger::new();
        logger.audit(
            "Transaction processed",
            Some(serde_json::json!({
                "amount": 1000,
                "currency": "USD"
            })),
        );

        let start = chrono::Utc::now() - chrono::Duration::hours(1);
        let end = chrono::Utc::now();

        let sox_report = logger.generate_sox_report(start, end);
        assert_eq!(sox_report.framework, ComplianceFramework::SOX);
        assert_eq!(sox_report.audit_events, 1);
    }

    #[test]
    fn test_logger_with_source() {
        let logger = SecureLogger::with_source("web-server-01");
        logger.info("Server started");

        let entries = logger.get_entries();
        assert_eq!(entries[0].source, Some("web-server-01".to_string()));
    }

    #[test]
    fn test_category_filtering() {
        let logger = SecureLogger::new();
        logger.log_with_category(
            SecurityLevel::SecurityEvent,
            "Login failed",
            None,
            "authentication",
        );
        logger.log_with_category(SecurityLevel::Info, "Page loaded", None, "web");

        let auth_events = logger.get_entries_by_category("authentication");
        assert_eq!(auth_events.len(), 1);
        assert_eq!(auth_events[0].message, "Login failed");
    }
}