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

131 lines
3.6 KiB
Rust
Raw Normal View History

2019-04-02 01:51:17 +00:00
//! TLS support for `tokio-postgres` and `postgres` via `native-tls.
2019-03-07 05:13:18 +00:00
//!
2019-04-02 01:51:17 +00:00
//! # Examples
2019-03-07 05:13:18 +00:00
//!
//! ```no_run
//! use native_tls::{Certificate, TlsConnector};
//! use postgres_native_tls::MakeTlsConnector;
2019-03-07 05:13:18 +00:00
//! use std::fs;
//!
2019-04-02 01:51:17 +00:00
//! # fn main() -> Result<(), Box<std::error::Error>> {
//! let cert = fs::read("database_cert.pem")?;
//! let cert = Certificate::from_pem(&cert)?;
2019-03-07 05:13:18 +00:00
//! let connector = TlsConnector::builder()
//! .add_root_certificate(cert)
2019-04-02 01:51:17 +00:00
//! .build()?;
2019-03-07 05:13:18 +00:00
//! let connector = MakeTlsConnector::new(connector);
//!
//! let connect_future = tokio_postgres::connect(
//! "host=localhost user=postgres sslmode=require",
//! connector,
//! );
//!
//! // ...
2019-04-02 01:51:17 +00:00
//! # Ok(())
//! # }
//! ```
//!
//! ```no_run
//! use native_tls::{Certificate, TlsConnector};
//! use postgres_native_tls::MakeTlsConnector;
2019-04-02 01:51:17 +00:00
//! use std::fs;
//!
//! # fn main() -> Result<(), Box<std::error::Error>> {
//! let cert = fs::read("database_cert.pem")?;
//! let cert = Certificate::from_pem(&cert)?;
//! let connector = TlsConnector::builder()
//! .add_root_certificate(cert)
//! .build()?;
//! let connector = MakeTlsConnector::new(connector);
//!
//! let client = postgres::Client::connect(
2019-04-02 01:51:17 +00:00
//! "host=localhost user=postgres sslmode=require",
//! connector,
//! )?;
//! # Ok(())
//! # }
2019-03-07 05:13:18 +00:00
//! ```
#![doc(html_root_url = "https://docs.rs/postgres-native-tls/0.3")]
2019-03-07 05:13:18 +00:00
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
2018-10-15 00:44:46 +00:00
2019-08-05 02:21:32 +00:00
use std::future::Future;
use std::pin::Pin;
2018-10-15 00:44:46 +00:00
use tokio_io::{AsyncRead, AsyncWrite};
2019-01-17 02:30:04 +00:00
#[cfg(feature = "runtime")]
2019-03-05 05:26:10 +00:00
use tokio_postgres::tls::MakeTlsConnect;
use tokio_postgres::tls::{ChannelBinding, TlsConnect};
2019-08-03 04:03:54 +00:00
use tokio_tls::TlsStream;
2018-10-15 00:44:46 +00:00
#[cfg(test)]
mod test;
2019-03-07 05:13:18 +00:00
/// A `MakeTlsConnect` implementation using the `native-tls` crate.
///
/// Requires the `runtime` Cargo feature (enabled by default).
2019-01-17 02:30:04 +00:00
#[cfg(feature = "runtime")]
#[derive(Clone)]
2019-01-17 02:32:26 +00:00
pub struct MakeTlsConnector(native_tls::TlsConnector);
2019-01-17 02:30:04 +00:00
#[cfg(feature = "runtime")]
impl MakeTlsConnector {
2019-03-07 05:13:18 +00:00
/// Creates a new connector.
2019-01-17 02:30:04 +00:00
pub fn new(connector: native_tls::TlsConnector) -> MakeTlsConnector {
2019-01-17 02:32:26 +00:00
MakeTlsConnector(connector)
2019-01-17 02:30:04 +00:00
}
}
#[cfg(feature = "runtime")]
impl<S> MakeTlsConnect<S> for MakeTlsConnector
where
2019-08-03 04:03:54 +00:00
S: AsyncRead + AsyncWrite + Unpin + 'static + Send,
2019-01-17 02:30:04 +00:00
{
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> {
2019-01-17 02:32:26 +00:00
Ok(TlsConnector::new(self.0.clone(), domain))
2019-01-17 02:30:04 +00:00
}
}
2019-03-07 05:13:18 +00:00
/// A `TlsConnect` implementation using the `native-tls` crate.
2018-10-15 00:44:46 +00:00
pub struct TlsConnector {
connector: tokio_tls::TlsConnector,
domain: String,
2018-10-15 00:44:46 +00:00
}
impl TlsConnector {
2019-03-07 05:13:18 +00:00
/// Creates a new connector configured to connect to the specified domain.
2019-01-17 02:30:04 +00:00
pub fn new(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
2018-10-15 00:44:46 +00:00
TlsConnector {
connector: tokio_tls::TlsConnector::from(connector),
domain: domain.to_string(),
2018-10-15 00:44:46 +00:00
}
}
}
impl<S> TlsConnect<S> for TlsConnector
where
2019-08-03 04:03:54 +00:00
S: AsyncRead + AsyncWrite + Unpin + 'static + Send,
{
type Stream = TlsStream<S>;
type Error = native_tls::Error;
2019-10-09 22:20:23 +00:00
#[allow(clippy::type_complexity)]
2019-08-05 02:21:32 +00:00
type Future = Pin<
Box<dyn Future<Output = Result<(TlsStream<S>, ChannelBinding), native_tls::Error>> + Send>,
>;
2018-10-15 00:44:46 +00:00
2019-08-03 04:03:54 +00:00
fn connect(self, stream: S) -> Self::Future {
let future = async move {
let stream = self.connector.connect(&self.domain, stream).await?;
2018-10-15 00:44:46 +00:00
2019-08-03 04:03:54 +00:00
// FIXME https://github.com/tokio-rs/tokio/issues/1383
let channel_binding = ChannelBinding::none();
2018-10-15 00:44:46 +00:00
2019-08-03 04:03:54 +00:00
Ok((stream, channel_binding))
2018-11-30 05:30:02 +00:00
};
2018-10-15 00:44:46 +00:00
2019-08-03 04:03:54 +00:00
Box::pin(future)
2018-10-15 00:44:46 +00:00
}
}