Skip to content

Commit fc69afa

Browse files
committed
feat: Add TLS connection support for outbound PG
Underneath it conditionally uses `NoTls` when `sslmode=disable` or sets up ssl connector otherwise. NOTE: By default, `tokio_postgres` is using `sslmode=prefer`, so to avoid SSL setup overhead `sslmode=disable` should be provided explicitly. Refs: #936 Signed-off-by: Konstantin Shabanov <mail@etehtsea.me>
1 parent 5a638f2 commit fc69afa

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

Cargo.lock

Lines changed: 21 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/outbound-pg/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ doctest = false
99

1010
[dependencies]
1111
anyhow = "1.0"
12+
native-tls = "0.2.11"
13+
postgres-native-tls = "0.5.0"
1214
spin-core = { path = "../core" }
1315
tokio = { version = "1", features = [ "rt-multi-thread" ] }
1416
tokio-postgres = { version = "0.7.7" }

crates/outbound-pg/src/lib.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use anyhow::anyhow;
2+
use native_tls::TlsConnector;
3+
use postgres_native_tls::MakeTlsConnector;
24
use spin_core::HostComponent;
35
use std::collections::HashMap;
46
use tokio_postgres::{
7+
config::SslMode,
58
types::{ToSql, Type},
6-
Client, NoTls, Row,
9+
Client, NoTls, Row, Socket,
710
};
811
use wit_bindgen_wasmtime::async_trait;
912

@@ -237,15 +240,42 @@ impl OutboundPg {
237240
}
238241

239242
async fn build_client(address: &str) -> anyhow::Result<Client> {
243+
let config = address.parse::<tokio_postgres::Config>()?;
244+
240245
tracing::log::debug!("Build new connection: {}", address);
241246

242-
let (client, connection) = tokio_postgres::connect(address, NoTls).await?;
247+
if config.get_ssl_mode() == SslMode::Disable {
248+
connect(config).await
249+
} else {
250+
connect_tls(config).await
251+
}
252+
}
253+
254+
async fn connect(config: tokio_postgres::Config) -> anyhow::Result<Client> {
255+
let (client, connection) = config.connect(NoTls).await?;
256+
257+
spawn(connection);
258+
259+
Ok(client)
260+
}
243261

262+
async fn connect_tls(config: tokio_postgres::Config) -> anyhow::Result<Client> {
263+
let builder = TlsConnector::builder();
264+
let connector = MakeTlsConnector::new(builder.build()?);
265+
let (client, connection) = config.connect(connector).await?;
266+
267+
spawn(connection);
268+
269+
Ok(client)
270+
}
271+
272+
fn spawn<T>(connection: tokio_postgres::Connection<Socket, T>)
273+
where
274+
T: tokio_postgres::tls::TlsStream + std::marker::Unpin + std::marker::Send + 'static,
275+
{
244276
tokio::spawn(async move {
245277
if let Err(e) = connection.await {
246278
tracing::warn!("Postgres connection error: {}", e);
247279
}
248280
});
249-
250-
Ok(client)
251281
}

0 commit comments

Comments
 (0)