Skip to content
Closed
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
302 changes: 289 additions & 13 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions packages/configuration/src/v2_0_0/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use url::Url;
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Database {
// Database configuration
/// Database driver. Possible values are: `sqlite3`, and `mysql`.
/// Database driver. Possible values are: `sqlite3`, `mysql`, and `postgresql`.
#[serde(default = "Database::default_driver")]
pub driver: Driver,

/// Database connection string. The format depends on the database driver.
/// 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
/// For `MySQL`, the format is `mysql://db_user:db_user_password@host:port/db_name`, for
/// example: `mysql://root:password@localhost:3306/torrust`.
/// For `PostgreSQL`, the format is `postgresql://db_user:db_user_password@host:port/db_name`, for
/// example: `postgresql://root:password@localhost:5432/torrust`.
#[serde(default = "Database::default_path")]
pub path: String,
}
Expand Down Expand Up @@ -51,6 +53,11 @@ impl Database {
url.set_password(Some("***")).expect("url password should be changed");
self.path = url.to_string();
}
Driver::PostgreSQL => {
let mut url = Url::parse(&self.path).expect("path for PostgreSQL driver should be a valid URL");
url.set_password(Some("***")).expect("url password should be changed");
self.path = url.to_string();
}
}
}
}
Expand All @@ -63,6 +70,8 @@ pub enum Driver {
Sqlite3,
/// The `MySQL` database driver.
MySQL,
/// The `PostgreSQL` database driver.
PostgreSQL,
}

#[cfg(test)]
Expand All @@ -81,4 +90,16 @@ mod tests {

assert_eq!(database.path, "mysql://root:***@localhost:3306/torrust".to_string());
}

#[test]
fn it_should_allow_masking_the_postgresql_user_password() {
let mut database = Database {
driver: Driver::PostgreSQL,
path: "postgresql://root:password@localhost:5432/torrust".to_string(),
};

database.mask_secrets();

assert_eq!(database.path, "postgresql://root:***@localhost:5432/torrust".to_string());
}
}
1 change: 1 addition & 0 deletions packages/tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ derive_more = { version = "2", features = [ "as_ref", "constructor", "from" ] }
mockall = "0"
r2d2 = "0"
r2d2_mysql = "25"
r2d2_postgres = "0.18"
r2d2_sqlite = { version = "0", features = [ "bundled" ] }
rand = "0"
serde = { version = "1", features = [ "derive" ] }
Expand Down
5 changes: 5 additions & 0 deletions packages/tracker-core/src/databases/driver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Database driver factory.
use mysql::Mysql;
use postgres::Postgres;
use serde::{Deserialize, Serialize};
use sqlite::Sqlite;

Expand All @@ -23,6 +24,8 @@ pub enum Driver {
Sqlite3,
/// The `MySQL` database driver.
MySQL,
/// The `PostgreSQL` database driver.
PostgreSQL,
}

/// It builds a new database driver.
Expand Down Expand Up @@ -62,6 +65,7 @@ pub enum Driver {
///
/// This function will panic if unable to create database tables.
pub mod mysql;
pub mod postgres;
pub mod sqlite;

/// It builds a new database driver.
Expand All @@ -77,6 +81,7 @@ pub(crate) fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>,
let database: Box<dyn Database> = match driver {
Driver::Sqlite3 => Box::new(Sqlite::new(db_path)?),
Driver::MySQL => Box::new(Mysql::new(db_path)?),
Driver::PostgreSQL => Box::new(Postgres::new(db_path)?),
};

database.create_database_tables().expect("Could not create database tables.");
Expand Down
Loading