ai-model-vault 4.6.1

DEPRECATED - renamed to `ironvault`. This crate is no longer maintained; 4.6.1 is identical to 4.6.0 apart from this notice.
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
//! Audit logging for compliance and security monitoring
//!
//! Compliance:
//! - CMMC AU.3.046: Create and retain audit logs
//! - CMMC AU.3.049: Protect audit information
//! - NIST SP 800-53 AU family

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use crate::blockchain::BlockchainAudit;
use crate::error::Result;

/// Maximum audit log size before rotation (10 MiB).
const MAX_LOG_SIZE: u64 = 10 * 1024 * 1024;
/// Number of rotated log files to keep.
const MAX_ROTATED_LOGS: u32 = 9;

/// Audit event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AuditEventType {
    /// Vault created
    VaultCreated,
    /// Vault opened
    VaultOpened,
    /// Model stored
    ModelStored,
    /// Model retrieved
    ModelRetrieved,
    /// Model deleted
    ModelDeleted,
    /// Version deleted
    VersionDeleted,
    /// Authentication succeeded
    AuthSuccess,
    /// Authentication failed
    AuthFailure,
    /// Configuration changed
    ConfigChanged,
    /// Security violation detected
    SecurityViolation,
    /// Integrity check failed
    IntegrityFailure,
    /// Key derived
    KeyDerived,
}

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEntry {
    /// Timestamp of event
    pub timestamp: DateTime<Utc>,

    /// Event type
    pub event_type: AuditEventType,

    /// Event description
    pub description: String,

    /// Associated model name (if applicable)
    pub model_name: Option<String>,

    /// Associated version (if applicable)
    pub version: Option<u32>,

    /// Success status
    pub success: bool,

    /// Additional metadata
    pub metadata: Option<serde_json::Value>,
}

/// Audit logger
pub struct AuditLogger {
    log_file: PathBuf,
    /// Optional hash-linked mirror of every entry written through [`Self::log`].
    ///
    /// Deliberately owned here rather than by `Vault`: `log` is the single
    /// choke point every helper routes through, so a call site cannot record
    /// to the plain log while skipping the chain. `Mutex` because
    /// [`BlockchainAudit::add_entry`] needs `&mut self` and `log` takes
    /// `&self`.
    chain: Option<Mutex<BlockchainAudit>>,
}

impl AuditLogger {
    /// Create new audit logger
    pub fn new(log_path: &Path) -> Result<Self> {
        // Ensure log directory exists
        if let Some(parent) = log_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        Ok(Self {
            log_file: log_path.to_path_buf(),
            chain: None,
        })
    }

    /// Create an audit logger that also mirrors entries into a blockchain.
    ///
    /// `block_size` is entries per block; see
    /// [`SecuritySettings::blockchain_block_size`](crate::config::SecuritySettings::blockchain_block_size)
    /// for why 1 is the default.
    pub fn with_chain(log_path: &Path, chain_dir: &Path, block_size: usize) -> Result<Self> {
        let mut logger = Self::new(log_path)?;
        // A zero block size would make `add_entry` finalize on every call via
        // `len() >= 0` while also never batching -- and `finalize_block` on an
        // empty pending set returns None, so entries would round-trip through
        // an empty block. Clamp instead of surprising the operator.
        let block_size = block_size.max(1);
        logger.chain = Some(Mutex::new(BlockchainAudit::new(chain_dir, block_size)?));
        Ok(logger)
    }

    /// Borrow the blockchain mirror, if this logger has one.
    pub fn chain(&self) -> Option<&Mutex<BlockchainAudit>> {
        self.chain.as_ref()
    }

