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
use std::{
fs::OpenOptions,
io::Write,
path::{Path, PathBuf},
};
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::{
error::Result,
fs_secure::{create_private_file_if_missing, set_permissions, PRIVATE_FILE_MODE},
types::{AgentId, Owner, SecretId},
};
/// Audit events emitted by the system.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum AuditEvent {
/// Secret was accessed.
SecretAccessed {
/// Secret that was accessed.
secret_id: SecretId,
/// Agent that accessed the secret.
by: AgentId,
},
/// Secret expired and was reaped.
SecretExpired {
/// Secret that expired.
secret_id: SecretId,
},
/// Secret was created.
SecretCreated {
/// Secret that was created.
secret_id: SecretId,
/// Agent that created the secret.
by: AgentId,
},
/// Secret was revoked.
SecretRevoked {
/// Secret that was revoked.
secret_id: SecretId,
/// Agent that revoked the secret.
by: AgentId,
},
/// Human secret request was created.
RequestCreated {
/// Request UUID.
request_id: Uuid,
/// Secret being requested.
secret_id: SecretId,
/// Agent that opened the request.
requested_by: AgentId,
/// Operator-visible request reason.
reason: String,
/// Request expiry timestamp.
expires_at: DateTime<Utc>,
},
/// Human secret request was approved.
RequestApproved {
/// Request UUID.
request_id: Uuid,
/// Secret that was requested.
secret_id: SecretId,
/// Original requester.
requested_by: AgentId,
/// Agent that approved the request.
approved_by: AgentId,
},
/// Human secret request was denied.
RequestDenied {
/// Request UUID.
request_id: Uuid,
/// Secret that was requested.
secret_id: SecretId,
/// Original requester.
requested_by: AgentId,
/// Agent that denied the request.
denied_by: AgentId,
},
/// Vault was created.
VaultCreated {
/// Vault logical name.
vault: String,
/// Vault owner domain.
owner: Owner,
},
/// Vault was mounted.
VaultMounted {
/// Vault logical name.
vault: String,
/// Agent that mounted the vault.
agent: AgentId,
/// Requested session TTL in minutes.
ttl_minutes: u64,
},
/// Vault was unmounted.
VaultUnmounted {
/// Vault logical name.
vault: String,
/// Unmount reason.
reason: String,
/// Agent associated with the operation.
agent: AgentId,
},
/// Vault session expired.
VaultSessionExpired {
/// Vault logical name.
vault: String,
},
/// Vault file handoff prompt was generated.
VaultHandoffPromptIssued {
/// Vault logical name.
vault: String,
/// Requesting agent.
requester: AgentId,
/// Trusted agent expected to retrieve the file.
trusted_agent: AgentId,
/// Requested file path inside the vault.
requested_file: String,
},
/// Agent GPG key was created.
GpgKeyCreated {
/// Agent the key belongs to.
agent: AgentId,
/// Primary key fingerprint.
fingerprint: String,
},
/// One CLI or daemon action completed.
CommandExecuted {
/// Agent that executed the command.
by: AgentId,
/// Invocation surface (for example: `cli`, `daemon`).
interface: String,
/// Logical action name.
command: String,
/// Optional action target (for example secret id).
target: Option<String>,
},
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AuditLine {
timestamp: DateTime<Utc>,
#[serde(flatten)]
event: AuditEvent,
}
/// JSONL append-only audit log writer.
pub struct AuditLog {
path: PathBuf,
}
impl AuditLog {
/// Creates a new audit log at `path`.
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
let file_path = path.as_ref().to_path_buf();
create_private_file_if_missing(&file_path, b"")?;
set_permissions(&file_path, PRIVATE_FILE_MODE)?;
Ok(Self { path: file_path })
}
/// Appends one event as a JSON line.
pub fn log(&self, event: AuditEvent) -> Result<()> {
let line = AuditLine {
timestamp: Utc::now(),
event,
};
let mut file = OpenOptions::new().append(true).open(&self.path)?;
serde_json::to_writer(&mut file, &line)?;
file.write_all(b"\n")?;
Ok(())
}
/// Returns the audit file path.
pub fn path(&self) -> &Path {
&self.path
}
}