//! The audit writer of the web layer: appends one entry for each admin write, after the change commits. //! //! A failed append is logged and never fails the request that made the change, because the change has //! already happened and the person asking should not see an error for it. A successful append wakes the //! open admin pages. `data` is built by the callers from ids and values only; secrets never go in it. use in_core::store::audit::NewAudit; use crate::live::Topic; use crate::state::AppState; /// What one audit entry records: who acted, what they did, and to what. pub struct Entry<'a> { /// The acting person's sub, or `None` for the system. pub actor: Option<&'a str>, /// Dotted verb, such as `quota.set`. pub action: &'a str, /// Kind of the changed thing, such as `member`. pub target_kind: &'a str, /// Id of the changed thing. pub target_id: Option<&'a str>, /// Details as a JSON object. Never a secret. pub data: serde_json::Value, } /// Appends `entry`. Never fails: a failure is logged with the verb, and the caller carries on. pub async fn record(state: &AppState, entry: Entry<'_>) { let data = entry.data.to_string(); let new = NewAudit { actor_id: entry.actor.map(str::to_owned), actor_kind: if entry.actor.is_some() { "member" } else { "system" }, action: entry.action.to_owned(), target_kind: entry.target_kind.to_owned(), target_id: entry.target_id.map(str::to_owned), ip: None, data, }; match state.store.insert_audit(new).await { Ok(_) => state.live.bump(&Topic::AdminAudit), Err(error) => tracing::error!( action = entry.action, error = %error, "audit: an entry could not be written; the change itself stands" ), } }