fabaccess-bffh/bffhd/audit.rs

66 lines
1.8 KiB
Rust
Raw Permalink Normal View History

use miette::Diagnostic;
2022-05-05 15:50:44 +02:00
use once_cell::sync::OnceCell;
2022-03-11 23:00:02 +01:00
use std::fs::{File, OpenOptions};
use std::io;
use std::io::{LineWriter, Write};
use std::sync::Mutex;
use thiserror::Error;
2022-03-11 23:00:02 +01:00
use crate::Config;
2022-05-05 15:50:44 +02:00
use serde::{Deserialize, Serialize};
2022-03-11 23:00:02 +01:00
use serde_json::Serializer;
2022-03-20 22:46:04 +01:00
pub static AUDIT: OnceCell<AuditLog> = OnceCell::new();
2022-07-24 16:07:49 +02:00
// TODO: Make the audit log a tracing layer
2022-03-11 23:00:02 +01:00
#[derive(Debug)]
pub struct AuditLog {
writer: Mutex<LineWriter<File>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
2022-03-20 22:46:04 +01:00
pub struct AuditLogLine<'a> {
2022-03-11 23:00:02 +01:00
timestamp: i64,
2022-03-20 22:46:04 +01:00
machine: &'a str,
state: &'a str,
2022-03-11 23:00:02 +01:00
}
#[derive(Debug, Error, Diagnostic)]
#[error(transparent)]
#[repr(transparent)]
pub struct Error(#[from] pub io::Error);
2022-03-11 23:00:02 +01:00
impl AuditLog {
pub fn new(config: &Config) -> Result<&'static Self, Error> {
2022-03-20 22:46:04 +01:00
AUDIT.get_or_try_init(|| {
tracing::debug!(path = %config.auditlog_path.display(), "Initializing audit log");
2022-05-05 15:50:44 +02:00
let fd = OpenOptions::new()
.create(true)
.append(true)
.open(&config.auditlog_path)?;
2022-03-20 22:46:04 +01:00
let writer = Mutex::new(LineWriter::new(fd));
Ok(Self { writer })
})
2022-03-11 23:00:02 +01:00
}
2022-03-12 01:27:41 +01:00
pub fn log(&self, machine: &str, state: &str) -> io::Result<()> {
let timestamp = chrono::Utc::now().timestamp();
2022-05-05 15:50:44 +02:00
let line = AuditLogLine {
timestamp,
machine,
state,
};
2022-03-20 22:46:04 +01:00
tracing::debug!(?line, "writing audit log line");
2022-03-11 23:00:02 +01:00
let mut guard = self.writer.lock().unwrap();
let mut writer: &mut LineWriter<File> = &mut *guard;
let mut ser = Serializer::new(&mut writer);
2022-05-05 15:50:44 +02:00
line.serialize(&mut ser)
.expect("failed to serialize audit log line");
2022-06-23 17:31:57 +02:00
writer.write_all("\n".as_bytes())?;
2022-03-11 23:00:02 +01:00
Ok(())
}
}