@@ -42,6 +42,10 @@ pub enum SslMode {
42
42
Prefer ,
43
43
/// Require the use of TLS.
44
44
Require ,
45
+ /// Require the use of TLS.
46
+ VerifyCa ,
47
+ /// Require the use of TLS.
48
+ VerifyFull ,
45
49
}
46
50
47
51
/// Channel binding configuration.
@@ -94,8 +98,12 @@ pub enum Host {
94
98
/// * `dbname` - The name of the database to connect to. Defaults to the username.
95
99
/// * `options` - Command line options used to configure the server.
96
100
/// * `application_name` - Sets the `application_name` parameter on the server.
101
+ /// * `sslcert` - Location of the client SSL certificate file.
102
+ /// * `sslkey` - Location for the secret key file used for the client certificate.
97
103
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
98
- /// if available, but not used otherwise. If set to `require`, TLS will be forced to be used. Defaults to `prefer`.
104
+ /// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
105
+ /// be used. Defaults to `prefer`.
106
+ /// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
99
107
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
100
108
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
101
109
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
@@ -161,7 +169,10 @@ pub struct Config {
161
169
pub ( crate ) dbname : Option < String > ,
162
170
pub ( crate ) options : Option < String > ,
163
171
pub ( crate ) application_name : Option < String > ,
172
+ pub ( crate ) ssl_cert : Option < PathBuf > ,
173
+ pub ( crate ) ssl_key : Option < PathBuf > ,
164
174
pub ( crate ) ssl_mode : SslMode ,
175
+ pub ( crate ) ssl_root_cert : Option < PathBuf > ,
165
176
pub ( crate ) host : Vec < Host > ,
166
177
pub ( crate ) port : Vec < u16 > ,
167
178
pub ( crate ) connect_timeout : Option < Duration > ,
@@ -187,7 +198,10 @@ impl Config {
187
198
dbname : None ,
188
199
options : None ,
189
200
application_name : None ,
201
+ ssl_cert : None ,
202
+ ssl_key : None ,
190
203
ssl_mode : SslMode :: Prefer ,
204
+ ssl_root_cert : None ,
191
205
host : vec ! [ ] ,
192
206
port : vec ! [ ] ,
193
207
connect_timeout : None ,
@@ -266,6 +280,32 @@ impl Config {
266
280
self . application_name . as_deref ( )
267
281
}
268
282
283
+ /// Sets the location of the client SSL certificate file.
284
+ ///
285
+ /// Defaults to `None`.
286
+ pub fn ssl_cert ( & mut self , ssl_cert : & str ) -> & mut Config {
287
+ self . ssl_cert = Some ( PathBuf :: from ( ssl_cert) ) ;
288
+ self
289
+ }
290
+
291
+ /// Gets the location of the client SSL certificate file.
292
+ pub fn get_ssl_cert ( & self ) -> Option < PathBuf > {
293
+ self . ssl_cert . clone ( )
294
+ }
295
+
296
+ /// Sets the location of the secret key file used for the client certificate.
297
+ ///
298
+ /// Defaults to `None`.
299
+ pub fn ssl_key ( & mut self , ssl_key : & str ) -> & mut Config {
300
+ self . ssl_key = Some ( PathBuf :: from ( ssl_key) ) ;
301
+ self
302
+ }
303
+
304
+ /// Gets the location of the secret key file used for the client certificate.
305
+ pub fn get_ssl_key ( & self ) -> Option < PathBuf > {
306
+ self . ssl_key . clone ( )
307
+ }
308
+
269
309
/// Sets the SSL configuration.
270
310
///
271
311
/// Defaults to `prefer`.
@@ -279,6 +319,19 @@ impl Config {
279
319
self . ssl_mode
280
320
}
281
321
322
+ /// Sets the location of SSL certificate authority (CA) certificate.
323
+ ///
324
+ /// Defaults to `None`.
325
+ pub fn ssl_root_cert ( & mut self , ssl_root_cert : & str ) -> & mut Config {
326
+ self . ssl_root_cert = Some ( PathBuf :: from ( ssl_root_cert) ) ;
327
+ self
328
+ }
329
+
330
+ /// Gets the location of SSL certificate authority (CA) certificate.
331
+ pub fn get_ssl_root_cert ( & self ) -> Option < PathBuf > {
332
+ self . ssl_root_cert . clone ( )
333
+ }
334
+
282
335
/// Adds a host to the configuration.
283
336
///
284
337
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
@@ -427,15 +480,35 @@ impl Config {
427
480
"application_name" => {
428
481
self . application_name ( & value) ;
429
482
}
483
+ "sslcert" => {
484
+ if std:: fs:: metadata ( & value) . is_err ( ) {
485
+ return Err ( Error :: config_parse ( Box :: new ( InvalidValue ( "sslcert" ) ) ) ) ;
486
+ }
487
+ self . ssl_cert ( & value) ;
488
+ }
489
+ "sslkey" => {
490
+ if std:: fs:: metadata ( & value) . is_err ( ) {
491
+ return Err ( Error :: config_parse ( Box :: new ( InvalidValue ( "sslkey" ) ) ) ) ;
492
+ }
493
+ self . ssl_key ( & value) ;
494
+ }
430
495
"sslmode" => {
431
496
let mode = match value {
432
497
"disable" => SslMode :: Disable ,
433
498
"prefer" => SslMode :: Prefer ,
434
499
"require" => SslMode :: Require ,
500
+ "verify-ca" => SslMode :: VerifyCa ,
501
+ "verify-full" => SslMode :: VerifyFull ,
435
502
_ => return Err ( Error :: config_parse ( Box :: new ( InvalidValue ( "sslmode" ) ) ) ) ,
436
503
} ;
437
504
self . ssl_mode ( mode) ;
438
505
}
506
+ "sslrootcert" => {
507
+ if std:: fs:: metadata ( & value) . is_err ( ) {
508
+ return Err ( Error :: config_parse ( Box :: new ( InvalidValue ( "sslrootcert" ) ) ) ) ;
509
+ }
510
+ self . ssl_root_cert ( & value) ;
511
+ }
439
512
"host" => {
440
513
for host in value. split ( ',' ) {
441
514
self . host ( host) ;
@@ -574,7 +647,10 @@ impl fmt::Debug for Config {
574
647
. field ( "dbname" , & self . dbname )
575
648
. field ( "options" , & self . options )
576
649
. field ( "application_name" , & self . application_name )
650
+ . field ( "ssl_cert" , & self . ssl_cert )
651
+ . field ( "ssl_key" , & self . ssl_key )
577
652
. field ( "ssl_mode" , & self . ssl_mode )
653
+ . field ( "ssl_root_cert" , & self . ssl_root_cert )
578
654
. field ( "host" , & self . host )
579
655
. field ( "port" , & self . port )
580
656
. field ( "connect_timeout" , & self . connect_timeout )
0 commit comments