Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add audit logging plugin #892

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ pub struct Plugins {
pub intercept: Option<Intercept>,
pub table_access: Option<TableAccess>,
pub query_logger: Option<QueryLogger>,
pub audit_logger: Option<AuditLogger>,
pub prewarmer: Option<Prewarmer>,
}

Expand All @@ -901,10 +902,11 @@ impl std::fmt::Display for Plugins {
}
write!(
f,
"interceptor: {}, table_access: {}, query_logger: {}, prewarmer: {}",
"interceptor: {}, table_access: {}, query_logger: {}, audit_logger: {}, prewarmer: {}",
is_enabled(self.intercept.as_ref()),
is_enabled(self.table_access.as_ref()),
is_enabled(self.query_logger.as_ref()),
is_enabled(self.audit_logger.as_ref()),
is_enabled(self.prewarmer.as_ref()),
)
}
Expand Down Expand Up @@ -939,12 +941,24 @@ pub struct QueryLogger {
pub enabled: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
pub struct AuditLogger {
pub enabled: bool,
pub patterns: Vec<String>,
}

impl Plugin for QueryLogger {
fn is_enabled(&self) -> bool {
self.enabled
}
}

impl Plugin for AuditLogger {
fn is_enabled(&self) -> bool {
self.enabled
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
pub struct Prewarmer {
pub enabled: bool,
Expand Down
71 changes: 71 additions & 0 deletions src/plugins/audit_logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Plugin for sanitizing and logging queries and their results
//! Replaces sensitive data matching configured regex patterns with <REDACTED>

use async_trait::async_trait;
use log::info;
use regex::Regex;
use sqlparser::ast::Statement;

use crate::{
errors::Error,
plugins::{Plugin, PluginOutput},
query_router::QueryRouter,
};

#[derive(Clone)]
pub struct AuditLogger<'a> {
pub enabled: bool,
pub patterns: &'a Vec<String>,
compiled_patterns: Vec<Regex>,
}

impl<'a> AuditLogger<'a> {
pub fn new(enabled: bool, patterns: &'a Vec<String>) -> Result<Self, Error> {
let compiled_patterns = patterns
.iter()
.map(|p| Regex::new(p))
.collect::<Result<Vec<Regex>, regex::Error>>()
.map_err(|_e| Error::BadConfig)?;

Ok(AuditLogger {
enabled,
patterns,
compiled_patterns,
})
}

fn sanitize(&self, text: &str) -> String {
let mut sanitized = text.to_string();
for pattern in &self.compiled_patterns {
sanitized = pattern.replace_all(&sanitized, "<REDACTED>").to_string();
}
sanitized
}
}

#[async_trait]
impl<'a> Plugin for AuditLogger<'a> {
async fn run(
&mut self,
query_router: &QueryRouter,
ast: &Vec<Statement>,
) -> Result<PluginOutput, Error> {
if !self.enabled {
return Ok(PluginOutput::Allow);
}

// Log sanitized queries
for stmt in ast {
let query = stmt.to_string();
let sanitized = self.sanitize(&query);
info!(
"[pool: {}][user: {}] Query: {}",
query_router.pool_settings().db,
query_router.pool_settings().user.username,
sanitized
);
}

Ok(PluginOutput::Allow)
}
}
2 changes: 2 additions & 0 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! - etc
//!
pub mod audit_logger;
pub mod intercept;
pub mod prewarmer;
pub mod query_logger;
Expand All @@ -18,6 +19,7 @@ use async_trait::async_trait;
use bytes::BytesMut;
use sqlparser::ast::Statement;

pub use audit_logger::AuditLogger;
pub use intercept::Intercept;
pub use query_logger::QueryLogger;
pub use table_access::TableAccess;
Expand Down
1 change: 1 addition & 0 deletions src/query_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@ mod test {
let plugins = Plugins {
table_access: Some(table_access),
intercept: None,
audit_logger: None,
query_logger: None,
prewarmer: None,
};
Expand Down