    /// Log an audit event
    pub fn log(&self, entry: AuditEntry) -> Result<()> {
        // Rotate log if it exceeds the size limit
        self.rotate_if_needed()?;

        let mut options = OpenOptions::new();
        options.create(true).append(true);
        crate::permissions::set_create_mode(&mut options);

        let mut file = options.open(&self.log_file)?;
        // Ensure restrictive ACLs on Windows too
        crate::permissions::restrict_file(&self.log_file)?;
        let json = serde_json::to_string(&entry)?;
        writeln!(file, "{}", json)?;

        // Mirror into the chain after the log write succeeds. Ordering matters:
        // the plain log is the primary record, and a chain failure must not be
        // able to lose an entry that would otherwise have been written.
        if let Some(chain) = &self.chain {
            let mut chain = chain
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            chain.add_entry(entry)?;
        }

        Ok(())
    }

    /// Rotate the audit log when it exceeds [`MAX_LOG_SIZE`].
    ///
    /// Keeps up to [`MAX_ROTATED_LOGS`] archived copies:
    /// `audit.log` → `audit.log.1` → `audit.log.2` → … → `audit.log.9` (deleted).
    fn rotate_if_needed(&self) -> Result<()> {
        let size = match std::fs::metadata(&self.log_file) {
            Ok(m) => m.len(),
            Err(_) => return Ok(()), // file doesn't exist yet
        };

        if size < MAX_LOG_SIZE {
            return Ok(());
        }

        // Shift existing rotated files: .9 → delete, .8 → .9, … .1 → .2
        for i in (1..MAX_ROTATED_LOGS).rev() {
            let src = self.log_file.with_extension(format!("log.{i}"));
            let dst = self.log_file.with_extension(format!("log.{}", i + 1));
            if src.exists() {
                let _ = std::fs::rename(&src, &dst);
            }
        }

        // Current → .1
        let rotated = self.log_file.with_extension("log.1");
        let _ = std::fs::rename(&self.log_file, &rotated);

        Ok(())
    }

    /// Log a model storage event
    pub fn log_model_stored(&self, model_name: &str, version: u32, success: bool) -> Result<()> {
        self.log(AuditEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::ModelStored,
            description: format!("Model '{}' version {} stored", model_name, version),
            model_name: Some(model_name.to_string()),
            version: Some(version),
            success,
            metadata: None,
        })
    }

    /// Log a model retrieval event
    pub fn log_model_retrieved(&self, model_name: &str, version: u32, success: bool) -> Result<()> {
        self.log(AuditEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::ModelRetrieved,
            description: format!("Model '{}' version {} retrieved", model_name, version),
            model_name: Some(model_name.to_string()),
            version: Some(version),
            success,
            metadata: None,
        })
    }

    /// Log an authentication event
    pub fn log_auth(&self, success: bool, _reason: Option<&str>) -> Result<()> {
        self.log(AuditEntry {
            timestamp: Utc::now(),
            event_type: if success {
                AuditEventType::AuthSuccess
            } else {
                AuditEventType::AuthFailure
            },
            description: if success {
                "Authentication successful".to_string()
            } else {
                "Authentication failed".to_string()
            },
            model_name: None,
            version: None,
            success,
            metadata: None,
        })
    }

    /// Log a security violation
    pub fn log_security_violation(&self, description: &str) -> Result<()> {
        self.log(AuditEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::SecurityViolation,
            description: description.to_string(),
            model_name: None,
            version: None,
            success: false,
            metadata: None,
        })
    }

    /// Read audit log entries
    pub fn read_entries(&self, limit: Option<usize>) -> Result<Vec<AuditEntry>> {
        if !self.log_file.exists() {
            return Ok(Vec::new());
        }

        let contents = std::fs::read_to_string(&self.log_file)?;
        let mut entries: Vec<AuditEntry> = contents
            .lines()
            .filter_map(|line| serde_json::from_str(line).ok())
            .collect();

        if let Some(n) = limit {
            entries.truncate(n);
        }

        Ok(entries)
    }
}

