mielin-cli 0.1.0-rc.1

Command-line interface and control plane for MielinOS distributed agent mesh
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
//! Audit logging system for tracking CLI operations
//!
//! This module provides comprehensive audit logging for all CLI operations,
//! including command execution, configuration changes, and security events.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs::{self, OpenOptions};
use std::io::Write as _;
use std::path::PathBuf;

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEntry {
    /// Timestamp of the event
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// User who executed the command
    pub user: String,
    /// Command that was executed
    pub command: String,
    /// Command arguments
    pub args: Vec<String>,
    /// Exit code (None if still running)
    pub exit_code: Option<i32>,
    /// Duration of command execution
    pub duration_ms: Option<u64>,
    /// Event type
    pub event_type: AuditEventType,
    /// Severity level
    pub severity: AuditSeverity,
    /// Additional metadata
    pub metadata: serde_json::Value,
}

/// Audit event types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditEventType {
    /// Command execution
    CommandExecution,
    /// Configuration change
    ConfigChange,
    /// Authentication event
    Authentication,
    /// Authorization event
    Authorization,
    /// Security event
    Security,
    /// System event
    System,
}

/// Audit severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AuditSeverity {
    /// Informational
    Info,
    /// Warning
    Warning,
    /// Error
    Error,
    /// Critical
    Critical,
}

/// Audit logger configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditConfig {
    /// Enable audit logging
    pub enabled: bool,
    /// Log file path
    pub log_path: PathBuf,
    /// Maximum log entries before rotation
    pub max_entries: usize,
    /// Number of rotated logs to keep
    pub keep_rotations: usize,
    /// Minimum severity to log
    pub min_severity: AuditSeverity,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            log_path: get_default_audit_log_path(),
            max_entries: 10000,
            keep_rotations: 5,
            min_severity: AuditSeverity::Info,
        }
    }
}

/// Audit logger
pub struct AuditLogger {
    config: AuditConfig,
}

