🏡 index : ~doyle/pisshoff.git

author Jordan Doyle <jordan@doyle.la> 2023-06-25 12:55:19.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-06-25 12:57:08.0 +01:00:00
commit
0e6a677b05e875e89a0fbe11dc4ea55f5ef1370b [patch]
tree
9cb7a4969d9faa1a67ec7fa0237f5a1693a1743e
parent
904e1bd265e142d10fa8ca4812a11a8388c5bf7c
download
0e6a677b05e875e89a0fbe11dc4ea55f5ef1370b.tar.gz

Flush to audits to disk after not receiving a write for 5 seconds



Diff

 flake.nix    | 10 ----------
 src/audit.rs | 37 +++++++++++++++++++++++++++++++------
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/flake.nix b/flake.nix
index 84bc4e7..e267263 100644
--- a/flake.nix
+++ a/flake.nix
@@ -40,16 +40,6 @@
            };

            config = mkIf cfg.enable {
              systemd.sockets.pisshoff = {
                wantedBy = [ "sockets.target" ];

                socketConfig = {
                  ListenStream = 22;
                  BindIPv6Only = "both";
                  Accept = "no";
                };
              };

              systemd.services.pisshoff = {
                enable = true;
                wantedBy = [ "multi-user.target" ];
diff --git a/src/audit.rs b/src/audit.rs
index d7c842d..4071cd7 100644
--- a/src/audit.rs
+++ a/src/audit.rs
@@ -1,14 +1,18 @@
use crate::config::Config;
use serde::Serialize;
use std::io::ErrorKind;
use std::sync::Arc;
use std::{
    fmt::{Debug, Formatter},
    io::ErrorKind,
    net::SocketAddr,
    sync::Arc,
    time::{Duration, Instant},
};
use tokio::io::{AsyncWriteExt, BufWriter};
use tokio::{fs::OpenOptions, task::JoinHandle};
use tokio::{
    fs::OpenOptions,
    io::{AsyncWriteExt, BufWriter},
    task::JoinHandle,
};
use tracing::debug;
use uuid::Uuid;

pub fn start_audit_writer(
@@ -26,11 +30,28 @@
            .open(&config.audit_output_file)
            .await?;
        let mut writer = BufWriter::new(file);
        let mut shutdown = false;

        while let Some(log) = recv.recv().await {
            let log =
                serde_json::to_vec(&log).map_err(|e| std::io::Error::new(ErrorKind::Other, e))?;
            writer.write_all(&log).await?;
        loop {
            tokio::select! {
                log = recv.recv(), if !shutdown => {
                    match log {
                        Some(log) => {
                            let log = serde_json::to_vec(&log)
                                .map_err(|e| std::io::Error::new(ErrorKind::Other, e))?;
                            writer.write_all(&log).await?;
                        }
                        None => {
                            shutdown = true;
                        }
                    }
                }
                _ = tokio::time::sleep(Duration::from_secs(5)), if !writer.buffer().is_empty() && !shutdown => {
                    debug!("Flushing audits to disk");
                    writer.flush().await?;
                }
                else => break,
            }
        }

        writer.flush().await?;