diff --git a/my-postgres-core/src/connection/connection_string.rs b/my-postgres-core/src/connection/connection_string.rs index a627b55..1311906 100644 --- a/my-postgres-core/src/connection/connection_string.rs +++ b/my-postgres-core/src/connection/connection_string.rs @@ -17,6 +17,7 @@ pub struct PostgresConnectionString { port: u16, db_name: Position, ssl_require: bool, + ssh: Option, } impl PostgresConnectionString { @@ -42,6 +43,11 @@ impl PostgresConnectionString { self.get_field_value(&self.db_name) } + pub fn get_ssh(&self) -> Option<&str> { + let result = self.get_field_value(self.ssh.as_ref()?); + Some(result) + } + pub fn get_ssl_require(&self) -> bool { self.ssl_require } @@ -61,6 +67,7 @@ impl PostgresConnectionString { let mut host = None; let mut port: u16 = POSTGRES_DEFAULT_PORT; let mut ssl_require = false; + let mut ssh = None; let mut pos = 0; while pos < conn_string.len() { @@ -97,6 +104,12 @@ impl PostgresConnectionString { to: end_of_value, }); } + "ssh" => { + ssh = Some(Position { + from: eq_pos + 1, + to: end_of_value, + }); + } "password" => { password = Some(Position { @@ -142,7 +155,29 @@ impl PostgresConnectionString { port, db_name: db_name.unwrap(), ssl_require, + ssh, + } + } + + #[cfg(feature = "with-ssh")] + pub fn get_ssh_target(&self) -> crate::ssh::SshTarget { + let result = crate::ssh::SshTarget::new(); + + if let Some(ssh) = self.get_ssh() { + let ssh_credentials = my_ssh::SshCredentials::try_from_str(ssh); + + if ssh_credentials.is_none() { + panic!( + "Invalid ssh credentials {} at postgres connection string of host: {}", + ssh, + self.get_field_value(&self.host) + ); + } + + result.set_credentials(ssh_credentials.unwrap().into()); } + + result } fn parse_column_separated(conn_string: Vec) -> Self { @@ -150,6 +185,7 @@ impl PostgresConnectionString { let mut password = None; let mut db_name = None; let mut host = None; + let mut ssh = None; let mut port: u16 = POSTGRES_DEFAULT_PORT; let mut ssl_require = false; @@ -182,6 +218,12 @@ impl PostgresConnectionString { to: end_of_value, }); } + "ssh" => { + ssh = Some(Position { + from: eq_pos + 1, + to: end_of_value, + }); + } "userid" => { user_name = Some(Position { from: eq_pos + 1, @@ -246,6 +288,7 @@ impl PostgresConnectionString { port, db_name: db_name.unwrap(), ssl_require, + ssh, } } @@ -335,6 +378,7 @@ impl PostgresConnectionString { to: db_name_end, }, ssl_require, + ssh: None, } } diff --git a/my-postgres-core/src/connection/postgres_connection.rs b/my-postgres-core/src/connection/postgres_connection.rs index 63c3954..70fa1fe 100644 --- a/my-postgres-core/src/connection/postgres_connection.rs +++ b/my-postgres-core/src/connection/postgres_connection.rs @@ -23,7 +23,6 @@ impl PostgresConnection { pub async fn new_as_single_connection( app_name: impl Into>, postgres_settings: Arc, - #[cfg(feature = "with-ssh")] ssh_target: Arc, #[cfg(feature = "with-logs-and-telemetry")] logger: Arc, ) -> Self { let app_name: StrOrString<'static> = app_name.into(); @@ -36,7 +35,7 @@ impl PostgresConnection { conn_string.get_db_name().to_string(), postgres_settings, #[cfg(feature = "with-ssh")] - ssh_target, + conn_string.get_ssh_target().into(), #[cfg(feature = "with-logs-and-telemetry")] logger, ) @@ -57,7 +56,6 @@ impl PostgresConnection { app_name: impl Into>, postgres_settings: Arc, max_pool_size: usize, - #[cfg(feature = "with-ssh")] ssh_target: Arc, #[cfg(feature = "with-logs-and-telemetry")] logger: Arc, ) -> Self { let app_name: StrOrString<'static> = app_name.into(); @@ -70,7 +68,7 @@ impl PostgresConnection { postgres_settings, max_pool_size, #[cfg(feature = "with-ssh")] - ssh_target, + conn_string.get_ssh_target().into(), #[cfg(feature = "with-logs-and-telemetry")] logger, )) diff --git a/my-postgres-core/src/my_postgres_builder.rs b/my-postgres-core/src/my_postgres_builder.rs index afb0864..87c3ff9 100644 --- a/my-postgres-core/src/my_postgres_builder.rs +++ b/my-postgres-core/src/my_postgres_builder.rs @@ -3,6 +3,7 @@ use std::{sync::Arc, time::Duration}; use rust_extensions::{date_time::DateTimeAsMicroseconds, StrOrString}; use crate::{ + ssh::SshTarget, table_schema::{PrimaryKeySchema, TableSchema, TableSchemaProvider}, MyPostgres, PostgresConnection, PostgresConnectionInstance, PostgresConnectionString, PostgresSettings, @@ -15,8 +16,6 @@ pub enum MyPostgresBuilder { table_schema_data: Vec, sql_request_timeout: Duration, sql_db_sync_timeout: Duration, - #[cfg(feature = "with-ssh")] - ssh_target: Arc, #[cfg(feature = "with-logs-and-telemetry")] logger: Arc, }, @@ -44,8 +43,6 @@ impl MyPostgresBuilder { table_schema_data: Vec::new(), sql_request_timeout: Duration::from_secs(5), sql_db_sync_timeout: Duration::from_secs(60), - #[cfg(feature = "with-ssh")] - ssh_target: Arc::new(crate::ssh::SshTarget::new()), #[cfg(feature = "with-logs-and-telemetry")] logger, } @@ -138,8 +135,6 @@ impl MyPostgresBuilder { sql_request_timeout, sql_db_sync_timeout, - #[cfg(feature = "with-ssh")] - ssh_target, #[cfg(feature = "with-logs-and-telemetry")] logger, } => { @@ -152,7 +147,7 @@ impl MyPostgresBuilder { conn_string.get_db_name().to_string(), postgres_settings, #[cfg(feature = "with-ssh")] - ssh_target, + conn_string.get_ssh_target().into(), #[cfg(feature = "with-logs-and-telemetry")] logger, )