impl AuditLogger {
    /// Create a new audit logger with the given configuration
    pub fn new(config: AuditConfig) -> Result<Self> {
        // Ensure audit log directory exists
        if let Some(parent) = config.log_path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create audit log directory: {}", parent.display())
            })?;
        }

        Ok(Self { config })
    }

    /// Create a new audit logger with default configuration
    pub fn with_default_config() -> Result<Self> {
        Self::new(AuditConfig::default())
    }

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

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

        // Check if rotation is needed
        self.rotate_if_needed()?;

        // Append entry to log file
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.config.log_path)
            .with_context(|| {
                format!(
                    "Failed to open audit log: {}",
                    self.config.log_path.display()
                )
            })?;

        let json = serde_json::to_string(&entry).context("Failed to serialize audit entry")?;

        writeln!(file, "{}", json).context("Failed to write audit entry")?;

        Ok(())
    }

    /// Log a command execution
    pub fn log_command(
        &self,
        command: &str,
        args: &[String],
        exit_code: Option<i32>,
        duration_ms: Option<u64>,
    ) -> Result<()> {
        let entry = AuditEntry {
            timestamp: chrono::Utc::now(),
            user: get_current_user(),
            command: command.to_string(),
            args: args.to_vec(),
            exit_code,
            duration_ms,
            event_type: AuditEventType::CommandExecution,
            severity: if exit_code == Some(0) || exit_code.is_none() {
                AuditSeverity::Info
            } else {
                AuditSeverity::Warning
            },
            metadata: serde_json::json!({
                "pid": std::process::id(),
            }),
        };

        self.log(entry)
    }

    /// Log a configuration change
    pub fn log_config_change(
        &self,
        key: &str,
        old_value: Option<&str>,
        new_value: &str,
    ) -> Result<()> {
        let entry = AuditEntry {
            timestamp: chrono::Utc::now(),
            user: get_current_user(),
            command: "config".to_string(),
            args: vec![key.to_string(), new_value.to_string()],
            exit_code: Some(0),
            duration_ms: None,
            event_type: AuditEventType::ConfigChange,
            severity: AuditSeverity::Info,
            metadata: serde_json::json!({
                "key": key,
                "old_value": old_value,
                "new_value": new_value,
            }),
        };

        self.log(entry)
    }

    /// Log a security event
    pub fn log_security_event(&self, message: &str, severity: AuditSeverity) -> Result<()> {
        let entry = AuditEntry {
            timestamp: chrono::Utc::now(),
            user: get_current_user(),
            command: "security".to_string(),
            args: vec![],
            exit_code: None,
            duration_ms: None,
            event_type: AuditEventType::Security,
            severity,
            metadata: serde_json::json!({
                "message": message,
            }),
        };

        self.log(entry)
    }

    /// Read all audit entries
    pub fn read_entries(&self) -> Result<Vec<AuditEntry>> {
        if !self.config.log_path.exists() {
            return Ok(vec![]);
        }

        let content = fs::read_to_string(&self.config.log_path).with_context(|| {
            format!(
                "Failed to read audit log: {}",
                self.config.log_path.display()
            )
        })?;

        let entries: Vec<AuditEntry> = content
            .lines()
            .filter(|line| !line.trim().is_empty())
            .filter_map(|line| serde_json::from_str(line).ok())
            .collect();

        Ok(entries)
    }

    /// Query audit entries with filters
    pub fn query_entries(
        &self,
        event_type: Option<AuditEventType>,
        severity: Option<AuditSeverity>,
        user: Option<&str>,
        since: Option<chrono::DateTime<chrono::Utc>>,
        until: Option<chrono::DateTime<chrono::Utc>>,
        limit: Option<usize>,
    ) -> Result<Vec<AuditEntry>> {
        let mut entries = self.read_entries()?;

        // Apply filters
        entries.retain(|entry| {
            if let Some(et) = event_type {
                if entry.event_type != et {
                    return false;
                }
            }

            if let Some(sev) = severity {
                if entry.severity < sev {
                    return false;
                }
            }

            if let Some(u) = user {
                if entry.user != u {
                    return false;
                }
            }

            if let Some(since_time) = since {
                if entry.timestamp < since_time {
                    return false;
                }
            }

            if let Some(until_time) = until {
                if entry.timestamp > until_time {
                    return false;
                }
            }

            true
        });

        // Sort by timestamp (newest first)
        entries.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

        // Apply limit
        if let Some(limit_count) = limit {
            entries.truncate(limit_count);
        }

        Ok(entries)
    }

    /// Rotate audit log if needed
    fn rotate_if_needed(&self) -> Result<()> {
        if !self.config.log_path.exists() {
            return Ok(());
        }

        let entries = self.read_entries()?;

        if entries.len() < self.config.max_entries {
            return Ok(());
        }

        // Rotate logs
        for i in (1..self.config.keep_rotations).rev() {
            let old_path = self.get_rotated_path(i);
            let new_path = self.get_rotated_path(i + 1);

            if old_path.exists() {
                fs::rename(&old_path, &new_path).with_context(|| {
                    format!(
                        "Failed to rotate log {} to {}",
                        old_path.display(),
                        new_path.display()
                    )
                })?;
            }
        }

        // Move current log to .1
        let rotated_path = self.get_rotated_path(1);
        fs::rename(&self.config.log_path, &rotated_path).with_context(|| {
            format!("Failed to rotate current log to {}", rotated_path.display())
        })?;

        // Remove old rotated logs
        let old_path = self.get_rotated_path(self.config.keep_rotations + 1);
        if old_path.exists() {
            fs::remove_file(&old_path).with_context(|| {
                format!("Failed to remove old rotated log: {}", old_path.display())
            })?;
        }

        Ok(())
    }

    /// Get rotated log path
    fn get_rotated_path(&self, rotation: usize) -> PathBuf {
        let mut path = self.config.log_path.clone();
        let file_name = path.file_name().unwrap().to_string_lossy();
        path.set_file_name(format!("{}.{}", file_name, rotation));
        path
    }

    /// Clear all audit logs
    pub fn clear(&self) -> Result<()> {
        // Remove current log
        if self.config.log_path.exists() {
            fs::remove_file(&self.config.log_path).with_context(|| {
                format!(
                    "Failed to remove audit log: {}",
                    self.config.log_path.display()
                )
            })?;
        }

        // Remove rotated logs
        for i in 1..=self.config.keep_rotations {
            let rotated_path = self.get_rotated_path(i);
            if rotated_path.exists() {
                fs::remove_file(&rotated_path).with_context(|| {
                    format!("Failed to remove rotated log: {}", rotated_path.display())
                })?;
            }
        }

        Ok(())
    }

    /// Get audit statistics
    pub fn get_stats(&self) -> Result<AuditStats> {
        let entries = self.read_entries()?;

        let total_entries = entries.len();
        let by_event_type = count_by_event_type(&entries);
        let by_severity = count_by_severity(&entries);
        let by_user = count_by_user(&entries);

        let oldest_entry = entries.iter().map(|e| e.timestamp).min();
        let newest_entry = entries.iter().map(|e| e.timestamp).max();

        Ok(AuditStats {
            total_entries,
            by_event_type,
            by_severity,
            by_user,
            oldest_entry,
            newest_entry,
        })
    }
}

