Skip to content

Commit cc77e6e

Browse files
beneschpetrosagg
authored andcommitted
Change SSL configuration to PEM bytes rather than files
1 parent 6d8777d commit cc77e6e

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

postgres/src/config.rs

Lines changed: 13 additions & 13 deletions
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;
67
use std::net::IpAddr;
78
use std::path::Path;
89
use std::str::FromStr;
910
use std::sync::Arc;
1011
use std::time::Duration;
11-
use std::{fmt, path::PathBuf};
1212
use tokio::runtime;
1313
#[doc(inline)]
1414
pub use tokio_postgres::config::{
@@ -221,29 +221,29 @@ impl Config {
221221
self.config.get_application_name()
222222
}
223223

224-
/// Sets the location of the client SSL certificate file.
224+
/// Sets the client SSL certificate in PEM format.
225225
///
226226
/// Defaults to `None`.
227-
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
227+
pub fn ssl_cert(&mut self, ssl_cert: &[u8]) -> &mut Config {
228228
self.config.ssl_cert(ssl_cert);
229229
self
230230
}
231231

232-
/// Gets the location of the client SSL certificate file.
233-
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
232+
/// Gets the location of the client SSL certificate in PEM format.
233+
pub fn get_ssl_cert(&self) -> Option<&[u8]> {
234234
self.config.get_ssl_cert()
235235
}
236236

237-
/// Sets the location of the secret key file used for the client certificate.
237+
/// Sets the client SSL key in PEM format.
238238
///
239239
/// Defaults to `None`.
240-
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
240+
pub fn ssl_key(&mut self, ssl_key: &[u8]) -> &mut Config {
241241
self.config.ssl_key(ssl_key);
242242
self
243243
}
244244

245-
/// Gets the location of the secret key file used for the client certificate.
246-
pub fn get_ssl_key(&self) -> Option<PathBuf> {
245+
/// Gets the client SSL key in PEM format.
246+
pub fn get_ssl_key(&self) -> Option<&[u8]> {
247247
self.config.get_ssl_key()
248248
}
249249

@@ -260,16 +260,16 @@ impl Config {
260260
self.config.get_ssl_mode()
261261
}
262262

263-
/// Sets the location of SSL certificate authority (CA) certificate.
263+
/// Sets the SSL certificate authority (CA) certificate in PEM format.
264264
///
265265
/// Defaults to `None`.
266-
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
266+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &[u8]) -> &mut Config {
267267
self.config.ssl_root_cert(ssl_root_cert);
268268
self
269269
}
270270

271-
/// Gets the location of SSL certificate authority (CA) certificate.
272-
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
271+
/// Gets the SSL certificate authority (CA) certificate in PEM format.
272+
pub fn get_ssl_root_cert(&self) -> Option<&[u8]> {
273273
self.config.get_ssl_root_cert()
274274
}
275275

tokio-postgres/src/config.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ pub struct Config {
223223
pub(crate) dbname: Option<String>,
224224
pub(crate) options: Option<String>,
225225
pub(crate) application_name: Option<String>,
226-
pub(crate) ssl_cert: Option<PathBuf>,
227-
pub(crate) ssl_key: Option<PathBuf>,
226+
pub(crate) ssl_cert: Option<Vec<u8>>,
227+
pub(crate) ssl_key: Option<Vec<u8>>,
228228
pub(crate) ssl_mode: SslMode,
229-
pub(crate) ssl_root_cert: Option<PathBuf>,
229+
pub(crate) ssl_root_cert: Option<Vec<u8>>,
230230
pub(crate) host: Vec<Host>,
231231
pub(crate) hostaddr: Vec<IpAddr>,
232232
pub(crate) port: Vec<u16>,
@@ -346,30 +346,30 @@ impl Config {
346346
self.application_name.as_deref()
347347
}
348348

349-
/// Sets the location of the client SSL certificate file.
349+
/// Sets the client SSL certificate in PEM format.
350350
///
351351
/// Defaults to `None`.
352-
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
353-
self.ssl_cert = Some(PathBuf::from(ssl_cert));
352+
pub fn ssl_cert(&mut self, ssl_cert: &[u8]) -> &mut Config {
353+
self.ssl_cert = Some(ssl_cert.into());
354354
self
355355
}
356356

357-
/// Gets the location of the client SSL certificate file.
358-
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
359-
self.ssl_cert.clone()
357+
/// Gets the location of the client SSL certificate in PEM format.
358+
pub fn get_ssl_cert(&self) -> Option<&[u8]> {
359+
self.ssl_cert.as_deref()
360360
}
361361

362-
/// Sets the location of the secret key file used for the client certificate.
362+
/// Sets the client SSL key in PEM format.
363363
///
364364
/// Defaults to `None`.
365-
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
366-
self.ssl_key = Some(PathBuf::from(ssl_key));
365+
pub fn ssl_key(&mut self, ssl_key: &[u8]) -> &mut Config {
366+
self.ssl_key = Some(ssl_key.into());
367367
self
368368
}
369369

370-
/// Gets the location of the secret key file used for the client certificate.
371-
pub fn get_ssl_key(&self) -> Option<PathBuf> {
372-
self.ssl_key.clone()
370+
/// Gets the client SSL key in PEM format.
371+
pub fn get_ssl_key(&self) -> Option<&[u8]> {
372+
self.ssl_key.as_deref()
373373
}
374374

375375
/// Sets the SSL configuration.
@@ -385,17 +385,17 @@ impl Config {
385385
self.ssl_mode
386386
}
387387

388-
/// Sets the location of SSL certificate authority (CA) certificate.
388+
/// Sets the SSL certificate authority (CA) certificate in PEM format.
389389
///
390390
/// Defaults to `None`.
391-
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
392-
self.ssl_root_cert = Some(PathBuf::from(ssl_root_cert));
391+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &[u8]) -> &mut Config {
392+
self.ssl_root_cert = Some(ssl_root_cert.into());
393393
self
394394
}
395395

396-
/// Gets the location of SSL certificate authority (CA) certificate.
397-
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
398-
self.ssl_root_cert.clone()
396+
/// Gets the SSL certificate authority (CA) certificate in PEM format.
397+
pub fn get_ssl_root_cert(&self) -> Option<&[u8]> {
398+
self.ssl_root_cert.as_deref()
399399
}
400400

401401
/// Adds a host to the configuration.
@@ -630,18 +630,22 @@ impl Config {
630630
"application_name" => {
631631
self.application_name(value);
632632
}
633-
"sslcert" => {
634-
if std::fs::metadata(value).is_err() {
633+
"sslcert" => match std::fs::read(value) {
634+
Ok(contents) => {
635+
self.ssl_cert(&contents);
636+
}
637+
Err(_) => {
635638
return Err(Error::config_parse(Box::new(InvalidValue("sslcert"))));
636639
}
637-
self.ssl_cert(value);
638-
}
639-
"sslkey" => {
640-
if std::fs::metadata(value).is_err() {
640+
},
641+
"sslkey" => match std::fs::read(value) {
642+
Ok(contents) => {
643+
self.ssl_key(&contents);
644+
}
645+
Err(_) => {
641646
return Err(Error::config_parse(Box::new(InvalidValue("sslkey"))));
642647
}
643-
self.ssl_key(value);
644-
}
648+
},
645649
"sslmode" => {
646650
let mode = match value {
647651
"disable" => SslMode::Disable,
@@ -653,12 +657,14 @@ impl Config {
653657
};
654658
self.ssl_mode(mode);
655659
}
656-
"sslrootcert" => {
657-
if std::fs::metadata(value).is_err() {
660+
"sslrootcert" => match std::fs::read(value) {
661+
Ok(contents) => {
662+
self.ssl_root_cert(&contents);
663+
}
664+
Err(_) => {
658665
return Err(Error::config_parse(Box::new(InvalidValue("sslrootcert"))));
659666
}
660-
self.ssl_root_cert(value);
661-
}
667+
},
662668
"host" => {
663669
for host in value.split(',') {
664670
self.host(host);

0 commit comments

Comments
 (0)