Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mask database password on the initial page #157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
82 changes: 72 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ impl Config {

impl Connection {
pub fn database_url(&self) -> anyhow::Result<String> {
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<String> {
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<String> {
match self.r#type {
DatabaseType::MySql => {
let user = self
Expand All @@ -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!(
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -268,7 +278,7 @@ impl Connection {
}

pub fn database_url_with_name(&self) -> anyhow::Result<String> {
let database_url = self.database_url()?;
let database_url = self.masked_database_url()?;

Ok(match &self.name {
Some(name) => format!(
Expand Down Expand Up @@ -325,10 +335,62 @@ fn expand_path(path: &Path) -> Option<PathBuf> {

#[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]
#[cfg(unix)]
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 =
Expand Down