impl Drop for AuditLogger {
    /// Flush any entries still pending in the chain.
    ///
    /// Only does work when `blockchain_block_size > 1`; at the default of 1
    /// every entry is already finalized inline. This narrows the loss window
    /// on a clean exit but cannot close it -- a crash or `SIGKILL` still takes
    /// whatever is pending, which is why the default is 1.
    fn drop(&mut self) {
        let Some(chain) = &self.chain else { return };
        let mut chain = chain
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if let Err(err) = chain.finalize_block() {
            // Drop cannot propagate, and swallowing this would mean losing
            // audit evidence silently -- the one failure mode this feature
            // exists to prevent.
            eprintln!("warning: failed to finalize pending audit block: {err}");
        }
    }
}

// ── Trait implementation ─────────────────────────────────────

impl crate::traits::AuditSink for AuditLogger {
    fn emit(&self, entry: AuditEntry) -> Result<()> {
        self.log(entry)
    }

    fn query(&self, limit: Option<usize>) -> Result<Vec<AuditEntry>> {
        self.read_entries(limit)
    }
}

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

    #[test]
    fn test_audit_logger_emit_and_query() {
        // Covers lines 197, 198 — AuditSink::query() trait impl
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        let sink: &dyn AuditSink = &logger;
        sink.emit(AuditEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::ModelStored,
            description: "Test entry".into(),
            model_name: Some("m1".into()),
            version: Some(1),
            success: true,
            metadata: None,
        })
        .unwrap();

        let entries = sink.query(Some(10)).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].description, "Test entry");
    }

    #[test]
    fn test_audit_logger_query_limit() {
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        for i in 0..5 {
            logger
                .log(AuditEntry {
                    timestamp: Utc::now(),
                    event_type: AuditEventType::ModelStored,
                    description: format!("entry {}", i),
                    model_name: None,
                    version: None,
                    success: true,
                    metadata: None,
                })
                .unwrap();
        }

        let entries = logger.read_entries(Some(3)).unwrap();
        assert_eq!(entries.len(), 3);

        let all = logger.read_entries(None).unwrap();
        assert_eq!(all.len(), 5);
    }

    #[test]
    fn test_log_auth_success() {
        // Covers L143 — log_auth with success=true
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        logger.log_auth(true, None).unwrap();

        let entries = logger.read_entries(None).unwrap();
        assert_eq!(entries.len(), 1);
        assert!(entries[0].success);
        assert_eq!(entries[0].description, "Authentication successful");
        assert!(matches!(entries[0].event_type, AuditEventType::AuthSuccess));
    }

    #[test]
    fn test_log_auth_failure() {
        // Covers L148 — log_auth with success=false
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        logger.log_auth(false, Some("bad password")).unwrap();

        let entries = logger.read_entries(None).unwrap();
        assert_eq!(entries.len(), 1);
        assert!(!entries[0].success);
        assert!(entries[0].description.contains("Authentication failed"));
        assert!(matches!(entries[0].event_type, AuditEventType::AuthFailure));
    }

    #[test]
    fn test_log_auth_failure_no_reason() {
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        logger.log_auth(false, None).unwrap();

        let entries = logger.read_entries(None).unwrap();
        assert!(entries[0].description.contains("Authentication failed"));
    }

    #[test]
    fn test_log_security_violation() {
        // Covers L158-166, L173
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("audit.log");
        let logger = AuditLogger::new(&log_path).unwrap();

        logger
            .log_security_violation("Unauthorized access attempt")
            .unwrap();

        let entries = logger.read_entries(None).unwrap();
        assert_eq!(entries.len(), 1);
        assert!(!entries[0].success);
        assert_eq!(entries[0].description, "Unauthorized access attempt");
        assert!(matches!(
            entries[0].event_type,
            AuditEventType::SecurityViolation
        ));
    }

    #[test]
    fn test_read_entries_no_file() {
        // Covers the early-return when log_file doesn't exist
        let temp_dir = tempfile::tempdir().unwrap();
        let log_path = temp_dir.path().join("nonexistent.log");
        let logger = AuditLogger {
            log_file: log_path,
            chain: None,
        };
        let entries = logger.read_entries(None).unwrap();
        assert!(entries.is_empty());
    }
}