forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

audit.rs50 lines, 1.8 KiBHistoryRawDownload
1//! The audit writer of the web layer: appends one entry for each admin write, after the change commits.
2//!
3//! A failed append is logged and never fails the request that made the change, because the change has
4//! already happened and the person asking should not see an error for it. A successful append wakes the
5//! open admin pages. `data` is built by the callers from ids and values only; secrets never go in it.
6
7use in_core::store::audit::NewAudit;
8
9use crate::live::Topic;
10use crate::state::AppState;
11
12/// What one audit entry records: who acted, what they did, and to what.
13pub struct Entry<'a> {
14 /// The acting person's sub, or `None` for the system.
15 pub actor: Option<&'a str>,
16 /// Dotted verb, such as `quota.set`.
17 pub action: &'a str,
18 /// Kind of the changed thing, such as `member`.
19 pub target_kind: &'a str,
20 /// Id of the changed thing.
21 pub target_id: Option<&'a str>,
22 /// Details as a JSON object. Never a secret.
23 pub data: serde_json::Value,
24}
25
26/// Appends `entry`. Never fails: a failure is logged with the verb, and the caller carries on.
27pub async fn record(state: &AppState, entry: Entry<'_>) {
28 let data = entry.data.to_string();
29 let new = NewAudit {
30 actor_id: entry.actor.map(str::to_owned),
31 actor_kind: if entry.actor.is_some() {
32 "member"
33 } else {
34 "system"
35 },
36 action: entry.action.to_owned(),
37 target_kind: entry.target_kind.to_owned(),
38 target_id: entry.target_id.map(str::to_owned),
39 ip: None,
40 data,
41 };
42 match state.store.insert_audit(new).await {
43 Ok(_) => state.live.bump(&Topic::AdminAudit),
44 Err(error) => tracing::error!(
45 action = entry.action,
46 error = %error,
47 "audit: an entry could not be written; the change itself stands"
48 ),
49 }
50}