From 5da538ac6d3a3a2e51d265957e34fe60b0f2c915 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Tue, 31 May 2022 08:22:59 +0900 Subject: [PATCH 1/2] mask database password on the initial page --- src/config.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3d41831..bcf813f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -181,6 +181,24 @@ impl Config { impl Connection { pub fn database_url(&self) -> anyhow::Result { + let password = self + .password + .as_ref() + .map_or(String::new(), |p| p.to_string()); + return self.build_database_url(password); + } + + fn masked_database_url(&self) -> anyhow::Result { + let password = self + .password + .as_ref() + .map_or(String::new(), |p| p.to_string()); + + let masked_password = "*".repeat(password.len()); + return self.build_database_url(masked_password); + } + + fn build_database_url(&self, password: String) -> anyhow::Result { match self.r#type { DatabaseType::MySql => { let user = self @@ -195,10 +213,6 @@ impl Connection { .port .as_ref() .ok_or_else(|| anyhow::anyhow!("type mysql needs the port field"))?; - let password = self - .password - .as_ref() - .map_or(String::new(), |p| p.to_string()); match self.database.as_ref() { Some(database) => Ok(format!( @@ -231,10 +245,6 @@ impl Connection { .port .as_ref() .ok_or_else(|| anyhow::anyhow!("type postgres needs the port field"))?; - let password = self - .password - .as_ref() - .map_or(String::new(), |p| p.to_string()); match self.database.as_ref() { Some(database) => Ok(format!( @@ -268,7 +278,7 @@ impl Connection { } pub fn database_url_with_name(&self) -> anyhow::Result { - let database_url = self.database_url()?; + let database_url = self.masked_database_url()?; Ok(match &self.name { Some(name) => format!( @@ -325,10 +335,61 @@ fn expand_path(path: &Path) -> Option { #[cfg(test)] mod test { - use super::{expand_path, KeyConfig, Path, PathBuf}; + use super::{expand_path, Connection, DatabaseType, KeyConfig, Path, PathBuf}; use serde_json::Value; use std::env; + #[test] + fn test_database_url() { + let mysql_conn = Connection { + r#type: DatabaseType::MySql, + name: None, + user: Some("root".to_owned()), + host: Some("localhost".to_owned()), + port: Some(3306), + path: None, + password: Some("password".to_owned()), + database: Some("city".to_owned()), + }; + + let mysql_result = mysql_conn.database_url().unwrap(); + assert_eq!( + mysql_result, + "mysql://root:password@localhost:3306/city".to_owned() + ); + + let postgres_conn = Connection { + r#type: DatabaseType::Postgres, + name: None, + user: Some("root".to_owned()), + host: Some("localhost".to_owned()), + port: Some(3306), + path: None, + password: Some("password".to_owned()), + database: Some("city".to_owned()), + }; + + let postgres_result = postgres_conn.database_url().unwrap(); + assert_eq!( + postgres_result, + "postgres://root:password@localhost:3306/city".to_owned() + ); + + let sqlite_conn = Connection { + r#type: DatabaseType::Sqlite, + name: None, + user: None, + host: None, + port: None, + path: Some(PathBuf::from("/home/user/sqlite3.db")), + password: None, + database: None, + }; + + let sqlite_result = sqlite_conn.database_url().unwrap(); + assert_eq!(sqlite_result, "sqlite:///home/user/sqlite3.db".to_owned()); + } + #[test] fn test_overlappted_key() { let value: Value = From 611439e07a85b9daea36624e2907ef4cf127403c Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Tue, 31 May 2022 15:08:25 +0900 Subject: [PATCH 2/2] use unix flag in test --- src/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config.rs b/src/config.rs index bcf813f..8b0933a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -340,6 +340,7 @@ mod test { use std::env; #[test] + #[cfg(unix)] fn test_database_url() { let mysql_conn = Connection { r#type: DatabaseType::MySql,