Skip to content

Commit 826788d

Browse files
committed
Add a MakeTlsConnector for native_tls
1 parent 41243ae commit 826788d

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

tokio-postgres-native-tls/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ version = "0.1.0"
44
authors = ["Steven Fackler <[email protected]>"]
55
edition = "2018"
66

7+
[features]
8+
default = ["runtime"]
9+
runtime = ["tokio-postgres/runtime"]
10+
711
[dependencies]
812
futures = "0.1"
913
native-tls = "0.2"
1014
tokio-io = "0.1"
11-
tokio-tls = "0.2"
15+
tokio-tls = "0.2.1"
1216
tokio-postgres = { version = "0.3", path = "../tokio-postgres", default-features = false }
1317

1418
[dev-dependencies]

tokio-postgres-native-tls/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,49 @@
22

33
use futures::{try_ready, Async, Future, Poll};
44
use tokio_io::{AsyncRead, AsyncWrite};
5+
#[cfg(feature = "runtime")]
6+
use tokio_postgres::MakeTlsConnect;
57
use tokio_postgres::{ChannelBinding, TlsConnect};
68
use tokio_tls::{Connect, TlsStream};
79

810
#[cfg(test)]
911
mod test;
1012

13+
#[cfg(feature = "runtime")]
14+
#[derive(Clone)]
15+
pub struct MakeTlsConnector(tokio_tls::TlsConnector);
16+
17+
#[cfg(feature = "runtime")]
18+
impl MakeTlsConnector {
19+
pub fn new(connector: native_tls::TlsConnector) -> MakeTlsConnector {
20+
MakeTlsConnector(tokio_tls::TlsConnector::from(connector))
21+
}
22+
}
23+
24+
#[cfg(feature = "runtime")]
25+
impl<S> MakeTlsConnect<S> for MakeTlsConnector
26+
where
27+
S: AsyncRead + AsyncWrite,
28+
{
29+
type Stream = TlsStream<S>;
30+
type TlsConnect = TlsConnector;
31+
type Error = native_tls::Error;
32+
33+
fn make_tls_connect(&mut self, domain: &str) -> Result<TlsConnector, native_tls::Error> {
34+
Ok(TlsConnector {
35+
connector: self.0.clone(),
36+
domain: domain.to_string(),
37+
})
38+
}
39+
}
40+
1141
pub struct TlsConnector {
1242
connector: tokio_tls::TlsConnector,
1343
domain: String,
1444
}
1545

1646
impl TlsConnector {
17-
pub fn with_connector(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
47+
pub fn new(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
1848
TlsConnector {
1949
connector: tokio_tls::TlsConnector::from(connector),
2050
domain: domain.to_string(),

tokio-postgres-native-tls/src/test.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use tokio::net::TcpStream;
44
use tokio::runtime::current_thread::Runtime;
55
use tokio_postgres::TlsConnect;
66

7+
#[cfg(feature = "runtime")]
8+
use crate::MakeTlsConnector;
79
use crate::TlsConnector;
810

911
fn smoke_test<T>(s: &str, tls: T)
@@ -45,7 +47,7 @@ fn require() {
4547
.unwrap();
4648
smoke_test(
4749
"user=ssl_user dbname=postgres sslmode=require",
48-
TlsConnector::with_connector(connector, "localhost"),
50+
TlsConnector::new(connector, "localhost"),
4951
);
5052
}
5153

@@ -59,7 +61,7 @@ fn prefer() {
5961
.unwrap();
6062
smoke_test(
6163
"user=ssl_user dbname=postgres",
62-
TlsConnector::with_connector(connector, "localhost"),
64+
TlsConnector::new(connector, "localhost"),
6365
);
6466
}
6567

@@ -73,6 +75,31 @@ fn scram_user() {
7375
.unwrap();
7476
smoke_test(
7577
"user=scram_user password=password dbname=postgres sslmode=require",
76-
TlsConnector::with_connector(connector, "localhost"),
78+
TlsConnector::new(connector, "localhost"),
7779
);
7880
}
81+
82+
#[test]
83+
#[cfg(feature = "runtime")]
84+
fn runtime() {
85+
let mut runtime = Runtime::new().unwrap();
86+
87+
let connector = native_tls::TlsConnector::builder()
88+
.add_root_certificate(
89+
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
90+
)
91+
.build()
92+
.unwrap();
93+
let connector = MakeTlsConnector::new(connector);
94+
95+
let connect = tokio_postgres::connect(
96+
"host=localhost port=5433 user=postgres sslmode=require",
97+
connector,
98+
);
99+
let (mut client, connection) = runtime.block_on(connect).unwrap();
100+
let connection = connection.map_err(|e| panic!("{}", e));
101+
runtime.spawn(connection);
102+
103+
let execute = client.batch_execute("SELECT 1");
104+
runtime.block_on(execute).unwrap();
105+
}

0 commit comments

Comments
 (0)