Skip to content

Commit 71e51f3

Browse files
committed
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 80d78d8 commit 71e51f3

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
@@ -5,11 +5,11 @@
55
use crate::connection::Connection;
66
use crate::Client;
77
use log::info;
8-
use std::fmt;
98
use std::path::Path;
109
use std::str::FromStr;
1110
use std::sync::Arc;
1211
use std::time::Duration;
12+
use std::{fmt, path::PathBuf};
1313
use tokio::runtime;
1414
#[doc(inline)]
1515
pub use tokio_postgres::config::{ChannelBinding, Host, SslMode, TargetSessionAttrs};
@@ -33,9 +33,12 @@ use tokio_postgres::{Error, Socket};
3333
/// * `dbname` - The name of the database to connect to. Defaults to the username.
3434
/// * `options` - Command line options used to configure the server.
3535
/// * `application_name` - Sets the `application_name` parameter on the server.
36+
/// * `sslcert` - Location of the client SSL certificate file.
37+
/// * `sslkey` - Location for the secret key file used for the client certificate.
3638
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
3739
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
3840
/// be used. Defaults to `prefer`.
41+
/// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
3942
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
4043
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
4144
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
@@ -184,6 +187,32 @@ impl Config {
184187
self.config.get_application_name()
185188
}
186189

190+
/// Sets the location of the client SSL certificate file.
191+
///
192+
/// Defaults to `None`.
193+
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
194+
self.config.ssl_cert(ssl_cert);
195+
self
196+
}
197+
198+
/// Gets the location of the client SSL certificate file.
199+
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
200+
self.config.get_ssl_cert()
201+
}
202+
203+
/// Sets the location of the secret key file used for the client certificate.
204+
///
205+
/// Defaults to `None`.
206+
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
207+
self.config.ssl_key(ssl_key);
208+
self
209+
}
210+
211+
/// Gets the location of the secret key file used for the client certificate.
212+
pub fn get_ssl_key(&self) -> Option<PathBuf> {
213+
self.config.get_ssl_key()
214+
}
215+
187216
/// Sets the SSL configuration.
188217
///
189218
/// Defaults to `prefer`.
@@ -197,6 +226,19 @@ impl Config {
197226
self.config.get_ssl_mode()
198227
}
199228

229+
/// Sets the location of SSL certificate authority (CA) certificate.
230+
///
231+
/// Defaults to `None`.
232+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
233+
self.config.ssl_root_cert(ssl_root_cert);
234+
self
235+
}
236+
237+
/// Gets the location of SSL certificate authority (CA) certificate.
238+
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
239+
self.config.get_ssl_root_cert()
240+
}
241+
200242
/// Adds a host to the configuration.
201243
///
202244
/// 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
@@ -98,9 +98,12 @@ pub enum Host {
9898
/// * `dbname` - The name of the database to connect to. Defaults to the username.
9999
/// * `options` - Command line options used to configure the server.
100100
/// * `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.
101103
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
102104
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
103105
/// be used. Defaults to `prefer`.
106+
/// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
104107
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
105108
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
106109
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
@@ -166,7 +169,10 @@ pub struct Config {
166169
pub(crate) dbname: Option<String>,
167170
pub(crate) options: Option<String>,
168171
pub(crate) application_name: Option<String>,
172+
pub(crate) ssl_cert: Option<PathBuf>,
173+
pub(crate) ssl_key: Option<PathBuf>,
169174
pub(crate) ssl_mode: SslMode,
175+
pub(crate) ssl_root_cert: Option<PathBuf>,
170176
pub(crate) host: Vec<Host>,
171177
pub(crate) port: Vec<u16>,
172178
pub(crate) connect_timeout: Option<Duration>,
@@ -192,7 +198,10 @@ impl Config {
192198
dbname: None,
193199
options: None,
194200
application_name: None,
201+
ssl_cert: None,
202+
ssl_key: None,
195203
ssl_mode: SslMode::Prefer,
204+
ssl_root_cert: None,
196205
host: vec![],
197206
port: vec![],
198207
connect_timeout: None,
@@ -271,6 +280,32 @@ impl Config {
271280
self.application_name.as_deref()
272281
}
273282

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+
274309
/// Sets the SSL configuration.
275310
///
276311
/// Defaults to `prefer`.
@@ -284,6 +319,19 @@ impl Config {
284319
self.ssl_mode
285320
}
286321

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+
287335
/// Adds a host to the configuration.
288336
///
289337
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
@@ -432,6 +480,18 @@ impl Config {
432480
"application_name" => {
433481
self.application_name(&value);
434482
}
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+
}
435495
"sslmode" => {
436496
let mode = match value {
437497
"disable" => SslMode::Disable,
@@ -443,6 +503,12 @@ impl Config {
443503
};
444504
self.ssl_mode(mode);
445505
}
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+
}
446512
"host" => {
447513
for host in value.split(',') {
448514
self.host(host);
@@ -581,7 +647,10 @@ impl fmt::Debug for Config {
581647
.field("dbname", &self.dbname)
582648
.field("options", &self.options)
583649
.field("application_name", &self.application_name)
650+
.field("ssl_cert", &self.ssl_cert)
651+
.field("ssl_key", &self.ssl_key)
584652
.field("ssl_mode", &self.ssl_mode)
653+
.field("ssl_root_cert", &self.ssl_root_cert)
585654
.field("host", &self.host)
586655
.field("port", &self.port)
587656
.field("connect_timeout", &self.connect_timeout)

0 commit comments

Comments
 (0)