Skip to content

Commit c8a054c

Browse files
ucepetrosagg
authored andcommitted
config: add ssl config params
Adds additional SSL config params: - sslcert - sslkey - sslrootcert More details at https://www.postgresql.org/docs/9.5/libpq-connect.html#LIBPQ-CONNSTRING.
1 parent 2e7372d commit c8a054c

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

postgres/src/config.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use crate::connection::Connection;
44
use crate::Client;
55
use log::info;
6-
use std::fmt;
76
use std::net::IpAddr;
87
use std::path::Path;
98
use std::str::FromStr;
109
use std::sync::Arc;
1110
use std::time::Duration;
11+
use std::{fmt, path::PathBuf};
1212
use tokio::runtime;
1313
#[doc(inline)]
1414
pub use tokio_postgres::config::{
@@ -34,9 +34,12 @@ use tokio_postgres::{Error, Socket};
3434
/// * `dbname` - The name of the database to connect to. Defaults to the username.
3535
/// * `options` - Command line options used to configure the server.
3636
/// * `application_name` - Sets the `application_name` parameter on the server.
37+
/// * `sslcert` - Location of the client SSL certificate file.
38+
/// * `sslkey` - Location for the secret key file used for the client certificate.
3739
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
3840
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
3941
/// be used. Defaults to `prefer`.
42+
/// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
4043
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
4144
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
4245
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
@@ -218,6 +221,32 @@ impl Config {
218221
self.config.get_application_name()
219222
}
220223

224+
/// Sets the location of the client SSL certificate file.
225+
///
226+
/// Defaults to `None`.
227+
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
228+
self.config.ssl_cert(ssl_cert);
229+
self
230+
}
231+
232+
/// Gets the location of the client SSL certificate file.
233+
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
234+
self.config.get_ssl_cert()
235+
}
236+
237+
/// Sets the location of the secret key file used for the client certificate.
238+
///
239+
/// Defaults to `None`.
240+
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
241+
self.config.ssl_key(ssl_key);
242+
self
243+
}
244+
245+
/// Gets the location of the secret key file used for the client certificate.
246+
pub fn get_ssl_key(&self) -> Option<PathBuf> {
247+
self.config.get_ssl_key()
248+
}
249+
221250
/// Sets the SSL configuration.
222251
///
223252
/// Defaults to `prefer`.
@@ -231,6 +260,19 @@ impl Config {
231260
self.config.get_ssl_mode()
232261
}
233262

263+
/// Sets the location of SSL certificate authority (CA) certificate.
264+
///
265+
/// Defaults to `None`.
266+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
267+
self.config.ssl_root_cert(ssl_root_cert);
268+
self
269+
}
270+
271+
/// Gets the location of SSL certificate authority (CA) certificate.
272+
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
273+
self.config.get_ssl_root_cert()
274+
}
275+
234276
/// Adds a host to the configuration.
235277
///
236278
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix

tokio-postgres/src/config.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ pub enum Host {
119119
/// * `dbname` - The name of the database to connect to. Defaults to the username.
120120
/// * `options` - Command line options used to configure the server.
121121
/// * `application_name` - Sets the `application_name` parameter on the server.
122+
/// * `sslcert` - Location of the client SSL certificate file.
123+
/// * `sslkey` - Location for the secret key file used for the client certificate.
122124
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
123125
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
124126
/// be used. Defaults to `prefer`.
127+
/// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
125128
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
126129
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
127130
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
@@ -217,7 +220,10 @@ pub struct Config {
217220
pub(crate) dbname: Option<String>,
218221
pub(crate) options: Option<String>,
219222
pub(crate) application_name: Option<String>,
223+
pub(crate) ssl_cert: Option<PathBuf>,
224+
pub(crate) ssl_key: Option<PathBuf>,
220225
pub(crate) ssl_mode: SslMode,
226+
pub(crate) ssl_root_cert: Option<PathBuf>,
221227
pub(crate) host: Vec<Host>,
222228
pub(crate) hostaddr: Vec<IpAddr>,
223229
pub(crate) port: Vec<u16>,
@@ -247,7 +253,10 @@ impl Config {
247253
dbname: None,
248254
options: None,
249255
application_name: None,
256+
ssl_cert: None,
257+
ssl_key: None,
250258
ssl_mode: SslMode::Prefer,
259+
ssl_root_cert: None,
251260
host: vec![],
252261
hostaddr: vec![],
253262
port: vec![],
@@ -334,6 +343,32 @@ impl Config {
334343
self.application_name.as_deref()
335344
}
336345

346+
/// Sets the location of the client SSL certificate file.
347+
///
348+
/// Defaults to `None`.
349+
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
350+
self.ssl_cert = Some(PathBuf::from(ssl_cert));
351+
self
352+
}
353+
354+
/// Gets the location of the client SSL certificate file.
355+
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
356+
self.ssl_cert.clone()
357+
}
358+
359+
/// Sets the location of the secret key file used for the client certificate.
360+
///
361+
/// Defaults to `None`.
362+
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
363+
self.ssl_key = Some(PathBuf::from(ssl_key));
364+
self
365+
}
366+
367+
/// Gets the location of the secret key file used for the client certificate.
368+
pub fn get_ssl_key(&self) -> Option<PathBuf> {
369+
self.ssl_key.clone()
370+
}
371+
337372
/// Sets the SSL configuration.
338373
///
339374
/// Defaults to `prefer`.
@@ -347,6 +382,19 @@ impl Config {
347382
self.ssl_mode
348383
}
349384

385+
/// Sets the location of SSL certificate authority (CA) certificate.
386+
///
387+
/// Defaults to `None`.
388+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
389+
self.ssl_root_cert = Some(PathBuf::from(ssl_root_cert));
390+
self
391+
}
392+
393+
/// Gets the location of SSL certificate authority (CA) certificate.
394+
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
395+
self.ssl_root_cert.clone()
396+
}
397+
350398
/// Adds a host to the configuration.
351399
///
352400
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
@@ -579,6 +627,18 @@ impl Config {
579627
"application_name" => {
580628
self.application_name(value);
581629
}
630+
"sslcert" => {
631+
if std::fs::metadata(value).is_err() {
632+
return Err(Error::config_parse(Box::new(InvalidValue("sslcert"))));
633+
}
634+
self.ssl_cert(value);
635+
}
636+
"sslkey" => {
637+
if std::fs::metadata(value).is_err() {
638+
return Err(Error::config_parse(Box::new(InvalidValue("sslkey"))));
639+
}
640+
self.ssl_key(value);
641+
}
582642
"sslmode" => {
583643
let mode = match value {
584644
"disable" => SslMode::Disable,
@@ -590,6 +650,12 @@ impl Config {
590650
};
591651
self.ssl_mode(mode);
592652
}
653+
"sslrootcert" => {
654+
if std::fs::metadata(value).is_err() {
655+
return Err(Error::config_parse(Box::new(InvalidValue("sslrootcert"))));
656+
}
657+
self.ssl_root_cert(value);
658+
}
593659
"host" => {
594660
for host in value.split(',') {
595661
self.host(host);
@@ -776,7 +842,10 @@ impl fmt::Debug for Config {
776842
.field("dbname", &self.dbname)
777843
.field("options", &self.options)
778844
.field("application_name", &self.application_name)
845+
.field("ssl_cert", &self.ssl_cert)
846+
.field("ssl_key", &self.ssl_key)
779847
.field("ssl_mode", &self.ssl_mode)
848+
.field("ssl_root_cert", &self.ssl_root_cert)
780849
.field("host", &self.host)
781850
.field("hostaddr", &self.hostaddr)
782851
.field("port", &self.port)

0 commit comments

Comments
 (0)