Add a MakeTlsConnector for native_tls

This commit is contained in:
Steven Fackler 2019-01-16 18:30:04 -08:00
parent 41243ae04f
commit 826788d7d9
3 changed files with 66 additions and 5 deletions

View File

@ -4,11 +4,15 @@ version = "0.1.0"
authors = ["Steven Fackler <sfackler@gmail.com>"]
edition = "2018"
[features]
default = ["runtime"]
runtime = ["tokio-postgres/runtime"]
[dependencies]
futures = "0.1"
native-tls = "0.2"
tokio-io = "0.1"
tokio-tls = "0.2"
tokio-tls = "0.2.1"
tokio-postgres = { version = "0.3", path = "../tokio-postgres", default-features = false }
[dev-dependencies]

View File

@ -2,19 +2,49 @@
use futures::{try_ready, Async, Future, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "runtime")]
use tokio_postgres::MakeTlsConnect;
use tokio_postgres::{ChannelBinding, TlsConnect};
use tokio_tls::{Connect, TlsStream};
#[cfg(test)]
mod test;
#[cfg(feature = "runtime")]
#[derive(Clone)]
pub struct MakeTlsConnector(tokio_tls::TlsConnector);
#[cfg(feature = "runtime")]
impl MakeTlsConnector {
pub fn new(connector: native_tls::TlsConnector) -> MakeTlsConnector {
MakeTlsConnector(tokio_tls::TlsConnector::from(connector))
}
}
#[cfg(feature = "runtime")]
impl<S> MakeTlsConnect<S> for MakeTlsConnector
where
S: AsyncRead + AsyncWrite,
{
type Stream = TlsStream<S>;
type TlsConnect = TlsConnector;
type Error = native_tls::Error;
fn make_tls_connect(&mut self, domain: &str) -> Result<TlsConnector, native_tls::Error> {
Ok(TlsConnector {
connector: self.0.clone(),
domain: domain.to_string(),
})
}
}
pub struct TlsConnector {
connector: tokio_tls::TlsConnector,
domain: String,
}
impl TlsConnector {
pub fn with_connector(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
pub fn new(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
TlsConnector {
connector: tokio_tls::TlsConnector::from(connector),
domain: domain.to_string(),

View File

@ -4,6 +4,8 @@ use tokio::net::TcpStream;
use tokio::runtime::current_thread::Runtime;
use tokio_postgres::TlsConnect;
#[cfg(feature = "runtime")]
use crate::MakeTlsConnector;
use crate::TlsConnector;
fn smoke_test<T>(s: &str, tls: T)
@ -45,7 +47,7 @@ fn require() {
.unwrap();
smoke_test(
"user=ssl_user dbname=postgres sslmode=require",
TlsConnector::with_connector(connector, "localhost"),
TlsConnector::new(connector, "localhost"),
);
}
@ -59,7 +61,7 @@ fn prefer() {
.unwrap();
smoke_test(
"user=ssl_user dbname=postgres",
TlsConnector::with_connector(connector, "localhost"),
TlsConnector::new(connector, "localhost"),
);
}
@ -73,6 +75,31 @@ fn scram_user() {
.unwrap();
smoke_test(
"user=scram_user password=password dbname=postgres sslmode=require",
TlsConnector::with_connector(connector, "localhost"),
TlsConnector::new(connector, "localhost"),
);
}
#[test]
#[cfg(feature = "runtime")]
fn runtime() {
let mut runtime = Runtime::new().unwrap();
let connector = native_tls::TlsConnector::builder()
.add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
)
.build()
.unwrap();
let connector = MakeTlsConnector::new(connector);
let connect = tokio_postgres::connect(
"host=localhost port=5433 user=postgres sslmode=require",
connector,
);
let (mut client, connection) = runtime.block_on(connect).unwrap();
let connection = connection.map_err(|e| panic!("{}", e));
runtime.spawn(connection);
let execute = client.batch_execute("SELECT 1");
runtime.block_on(execute).unwrap();
}