Skip to content

Commit 9fa29a7

Browse files
authored
Merge pull request #1003 from etehtsea/936-pg-tls
feat: Add TLS connection support for outbound PG
2 parents eb3de4f + fd567e2 commit 9fa29a7

File tree

9 files changed

+65
-18
lines changed

9 files changed

+65
-18
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: 36 additions & 6 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> {
240-
tracing::log::debug!("Build new connection: {}", address);
243+
let config = address.parse::<tokio_postgres::Config>()?;
241244

242-
let (client, connection) = tokio_postgres::connect(address, NoTls).await?;
245+
tracing::debug!("Build new connection: {}", address);
243246

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+
}
261+
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 {
246-
tracing::warn!("Postgres connection error: {}", e);
278+
tracing::error!("Postgres connection error: {}", e);
247279
}
248280
});
249-
250-
Ok(client)
251281
}

examples/config-rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/http-rust-outbound-http/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/http-rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/redis-rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust-outbound-pg/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust-outbound-redis/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)