Skip to main content

Crate apollo_redaction

Crate apollo_redaction 

Source
Expand description

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:

§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");
StrategySample inputSample output
SimpleRedactorany[REDACTED]
FullRedactorsecret******
PasswordRedactorany********
IpRedactor192.168.1.100192.168.*.*
UrlHostRedactorhttps://username:passwd@mytelemetryhost.com/v1/traces****/v1/traces
UrlPasswordRedactorhttps://alice:s3cr3t@proxy.example.com/https://alice:***@proxy.example.com/
EmailRedactorjanedoe@gmail.comj***e@gmail.com
First4Redactorsk_live_abc123sk_l****
Last4Redactor4242424242424242************4242
CardRedactor4242-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.

Modules§

ser
Serde serialization helpers.
strategy

Structs§

Redacted
A wrapper type for redacting values, so the actual values are not accidentally printed out.

Traits§

RedactionStrategy
A redaction strategy tells apollo-redaction how to redact values for Display and Debug.