Skip to content

Commit

Permalink
fix: [#948] mask secrets in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jul 4, 2024
1 parent 16aa652 commit 4673514
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub type AccessTokens = HashMap<String, String>;
pub const LATEST_VERSION: &str = "2";

/// Info about the configuration specification.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
pub struct Metadata {
#[serde(default = "Metadata::default_version")]
#[serde(flatten)]
Expand All @@ -70,7 +70,7 @@ impl Metadata {
}

/// The configuration version.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
pub struct Version {
#[serde(default = "Version::default_semver")]
version: String,
Expand Down
41 changes: 40 additions & 1 deletion packages/configuration/src/v2/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::DatabaseDriver;
use url::Url;

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
Expand All @@ -13,7 +14,7 @@ pub struct Database {
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
/// `./storage/tracker/lib/database/sqlite3.db`.
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
/// example: `root:password@localhost:3306/torrust`.
/// example: `mysql://root:password@localhost:3306/torrust`.
#[serde(default = "Database::default_path")]
pub path: String,
}
Expand All @@ -35,4 +36,42 @@ impl Database {
fn default_path() -> String {
String::from("./storage/tracker/lib/database/sqlite3.db")
}

/// Masks secrets in the configuration.
///
/// # Panics
///
/// Will panic if the database path for `MySQL` is not a valid URL.
pub fn mask_secrets(&mut self) {
match self.driver {
DatabaseDriver::Sqlite3 => {
// Nothing to mask
}
DatabaseDriver::MySQL => {
let mut url = Url::parse(&self.path).expect("path for MySQL driver should be a valid URL");
url.set_password(Some("***")).expect("url password should be changed");
self.path = url.to_string();
}
}
}
}

#[cfg(test)]
mod tests {

use torrust_tracker_primitives::DatabaseDriver;

use super::Database;

#[test]
fn it_should_allow_masking_the_mysql_user_password() {
let mut database = Database {
driver: DatabaseDriver::MySQL,
path: "mysql://root:password@localhost:3306/torrust".to_string(),
};

database.mask_secrets();

assert_eq!(database.path, "mysql://root:***@localhost:3306/torrust".to_string());
}
}
14 changes: 13 additions & 1 deletion packages/configuration/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_TRACKER_CONFIG_OVERRIDE_";
const CONFIG_OVERRIDE_SEPARATOR: &str = "__";

/// Core configuration for the tracker.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
pub struct Configuration {
/// Configuration metadata.
#[serde(flatten)]
Expand Down Expand Up @@ -380,6 +380,18 @@ impl Configuration {
// code-review: do we need to use Figment also to serialize into json?
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
}

/// Masks secrets in the configuration.
#[must_use]
pub fn mask_secrets(mut self) -> Self {
self.core.database.mask_secrets();

Check warning on line 387 in packages/configuration/src/v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/mod.rs#L386-L387

Added lines #L386 - L387 were not covered by tests

if let Some(ref mut api) = self.http_api {
api.mask_secrets();

Check warning on line 390 in packages/configuration/src/v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/mod.rs#L389-L390

Added lines #L389 - L390 were not covered by tests
}

self
}

Check warning on line 394 in packages/configuration/src/v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/mod.rs#L393-L394

Added lines #L393 - L394 were not covered by tests
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions packages/configuration/src/v2/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ impl HttpApi {
pub fn override_admin_token(&mut self, api_admin_token: &str) {
self.access_tokens.insert("admin".to_string(), api_admin_token.to_string());
}

pub fn mask_secrets(&mut self) {
for token in self.access_tokens.values_mut() {
*token = "***".to_string();

Check warning on line 67 in packages/configuration/src/v2/tracker_api.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/tracker_api.rs#L65-L67

Added lines #L65 - L67 were not covered by tests
}
}

Check warning on line 69 in packages/configuration/src/v2/tracker_api.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/tracker_api.rs#L69

Added line #L69 was not covered by tests
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn setup() -> (Configuration, Arc<Tracker>) {

let tracker = initialize_with_configuration(&configuration);

info!("Configuration:\n{}", configuration.to_json());
info!("Configuration:\n{}", configuration.clone().mask_secrets().to_json());

Check warning on line 33 in src/bootstrap/app.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/app.rs#L33

Added line #L33 was not covered by tests

(configuration, tracker)
}
Expand Down

0 comments on commit 4673514

Please sign in to comment.