171 行
4.3 KiB
Rust
171 行
4.3 KiB
Rust
|
|
use std::io::{self, Write};
|
||
|
|
|
||
|
|
use tracing_subscriber::fmt::MakeWriter;
|
||
|
|
|
||
|
|
use crate::diagnostics::redaction::Redactor;
|
||
|
|
|
||
|
|
#[derive(Clone)]
|
||
|
|
pub struct RedactingMakeWriter<M> {
|
||
|
|
inner: M,
|
||
|
|
redactor: Redactor,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<M> RedactingMakeWriter<M> {
|
||
|
|
pub fn new(inner: M) -> Self {
|
||
|
|
Self {
|
||
|
|
inner,
|
||
|
|
redactor: Redactor::default(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
fn with_redactor(inner: M, redactor: Redactor) -> Self {
|
||
|
|
Self { inner, redactor }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'writer, M> MakeWriter<'writer> for RedactingMakeWriter<M>
|
||
|
|
where
|
||
|
|
M: MakeWriter<'writer>,
|
||
|
|
M::Writer: Write,
|
||
|
|
{
|
||
|
|
type Writer = RedactingWriter<M::Writer>;
|
||
|
|
|
||
|
|
fn make_writer(&'writer self) -> Self::Writer {
|
||
|
|
RedactingWriter {
|
||
|
|
inner: self.inner.make_writer(),
|
||
|
|
redactor: self.redactor.clone(),
|
||
|
|
buffer: Vec::new(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct RedactingWriter<W: Write> {
|
||
|
|
inner: W,
|
||
|
|
redactor: Redactor,
|
||
|
|
buffer: Vec<u8>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<W: Write> RedactingWriter<W> {
|
||
|
|
fn flush_redacted(&mut self) -> io::Result<()> {
|
||
|
|
if self.buffer.is_empty() {
|
||
|
|
return Ok(());
|
||
|
|
}
|
||
|
|
|
||
|
|
let rendered = String::from_utf8_lossy(&self.buffer);
|
||
|
|
let redacted = self.redactor.redact(&rendered);
|
||
|
|
self.inner.write_all(redacted.as_bytes())?;
|
||
|
|
self.buffer.clear();
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<W: Write> Write for RedactingWriter<W> {
|
||
|
|
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
|
||
|
|
self.buffer.extend_from_slice(buffer);
|
||
|
|
Ok(buffer.len())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn flush(&mut self) -> io::Result<()> {
|
||
|
|
self.flush_redacted()?;
|
||
|
|
self.inner.flush()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<W: Write> Drop for RedactingWriter<W> {
|
||
|
|
fn drop(&mut self) {
|
||
|
|
let _ = self.flush_redacted();
|
||
|
|
let _ = self.inner.flush();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn init() {
|
||
|
|
tracing_subscriber::fmt()
|
||
|
|
.with_env_filter(
|
||
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
||
|
|
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
|
||
|
|
)
|
||
|
|
.with_target(false)
|
||
|
|
.compact()
|
||
|
|
.with_writer(RedactingMakeWriter::new(std::io::stderr))
|
||
|
|
.init();
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use std::sync::{Arc, Mutex};
|
||
|
|
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[derive(Clone, Default)]
|
||
|
|
struct SharedMakeWriter {
|
||
|
|
output: Arc<Mutex<Vec<u8>>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl SharedMakeWriter {
|
||
|
|
fn contents(&self) -> String {
|
||
|
|
String::from_utf8(self.output.lock().expect("output lock").clone())
|
||
|
|
.expect("UTF-8 output")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
struct SharedWriter {
|
||
|
|
output: Arc<Mutex<Vec<u8>>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Write for SharedWriter {
|
||
|
|
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
|
||
|
|
self.output
|
||
|
|
.lock()
|
||
|
|
.expect("output lock")
|
||
|
|
.extend_from_slice(buffer);
|
||
|
|
Ok(buffer.len())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn flush(&mut self) -> io::Result<()> {
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'writer> MakeWriter<'writer> for SharedMakeWriter {
|
||
|
|
type Writer = SharedWriter;
|
||
|
|
|
||
|
|
fn make_writer(&'writer self) -> Self::Writer {
|
||
|
|
SharedWriter {
|
||
|
|
output: self.output.clone(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn redacts_structured_fields_and_message_content_before_writing() {
|
||
|
|
let sink = SharedMakeWriter::default();
|
||
|
|
let known_secret = format!("canary-{}", uuid::Uuid::new_v4());
|
||
|
|
let subscriber = tracing_subscriber::fmt()
|
||
|
|
.without_time()
|
||
|
|
.with_ansi(false)
|
||
|
|
.with_target(false)
|
||
|
|
.with_writer(RedactingMakeWriter::with_redactor(
|
||
|
|
sink.clone(),
|
||
|
|
Redactor::new([&known_secret]),
|
||
|
|
))
|
||
|
|
.finish();
|
||
|
|
|
||
|
|
tracing::subscriber::with_default(subscriber, || {
|
||
|
|
tracing::info!(
|
||
|
|
password = "test-password",
|
||
|
|
authorization = "Bearer abcdefghijklmnop",
|
||
|
|
endpoint = "https://alice:password@example.test/path",
|
||
|
|
"known={known_secret}"
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
let output = sink.contents();
|
||
|
|
assert!(!output.contains("test-password"));
|
||
|
|
assert!(!output.contains("abcdefghijklmnop"));
|
||
|
|
assert!(!output.contains("alice:password"));
|
||
|
|
assert!(!output.contains(&known_secret));
|
||
|
|
assert!(output.contains(crate::diagnostics::redaction::REDACTED));
|
||
|
|
}
|
||
|
|
}
|