Update postgres-native-tls

This commit is contained in:
Steven Fackler 2019-08-02 21:03:54 -07:00
parent ad31290f5f
commit 59923bd425
3 changed files with 64 additions and 73 deletions

View File

@ -16,12 +16,12 @@ default = ["runtime"]
runtime = ["tokio-postgres/runtime"] runtime = ["tokio-postgres/runtime"]
[dependencies] [dependencies]
futures = "0.1" futures-preview = "0.3.0-alpha.17"
native-tls = "0.2" native-tls = "0.2"
tokio-io = "0.1" tokio-io = { git = "https://github.com/tokio-rs/tokio" }
tokio-tls = "0.2.1" tokio-tls = { git = "https://github.com/tokio-rs/tokio" }
tokio-postgres = { version = "0.4.0-rc.1", path = "../tokio-postgres", default-features = false } tokio-postgres = { version = "0.4.0-rc.1", path = "../tokio-postgres", default-features = false }
[dev-dependencies] [dev-dependencies]
tokio = "0.1.7" tokio = { git = "https://github.com/tokio-rs/tokio" }
postgres = { version = "0.16.0-rc.1", path = "../postgres" } #postgres = { version = "0.16.0-rc.1", path = "../postgres" }

View File

@ -47,13 +47,15 @@
//! ``` //! ```
#![doc(html_root_url = "https://docs.rs/postgres-native-tls/0.2.0-rc.1")] #![doc(html_root_url = "https://docs.rs/postgres-native-tls/0.2.0-rc.1")]
#![warn(rust_2018_idioms, clippy::all, missing_docs)] #![warn(rust_2018_idioms, clippy::all, missing_docs)]
#![feature(async_await)]
use futures::{try_ready, Async, Future, Poll};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
use tokio_postgres::tls::MakeTlsConnect; use tokio_postgres::tls::MakeTlsConnect;
use tokio_postgres::tls::{ChannelBinding, TlsConnect}; use tokio_postgres::tls::{ChannelBinding, TlsConnect};
use tokio_tls::{Connect, TlsStream}; use tokio_tls::TlsStream;
use std::pin::Pin;
use std::future::Future;
#[cfg(test)] #[cfg(test)]
mod test; mod test;
@ -76,7 +78,7 @@ impl MakeTlsConnector {
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
impl<S> MakeTlsConnect<S> for MakeTlsConnector impl<S> MakeTlsConnect<S> for MakeTlsConnector
where where
S: AsyncRead + AsyncWrite, S: AsyncRead + AsyncWrite + Unpin + 'static + Send,
{ {
type Stream = TlsStream<S>; type Stream = TlsStream<S>;
type TlsConnect = TlsConnector; type TlsConnect = TlsConnector;
@ -105,35 +107,22 @@ impl TlsConnector {
impl<S> TlsConnect<S> for TlsConnector impl<S> TlsConnect<S> for TlsConnector
where where
S: AsyncRead + AsyncWrite, S: AsyncRead + AsyncWrite + Unpin + 'static + Send,
{ {
type Stream = TlsStream<S>; type Stream = TlsStream<S>;
type Error = native_tls::Error; type Error = native_tls::Error;
type Future = TlsConnectFuture<S>; type Future = Pin<Box<dyn Future<Output = Result<(TlsStream<S>, ChannelBinding), native_tls::Error>> + Send>>;
fn connect(self, stream: S) -> TlsConnectFuture<S> { fn connect(self, stream: S) -> Self::Future {
TlsConnectFuture(self.connector.connect(&self.domain, stream)) let future = async move {
} let stream = self.connector.connect(&self.domain, stream).await?;
}
/// The future returned by `TlsConnector`. // FIXME https://github.com/tokio-rs/tokio/issues/1383
pub struct TlsConnectFuture<S>(Connect<S>); let channel_binding = ChannelBinding::none();
impl<S> Future for TlsConnectFuture<S> Ok((stream, channel_binding))
where
S: AsyncRead + AsyncWrite,
{
type Item = (TlsStream<S>, ChannelBinding);
type Error = native_tls::Error;
fn poll(&mut self) -> Poll<(TlsStream<S>, ChannelBinding), native_tls::Error> {
let stream = try_ready!(self.0.poll());
let channel_binding = match stream.get_ref().tls_server_end_point().unwrap_or(None) {
Some(buf) => ChannelBinding::tls_server_end_point(buf),
None => ChannelBinding::none(),
}; };
Ok(Async::Ready((stream, channel_binding))) Box::pin(future)
} }
} }

View File

@ -1,44 +1,40 @@
use futures::{Future, Stream};
use native_tls::{self, Certificate}; use native_tls::{self, Certificate};
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::runtime::current_thread::Runtime;
use tokio_postgres::tls::TlsConnect; use tokio_postgres::tls::TlsConnect;
use futures::{FutureExt, TryStreamExt};
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
use crate::MakeTlsConnector; use crate::MakeTlsConnector;
use crate::TlsConnector; use crate::TlsConnector;
fn smoke_test<T>(s: &str, tls: T) async fn smoke_test<T>(s: &str, tls: T)
where where
T: TlsConnect<TcpStream>, T: TlsConnect<TcpStream>,
T::Stream: 'static, T::Stream: 'static + Send,
{ {
let mut runtime = Runtime::new().unwrap(); let stream = TcpStream::connect(&"127.0.0.1:5433".parse().unwrap())
.await
.unwrap();
let builder = s.parse::<tokio_postgres::Config>().unwrap(); let builder = s.parse::<tokio_postgres::Config>().unwrap();
let (mut client, connection) = builder.connect_raw(stream, tls).await.unwrap();
let handshake = TcpStream::connect(&"127.0.0.1:5433".parse().unwrap()) let connection = connection.map(|r| r.unwrap());
.map_err(|e| panic!("{}", e)) tokio::spawn(connection);
.and_then(|s| builder.connect_raw(s, tls));
let (mut client, connection) = runtime.block_on(handshake).unwrap();
let connection = connection.map_err(|e| panic!("{}", e));
runtime.spawn(connection);
let prepare = client.prepare("SELECT 1::INT4"); let stmt = client.prepare("SELECT $1::INT4").await.unwrap();
let statement = runtime.block_on(prepare).unwrap(); let rows = client
let select = client.query(&statement, &[]).collect().map(|rows| { .query(&stmt, &[&1i32])
assert_eq!(rows.len(), 1); .try_collect::<Vec<_>>()
assert_eq!(rows[0].get::<_, i32>(0), 1); .await
}); .unwrap();
runtime.block_on(select).unwrap();
drop(statement); assert_eq!(rows.len(), 1);
drop(client); assert_eq!(rows[0].get::<_, i32>(0), 1);
runtime.run().unwrap();
} }
#[test] #[tokio::test]
fn require() { async fn require() {
let connector = native_tls::TlsConnector::builder() let connector = native_tls::TlsConnector::builder()
.add_root_certificate( .add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(), Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
@ -48,11 +44,11 @@ fn require() {
smoke_test( smoke_test(
"user=ssl_user dbname=postgres sslmode=require", "user=ssl_user dbname=postgres sslmode=require",
TlsConnector::new(connector, "localhost"), TlsConnector::new(connector, "localhost"),
); ).await;
} }
#[test] #[tokio::test]
fn prefer() { async fn prefer() {
let connector = native_tls::TlsConnector::builder() let connector = native_tls::TlsConnector::builder()
.add_root_certificate( .add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(), Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
@ -62,11 +58,11 @@ fn prefer() {
smoke_test( smoke_test(
"user=ssl_user dbname=postgres", "user=ssl_user dbname=postgres",
TlsConnector::new(connector, "localhost"), TlsConnector::new(connector, "localhost"),
); ).await;
} }
#[test] #[tokio::test]
fn scram_user() { async fn scram_user() {
let connector = native_tls::TlsConnector::builder() let connector = native_tls::TlsConnector::builder()
.add_root_certificate( .add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(), Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
@ -76,14 +72,12 @@ fn scram_user() {
smoke_test( smoke_test(
"user=scram_user password=password dbname=postgres sslmode=require", "user=scram_user password=password dbname=postgres sslmode=require",
TlsConnector::new(connector, "localhost"), TlsConnector::new(connector, "localhost"),
); ).await;
} }
#[test] #[tokio::test]
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
fn runtime() { async fn runtime() {
let mut runtime = Runtime::new().unwrap();
let connector = native_tls::TlsConnector::builder() let connector = native_tls::TlsConnector::builder()
.add_root_certificate( .add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(), Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
@ -92,14 +86,22 @@ fn runtime() {
.unwrap(); .unwrap();
let connector = MakeTlsConnector::new(connector); let connector = MakeTlsConnector::new(connector);
let connect = tokio_postgres::connect( let (mut client, connection) = tokio_postgres::connect(
"host=localhost port=5433 user=postgres sslmode=require", "host=localhost port=5433 user=postgres sslmode=require",
connector, connector,
); )
let (mut client, connection) = runtime.block_on(connect).unwrap(); .await
let connection = connection.map_err(|e| panic!("{}", e)); .unwrap();
runtime.spawn(connection); let connection = connection.map(|r| r.unwrap());
tokio::spawn(connection);
let execute = client.simple_query("SELECT 1").for_each(|_| Ok(())); let stmt = client.prepare("SELECT $1::INT4").await.unwrap();
runtime.block_on(execute).unwrap(); let rows = client
.query(&stmt, &[&1i32])
.try_collect::<Vec<_>>()
.await
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
} }