summaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-05-17 13:39:05 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-05-17 14:00:53 +0200
commit3b1701075a0591ea8d349b4b3fcfff2140185159 (patch)
tree03af497bc4b475e2280e74fa71efb587cea60071 /src/db
parentddbd9629b3188c9f08d023829683c40ab9e1448b (diff)
Fix: Do not print database password in debug log
We have to manually implement Debug here, because otherwise we would print the database password in the debug output, which is CLEARLY a bad idea. Hence, change the debug!() call in the establish_connection() function to (debug-)print `self` and hand-implement the `Debug` trait. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/db')
-rw-r--r--src/db/connection.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/db/connection.rs b/src/db/connection.rs
index 3e77cdb..cf9a0c0 100644
--- a/src/db/connection.rs
+++ b/src/db/connection.rs
@@ -20,7 +20,7 @@ use log::debug;
use crate::config::Configuration;
-#[derive(Debug, Getters)]
+#[derive(Getters)]
pub struct DbConnectionConfig<'a> {
#[getset(get = "pub")]
database_host: &'a str,
@@ -41,6 +41,18 @@ pub struct DbConnectionConfig<'a> {
database_connection_timeout: u16,
}
+impl<'a> std::fmt::Debug for DbConnectionConfig<'a> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
+ write!(f, "postgres://{user}:PASSWORD@{host}:{port}/{name}?connect_timeout={timeout}",
+ host = self.database_host,
+ port = self.database_port,
+ user = self.database_user,
+ name = self.database_name,
+ timeout = self.database_connection_timeout
+ )
+ }
+}
+
impl<'a> DbConnectionConfig<'a> {
pub fn parse(config: &'a Configuration, cli: &'a ArgMatches) -> Result<DbConnectionConfig<'a>> {
Ok(DbConnectionConfig {
@@ -67,6 +79,7 @@ impl<'a> DbConnectionConfig<'a> {
}
pub fn establish_connection(self) -> Result<PgConnection> {
+ debug!("Trying to connect to database: {:?}", self);
let database_uri: String = format!(
"postgres://{user}:{password}@{host}:{port}/{name}?connect_timeout={timeout}",
host = self.database_host,
@@ -76,7 +89,6 @@ impl<'a> DbConnectionConfig<'a> {
name = self.database_name,
timeout = self.database_connection_timeout,
);
- debug!("Trying to connect to database: {}", database_uri);
PgConnection::establish(&database_uri).map_err(Error::from)
}