Skip to content

Commit 686ed85

Browse files
committed
Change SSL configuration to PEM bytes rather than files
1 parent 87257b2 commit 686ed85

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
@@ -9,7 +9,7 @@ use std::path::Path;
99
use std::str::FromStr;
1010
use std::sync::Arc;
1111
use std::time::Duration;
12-
use std::{fmt, path::PathBuf};
12+
use std::fmt;
1313
use tokio::runtime;
1414
#[doc(inline)]
1515
pub use tokio_postgres::config::{ChannelBinding, Host, SslMode, TargetSessionAttrs};
@@ -187,29 +187,29 @@ impl Config {
187187
self.config.get_application_name()
188188
}
189189

190-
/// Sets the location of the client SSL certificate file.
190+
/// Sets the client SSL certificate in PEM format.
191191
///
192192
/// Defaults to `None`.
193-
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
193+
pub fn ssl_cert(&mut self, ssl_cert: &[u8]) -> &mut Config {
194194
self.config.ssl_cert(ssl_cert);
195195
self
196196
}
197197

198-
/// Gets the location of the client SSL certificate file.
199-
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
198+
/// Gets the location of the client SSL certificate in PEM format.
199+
pub fn get_ssl_cert(&self) -> Option<&[u8]> {
200200
self.config.get_ssl_cert()
201201
}
202202

203-
/// Sets the location of the secret key file used for the client certificate.
203+
/// Sets the client SSL key in PEM format.
204204
///
205205
/// Defaults to `None`.
206-
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
206+
pub fn ssl_key(&mut self, ssl_key: &[u8]) -> &mut Config {
207207
self.config.ssl_key(ssl_key);
208208
self
209209
}
210210

211-
/// Gets the location of the secret key file used for the client certificate.
212-
pub fn get_ssl_key(&self) -> Option<PathBuf> {
211+
/// Gets the client SSL key in PEM format.
212+
pub fn get_ssl_key(&self) -> Option<&[u8]> {
213213
self.config.get_ssl_key()
214214
}
215215

@@ -226,16 +226,16 @@ impl Config {
226226
self.config.get_ssl_mode()
227227
}
228228

229-
/// Sets the location of SSL certificate authority (CA) certificate.
229+
/// Sets the SSL certificate authority (CA) certificate in PEM format.
230230
///
231231
/// Defaults to `None`.
232-
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
232+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &[u8]) -> &mut Config {
233233
self.config.ssl_root_cert(ssl_root_cert);
234234
self
235235
}
236236

237-
/// Gets the location of SSL certificate authority (CA) certificate.
238-
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
237+
/// Gets the SSL certificate authority (CA) certificate in PEM format.
238+
pub fn get_ssl_root_cert(&self) -> Option<&[u8]> {
239239
self.config.get_ssl_root_cert()
240240
}
241241

