Skip to content

Commit

Permalink
Added ability to parse ssh configuration from connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Sep 26, 2024
1 parent 8eaf906 commit 59f02a5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
44 changes: 44 additions & 0 deletions my-postgres-core/src/connection/connection_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct PostgresConnectionString {
port: u16,
db_name: Position,
ssl_require: bool,
ssh: Option<Position>,
}

impl PostgresConnectionString {
Expand All @@ -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
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -142,14 +155,37 @@ 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<u8>) -> Self {
let mut user_name = None;
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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -246,6 +288,7 @@ impl PostgresConnectionString {
port,
db_name: db_name.unwrap(),
ssl_require,
ssh,
}
}

Expand Down Expand Up @@ -335,6 +378,7 @@ impl PostgresConnectionString {
to: db_name_end,
},
ssl_require,
ssh: None,
}
}

Expand Down
6 changes: 2 additions & 4 deletions my-postgres-core/src/connection/postgres_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl PostgresConnection {
pub async fn new_as_single_connection(
app_name: impl Into<StrOrString<'static>>,
postgres_settings: Arc<dyn PostgresSettings + Sync + Send + 'static>,
#[cfg(feature = "with-ssh")] ssh_target: Arc<crate::ssh::SshTarget>,
#[cfg(feature = "with-logs-and-telemetry")] logger: Arc<dyn Logger + Sync + Send + 'static>,
) -> Self {
let app_name: StrOrString<'static> = app_name.into();
Expand All @@ -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,
)
Expand All @@ -57,7 +56,6 @@ impl PostgresConnection {
app_name: impl Into<StrOrString<'static>>,
postgres_settings: Arc<dyn PostgresSettings + Sync + Send + 'static>,
max_pool_size: usize,
#[cfg(feature = "with-ssh")] ssh_target: Arc<crate::ssh::SshTarget>,
#[cfg(feature = "with-logs-and-telemetry")] logger: Arc<dyn Logger + Sync + Send + 'static>,
) -> Self {
let app_name: StrOrString<'static> = app_name.into();
Expand All @@ -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,
))
Expand Down
9 changes: 2 additions & 7 deletions my-postgres-core/src/my_postgres_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,8 +16,6 @@ pub enum MyPostgresBuilder {
table_schema_data: Vec<TableSchema>,
sql_request_timeout: Duration,
sql_db_sync_timeout: Duration,
#[cfg(feature = "with-ssh")]
ssh_target: Arc<crate::ssh::SshTarget>,
#[cfg(feature = "with-logs-and-telemetry")]
logger: Arc<dyn rust_extensions::Logger + Send + Sync + 'static>,
},
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
} => {
Expand All @@ -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,
)
Expand Down

0 comments on commit 59f02a5

Please sign in to comment.