-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide secrets from config in logs. (#56)
- Loading branch information
Showing
3 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::fmt; | ||
use std::ops::Deref; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct SecretString(String); | ||
|
||
impl SecretString { | ||
#[must_use] | ||
pub fn new(str: String) -> Self { | ||
Self(str) | ||
} | ||
|
||
#[must_use] | ||
pub fn expose(&self) -> &str { | ||
self.0.as_str() | ||
} | ||
|
||
fn format(&self) -> String { | ||
"********".to_owned() | ||
} | ||
} | ||
|
||
impl fmt::Display for SecretString { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.format().fmt(f) | ||
} | ||
} | ||
|
||
impl fmt::Debug for SecretString { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.format().fmt(f) | ||
} | ||
} | ||
|
||
impl From<String> for SecretString { | ||
fn from(str: String) -> Self { | ||
Self::new(str) | ||
} | ||
} | ||
|
||
impl From<SecretString> for String { | ||
fn from(secret_string: SecretString) -> Self { | ||
secret_string.0 | ||
} | ||
} | ||
|
||
impl Deref for SecretString { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0.deref() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_url_expose() { | ||
let secret = SecretString::from( | ||
"postgres://user:password@localhost:5432/database".to_string(), | ||
); | ||
assert_eq!( | ||
secret.expose(), | ||
"postgres://user:password@localhost:5432/database" | ||
); | ||
} | ||
} |