From e3d4759358a9fb1e7e2b7804c61b0b9c480c698b Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Wed, 19 Jul 2023 15:15:12 -0400 Subject: [PATCH 1/2] fix (#234) --- nexus/catalog/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nexus/catalog/src/lib.rs b/nexus/catalog/src/lib.rs index 1597f61ea6..436d592bf6 100644 --- a/nexus/catalog/src/lib.rs +++ b/nexus/catalog/src/lib.rs @@ -85,7 +85,7 @@ impl CatalogConfig { connection_string.push_str(&self.user); if !self.password.is_empty() { connection_string.push_str(" password="); - let encoded_password = urlencoding::encode(&config.password); + let encoded_password = urlencoding::encode(&self.password); connection_string.push_str(&encoded_password); } connection_string.push_str(" dbname="); From 62f8c75b1c7abe2eed06c6b7e7b498e7592020e4 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Wed, 19 Jul 2023 16:27:11 -0400 Subject: [PATCH 2/2] Url encode Postgres passwords for special character support (#235) --- nexus/catalog/src/lib.rs | 21 +++++++++++---------- nexus/peer-postgres/src/lib.rs | 25 +++++++++++++------------ nexus/server/src/main.rs | 2 +- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/nexus/catalog/src/lib.rs b/nexus/catalog/src/lib.rs index 436d592bf6..76058b5cb8 100644 --- a/nexus/catalog/src/lib.rs +++ b/nexus/catalog/src/lib.rs @@ -10,6 +10,7 @@ use pt::{ peerdb_peers::{peer::Config, DbType, Peer}, }; use tokio_postgres::{types, Client}; +use urlencoding::encode; mod embedded { use refinery::embed_migrations; @@ -76,20 +77,20 @@ impl CatalogConfig { // Get the connection string for this catalog configuration pub fn get_connection_string(&self) -> String { - let mut connection_string = String::new(); - connection_string.push_str("host="); - connection_string.push_str(&self.host); - connection_string.push_str(" port="); - connection_string.push_str(&self.port.to_string()); - connection_string.push_str(" user="); + let mut connection_string = String::from("postgres://"); + connection_string.push_str(&self.user); if !self.password.is_empty() { - connection_string.push_str(" password="); - let encoded_password = urlencoding::encode(&self.password); - connection_string.push_str(&encoded_password); + connection_string.push(':'); + connection_string.push_str(&encode(&self.password)); } - connection_string.push_str(" dbname="); + connection_string.push('@'); + connection_string.push_str(&self.host); + connection_string.push(':'); + connection_string.push_str(&self.port.to_string()); + connection_string.push('/'); connection_string.push_str(&self.database); + connection_string } } diff --git a/nexus/peer-postgres/src/lib.rs b/nexus/peer-postgres/src/lib.rs index 1b506d2b02..13e60d87a2 100644 --- a/nexus/peer-postgres/src/lib.rs +++ b/nexus/peer-postgres/src/lib.rs @@ -22,22 +22,23 @@ pub struct PostgresQueryExecutor { } fn get_connection_string(config: &PostgresConfig) -> String { - let mut connection_string = String::new(); - connection_string.push_str("host="); - connection_string.push_str(&config.host); - connection_string.push_str(" port="); - connection_string.push_str(&config.port.to_string()); - connection_string.push_str(" user="); + let mut connection_string = String::from("postgres://"); + connection_string.push_str(&config.user); if !config.password.is_empty() { - connection_string.push_str(" password="); - let encoded_password = urlencoding::encode(&config.password); - connection_string.push_str(&encoded_password); + connection_string.push(':'); + connection_string.push_str(&urlencoding::encode(&config.password)); } - connection_string.push_str(" dbname="); + connection_string.push('@'); + connection_string.push_str(&config.host); + connection_string.push(':'); + connection_string.push_str(&config.port.to_string()); + connection_string.push('/'); connection_string.push_str(&config.database); - connection_string.push_str(" connect_timeout="); - connection_string.push_str("15"); + + // Add the timeout as a query parameter + connection_string.push_str("?connect_timeout=15"); + connection_string } diff --git a/nexus/server/src/main.rs b/nexus/server/src/main.rs index 1f353c69f4..e0953ac54c 100644 --- a/nexus/server/src/main.rs +++ b/nexus/server/src/main.rs @@ -832,7 +832,7 @@ async fn run_migrations(config: &CatalogConfig) -> anyhow::Result<()> { } Err(err) => { tracing::warn!( - "Failed to connect to catalog. Retrying in 30 seconds. {}", + "Failed to connect to catalog. Retrying in 30 seconds. {:?}", err ); tokio::time::sleep(Duration::from_secs(30)).await;