Skip to content

Commit

Permalink
feat: Add support for redacting postgres/redis/smtp URIs (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski authored Nov 14, 2024
1 parent ed47af2 commit cf030f7
Show file tree
Hide file tree
Showing 25 changed files with 183 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = []

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = []

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = ['foo']

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = []

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]
min-idle = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = []

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]
max-connections = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ num-workers = 1
queues = []

[redis]
uri = 'redis://localhost:6379'
uri = 'redis://[Sensitive]'

[redis.enqueue-pool]

Expand Down
87 changes: 87 additions & 0 deletions src/testing/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::thread::current;
use typed_builder::TypedBuilder;

const BEARER_TOKEN_REGEX: &str = r"Bearer [\w\.-]+";
const POSTGRES_URI_REGEX: &str = r"postgres://(\w|\d|@|:|\/|\.)+";
const REDIS_URI_REGEX: &str = r"redis://(\w|\d|@|:|\/|\.)+";
const SMTP_URI_REGEX: &str = r"smtp://(\w|\d|@|:|\/|\.)+";

/// Configure which settings to apply on the snapshot [Settings].
///
Expand Down Expand Up @@ -88,6 +91,24 @@ pub struct TestCaseConfig {
#[builder(default = true)]
pub redact_auth_tokens: bool,

/// Whether to Postgres URIs from snapshots. This is useful for tests involving
/// dynamically created Postgres instances that will be different on every test run, or involve
/// real Postgres instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_postgres_uri: bool,

/// Whether to Redis URIs from snapshots. This is useful for tests involving
/// dynamically created Redis instances that will be different on every test run, or involve
/// real Redis instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_redis_uri: bool,

/// Whether to SMTP URIs from snapshots. This is useful for tests involving
/// dynamically created SMTP instances that will be different on every test run, or involve
/// real SMTP instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_smtp_uri: bool,

/// Whether to automatically bind the [Settings] to the current scope. If `true`, the settings
/// will be automatically applied for the test in which the [TestCase] was built. If `false`,
/// the settings will only be applied after manually calling [Settings::bind_to_scope], or
Expand Down Expand Up @@ -176,6 +197,15 @@ impl From<TestCaseConfig> for TestCase {
if value.redact_auth_tokens {
snapshot_redact_bearer_tokens(&mut settings);
}
if value.redact_postgres_uri {
snapshot_redact_postgres_uri(&mut settings);
}
if value.redact_redis_uri {
snapshot_redact_redis_uri(&mut settings);
}
if value.redact_smtp_uri {
snapshot_redact_smtp_uri(&mut settings);
}

let _settings_guard = if value.bind_scope {
Some(settings.bind_to_scope())
Expand Down Expand Up @@ -214,6 +244,27 @@ pub fn snapshot_redact_bearer_tokens(settings: &mut Settings) -> &mut Settings {
settings
}

/// Redact instances of Postgres URIs in snapshots. Applies a filter on the [Settings] to replace
/// sub-strings matching [`POSTGRES_URI_REGEX`] with `postgres://[Sensitive]`.
pub fn snapshot_redact_postgres_uri(settings: &mut Settings) -> &mut Settings {
settings.add_filter(POSTGRES_URI_REGEX, "postgres://[Sensitive]");
settings
}

/// Redact instances of Redis URIs in snapshots. Applies a filter on the [Settings] to replace
/// sub-strings matching [`REDIS_URI_REGEX`] with `redis://[Sensitive]`.
pub fn snapshot_redact_redis_uri(settings: &mut Settings) -> &mut Settings {
settings.add_filter(REDIS_URI_REGEX, "redis://[Sensitive]");
settings
}

/// Redact instances of Smtp URIs in snapshots. Applies a filter on the [Settings] to replace
/// sub-strings matching [`SMTP_URI_REGEX`] with `smtp://[Sensitive]`.
pub fn snapshot_redact_smtp_uri(settings: &mut Settings) -> &mut Settings {
settings.add_filter(SMTP_URI_REGEX, "smtp://[Sensitive]");
settings
}

/// Extract the last segment of the current thread name to use as the test case description.
///
/// See: <https://github.com/adriangb/pgpq/blob/b0b0f8c77c862c0483d81571e76f3a2b746136fc/pgpq/src/lib.rs#L649-L669>
Expand Down Expand Up @@ -315,6 +366,42 @@ mod tests {
assert_snapshot!(format!("Foo {token} bar"));
}

#[rstest]
#[case("postgres://example:[email protected]:1234/example")]
#[case("postgres://example:1234")]
#[case("postgres://localhost")]
#[case("postgres://example.com")]
#[case("postgres://192.168.1.1:3000")]
#[case("postgres://192.168.1.1:3000/example")]
#[cfg_attr(coverage_nightly, coverage(off))]
fn postgres_uri(_case: TestCase, #[case] uri: &str) {
assert_snapshot!(format!("uri = {uri}"));
}

#[rstest]
#[case("redis://example:[email protected]:1234/example")]
#[case("redis://example:1234")]
#[case("redis://localhost")]
#[case("redis://example.com")]
#[case("redis://192.168.1.1:3000")]
#[case("redis://192.168.1.1:3000/example")]
#[cfg_attr(coverage_nightly, coverage(off))]
fn redis_uri(_case: TestCase, #[case] uri: &str) {
assert_snapshot!(format!("uri = {uri}"));
}

#[rstest]
#[case("smtp://example:[email protected]:1234/example")]
#[case("smtp://example:1234")]
#[case("smtp://localhost")]
#[case("smtp://example.com")]
#[case("smtp://192.168.1.1:3000")]
#[case("smtp://192.168.1.1:3000/example")]
#[cfg_attr(coverage_nightly, coverage(off))]
fn smtp_uri(_case: TestCase, #[case] uri: &str) {
assert_snapshot!(format!("uri = {uri}"));
}

#[rstest]
#[case("")]
#[case("foo")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = postgres://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = redis://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: "format!(\"uri = {uri}\")"
---
uri = smtp://[Sensitive]

0 comments on commit cf030f7

Please sign in to comment.