torsh-package 0.1.2

Model packaging and distribution utilities for ToRSh
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Audit logging for security compliance and tracking
//!
//! This module provides comprehensive audit logging for all package operations
//! including downloads, uploads, access control changes, and security events.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use torsh_core::error::{Result, TorshError};

/// Audit event type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditEventType {
    /// Package downloaded
    PackageDownload,
    /// Package uploaded/published
    PackageUpload,
    /// Package deleted
    PackageDelete,
    /// Package version yanked
    PackageYank,
    /// Package version unyanked
    PackageUnyank,
    /// User authentication
    UserAuthentication,
    /// User authorization check
    UserAuthorization,
    /// Access granted
    AccessGranted,
    /// Access denied
    AccessDenied,
    /// Role assigned
    RoleAssigned,
    /// Role revoked
    RoleRevoked,
    /// Permission changed
    PermissionChanged,
    /// Security violation detected
    SecurityViolation,
    /// Package integrity check
    IntegrityCheck,
    /// Package signature verification
    SignatureVerification,
    /// Configuration change
    ConfigurationChange,
    /// System event
    SystemEvent,
}

/// Audit event severity
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AuditSeverity {
    /// Informational event
    Info,
    /// Warning level
    Warning,
    /// Error level
    Error,
    /// Critical security event
    Critical,
}

/// Audit event record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEvent {
    /// Event ID (unique)
    pub id: String,
    /// Event type
    pub event_type: AuditEventType,
    /// Severity level
    pub severity: AuditSeverity,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// User ID who performed the action
    pub user_id: Option<String>,
    /// IP address of the client
    pub ip_address: Option<String>,
    /// User agent string
    pub user_agent: Option<String>,
    /// Action performed
    pub action: String,
    /// Resource affected (e.g., package name)
    pub resource: Option<String>,
    /// Result of the action (success/failure)
    pub result: ActionResult,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
    /// Error message if action failed
    pub error: Option<String>,
}

/// Action result
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActionResult {
    /// Action succeeded
    Success,
    /// Action failed
    Failure,
    /// Action was denied
    Denied,
}

/// Audit log configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogConfig {
    /// Enable audit logging
    pub enabled: bool,
    /// Log file path
    pub log_path: PathBuf,
    /// Maximum log file size in bytes before rotation
    pub max_file_size: u64,
    /// Number of rotated files to keep
    pub max_files: usize,
    /// Log format
    pub format: AuditLogFormat,
    /// Minimum severity to log
    pub min_severity: AuditSeverity,
    /// Enable real-time log streaming
    pub stream_enabled: bool,
    /// Buffer size for log entries
    pub buffer_size: usize,
}

/// Audit log format
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditLogFormat {
    /// JSON format (one event per line)
    Json,
    /// CSV format
    Csv,
    /// Plain text format
    Text,
    /// Syslog format
    Syslog,
}

/// Audit logger
pub struct AuditLogger {
    /// Configuration
    config: AuditLogConfig,
    /// Event buffer
    buffer: Vec<AuditEvent>,
    /// Event listeners for real-time streaming
    listeners: Vec<Box<dyn AuditListener>>,
    /// Statistics
    statistics: AuditStatistics,
}

/// Audit listener for real-time event streaming
pub trait AuditListener: Send + Sync {
    /// Called when an event is logged
    fn on_event(&mut self, event: &AuditEvent);

    /// Called on flush
    fn on_flush(&mut self);
}

/// Audit statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditStatistics {
    /// Total events logged
    pub total_events: u64,
    /// Events by type
    pub events_by_type: HashMap<String, u64>,
    /// Events by severity
    pub events_by_severity: HashMap<String, u64>,
    /// Failed actions
    pub failed_actions: u64,
    /// Security violations
    pub security_violations: u64,
    /// Unique users
    pub unique_users: u64,
}

/// Audit query filter
#[derive(Debug, Clone, Default)]
pub struct AuditQuery {
    /// Filter by event type
    pub event_types: Vec<AuditEventType>,
    /// Filter by severity
    pub min_severity: Option<AuditSeverity>,
    /// Filter by user ID
    pub user_id: Option<String>,
    /// Filter by resource
    pub resource: Option<String>,
    /// Filter by result
    pub result: Option<ActionResult>,
    /// Start time for query range
    pub start_time: Option<DateTime<Utc>>,
    /// End time for query range
    pub end_time: Option<DateTime<Utc>>,
    /// Maximum number of results
    pub limit: Option<usize>,
}

