1
- #![ warn( rust_2018_idioms, clippy:: all) ]
1
+ //! TLS support for `tokio-postgres` via `openssl`.
2
+ //!
3
+ //! # Example
4
+ //!
5
+ //! ```no_run
6
+ //! use openssl::ssl::{SslConnector, SslMethod};
7
+ //! use tokio_postgres_openssl::MakeTlsConnector;
8
+ //!
9
+ //! let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
10
+ //! builder.set_ca_file("database_cert.pem").unwrap();
11
+ //! let connector = MakeTlsConnector::new(builder.build());
12
+ //!
13
+ //! let connect_future = tokio_postgres::connect(
14
+ //! "host=localhost user=postgres sslmode=require",
15
+ //! connector,
16
+ //! );
17
+ //!
18
+ //! // ...
19
+ //! ```
20
+
21
+ #![ warn( rust_2018_idioms, clippy:: all, missing_docs) ]
2
22
3
23
use futures:: { try_ready, Async , Future , Poll } ;
4
24
#[ cfg( feature = "runtime" ) ]
@@ -20,25 +40,32 @@ use tokio_postgres::tls::{ChannelBinding, TlsConnect};
20
40
#[ cfg( test) ]
21
41
mod test;
22
42
43
+ /// A `MakeTlsConnect` implementation using the `openssl` crate.
44
+ ///
45
+ /// Requires the `runtime` Cargo feature (enabled by default).
23
46
#[ cfg( feature = "runtime" ) ]
24
47
#[ derive( Clone ) ]
25
48
pub struct MakeTlsConnector {
26
49
connector : SslConnector ,
27
- config : Arc < dyn Fn ( & mut ConnectConfiguration ) -> Result < ( ) , ErrorStack > + Sync + Send > ,
50
+ config : Arc < dyn Fn ( & mut ConnectConfiguration , & str ) -> Result < ( ) , ErrorStack > + Sync + Send > ,
28
51
}
29
52
30
53
#[ cfg( feature = "runtime" ) ]
31
54
impl MakeTlsConnector {
55
+ /// Creates a new connector.
32
56
pub fn new ( connector : SslConnector ) -> MakeTlsConnector {
33
57
MakeTlsConnector {
34
58
connector,
35
- config : Arc :: new ( |_| Ok ( ( ) ) ) ,
59
+ config : Arc :: new ( |_, _ | Ok ( ( ) ) ) ,
36
60
}
37
61
}
38
62
63
+ /// Sets a callback used to apply per-connection configuration.
64
+ ///
65
+ /// The the callback is provided the domain name along with the `ConnectConfiguration`.
39
66
pub fn set_callback < F > ( & mut self , f : F )
40
67
where
41
- F : Fn ( & mut ConnectConfiguration ) -> Result < ( ) , ErrorStack > + ' static + Sync + Send ,
68
+ F : Fn ( & mut ConnectConfiguration , & str ) -> Result < ( ) , ErrorStack > + ' static + Sync + Send ,
42
69
{
43
70
self . config = Arc :: new ( f) ;
44
71
}
@@ -55,17 +82,19 @@ where
55
82
56
83
fn make_tls_connect ( & mut self , domain : & str ) -> Result < TlsConnector , ErrorStack > {
57
84
let mut ssl = self . connector . configure ( ) ?;
58
- ( self . config ) ( & mut ssl) ?;
85
+ ( self . config ) ( & mut ssl, domain ) ?;
59
86
Ok ( TlsConnector :: new ( ssl, domain) )
60
87
}
61
88
}
62
89
90
+ /// A `TlsConnect` implementation using the `openssl` crate.
63
91
pub struct TlsConnector {
64
92
ssl : ConnectConfiguration ,
65
93
domain : String ,
66
94
}
67
95
68
96
impl TlsConnector {
97
+ /// Creates a new connector configured to connect to the specified domain.
69
98
pub fn new ( ssl : ConnectConfiguration , domain : & str ) -> TlsConnector {
70
99
TlsConnector {
71
100
ssl,
87
116
}
88
117
}
89
118
119
+ /// The future returned by `TlsConnector`.
90
120
pub struct TlsConnectFuture < S > ( ConnectAsync < S > ) ;
91
121
92
122
impl < S > Future for TlsConnectFuture < S >
0 commit comments