11use serde:: { Deserialize , Serialize } ;
22use torrust_tracker_primitives:: DatabaseDriver ;
3+ use url:: Url ;
34
45#[ allow( clippy:: struct_excessive_bools) ]
56#[ derive( Serialize , Deserialize , PartialEq , Eq , Debug , Clone ) ]
@@ -13,7 +14,7 @@ pub struct Database {
1314 /// For `Sqlite3`, the format is `path/to/database.db`, for example:
1415 /// `./storage/tracker/lib/database/sqlite3.db`.
1516 /// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
16- /// example: `root:password@localhost:3306/torrust`.
17+ /// example: `mysql:// root:password@localhost:3306/torrust`.
1718 #[ serde( default = "Database::default_path" ) ]
1819 pub path : String ,
1920}
@@ -35,4 +36,42 @@ impl Database {
3536 fn default_path ( ) -> String {
3637 String :: from ( "./storage/tracker/lib/database/sqlite3.db" )
3738 }
39+
40+ /// Masks secrets in the configuration.
41+ ///
42+ /// # Panics
43+ ///
44+ /// Will panic if the database path for `MySQL` is not a valid URL.
45+ pub fn mask_secrets ( & mut self ) {
46+ match self . driver {
47+ DatabaseDriver :: Sqlite3 => {
48+ // Nothing to mask
49+ }
50+ DatabaseDriver :: MySQL => {
51+ let mut url = Url :: parse ( & self . path ) . expect ( "path for MySQL driver should be a valid URL" ) ;
52+ url. set_password ( Some ( "***" ) ) . expect ( "url password should be changed" ) ;
53+ self . path = url. to_string ( ) ;
54+ }
55+ }
56+ }
57+ }
58+
59+ #[ cfg( test) ]
60+ mod tests {
61+
62+ use torrust_tracker_primitives:: DatabaseDriver ;
63+
64+ use super :: Database ;
65+
66+ #[ test]
67+ fn it_should_allow_masking_the_mysql_user_password ( ) {
68+ let mut database = Database {
69+ driver : DatabaseDriver :: MySQL ,
70+ path : "mysql://root:password@localhost:3306/torrust" . to_string ( ) ,
71+ } ;
72+
73+ database. mask_secrets ( ) ;
74+
75+ assert_eq ! ( database. path, "mysql://root:***@localhost:3306/torrust" . to_string( ) ) ;
76+ }
3877}
0 commit comments