/// Audit statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditStats {
    pub total_entries: usize,
    pub by_event_type: std::collections::HashMap<String, usize>,
    pub by_severity: std::collections::HashMap<String, usize>,
    pub by_user: std::collections::HashMap<String, usize>,
    pub oldest_entry: Option<chrono::DateTime<chrono::Utc>>,
    pub newest_entry: Option<chrono::DateTime<chrono::Utc>>,
}

fn count_by_event_type(entries: &[AuditEntry]) -> std::collections::HashMap<String, usize> {
    let mut counts = std::collections::HashMap::new();
    for entry in entries {
        let key = format!("{:?}", entry.event_type);
        *counts.entry(key).or_insert(0) += 1;
    }
    counts
}

fn count_by_severity(entries: &[AuditEntry]) -> std::collections::HashMap<String, usize> {
    let mut counts = std::collections::HashMap::new();
    for entry in entries {
        let key = format!("{:?}", entry.severity);
        *counts.entry(key).or_insert(0) += 1;
    }
    counts
}

fn count_by_user(entries: &[AuditEntry]) -> std::collections::HashMap<String, usize> {
    let mut counts = std::collections::HashMap::new();
    for entry in entries {
        *counts.entry(entry.user.clone()).or_insert(0) += 1;
    }
    counts
}

/// Get default audit log path
fn get_default_audit_log_path() -> PathBuf {
    if let Some(config_dir) = dirs::config_dir() {
        config_dir.join("mielin").join("audit.log")
    } else {
        PathBuf::from(".mielin_audit.log")
    }
}

/// Get current user
fn get_current_user() -> String {
    std::env::var("USER")
        .or_else(|_| std::env::var("USERNAME"))
        .unwrap_or_else(|_| "unknown".to_string())
}

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

    #[test]
    fn test_audit_entry_creation() {
        let entry = AuditEntry {
            timestamp: chrono::Utc::now(),
            user: "test_user".to_string(),
            command: "node".to_string(),
            args: vec!["list".to_string()],
            exit_code: Some(0),
            duration_ms: Some(100),
            event_type: AuditEventType::CommandExecution,
            severity: AuditSeverity::Info,
            metadata: serde_json::json!({}),
        };

        assert_eq!(entry.command, "node");
        assert_eq!(entry.severity, AuditSeverity::Info);
    }

    #[test]
    fn test_audit_config_default() {
        let config = AuditConfig::default();
        assert!(config.enabled);
        assert_eq!(config.max_entries, 10000);
        assert_eq!(config.keep_rotations, 5);
        assert_eq!(config.min_severity, AuditSeverity::Info);
    }

    #[test]
    fn test_get_current_user() {
        let user = get_current_user();
        assert!(!user.is_empty());
    }

    #[test]
    fn test_severity_ordering() {
        assert!(AuditSeverity::Info < AuditSeverity::Warning);
        assert!(AuditSeverity::Warning < AuditSeverity::Error);
        assert!(AuditSeverity::Error < AuditSeverity::Critical);
    }
}