impl AuditEvent {
    /// Create a new audit event
    pub fn new(event_type: AuditEventType, action: String) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            event_type,
            severity: AuditSeverity::Info,
            timestamp: Utc::now(),
            user_id: None,
            ip_address: None,
            user_agent: None,
            action,
            resource: None,
            result: ActionResult::Success,
            metadata: HashMap::new(),
            error: None,
        }
    }

    /// Set severity
    pub fn with_severity(mut self, severity: AuditSeverity) -> Self {
        self.severity = severity;
        self
    }

    /// Set user ID
    pub fn with_user(mut self, user_id: String) -> Self {
        self.user_id = Some(user_id);
        self
    }

    /// Set IP address
    pub fn with_ip(mut self, ip: String) -> Self {
        self.ip_address = Some(ip);
        self
    }

    /// Set resource
    pub fn with_resource(mut self, resource: String) -> Self {
        self.resource = Some(resource);
        self
    }

    /// Set result
    pub fn with_result(mut self, result: ActionResult) -> Self {
        self.result = result;
        self
    }

    /// Set error message
    pub fn with_error(mut self, error: String) -> Self {
        self.error = Some(error);
        self
    }

    /// Add metadata
    pub fn add_metadata(mut self, key: String, value: String) -> Self {
        self.metadata.insert(key, value);
        self
    }

    /// Format as JSON
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string(self)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to serialize event: {}", e)))
    }

    /// Format as text
    pub fn to_text(&self) -> String {
        format!(
            "[{}] {} - {} - {} - {} - User: {:?} - Resource: {:?} - Result: {:?}{}",
            self.timestamp.format("%Y-%m-%d %H:%M:%S"),
            self.severity_str(),
            self.event_type_str(),
            self.action,
            self.id,
            self.user_id,
            self.resource,
            self.result,
            self.error
                .as_ref()
                .map_or(String::new(), |e| format!(" - Error: {}", e))
        )
    }

    /// Get severity as string
    fn severity_str(&self) -> &str {
        match self.severity {
            AuditSeverity::Info => "INFO",
            AuditSeverity::Warning => "WARN",
            AuditSeverity::Error => "ERROR",
            AuditSeverity::Critical => "CRIT",
        }
    }

    /// Get event type as string
    fn event_type_str(&self) -> &str {
        match self.event_type {
            AuditEventType::PackageDownload => "DOWNLOAD",
            AuditEventType::PackageUpload => "UPLOAD",
            AuditEventType::PackageDelete => "DELETE",
            AuditEventType::PackageYank => "YANK",
            AuditEventType::PackageUnyank => "UNYANK",
            AuditEventType::UserAuthentication => "AUTH",
            AuditEventType::UserAuthorization => "AUTHZ",
            AuditEventType::AccessGranted => "ACCESS_GRANTED",
            AuditEventType::AccessDenied => "ACCESS_DENIED",
            AuditEventType::RoleAssigned => "ROLE_ASSIGN",
            AuditEventType::RoleRevoked => "ROLE_REVOKE",
            AuditEventType::PermissionChanged => "PERM_CHANGE",
            AuditEventType::SecurityViolation => "SECURITY_VIOLATION",
            AuditEventType::IntegrityCheck => "INTEGRITY_CHECK",
            AuditEventType::SignatureVerification => "SIGNATURE_VERIFY",
            AuditEventType::ConfigurationChange => "CONFIG_CHANGE",
            AuditEventType::SystemEvent => "SYSTEM",
        }
    }
}

impl Default for AuditLogConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            log_path: PathBuf::from("/var/log/torsh/audit.log"),
            max_file_size: 100 * 1024 * 1024, // 100 MB
            max_files: 10,
            format: AuditLogFormat::Json,
            min_severity: AuditSeverity::Info,
            stream_enabled: false,
            buffer_size: 1000,
        }
    }
}

