fabaccess-bffh/bffhd/audit.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

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;
2022-03-20 22:46:04 +01:00
use once_cell::sync::OnceCell;
2022-03-11 23:00:02 +01:00
use crate::Config;
use serde::{Serialize, Deserialize};
use serde_json::Serializer;
2022-03-20 22:46:04 +01:00
pub static AUDIT: OnceCell<AuditLog> = OnceCell::new();
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
}
impl AuditLog {
2022-03-20 22:46:04 +01:00
pub fn new(config: &Config) -> io::Result<&'static Self> {
AUDIT.get_or_try_init(|| {
tracing::debug!(path = %config.auditlog_path.display(), "Initializing audit log");
let fd = OpenOptions::new().create(true).append(true).open(&config.auditlog_path)?;
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-03-20 22:46:04 +01:00
let line = AuditLogLine { timestamp, machine, state };
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);
line.serialize(&mut ser).expect("failed to serialize audit log line");
writer.write("\n".as_bytes())?;
Ok(())
}
}