apollo-redaction 0.2.0

Redaction utility for sensitive data in error messages, debug logs, etc.
Documentation

Redaction utility for sensitive data in error messages, debug logs, etc.

Usage

Wrap values in [Redacted] to prevent them from being printed directly.

use apollo_redaction::Redacted;
use apollo_redaction::strategy::SimpleRedactor;

let secret = Redacted::<_, SimpleRedactor>::new("hunter2");
assert_eq!(secret.to_string(), "[REDACTED]");
assert_eq!(secret.unredact().to_string(), "hunter2");

Deserialization

Often, you won't create [Redacted] instances manually. Instead, you might deserialize them from a configuration structure:

use apollo_redaction::Redacted;

#[derive(Debug, serde::Deserialize)]
struct RedisConfig {
    username: Redacted<String>,
    password: Redacted<String>,
}

let redis_config: RedisConfig = serde_json::from_str(r#"
    {
        "username": "admin",
        "password": "topsecret"
    }
"#).unwrap();

// If you did eg. `dbg!(redis_config)`...
assert_eq!(
    format!("{redis_config:?}"),
    r#"RedisConfig { username: [REDACTED], password: [REDACTED] }"#,
);

Serialization

Values wrapped in a [Redacted] cannot be serialized with [serde]. This is to prevent accidentally serializing either the redacted or the unredacted form when you don't expect it. You can use the #[serde(serialize_with)] attribute to serialize a [Redacted] field.

apollo-redaction comes with serialization helpers:

  • [ser::unredacted] - serializes the unredacted representation
  • [ser::display_redacted] - serializes the redacted [Display] representation

Strategies

A strategy must be provided as a type parameter. For example:

use apollo_redaction::Redacted;
use apollo_redaction::strategy::UrlHostRedactor;
use apollo_redaction::strategy::EmailRedactor;

// when deserializing a field...
struct TelemetryConfig {
    url: Redacted<String, UrlHostRedactor>,
}
// when constructing a value manually...
let email = Redacted::<_, EmailRedactor>::new("myemail@address.com");
Strategy Sample input Sample output
[SimpleRedactor] any [REDACTED]
FullRedactor secret ******
PasswordRedactor any ********
IpRedactor 192.168.1.100 192.168.*.*
UrlHostRedactor https://username:passwd@mytelemetryhost.com/v1/traces ****/v1/traces
UrlPasswordRedactor https://alice:s3cr3t@proxy.example.com/ https://alice:***@proxy.example.com/
EmailRedactor janedoe@gmail.com j***e@gmail.com
First4Redactor sk_live_abc123 sk_l****
Last4Redactor 4242424242424242 ************4242
CardRedactor 4242-4242-4242-4242 ****-****-****-4242
FixedRedactor<N> password123 (N=4) pass*******

Feature flags

  • schemars enables a transparent [schemars::JsonSchema] implementation for Redacted<T> when T implements schemars::JsonSchema.