tokio-postgres/src/config.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ pub struct Config {
171171
pub(crate) dbname: Option<String>,
172172
pub(crate) options: Option<String>,
173173
pub(crate) application_name: Option<String>,
174-
pub(crate) ssl_cert: Option<PathBuf>,
175-
pub(crate) ssl_key: Option<PathBuf>,
174+
pub(crate) ssl_cert: Option<Vec<u8>>,
175+
pub(crate) ssl_key: Option<Vec<u8>>,
176176
pub(crate) ssl_mode: SslMode,
177-
pub(crate) ssl_root_cert: Option<PathBuf>,
177+
pub(crate) ssl_root_cert: Option<Vec<u8>>,
178178
pub(crate) host: Vec<Host>,
179179
pub(crate) port: Vec<u16>,
180180
pub(crate) connect_timeout: Option<Duration>,
@@ -282,30 +282,30 @@ impl Config {
282282
self.application_name.as_deref()
283283
}
284284

285-
/// Sets the location of the client SSL certificate file.
285+
/// Sets the client SSL certificate in PEM format.
286286
///
287287
/// Defaults to `None`.
288-
pub fn ssl_cert(&mut self, ssl_cert: &str) -> &mut Config {
289-
self.ssl_cert = Some(PathBuf::from(ssl_cert));
288+
pub fn ssl_cert(&mut self, ssl_cert: &[u8]) -> &mut Config {
289+
self.ssl_cert = Some(ssl_cert.into());
290290
self
291291
}
292292

293-
/// Gets the location of the client SSL certificate file.
294-
pub fn get_ssl_cert(&self) -> Option<PathBuf> {
295-
self.ssl_cert.clone()
293+
/// Gets the location of the client SSL certificate in PEM format.
294+
pub fn get_ssl_cert(&self) -> Option<&[u8]> {
295+
self.ssl_cert.as_deref()
296296
}
297297

298-
/// Sets the location of the secret key file used for the client certificate.
298+
/// Sets the client SSL key in PEM format.
299299
///
300300
/// Defaults to `None`.
301-
pub fn ssl_key(&mut self, ssl_key: &str) -> &mut Config {
302-
self.ssl_key = Some(PathBuf::from(ssl_key));
301+
pub fn ssl_key(&mut self, ssl_key: &[u8]) -> &mut Config {
302+
self.ssl_key = Some(ssl_key.into());
303303
self
304304
}
305305

306-
/// Gets the location of the secret key file used for the client certificate.
307-
pub fn get_ssl_key(&self) -> Option<PathBuf> {
308-
self.ssl_key.clone()
306+
/// Gets the client SSL key in PEM format.
307+
pub fn get_ssl_key(&self) -> Option<&[u8]> {
308+
self.ssl_key.as_deref()
309309
}
310310

311311
/// Sets the SSL configuration.
@@ -321,17 +321,17 @@ impl Config {
321321
self.ssl_mode
322322
}
323323

324-
/// Sets the location of SSL certificate authority (CA) certificate.
324+
/// Sets the SSL certificate authority (CA) certificate in PEM format.
325325
///
326326
/// Defaults to `None`.
327-
pub fn ssl_root_cert(&mut self, ssl_root_cert: &str) -> &mut Config {
328-
self.ssl_root_cert = Some(PathBuf::from(ssl_root_cert));
327+
pub fn ssl_root_cert(&mut self, ssl_root_cert: &[u8]) -> &mut Config {
328+
self.ssl_root_cert = Some(ssl_root_cert.into());
329329
self
330330
}
331331

332-
/// Gets the location of SSL certificate authority (CA) certificate.
333-
pub fn get_ssl_root_cert(&self) -> Option<PathBuf> {
334-
self.ssl_root_cert.clone()
332+
/// Gets the SSL certificate authority (CA) certificate in PEM format.
333+
pub fn get_ssl_root_cert(&self) -> Option<&[u8]> {
334+
self.ssl_root_cert.as_deref()
335335
}
336336

337337
/// Adds a host to the configuration.
@@ -482,18 +482,22 @@ impl Config {
482482
"application_name" => {
483483
self.application_name(value);
484484
}
485-
"sslcert" => {
486-
if std::fs::metadata(&value).is_err() {
485+
"sslcert" => match std::fs::read(&value) {
486+
Ok(contents) => {
487+
self.ssl_cert(&contents);
488+
}
489+
Err(_) => {
487490
return Err(Error::config_parse(Box::new(InvalidValue("sslcert"))));
488491
}
489-
self.ssl_cert(value);
490-
}
491-
"sslkey" => {
492-
if std::fs::metadata(&value).is_err() {
492+
},
493+
"sslkey" => match std::fs::read(&value) {
494+
Ok(contents) => {
495+
self.ssl_key(&contents);
496+
}
497+
Err(_) => {
493498
return Err(Error::config_parse(Box::new(InvalidValue("sslkey"))));
494499
}
495-
self.ssl_key(value);
496-
}
500+
},
497501
"sslmode" => {
498502
let mode = match value {
499503
"disable" => SslMode::Disable,
@@ -505,12 +509,14 @@ impl Config {
505509
};
506510
self.ssl_mode(mode);
507511
}
508-
"sslrootcert" => {
509-
if std::fs::metadata(&value).is_err() {
512+
"sslrootcert" => match std::fs::read(&value) {
513+
Ok(contents) => {
514+
self.ssl_root_cert(&contents);
515+
}
516+
Err(_) => {
510517
return Err(Error::config_parse(Box::new(InvalidValue("sslrootcert"))));
511518
}
512-
self.ssl_root_cert(value);
513-
}
519+
},
514520
"host" => {
515521
for host in value.split(',') {
516522
self.host(host);

0 commit comments

Comments
 (0)