From 1bd0f006f1cb66f6e3982e98d7c1c4c92cfd2ebe Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:16:45 +0100 Subject: [PATCH 01/28] Initial implementation --- .gitignore | 3 +++ Cargo.toml | 13 ++++++++++++ src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..693699042 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..d4129657c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tokio-postgres-rustls" +version = "0.1.0" +authors = ["jbg "] +edition = "2018" + +[dependencies] +futures = "0.1.28" +rustls = "0.15.2" +tokio-io = "0.1.12" +tokio-postgres = "0.4.0-rc.3" +tokio-rustls = "0.10.0-alpha.3" +webpki = "0.19.1" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 000000000..a1d376f74 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,61 @@ +use std::{ + io, + sync::Arc, +}; + +use futures::Future; +use rustls::ClientConfig; +use tokio_io::{AsyncRead, AsyncWrite}; +use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; +use tokio_rustls::{client::TlsStream, TlsConnector}; +use webpki::{DNSName, DNSNameRef}; + + +pub struct MakeRustlsConnect { + config: Arc, +} + +impl MakeRustlsConnect { + pub fn new(config: ClientConfig) -> Self { + Self { config: Arc::new(config) } + } +} + +impl MakeTlsConnect for MakeRustlsConnect +where + S: AsyncRead + AsyncWrite + 'static +{ + type Stream = TlsStream; + type TlsConnect = RustlsConnect; + type Error = io::Error; + + fn make_tls_connect(&mut self, hostname: &str) -> Result { + DNSNameRef::try_from_ascii_str(hostname) + .map(|dns_name| RustlsConnect { + hostname: dns_name.to_owned(), + connector: Arc::clone(&self.config).into(), + }) + .map_err(|_| io::ErrorKind::InvalidInput.into()) + } +} + +pub struct RustlsConnect { + hostname: DNSName, + connector: TlsConnector, +} + +impl TlsConnect for RustlsConnect +where + S: AsyncRead + AsyncWrite + 'static +{ + type Stream = TlsStream; + type Error = io::Error; + type Future = Box>; + + fn connect(self, stream: S) -> Self::Future { + Box::new( + self.connector.connect(self.hostname.as_ref(), stream) + .map(|s| (s, ChannelBinding::none())) // TODO + ) + } +} From 06c58e125e6b427b28de7f618f56c1eb2af59655 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:26:49 +0100 Subject: [PATCH 02/28] added description and license to Cargo.toml --- Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index d4129657c..0a01f76b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,10 @@ [package] name = "tokio-postgres-rustls" +description = "Rustls integration for tokio-postgres" version = "0.1.0" authors = ["jbg "] edition = "2018" +license = "MIT" [dependencies] futures = "0.1.28" From 514ac75d49873dea813004a432f56aede6d1a473 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:30:46 +0100 Subject: [PATCH 03/28] added README --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..57bd2afbc --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# tokio-postgres-rustls +This is an integration between the [rustls TLS stack](https://github.com/ctz/rustls) +and the [tokio-postgres asynchronous PostgreSQL client library](https://github.com/sfackler/rust-postgres). + +[![Crate](https://img.shields.io/crates/v/tokio-postgres-rustls.svg)](https://crates.io/crates/tokio-postgres-rustls) + +[API Documentation](https://docs.rs/tokio-postgres-rustls/) + +# License +tokio-postgres-rustls is distributed under the MIT license. + From 051d5b83953c6b0ca9674ba9ca82a7d46a307852 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:38:17 +0100 Subject: [PATCH 04/28] added example to README --- Cargo.toml | 1 + README.md | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0a01f76b2..c4112374d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ version = "0.1.0" authors = ["jbg "] edition = "2018" license = "MIT" +readme = "README.md" [dependencies] futures = "0.1.28" diff --git a/README.md b/README.md index 57bd2afbc..93de0239b 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@ and the [tokio-postgres asynchronous PostgreSQL client library](https://github.c [API Documentation](https://docs.rs/tokio-postgres-rustls/) +# Example + +``` +let config = rustls::ClientConfig::new(); +let tls = tokio_postgres::rustls::MakeRustlsConnect::new(config); +let connect_fut = tokio_postgres::connect("host=localhost user=postgres", tls); +// ... +``` + # License tokio-postgres-rustls is distributed under the MIT license. From b9531bb01f791de39aeec492cdabb4c2942e7c32 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:38:51 +0100 Subject: [PATCH 05/28] fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93de0239b..6ebc8d5ea 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ and the [tokio-postgres asynchronous PostgreSQL client library](https://github.c ``` let config = rustls::ClientConfig::new(); -let tls = tokio_postgres::rustls::MakeRustlsConnect::new(config); +let tls = tokio_postgres_rustls::MakeRustlsConnect::new(config); let connect_fut = tokio_postgres::connect("host=localhost user=postgres", tls); // ... ``` From c9f53c059e55b64f817e1dc147d1b37e87e1d46b Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:58:37 +0100 Subject: [PATCH 06/28] added Send bound on the underlying stream, added test --- Cargo.toml | 5 ++++- src/lib.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c4112374d..cf19abfd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.1.0" +version = "0.2.0" authors = ["jbg "] edition = "2018" license = "MIT" @@ -14,3 +14,6 @@ tokio-io = "0.1.12" tokio-postgres = "0.4.0-rc.3" tokio-rustls = "0.10.0-alpha.3" webpki = "0.19.1" + +[dev-dependencies] +tokio = "0.1.21" diff --git a/src/lib.rs b/src/lib.rs index a1d376f74..1746e6adc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ impl MakeRustlsConnect { impl MakeTlsConnect for MakeRustlsConnect where - S: AsyncRead + AsyncWrite + 'static + S: AsyncRead + AsyncWrite + Send + 'static { type Stream = TlsStream; type TlsConnect = RustlsConnect; @@ -46,11 +46,11 @@ pub struct RustlsConnect { impl TlsConnect for RustlsConnect where - S: AsyncRead + AsyncWrite + 'static + S: AsyncRead + AsyncWrite + Send + 'static { type Stream = TlsStream; type Error = io::Error; - type Future = Box>; + type Future = Box + Send>; fn connect(self, stream: S) -> Self::Future { Box::new( @@ -59,3 +59,31 @@ where ) } } + +#[cfg(test)] +mod tests { + use futures::{Future, Stream}; + use tokio::runtime::current_thread; + + #[test] + fn it_works() { + let config = rustls::ClientConfig::new(); + let tls = super::MakeRustlsConnect::new(config); + current_thread::block_on_all( + tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls) + .map(|(client, connection)| { + tokio::spawn( + connection.map_err(|e| panic!("{:?}", e)) + ); + client + }) + .and_then(|mut client| { + client.prepare("SELECT 1") + .map(|s| (client, s)) + }) + .and_then(|(mut client, statement)| { + client.query(&statement, &[]).collect() + }) + ).unwrap(); + } +} From 567bec7f9d4374494904c5c6613334f908655d49 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Fri, 12 Jul 2019 23:59:30 +0100 Subject: [PATCH 07/28] updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ebc8d5ea..1202d64f4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ and the [tokio-postgres asynchronous PostgreSQL client library](https://github.c ``` let config = rustls::ClientConfig::new(); let tls = tokio_postgres_rustls::MakeRustlsConnect::new(config); -let connect_fut = tokio_postgres::connect("host=localhost user=postgres", tls); +let connect_fut = tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls); // ... ``` From 567ffa566f485c1ac8cc4caeb1e4963b2fe9185c Mon Sep 17 00:00:00 2001 From: jbg <39903+jbg@users.noreply.github.com> Date: Fri, 12 Jul 2019 23:00:26 +0000 Subject: [PATCH 08/28] Added license file --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..75ac7beae --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 jbg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 55449c784d8bc77e4ab21d2d419be881fb5f45d7 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Thu, 26 Dec 2019 16:16:35 +0100 Subject: [PATCH 09/28] Updated to support tokio 0.2 and latest versions of all other deps --- Cargo.toml | 17 ++++---- src/lib.rs | 115 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 90 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf19abfd8..dc5db1f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,20 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.2.0" +version = "0.3.0" authors = ["jbg "] edition = "2018" license = "MIT" readme = "README.md" [dependencies] -futures = "0.1.28" -rustls = "0.15.2" -tokio-io = "0.1.12" -tokio-postgres = "0.4.0-rc.3" -tokio-rustls = "0.10.0-alpha.3" -webpki = "0.19.1" +bytes = "0.5.3" +futures = "0.3.1" +rustls = "0.16.0" +tokio = "0.2.6" +tokio-postgres = "0.5.1" +tokio-rustls = "0.12.1" +webpki = "0.21.0" [dev-dependencies] -tokio = "0.1.21" +tokio = { version = "0.2.6", features = ["macros"] } diff --git a/src/lib.rs b/src/lib.rs index 1746e6adc..81716a1cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,18 @@ +#![feature(type_alias_impl_trait)] + use std::{ io, + future::Future, + mem::MaybeUninit, + pin::Pin, sync::Arc, + task::{Context, Poll}, }; -use futures::Future; +use bytes::{Buf, BufMut}; +use futures::future::TryFutureExt; use rustls::ClientConfig; -use tokio_io::{AsyncRead, AsyncWrite}; +use tokio::io::{AsyncRead, AsyncWrite}; use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; use webpki::{DNSName, DNSNameRef}; @@ -23,13 +30,13 @@ impl MakeRustlsConnect { impl MakeTlsConnect for MakeRustlsConnect where - S: AsyncRead + AsyncWrite + Send + 'static + S: AsyncRead + AsyncWrite + Unpin, { - type Stream = TlsStream; + type Stream = RustlsStream; type TlsConnect = RustlsConnect; - type Error = io::Error; + type Error = std::io::Error; - fn make_tls_connect(&mut self, hostname: &str) -> Result { + fn make_tls_connect(&mut self, hostname: &str) -> std::io::Result { DNSNameRef::try_from_ascii_str(hostname) .map(|dns_name| RustlsConnect { hostname: dns_name.to_owned(), @@ -46,44 +53,84 @@ pub struct RustlsConnect { impl TlsConnect for RustlsConnect where - S: AsyncRead + AsyncWrite + Send + 'static + S: AsyncRead + AsyncWrite + Unpin, { - type Stream = TlsStream; - type Error = io::Error; - type Future = Box + Send>; + type Stream = RustlsStream; + type Error = std::io::Error; + type Future = impl Future>>; fn connect(self, stream: S) -> Self::Future { - Box::new( - self.connector.connect(self.hostname.as_ref(), stream) - .map(|s| (s, ChannelBinding::none())) // TODO - ) + self.connector.connect(self.hostname.as_ref(), stream) + .map_ok(|s| RustlsStream(Box::pin(s))) + } +} + +pub struct RustlsStream(Pin>>); + +impl tokio_postgres::tls::TlsStream for RustlsStream +where + S: AsyncRead + AsyncWrite + Unpin, +{ + fn channel_binding(&self) -> ChannelBinding { + ChannelBinding::none() // TODO + } +} + +impl AsyncRead for RustlsStream +where + S: AsyncRead + AsyncWrite + Unpin, +{ + fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll> { + self.0.as_mut().poll_read(cx, buf) + } + + unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit]) -> bool { + self.0.prepare_uninitialized_buffer(buf) + } + + fn poll_read_buf(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut B) -> Poll> + where + Self: Sized, + { + self.0.as_mut().poll_read_buf(cx, buf) + } +} + +impl AsyncWrite for RustlsStream +where + S: AsyncRead + AsyncWrite + Unpin, +{ + fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll> { + self.0.as_mut().poll_write(cx, buf) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + self.0.as_mut().poll_flush(cx) + } + + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + self.0.as_mut().poll_shutdown(cx) + } + + fn poll_write_buf(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut B) -> Poll> + where + Self: Sized, + { + self.0.as_mut().poll_write_buf(cx, buf) } } #[cfg(test)] mod tests { - use futures::{Future, Stream}; - use tokio::runtime::current_thread; + use futures::future::TryFutureExt; - #[test] - fn it_works() { + #[tokio::test] + async fn it_works() { let config = rustls::ClientConfig::new(); let tls = super::MakeRustlsConnect::new(config); - current_thread::block_on_all( - tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls) - .map(|(client, connection)| { - tokio::spawn( - connection.map_err(|e| panic!("{:?}", e)) - ); - client - }) - .and_then(|mut client| { - client.prepare("SELECT 1") - .map(|s| (client, s)) - }) - .and_then(|(mut client, statement)| { - client.query(&statement, &[]).collect() - }) - ).unwrap(); + let (client, conn) = tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls).await.unwrap(); + tokio::spawn(conn.map_err(|e| panic!("{:?}", e))); + let stmt = client.prepare("SELECT 1").await.unwrap(); + let _ = client.query(&stmt, &[]).await.unwrap(); } } From 3bba7052a6836d7618f96be8f5c49ffc6a6df8a9 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Thu, 26 Dec 2019 16:33:06 +0100 Subject: [PATCH 10/28] removed impl_trait_type_alias, implemented ChannelBinding --- Cargo.toml | 1 + src/lib.rs | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dc5db1f79..4efbd07cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ readme = "README.md" [dependencies] bytes = "0.5.3" futures = "0.3.1" +ring = "0.16.9" rustls = "0.16.0" tokio = "0.2.6" tokio-postgres = "0.5.1" diff --git a/src/lib.rs b/src/lib.rs index 81716a1cb..c3f29d2a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(type_alias_impl_trait)] - use std::{ io, future::Future, @@ -10,8 +8,9 @@ use std::{ }; use bytes::{Buf, BufMut}; -use futures::future::TryFutureExt; -use rustls::ClientConfig; +use futures::future::{FutureExt, TryFutureExt}; +use ring::digest; +use rustls::{ClientConfig, Session}; use tokio::io::{AsyncRead, AsyncWrite}; use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; @@ -30,13 +29,13 @@ impl MakeRustlsConnect { impl MakeTlsConnect for MakeRustlsConnect where - S: AsyncRead + AsyncWrite + Unpin, + S: AsyncRead + AsyncWrite + Unpin + Send + 'static, { type Stream = RustlsStream; type TlsConnect = RustlsConnect; - type Error = std::io::Error; + type Error = io::Error; - fn make_tls_connect(&mut self, hostname: &str) -> std::io::Result { + fn make_tls_connect(&mut self, hostname: &str) -> io::Result { DNSNameRef::try_from_ascii_str(hostname) .map(|dns_name| RustlsConnect { hostname: dns_name.to_owned(), @@ -53,15 +52,16 @@ pub struct RustlsConnect { impl TlsConnect for RustlsConnect where - S: AsyncRead + AsyncWrite + Unpin, + S: AsyncRead + AsyncWrite + Unpin + Send + 'static, { type Stream = RustlsStream; - type Error = std::io::Error; - type Future = impl Future>>; + type Error = io::Error; + type Future = Pin>>>>; fn connect(self, stream: S) -> Self::Future { self.connector.connect(self.hostname.as_ref(), stream) .map_ok(|s| RustlsStream(Box::pin(s))) + .boxed() } } @@ -72,7 +72,14 @@ where S: AsyncRead + AsyncWrite + Unpin, { fn channel_binding(&self) -> ChannelBinding { - ChannelBinding::none() // TODO + let (_, session) = self.0.get_ref(); + match session.get_peer_certificates() { + Some(certs) if certs.len() > 0 => { + let sha256 = digest::digest(&digest::SHA256, certs[0].as_ref()); + ChannelBinding::tls_server_end_point(sha256.as_ref().into()) + }, + _ => ChannelBinding::none(), + } } } From 2b2965e0fbd7cfa1dcbb8bba3f795785f9abe903 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Thu, 26 Dec 2019 16:50:14 +0100 Subject: [PATCH 11/28] rustfmt, logging in tests to see rustls errors --- Cargo.toml | 1 + src/lib.rs | 47 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4efbd07cf..9051a3e05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ tokio-rustls = "0.12.1" webpki = "0.21.0" [dev-dependencies] +env_logger = { version = "0.7.1", default-features = false } tokio = { version = "0.2.6", features = ["macros"] } diff --git a/src/lib.rs b/src/lib.rs index c3f29d2a4..85da516bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use std::{ - io, future::Future, + io, mem::MaybeUninit, pin::Pin, sync::Arc, @@ -16,14 +16,15 @@ use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; use webpki::{DNSName, DNSNameRef}; - pub struct MakeRustlsConnect { config: Arc, } impl MakeRustlsConnect { pub fn new(config: ClientConfig) -> Self { - Self { config: Arc::new(config) } + Self { + config: Arc::new(config), + } } } @@ -59,7 +60,8 @@ where type Future = Pin>>>>; fn connect(self, stream: S) -> Self::Future { - self.connector.connect(self.hostname.as_ref(), stream) + self.connector + .connect(self.hostname.as_ref(), stream) .map_ok(|s| RustlsStream(Box::pin(s))) .boxed() } @@ -77,7 +79,7 @@ where Some(certs) if certs.len() > 0 => { let sha256 = digest::digest(&digest::SHA256, certs[0].as_ref()); ChannelBinding::tls_server_end_point(sha256.as_ref().into()) - }, + } _ => ChannelBinding::none(), } } @@ -87,7 +89,11 @@ impl AsyncRead for RustlsStream where S: AsyncRead + AsyncWrite + Unpin, { - fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll> { + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context, + buf: &mut [u8], + ) -> Poll> { self.0.as_mut().poll_read(cx, buf) } @@ -95,7 +101,11 @@ where self.0.prepare_uninitialized_buffer(buf) } - fn poll_read_buf(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut B) -> Poll> + fn poll_read_buf( + mut self: Pin<&mut Self>, + cx: &mut Context, + buf: &mut B, + ) -> Poll> where Self: Sized, { @@ -107,7 +117,11 @@ impl AsyncWrite for RustlsStream where S: AsyncRead + AsyncWrite + Unpin, { - fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll> { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context, + buf: &[u8], + ) -> Poll> { self.0.as_mut().poll_write(cx, buf) } @@ -119,7 +133,11 @@ where self.0.as_mut().poll_shutdown(cx) } - fn poll_write_buf(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut B) -> Poll> + fn poll_write_buf( + mut self: Pin<&mut Self>, + cx: &mut Context, + buf: &mut B, + ) -> Poll> where Self: Sized, { @@ -133,11 +151,16 @@ mod tests { #[tokio::test] async fn it_works() { + env_logger::builder().is_test(true).try_init().unwrap(); + let config = rustls::ClientConfig::new(); let tls = super::MakeRustlsConnect::new(config); - let (client, conn) = tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls).await.unwrap(); + let (client, conn) = + tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls) + .await + .expect("connect"); tokio::spawn(conn.map_err(|e| panic!("{:?}", e))); - let stmt = client.prepare("SELECT 1").await.unwrap(); - let _ = client.query(&stmt, &[]).await.unwrap(); + let stmt = client.prepare("SELECT 1").await.expect("prepare"); + let _ = client.query(&stmt, &[]).await.expect("query"); } } From 5c90d16956536ce8e4ea4cc9be5de2bb2aba0c29 Mon Sep 17 00:00:00 2001 From: Jasper Bryant-Greene Date: Thu, 26 Dec 2019 16:50:58 +0100 Subject: [PATCH 12/28] added repository key to Cargo.toml --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 9051a3e05..e87180265 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" version = "0.3.0" authors = ["jbg "] +repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" license = "MIT" readme = "README.md" From 34dfbd48064c59e813a96f3cab19aae030748a36 Mon Sep 17 00:00:00 2001 From: Jasper Date: Mon, 30 Dec 2019 19:45:09 +0000 Subject: [PATCH 13/28] made MakeRustlsConnect Clone --- Cargo.toml | 2 +- src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e87180265..dd24e9db6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.3.0" +version = "0.3.1" authors = ["jbg "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index 85da516bb..cdc50bffc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; use webpki::{DNSName, DNSNameRef}; +#[derive(Clone)] pub struct MakeRustlsConnect { config: Arc, } From 7f7e37904b27c5e02c1a39bdc3ae940a610dcb7f Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Thu, 9 Apr 2020 15:47:54 +0700 Subject: [PATCH 14/28] Updated deps --- Cargo.toml | 22 +++++++++++----------- LICENSE | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd24e9db6..a22a9b103 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,23 +1,23 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.3.1" -authors = ["jbg "] +version = "0.4.0" +authors = ["Jasper "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" license = "MIT" readme = "README.md" [dependencies] -bytes = "0.5.3" -futures = "0.3.1" -ring = "0.16.9" -rustls = "0.16.0" -tokio = "0.2.6" -tokio-postgres = "0.5.1" -tokio-rustls = "0.12.1" -webpki = "0.21.0" +bytes = "0.5.4" +futures = "0.3.4" +ring = "0.16.11" +rustls = "0.17.0" +tokio = "0.2.16" +tokio-postgres = "0.5.3" +tokio-rustls = "0.13.0" +webpki = "0.21.2" [dev-dependencies] env_logger = { version = "0.7.1", default-features = false } -tokio = { version = "0.2.6", features = ["macros"] } +tokio = { version = "0.2.16", features = ["macros"] } diff --git a/LICENSE b/LICENSE index 75ac7beae..f5b2b76ea 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 jbg +Copyright (c) 2019 Jasper Hugo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 13fd9505cc76b372ac56ae78495a14f41c608931 Mon Sep 17 00:00:00 2001 From: dvic Date: Fri, 24 Jan 2020 19:47:52 +0100 Subject: [PATCH 15/28] indicate that future of TlsConnect is Send --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cdc50bffc..218e7d632 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ where { type Stream = RustlsStream; type Error = io::Error; - type Future = Pin>>>>; + type Future = Pin>> + Send>>; fn connect(self, stream: S) -> Self::Future { self.connector From 9b7241e07d3b871be650ac8fd3c00a3ddd64b27b Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Thu, 9 Apr 2020 15:52:43 +0700 Subject: [PATCH 16/28] 0.4.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a22a9b103..23b6ae792 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.4.0" +version = "0.4.1" authors = ["Jasper "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" From 6a7a5d6af51c375aea0ac8f0dcce79dd7d91273c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 6 Jul 2020 10:14:58 +0200 Subject: [PATCH 17/28] Upgrade rustls to 0.18 --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 23b6ae792..20299f56a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.4.1" +version = "0.5.0" authors = ["Jasper "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" @@ -12,10 +12,10 @@ readme = "README.md" bytes = "0.5.4" futures = "0.3.4" ring = "0.16.11" -rustls = "0.17.0" +rustls = "0.18.0" tokio = "0.2.16" tokio-postgres = "0.5.3" -tokio-rustls = "0.13.0" +tokio-rustls = "0.14.0" webpki = "0.21.2" [dev-dependencies] From 73eb49568b8dc7c9efdac17e412862a3a307695a Mon Sep 17 00:00:00 2001 From: Michael Sowka Date: Tue, 1 Dec 2020 21:47:49 +0100 Subject: [PATCH 18/28] update to new tokio --- Cargo.toml | 22 +++++++++++----------- README.md | 1 - src/lib.rs | 34 ++++------------------------------ 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 20299f56a..3c1ad9b71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.5.0" +version = "0.6.0" authors = ["Jasper "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" @@ -9,15 +9,15 @@ license = "MIT" readme = "README.md" [dependencies] -bytes = "0.5.4" -futures = "0.3.4" -ring = "0.16.11" -rustls = "0.18.0" -tokio = "0.2.16" -tokio-postgres = "0.5.3" -tokio-rustls = "0.14.0" -webpki = "0.21.2" +bytes = "0.6" +futures = "0.3" +ring = "0.16" +rustls = "0.19" +tokio = "0.3" +tokio-postgres = "0.6" +tokio-rustls = "0.21" +webpki = "0.21" [dev-dependencies] -env_logger = { version = "0.7.1", default-features = false } -tokio = { version = "0.2.16", features = ["macros"] } +env_logger = { version = "0.8", default-features = false } +tokio = { version = "0.3", features = ["macros", "rt"] } diff --git a/README.md b/README.md index 1202d64f4..c08a8bcd4 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,3 @@ let connect_fut = tokio_postgres::connect("sslmode=require host=localhost user=p # License tokio-postgres-rustls is distributed under the MIT license. - diff --git a/src/lib.rs b/src/lib.rs index 218e7d632..6b3986131 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,15 @@ use std::{ future::Future, io, - mem::MaybeUninit, pin::Pin, sync::Arc, task::{Context, Poll}, }; -use bytes::{Buf, BufMut}; use futures::future::{FutureExt, TryFutureExt}; use ring::digest; use rustls::{ClientConfig, Session}; -use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; use webpki::{DNSName, DNSNameRef}; @@ -93,25 +91,11 @@ where fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context, - buf: &mut [u8], - ) -> Poll> { + buf: &mut ReadBuf<'_>, + ) -> Poll> { self.0.as_mut().poll_read(cx, buf) } - unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit]) -> bool { - self.0.prepare_uninitialized_buffer(buf) - } - - fn poll_read_buf( - mut self: Pin<&mut Self>, - cx: &mut Context, - buf: &mut B, - ) -> Poll> - where - Self: Sized, - { - self.0.as_mut().poll_read_buf(cx, buf) - } } impl AsyncWrite for RustlsStream @@ -134,16 +118,6 @@ where self.0.as_mut().poll_shutdown(cx) } - fn poll_write_buf( - mut self: Pin<&mut Self>, - cx: &mut Context, - buf: &mut B, - ) -> Poll> - where - Self: Sized, - { - self.0.as_mut().poll_write_buf(cx, buf) - } } #[cfg(test)] @@ -157,7 +131,7 @@ mod tests { let config = rustls::ClientConfig::new(); let tls = super::MakeRustlsConnect::new(config); let (client, conn) = - tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls) + tokio_postgres::connect("sslmode=require host=localhost port=5432 user=postgres", tls) .await .expect("connect"); tokio::spawn(conn.map_err(|e| panic!("{:?}", e))); From 84e6860a99148025d16783116c975a0f42bf65a8 Mon Sep 17 00:00:00 2001 From: Michael Sowka Date: Tue, 1 Dec 2020 21:49:04 +0100 Subject: [PATCH 19/28] update deps && tokio 0.3 --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3c1ad9b71..fca4a47d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ license = "MIT" readme = "README.md" [dependencies] -bytes = "0.6" futures = "0.3" ring = "0.16" rustls = "0.19" From ba8c5ac3520d703f9cd27708ff0a82032a4b3bbe Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Thu, 14 Jan 2021 16:06:56 +0000 Subject: [PATCH 20/28] Update to tokio 1 --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fca4a47d6..2f876e507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,11 @@ readme = "README.md" futures = "0.3" ring = "0.16" rustls = "0.19" -tokio = "0.3" -tokio-postgres = "0.6" -tokio-rustls = "0.21" +tokio = "1" +tokio-postgres = "0.7" +tokio-rustls = "0.22" webpki = "0.21" [dev-dependencies] env_logger = { version = "0.8", default-features = false } -tokio = { version = "0.3", features = ["macros", "rt"] } +tokio = { version = "1", features = ["macros", "rt"] } From a841a3ecef51147dd6c118daaefcf079706a7adf Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Fri, 22 Jan 2021 16:37:33 +0700 Subject: [PATCH 21/28] bumped version to 0.7.0 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f876e507..986c44d3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.6.0" -authors = ["Jasper "] +version = "0.7.0" +authors = ["Jasper Hugo "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" license = "MIT" From f910c04433c601d6a604ac62ba0a366723b3f73c Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 9 Dec 2020 15:04:24 +0100 Subject: [PATCH 22/28] Add support for UDS connections --- src/lib.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6b3986131..8710690a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,15 +37,17 @@ where fn make_tls_connect(&mut self, hostname: &str) -> io::Result { DNSNameRef::try_from_ascii_str(hostname) - .map(|dns_name| RustlsConnect { + .map(|dns_name| RustlsConnect(Some(RustlsConnectData { hostname: dns_name.to_owned(), connector: Arc::clone(&self.config).into(), - }) - .map_err(|_| io::ErrorKind::InvalidInput.into()) + }))) + .or(Ok(RustlsConnect(None))) } } -pub struct RustlsConnect { +pub struct RustlsConnect(Option); + +struct RustlsConnectData { hostname: DNSName, connector: TlsConnector, } @@ -59,10 +61,13 @@ where type Future = Pin>> + Send>>; fn connect(self, stream: S) -> Self::Future { - self.connector - .connect(self.hostname.as_ref(), stream) - .map_ok(|s| RustlsStream(Box::pin(s))) - .boxed() + match self.0 { + None => Box::pin(core::future::ready(Err(io::ErrorKind::InvalidInput.into()))), + Some(c) => c.connector + .connect(c.hostname.as_ref(), stream) + .map_ok(|s| RustlsStream(Box::pin(s))) + .boxed() + } } } From debd547b08a80c139ac7dac49705ca42887d7530 Mon Sep 17 00:00:00 2001 From: Jasper Hugo Date: Wed, 10 Feb 2021 13:51:36 +0700 Subject: [PATCH 23/28] 0.8.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 986c44d3d..3ad9e8e49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.7.0" +version = "0.8.0" authors = ["Jasper Hugo "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" From 4139951982f3ba8e269e8af70f0f213b1717195d Mon Sep 17 00:00:00 2001 From: Karsten Borgwaldt Date: Sun, 31 Oct 2021 15:07:00 +0100 Subject: [PATCH 24/28] updated dependencies rustls and tokio-rustls --- Cargo.toml | 7 +++---- README.md | 5 ++++- src/lib.rs | 46 ++++++++++++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ad9e8e49..314201ef7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.8.0" +version = "0.8.1" authors = ["Jasper Hugo "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" @@ -11,11 +11,10 @@ readme = "README.md" [dependencies] futures = "0.3" ring = "0.16" -rustls = "0.19" +rustls = "0.20" tokio = "1" tokio-postgres = "0.7" -tokio-rustls = "0.22" -webpki = "0.21" +tokio-rustls = "0.23" [dev-dependencies] env_logger = { version = "0.8", default-features = false } diff --git a/README.md b/README.md index c08a8bcd4..b9b5cafb0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,10 @@ and the [tokio-postgres asynchronous PostgreSQL client library](https://github.c # Example ``` -let config = rustls::ClientConfig::new(); +let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(rustls::RootCertStore::empty()) + .with_no_client_auth(); let tls = tokio_postgres_rustls::MakeRustlsConnect::new(config); let connect_fut = tokio_postgres::connect("sslmode=require host=localhost user=postgres", tls); // ... diff --git a/src/lib.rs b/src/lib.rs index 8710690a2..54165da13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ use std::{ + convert::TryFrom, future::Future, io, pin::Pin, @@ -8,11 +9,10 @@ use std::{ use futures::future::{FutureExt, TryFutureExt}; use ring::digest; -use rustls::{ClientConfig, Session}; +use rustls::{ClientConfig, ServerName}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect}; use tokio_rustls::{client::TlsStream, TlsConnector}; -use webpki::{DNSName, DNSNameRef}; #[derive(Clone)] pub struct MakeRustlsConnect { @@ -36,11 +36,13 @@ where type Error = io::Error; fn make_tls_connect(&mut self, hostname: &str) -> io::Result { - DNSNameRef::try_from_ascii_str(hostname) - .map(|dns_name| RustlsConnect(Some(RustlsConnectData { - hostname: dns_name.to_owned(), - connector: Arc::clone(&self.config).into(), - }))) + ServerName::try_from(hostname) + .map(|dns_name| { + RustlsConnect(Some(RustlsConnectData { + hostname: dns_name, + connector: Arc::clone(&self.config).into(), + })) + }) .or(Ok(RustlsConnect(None))) } } @@ -48,7 +50,7 @@ where pub struct RustlsConnect(Option); struct RustlsConnectData { - hostname: DNSName, + hostname: ServerName, connector: TlsConnector, } @@ -63,10 +65,11 @@ where fn connect(self, stream: S) -> Self::Future { match self.0 { None => Box::pin(core::future::ready(Err(io::ErrorKind::InvalidInput.into()))), - Some(c) => c.connector - .connect(c.hostname.as_ref(), stream) + Some(c) => c + .connector + .connect(c.hostname, stream) .map_ok(|s| RustlsStream(Box::pin(s))) - .boxed() + .boxed(), } } } @@ -79,8 +82,8 @@ where { fn channel_binding(&self) -> ChannelBinding { let (_, session) = self.0.get_ref(); - match session.get_peer_certificates() { - Some(certs) if certs.len() > 0 => { + match session.peer_certificates() { + Some(certs) if !certs.is_empty() => { let sha256 = digest::digest(&digest::SHA256, certs[0].as_ref()); ChannelBinding::tls_server_end_point(sha256.as_ref().into()) } @@ -100,7 +103,6 @@ where ) -> Poll> { self.0.as_mut().poll_read(cx, buf) } - } impl AsyncWrite for RustlsStream @@ -122,7 +124,6 @@ where fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { self.0.as_mut().poll_shutdown(cx) } - } #[cfg(test)] @@ -133,12 +134,17 @@ mod tests { async fn it_works() { env_logger::builder().is_test(true).try_init().unwrap(); - let config = rustls::ClientConfig::new(); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(rustls::RootCertStore::empty()) + .with_no_client_auth(); let tls = super::MakeRustlsConnect::new(config); - let (client, conn) = - tokio_postgres::connect("sslmode=require host=localhost port=5432 user=postgres", tls) - .await - .expect("connect"); + let (client, conn) = tokio_postgres::connect( + "sslmode=require host=localhost port=5432 user=postgres", + tls, + ) + .await + .expect("connect"); tokio::spawn(conn.map_err(|e| panic!("{:?}", e))); let stmt = client.prepare("SELECT 1").await.expect("prepare"); let _ = client.query(&stmt, &[]).await.expect("query"); From 32b245a63c7c13853d22a63ee1d98bacf97ec6c9 Mon Sep 17 00:00:00 2001 From: Karsten Borgwaldt Date: Sun, 31 Oct 2021 21:54:54 +0100 Subject: [PATCH 25/28] unit test only: trust any certificate --- Cargo.toml | 2 ++ src/lib.rs | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 314201ef7..9691d8890 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,5 @@ tokio-rustls = "0.23" [dev-dependencies] env_logger = { version = "0.8", default-features = false } tokio = { version = "1", features = ["macros", "rt"] } +rustls = { version = "0.20", features = ["dangerous_configuration"] } + diff --git a/src/lib.rs b/src/lib.rs index 54165da13..a3a4fc235 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,16 +128,37 @@ where #[cfg(test)] mod tests { + use super::*; use futures::future::TryFutureExt; + use rustls::{client::ServerCertVerified, client::ServerCertVerifier, Certificate, Error}; + use std::time::SystemTime; + + struct AcceptAllVerifier {} + impl ServerCertVerifier for AcceptAllVerifier { + fn verify_server_cert( + &self, + _end_entity: &Certificate, + _intermediates: &[Certificate], + _server_name: &ServerName, + _scts: &mut dyn Iterator, + _ocsp_response: &[u8], + _now: SystemTime, + ) -> Result { + Ok(ServerCertVerified::assertion()) + } + } #[tokio::test] async fn it_works() { env_logger::builder().is_test(true).try_init().unwrap(); - let config = rustls::ClientConfig::builder() + let mut config = rustls::ClientConfig::builder() .with_safe_defaults() .with_root_certificates(rustls::RootCertStore::empty()) .with_no_client_auth(); + config + .dangerous() + .set_certificate_verifier(Arc::new(AcceptAllVerifier {})); let tls = super::MakeRustlsConnect::new(config); let (client, conn) = tokio_postgres::connect( "sslmode=require host=localhost port=5432 user=postgres", From 76334a6b28314bfa15e0ddc8225ac3963881fcda Mon Sep 17 00:00:00 2001 From: Karsten Borgwaldt Date: Tue, 16 Nov 2021 15:21:12 +0100 Subject: [PATCH 26/28] Update minor version due to breaking API change Thanks for the hint, @jbg. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9691d8890..520ee6576 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tokio-postgres-rustls" description = "Rustls integration for tokio-postgres" -version = "0.8.1" +version = "0.9.0" authors = ["Jasper Hugo "] repository = "https://github.com/jbg/tokio-postgres-rustls" edition = "2018" From 4c9f505051c658f0fca2cb5b074a9742833be7ec Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 1 Nov 2022 15:17:18 -0400 Subject: [PATCH 27/28] Moved all code to /postgres-rustls dir --- .gitignore => postgres-rustls/.gitignore | 0 Cargo.toml => postgres-rustls/Cargo.toml | 0 LICENSE => postgres-rustls/LICENSE | 0 README.md => postgres-rustls/README.md | 0 {src => postgres-rustls/src}/lib.rs | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename .gitignore => postgres-rustls/.gitignore (100%) rename Cargo.toml => postgres-rustls/Cargo.toml (100%) rename LICENSE => postgres-rustls/LICENSE (100%) rename README.md => postgres-rustls/README.md (100%) rename {src => postgres-rustls/src}/lib.rs (100%) diff --git a/.gitignore b/postgres-rustls/.gitignore similarity index 100% rename from .gitignore rename to postgres-rustls/.gitignore diff --git a/Cargo.toml b/postgres-rustls/Cargo.toml similarity index 100% rename from Cargo.toml rename to postgres-rustls/Cargo.toml diff --git a/LICENSE b/postgres-rustls/LICENSE similarity index 100% rename from LICENSE rename to postgres-rustls/LICENSE diff --git a/README.md b/postgres-rustls/README.md similarity index 100% rename from README.md rename to postgres-rustls/README.md diff --git a/src/lib.rs b/postgres-rustls/src/lib.rs similarity index 100% rename from src/lib.rs rename to postgres-rustls/src/lib.rs From dad5bca94ed09ed95a44fc5154cb04ec8b521aa3 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 1 Nov 2022 15:31:09 -0400 Subject: [PATCH 28/28] bump rustls dep, include new member --- Cargo.toml | 1 + postgres-rustls/Cargo.toml | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4752836a7..f56e4e18f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "postgres-native-tls", "postgres-openssl", "postgres-protocol", + "postgres-rustls", "postgres-types", "tokio-postgres", ] diff --git a/postgres-rustls/Cargo.toml b/postgres-rustls/Cargo.toml index 520ee6576..9d619645c 100644 --- a/postgres-rustls/Cargo.toml +++ b/postgres-rustls/Cargo.toml @@ -17,7 +17,6 @@ tokio-postgres = "0.7" tokio-rustls = "0.23" [dev-dependencies] -env_logger = { version = "0.8", default-features = false } +env_logger = { version = "0.9", default-features = false } tokio = { version = "1", features = ["macros", "rt"] } rustls = { version = "0.20", features = ["dangerous_configuration"] } -