Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact passwords in Client's Debug implementation #106

Merged
merged 4 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust_release: [1.46, stable, nightly]
rust_release: ["1.53", stable, nightly]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<a href="https://www.rust-lang.org/en-US/">
<img src="https://img.shields.io/badge/Made%20with-Rust-orange.svg" alt='Build with Rust' />
</a>
<a href="https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html">
<img src="https://img.shields.io/badge/rustc-1.46+-yellow.svg" alt='Minimum Rust Version' />
<a href="https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html">
<img src="https://img.shields.io/badge/rustc-1.53+-yellow.svg" alt='Minimum Rust Version' />
</a>
</p>

Expand Down
4 changes: 2 additions & 2 deletions README.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<a href="https://www.rust-lang.org/en-US/">
<img src="https://img.shields.io/badge/Made%20with-Rust-orange.svg" alt='Build with Rust' />
</a>
<a href="https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html">
<img src="https://img.shields.io/badge/rustc-1.46+-yellow.svg" alt='Minimum Rust Version' />
<a href="https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html">
<img src="https://img.shields.io/badge/rustc-1.53+-yellow.svg" alt='Minimum Rust Version' />
</a>
</p>

Expand Down
1 change: 1 addition & 0 deletions influxdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ wasm-client = ["surf", "surf/wasm-client"]

[dev-dependencies]
async-std = { version = "1.6.5", features = ["attributes", "tokio02", "tokio1"] }
indoc = "1.0"
tokio = { version = "1.7", features = ["macros", "rt-multi-thread"] }
51 changes: 48 additions & 3 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,49 @@ use futures_util::TryFutureExt;
use http::StatusCode;
#[cfg(feature = "reqwest")]
use reqwest::{Client as HttpClient, Response as HttpResponse};
use std::collections::{BTreeMap, HashMap};
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
#[cfg(feature = "surf")]
use surf::{Client as HttpClient, Response as HttpResponse};

use crate::query::QueryType;
use crate::Error;
use crate::Query;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone, Debug)]
#[derive(Clone)]
/// Internal Representation of a Client
pub struct Client {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) client: HttpClient,
}

struct RedactPassword<'a>(&'a HashMap<&'static str, String>);

impl<'a> Debug for RedactPassword<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let entries = self
.0
.iter()
.map(|(k, v)| match *k {
"p" => (*k, "<redacted>"),
_ => (*k, v.as_str()),
})
.collect::<BTreeMap<&'static str, &str>>();
f.debug_map().entries(entries.into_iter()).finish()
}
}

impl Debug for Client {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("url", &self.url)
.field("parameters", &RedactPassword(&self.parameters))
.finish_non_exhaustive()
}
}

impl Client {
/// Instantiates a new [`Client`](crate::Client)
///
Expand Down Expand Up @@ -260,6 +286,25 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {
#[cfg(test)]
mod tests {
use super::Client;
use indoc::indoc;

#[test]
fn test_client_debug_redacted_password() {
let client = Client::new("https://localhost:8086", "db").with_auth("user", "pass");
let actual = format!("{:#?}", client);
let expected = indoc! { r#"
Client {
url: "https://localhost:8086",
parameters: {
"db": "db",
"p": "<redacted>",
"u": "user",
},
..
}
"# };
assert_eq!(actual.trim(), expected.trim());
}

#[test]
fn test_fn_database() {
Expand Down