Skip to main content

faucet_core/
redact.rs

1//! A process-global redaction hook (#456 H5).
2//!
3//! Secrets are resolved on raw config text long before they become typed values,
4//! so the CLI tracks the resolved *values* in `cli::secrets::registry` and scrubs
5//! them from anything it emits. `faucet-core` cannot see that registry — it has
6//! no secrets layer and must not gain one — yet core is where two outbound
7//! surfaces are built:
8//!
9//! - the **DLQ envelope**'s `error.message` ([`crate::dlq::build_envelope`]),
10//!   which is written to a file or object store, and
11//! - any error text a host application forwards onward.
12//!
13//! An error string routinely embeds the material that produced it: `reqwest`'s
14//! `Display` includes the request URL, so a REST source whose API key rides a
15//! query parameter leaks the key; connection-string leakage in a CDC error has
16//! already been a filed bug here (#84).
17//!
18//! So core exposes a hook: a host installs a scrubber once at startup, and core
19//! routes outbound text through [`redact`]. With no hook installed, [`redact`] is
20//! the identity function and costs one atomic load — library users who never
21//! resolve secrets pay nothing and see no behaviour change.
22
23use std::sync::OnceLock;
24
25/// A scrubber: takes text, returns it with every known secret replaced.
26pub type Redactor = Box<dyn Fn(&str) -> String + Send + Sync>;
27
28fn hook() -> &'static OnceLock<Redactor> {
29    static HOOK: OnceLock<Redactor> = OnceLock::new();
30    &HOOK
31}
32
33/// Install the process-wide redactor. The **first** call wins; later calls are
34/// ignored and return `false`, so a second `install_observability` (or a test
35/// that runs after one) can never swap the scrubber out from under a run.
36pub fn install(redactor: Redactor) -> bool {
37    hook().set(redactor).is_ok()
38}
39
40/// Whether a redactor has been installed.
41pub fn is_installed() -> bool {
42    hook().get().is_some()
43}
44
45/// Scrub `text` with the installed redactor, or return it unchanged when none is
46/// installed.
47///
48/// Call this on every string core hands to a destination outside the process.
49pub fn redact(text: &str) -> String {
50    match hook().get() {
51        Some(f) => f(text),
52        None => text.to_owned(),
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    /// The hook is process-global and `install` is first-wins, so the whole
61    /// contract is asserted in one test — two `#[test]` fns would race for the
62    /// single `OnceLock`.
63    #[test]
64    fn install_is_first_wins_and_redact_applies_it() {
65        // Before install: identity, and reported as absent.
66        if !is_installed() {
67            assert_eq!(redact("token=abcd"), "token=abcd");
68        }
69
70        assert!(install(Box::new(|s: &str| s.replace("abcd", "***"))));
71        assert!(is_installed());
72        assert_eq!(redact("token=abcd"), "token=***");
73        assert_eq!(redact("nothing to do"), "nothing to do");
74
75        // A second install is refused — the first redactor stays in force.
76        assert!(!install(Box::new(|_: &str| "clobbered".to_owned())));
77        assert_eq!(redact("token=abcd"), "token=***");
78    }
79}