forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.rs
More file actions
322 lines (286 loc) · 9.67 KB
/
audit.rs
File metadata and controls
322 lines (286 loc) · 9.67 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Append-only hash-chain audit log with SHA-256 integrity verification.
use crate::types::{AuditEntry, AuditFilter};
use sha2::{Digest, Sha256};
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
/// Append-only hash-chained audit logger.
///
/// Each entry's hash covers `seq|timestamp|agent_id|action|decision|prev_hash`,
/// creating a tamper-evident chain from the genesis entry.
pub struct AuditLogger {
entries: Mutex<Vec<AuditEntry>>,
max_entries: Option<usize>,
}
impl AuditLogger {
/// Create an empty audit logger with no entry limit.
pub fn new() -> Self {
Self {
entries: Mutex::new(Vec::new()),
max_entries: None,
}
}
/// Create an audit logger that retains at most `max` entries,
/// evicting the oldest when the limit is exceeded.
pub fn with_max_entries(max: usize) -> Self {
Self {
entries: Mutex::new(Vec::new()),
max_entries: Some(max),
}
}
/// Append a new entry to the audit chain and return it.
pub fn log(&self, agent_id: &str, action: &str, decision: &str) -> AuditEntry {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
let seq = entries.len() as u64;
let prev_hash = entries
.last()
.map(|e| e.hash.clone())
.unwrap_or_default();
let timestamp = iso8601_now();
let hash_input = format!(
"{}|{}|{}|{}|{}|{}",
seq, timestamp, agent_id, action, decision, prev_hash
);
let hash = sha256_hex(&hash_input);
let entry = AuditEntry {
seq,
timestamp,
agent_id: agent_id.to_string(),
action: action.to_string(),
decision: decision.to_string(),
previous_hash: prev_hash,
hash,
};
entries.push(entry.clone());
// Evict oldest entries when the retention limit is exceeded.
if let Some(max) = self.max_entries {
if entries.len() > max {
let overflow = entries.len() - max;
entries.drain(..overflow);
}
}
entry
}
/// Verify the integrity of the entire hash chain.
///
/// Returns `true` if every entry's hash is correct and linked to the
/// previous entry's hash.
pub fn verify(&self) -> bool {
let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
for (i, entry) in entries.iter().enumerate() {
let expected_prev = if i == 0 {
String::new()
} else {
entries[i - 1].hash.clone()
};
if entry.previous_hash != expected_prev {
return false;
}
let hash_input = format!(
"{}|{}|{}|{}|{}|{}",
entry.seq,
entry.timestamp,
entry.agent_id,
entry.action,
entry.decision,
entry.previous_hash
);
if entry.hash != sha256_hex(&hash_input) {
return false;
}
}
true
}
/// Return all audit entries.
pub fn entries(&self) -> Vec<AuditEntry> {
self.entries
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
/// Serialise all audit entries to a JSON string.
pub fn export_json(&self) -> String {
let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
serde_json::to_string(&*entries).unwrap_or_else(|_| "[]".to_string())
}
/// Return entries matching the given filter.
pub fn get_entries(&self, filter: &AuditFilter) -> Vec<AuditEntry> {
self.entries
.lock()
.unwrap_or_else(|e| e.into_inner())
.iter()
.filter(|e| {
if let Some(ref id) = filter.agent_id {
if e.agent_id != *id {
return false;
}
}
if let Some(ref action) = filter.action {
if e.action != *action {
return false;
}
}
if let Some(ref decision) = filter.decision {
if e.decision != *decision {
return false;
}
}
true
})
.cloned()
.collect()
}
}
impl Default for AuditLogger {
fn default() -> Self {
Self::new()
}
}
fn sha256_hex(input: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(input.as_bytes());
let result = hasher.finalize();
hex_encode(&result)
}
fn hex_encode(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{:02x}", b)).collect()
}
fn iso8601_now() -> String {
let d = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let secs = d.as_secs();
let days = secs / 86400;
let time_of_day = secs % 86400;
let hours = time_of_day / 3600;
let minutes = (time_of_day % 3600) / 60;
let seconds = time_of_day % 60;
// Days since epoch → civil date (Howard Hinnant algorithm)
let z = days as i64 + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d_val = doy - (153 * mp + 2) / 5 + 1;
let m_val = if mp < 10 { mp + 3 } else { mp - 9 };
let y_val = if m_val <= 2 { y + 1 } else { y };
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
y_val, m_val, d_val, hours, minutes, seconds
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_append_and_verify() {
let logger = AuditLogger::new();
logger.log("agent-1", "data.read", "allow");
logger.log("agent-1", "shell:rm", "deny");
logger.log("agent-2", "deploy.prod", "requires_approval");
assert!(logger.verify());
assert_eq!(logger.entries().len(), 3);
}
#[test]
fn test_genesis_has_empty_prev_hash() {
let logger = AuditLogger::new();
let entry = logger.log("agent-1", "test", "allow");
assert!(entry.previous_hash.is_empty());
}
#[test]
fn test_chain_links() {
let logger = AuditLogger::new();
let e1 = logger.log("a", "action1", "allow");
let e2 = logger.log("a", "action2", "deny");
assert_eq!(e2.previous_hash, e1.hash);
}
#[test]
fn test_tamper_detection() {
let logger = AuditLogger::new();
logger.log("agent-1", "data.read", "allow");
logger.log("agent-1", "data.write", "allow");
// Tamper with the first entry
{
let mut entries = logger.entries.lock().unwrap();
entries[0].action = "tampered".to_string();
}
assert!(!logger.verify());
}
#[test]
fn test_filter() {
let logger = AuditLogger::new();
logger.log("agent-1", "data.read", "allow");
logger.log("agent-2", "data.write", "deny");
logger.log("agent-1", "shell:ls", "deny");
let filter = AuditFilter {
agent_id: Some("agent-1".to_string()),
..Default::default()
};
let filtered = logger.get_entries(&filter);
assert_eq!(filtered.len(), 2);
let filter = AuditFilter {
decision: Some("deny".to_string()),
..Default::default()
};
let filtered = logger.get_entries(&filter);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_sha256_not_placeholder() {
let hash = sha256_hex("test");
// SHA-256 of "test" is a known value
assert_eq!(hash.len(), 64); // 32 bytes = 64 hex chars
assert_eq!(
hash,
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
);
}
#[test]
fn test_export_json() {
let logger = AuditLogger::new();
logger.log("agent-1", "data.read", "allow");
logger.log("agent-2", "shell:rm", "deny");
let json = logger.export_json();
let parsed: Vec<AuditEntry> = serde_json::from_str(&json).unwrap();
assert_eq!(parsed.len(), 2);
assert_eq!(parsed[0].agent_id, "agent-1");
assert_eq!(parsed[1].agent_id, "agent-2");
}
#[test]
fn test_export_json_empty() {
let logger = AuditLogger::new();
let json = logger.export_json();
assert_eq!(json, "[]");
}
#[test]
fn test_max_entries_eviction() {
let logger = AuditLogger::with_max_entries(3);
for i in 0..5 {
logger.log("agent", &format!("action-{}", i), "allow");
}
let entries = logger.entries();
assert_eq!(entries.len(), 3);
// Oldest entries (action-0, action-1) should have been evicted
assert_eq!(entries[0].action, "action-2");
assert_eq!(entries[1].action, "action-3");
assert_eq!(entries[2].action, "action-4");
}
#[test]
fn test_max_entries_not_exceeded() {
let logger = AuditLogger::with_max_entries(10);
logger.log("a", "x", "allow");
logger.log("b", "y", "deny");
assert_eq!(logger.entries().len(), 2);
}
#[test]
fn test_no_limit_grows_unbounded() {
let logger = AuditLogger::new();
for i in 0..100 {
logger.log("a", &format!("act-{}", i), "allow");
}
assert_eq!(logger.entries().len(), 100);
}
}