impl AuditLogConfig {
    /// Create new configuration
    pub fn new<P: AsRef<Path>>(log_path: P) -> Self {
        Self {
            log_path: log_path.as_ref().to_path_buf(),
            ..Default::default()
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.max_file_size == 0 {
            return Err(TorshError::InvalidArgument(
                "Max file size must be greater than zero".to_string(),
            ));
        }

        if self.max_files == 0 {
            return Err(TorshError::InvalidArgument(
                "Max files must be greater than zero".to_string(),
            ));
        }

        Ok(())
    }
}

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

impl AuditStatistics {
    /// Create new statistics
    pub fn new() -> Self {
        Self {
            total_events: 0,
            events_by_type: HashMap::new(),
            events_by_severity: HashMap::new(),
            failed_actions: 0,
            security_violations: 0,
            unique_users: 0,
        }
    }

    /// Update statistics with an event
    pub fn update(&mut self, event: &AuditEvent) {
        self.total_events += 1;

        // Count by type
        let type_key = format!("{:?}", event.event_type);
        *self.events_by_type.entry(type_key).or_insert(0) += 1;

        // Count by severity
        let severity_key = format!("{:?}", event.severity);
        *self.events_by_severity.entry(severity_key).or_insert(0) += 1;

        // Count failures
        if event.result == ActionResult::Failure {
            self.failed_actions += 1;
        }

        // Count security violations
        if event.event_type == AuditEventType::SecurityViolation {
            self.security_violations += 1;
        }
    }
}

impl AuditLogger {
    /// Create a new audit logger
    pub fn new(config: AuditLogConfig) -> Result<Self> {
        config.validate()?;

        Ok(Self {
            config,
            buffer: Vec::new(),
            listeners: Vec::new(),
            statistics: AuditStatistics::new(),
        })
    }

    /// Log an event
    pub fn log(&mut self, event: AuditEvent) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        // Check severity filter
        if event.severity < self.config.min_severity {
            return Ok(());
        }

        // Update statistics
        self.statistics.update(&event);

        // Notify listeners
        for listener in &mut self.listeners {
            listener.on_event(&event);
        }

        // Add to buffer
        self.buffer.push(event);

        // Flush if buffer is full
        if self.buffer.len() >= self.config.buffer_size {
            self.flush()?;
        }

        Ok(())
    }

    /// Log package download
    pub fn log_download(&mut self, user_id: &str, package: &str, version: &str) -> Result<()> {
        let event = AuditEvent::new(
            AuditEventType::PackageDownload,
            format!("Download package {}", package),
        )
        .with_user(user_id.to_string())
        .with_resource(format!("{}:{}", package, version))
        .with_severity(AuditSeverity::Info);

        self.log(event)
    }

    /// Log package upload
    pub fn log_upload(&mut self, user_id: &str, package: &str, version: &str) -> Result<()> {
        let event = AuditEvent::new(
            AuditEventType::PackageUpload,
            format!("Upload package {}", package),
        )
        .with_user(user_id.to_string())
        .with_resource(format!("{}:{}", package, version))
        .with_severity(AuditSeverity::Info);

        self.log(event)
    }

    /// Log access denial
    pub fn log_access_denied(&mut self, user_id: &str, resource: &str, reason: &str) -> Result<()> {
        let event = AuditEvent::new(
            AuditEventType::AccessDenied,
            format!("Access denied to {}", resource),
        )
        .with_user(user_id.to_string())
        .with_resource(resource.to_string())
        .with_result(ActionResult::Denied)
        .with_severity(AuditSeverity::Warning)
        .add_metadata("reason".to_string(), reason.to_string());

        self.log(event)
    }

    /// Log security violation
    pub fn log_security_violation(
        &mut self,
        user_id: Option<&str>,
        violation: &str,
        details: &str,
    ) -> Result<()> {
        let mut event = AuditEvent::new(
            AuditEventType::SecurityViolation,
            format!("Security violation: {}", violation),
        )
        .with_severity(AuditSeverity::Critical)
        .with_result(ActionResult::Failure)
        .add_metadata("details".to_string(), details.to_string());

        if let Some(uid) = user_id {
            event = event.with_user(uid.to_string());
        }

        self.log(event)
    }

    /// Flush buffered events to disk
    pub fn flush(&mut self) -> Result<()> {
        if self.buffer.is_empty() {
            return Ok(());
        }

        // In production, would write to file
        // For now, just clear the buffer
        self.buffer.clear();

        // Notify listeners
        for listener in &mut self.listeners {
            listener.on_flush();
        }

        Ok(())
    }

    /// Add an event listener
    pub fn add_listener(&mut self, listener: Box<dyn AuditListener>) {
        self.listeners.push(listener);
    }

