Import auditlog from v0.3.2

This commit is contained in:
Nadja Reitzenstein 2022-03-11 23:00:02 +01:00
parent c54b44e720
commit 3e4350d2cf
2 changed files with 44 additions and 0 deletions

43
bffhd/audit.rs Normal file
View File

@ -0,0 +1,43 @@
use std::fs::{File, OpenOptions};
use std::io;
use std::io::{LineWriter, Write};
use std::sync::Mutex;
use crate::Config;
use serde::{Serialize, Deserialize};
use serde_json::Serializer;
use time::OffsetDateTime;
use crate::db::machine::{MachineIdentifier, MachineState};
#[derive(Debug)]
pub struct AuditLog {
writer: Mutex<LineWriter<File>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogLine {
timestamp: i64,
machine: MachineIdentifier,
state: MachineState,
}
impl AuditLog {
pub fn new(config: &Config) -> io::Result<Self> {
let fd = OpenOptions::new().create(true).append(true).open(&config.auditlog_path)?;
let writer = Mutex::new(LineWriter::new(fd));
Ok(Self { writer })
}
pub fn log(&self, machine: &MachineIdentifier, state: &MachineState) -> io::Result<()> {
let timestamp = OffsetDateTime::now_utc().unix_timestamp();
let line = AuditLogLine { timestamp, machine: machine.clone(), state: state.clone() };
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(())
}
}

View File

@ -36,6 +36,7 @@ pub mod utils;
mod tls;
mod keylog;
mod logging;
mod audit;
use std::fs::File;
use std::io::BufReader;