    /// Query events (simplified - in production would query from persistent storage)
    pub fn query(&self, _query: &AuditQuery) -> Vec<AuditEvent> {
        // In production, would query from log files or database
        // For now, return buffered events
        self.buffer.clone()
    }

    /// Get statistics
    pub fn get_statistics(&self) -> &AuditStatistics {
        &self.statistics
    }

    /// Get event count by type
    pub fn get_event_count(&self, event_type: &AuditEventType) -> u64 {
        let key = format!("{:?}", event_type);
        self.statistics
            .events_by_type
            .get(&key)
            .copied()
            .unwrap_or(0)
    }
}

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

    #[test]
    fn test_audit_event_creation() {
        let event = AuditEvent::new(
            AuditEventType::PackageDownload,
            "Download test-package".to_string(),
        )
        .with_user("user1".to_string())
        .with_resource("test-package:1.0.0".to_string())
        .with_severity(AuditSeverity::Info);

        assert_eq!(event.event_type, AuditEventType::PackageDownload);
        assert_eq!(event.user_id, Some("user1".to_string()));
        assert_eq!(event.result, ActionResult::Success);
    }

    #[test]
    fn test_audit_event_formatting() {
        let event = AuditEvent::new(AuditEventType::PackageDownload, "Download test".to_string());

        let json = event.to_json().unwrap();
        assert!(json.contains("PackageDownload"));

        let text = event.to_text();
        assert!(text.contains("DOWNLOAD"));
        assert!(text.contains("INFO"));
    }

    #[test]
    fn test_audit_logger() {
        let config = AuditLogConfig::new(std::env::temp_dir().join("test-audit.log"));
        let mut logger = AuditLogger::new(config).unwrap();

        let event = AuditEvent::new(AuditEventType::PackageDownload, "Test download".to_string());

        logger.log(event).unwrap();
        assert_eq!(logger.statistics.total_events, 1);
        assert_eq!(logger.buffer.len(), 1);
    }

    #[test]
    fn test_log_download() {
        let config = AuditLogConfig::new(std::env::temp_dir().join("test-audit.log"));
        let mut logger = AuditLogger::new(config).unwrap();

        logger
            .log_download("user1", "test-package", "1.0.0")
            .unwrap();

        assert_eq!(logger.get_event_count(&AuditEventType::PackageDownload), 1);
    }

    #[test]
    fn test_log_access_denied() {
        let config = AuditLogConfig::new(std::env::temp_dir().join("test-audit.log"));
        let mut logger = AuditLogger::new(config).unwrap();

        logger
            .log_access_denied("user1", "test-package", "Insufficient permissions")
            .unwrap();

        assert_eq!(logger.get_event_count(&AuditEventType::AccessDenied), 1);
    }

    #[test]
    fn test_security_violation_logging() {
        let config = AuditLogConfig::new(std::env::temp_dir().join("test-audit.log"));
        let mut logger = AuditLogger::new(config).unwrap();

        logger
            .log_security_violation(Some("user1"), "Suspicious activity", "Details here")
            .unwrap();

        assert_eq!(logger.statistics.security_violations, 1);
    }

    #[test]
    fn test_statistics_update() {
        let mut stats = AuditStatistics::new();

        let event1 = AuditEvent::new(AuditEventType::PackageDownload, "Download".to_string());
        let event2 = AuditEvent::new(AuditEventType::PackageUpload, "Upload".to_string())
            .with_result(ActionResult::Failure);

        stats.update(&event1);
        stats.update(&event2);

        assert_eq!(stats.total_events, 2);
        assert_eq!(stats.failed_actions, 1);
    }

    #[test]
    fn test_buffer_flush() {
        let mut config = AuditLogConfig::new(std::env::temp_dir().join("test-audit.log"));
        config.buffer_size = 2;

        let mut logger = AuditLogger::new(config).unwrap();

        logger
            .log(AuditEvent::new(
                AuditEventType::PackageDownload,
                "Test1".to_string(),
            ))
            .unwrap();
        assert_eq!(logger.buffer.len(), 1);

        logger
            .log(AuditEvent::new(
                AuditEventType::PackageDownload,
                "Test2".to_string(),
            ))
            .unwrap();

        // Buffer should be flushed after reaching buffer_size
        assert_eq!(logger.buffer.len(), 0);
    }
}