Skip to content

Commit

Permalink
RUST-791 use PathBuf instead of String for paths (C-CUSTOM-TYPE)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfreed committed May 11, 2021
1 parent ec07245 commit 9cb7dc6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
27 changes: 16 additions & 11 deletions src/client/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
fs::File,
hash::{Hash, Hasher},
io::{BufReader, Seek, SeekFrom},
path::PathBuf,
str::FromStr,
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -564,13 +565,13 @@ pub struct TlsOptions {
/// The path to the CA file that the [`Client`](../struct.Client.html) should use for TLS. If
/// none is specified, then the driver will use the Mozilla root certificates from the
/// `webpki-roots` crate.
pub ca_file_path: Option<String>,
pub ca_file_path: Option<PathBuf>,

/// The path to the certificate file that the [`Client`](../struct.Client.html) should present
/// to the server to verify its identify. If none is specified, then the
/// [`Client`](../struct.Client.html) will not attempt to verify its identity to the
/// server.
pub cert_key_file_path: Option<String>,
pub cert_key_file_path: Option<PathBuf>,
}

struct NoCertVerifier {}
Expand Down Expand Up @@ -603,7 +604,10 @@ impl TlsOptions {
store
.add_pem_file(&mut BufReader::new(File::open(&path)?))
.map_err(|_| ErrorKind::InvalidTlsConfig {
message: format!("Unable to parse PEM-encoded root certificate from {}", path),
message: format!(
"Unable to parse PEM-encoded root certificate from {}",
path.display()
),
})?;
} else {
store.add_server_trust_anchors(&TLS_SERVER_ROOTS);
Expand All @@ -619,7 +623,7 @@ impl TlsOptions {
return Err(ErrorKind::InvalidTlsConfig {
message: format!(
"Unable to parse PEM-encoded client certificate from {}",
path
path.display()
),
}
.into())
Expand All @@ -631,7 +635,10 @@ impl TlsOptions {
Ok(key) => key,
Err(()) => {
return Err(ErrorKind::InvalidTlsConfig {
message: format!("Unable to parse PEM-encoded RSA key from {}", path),
message: format!(
"Unable to parse PEM-encoded RSA key from {}",
path.display()
),
}
.into())
}
Expand Down Expand Up @@ -1623,13 +1630,11 @@ impl ClientOptionsParser {
.into());
}
Some(Tls::Enabled(ref mut options)) => {
options.ca_file_path = Some(value.to_string());
options.ca_file_path = Some(value.into());
}
None => {
self.tls = Some(Tls::Enabled(
TlsOptions::builder()
.ca_file_path(value.to_string())
.build(),
TlsOptions::builder().ca_file_path(value.into()).build(),
))
}
},
Expand All @@ -1641,12 +1646,12 @@ impl ClientOptionsParser {
.into());
}
Some(Tls::Enabled(ref mut options)) => {
options.cert_key_file_path = Some(value.to_string());
options.cert_key_file_path = Some(value.into());
}
None => {
self.tls = Some(Tls::Enabled(
TlsOptions::builder()
.cert_key_file_path(value.to_string())
.cert_key_file_path(value.into())
.build(),
))
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/options/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ fn document_from_client_options(mut options: ClientOptions) -> Document {

if let Some(s) = ca_file_path {
doc.insert("tls", true);
doc.insert("tlscafile", s);
doc.insert("tlscafile", s.to_str().unwrap());
}

if let Some(s) = cert_key_file_path {
doc.insert("tlscertificatekeyfile", s);
doc.insert("tlscertificatekeyfile", s.to_str().unwrap());
}

if let Some(b) = allow_invalid_certificates {
Expand Down
20 changes: 18 additions & 2 deletions src/cmap/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,26 @@ impl Matchable for TlsOptions {
fn content_matches(&self, expected: &TlsOptions) -> bool {
self.allow_invalid_certificates
.matches(&expected.allow_invalid_certificates)
&& self.ca_file_path.matches(&expected.ca_file_path)
&& self
.ca_file_path
.as_ref()
.map(|pb| pb.display().to_string())
.matches(
&expected
.ca_file_path
.as_ref()
.map(|pb| pb.display().to_string()),
)
&& self
.cert_key_file_path
.matches(&expected.cert_key_file_path)
.as_ref()
.map(|pb| pb.display().to_string())
.matches(
&expected
.cert_key_file_path
.as_ref()
.map(|pb| pb.display().to_string()),
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async fn auth_test_uri(
uri.push_str("&tlsCAFile=");
uri.push_str(
&percent_encoding::utf8_percent_encode(
ca_file_path,
ca_file_path.to_str().unwrap(),
percent_encoding::NON_ALPHANUMERIC,
)
.to_string(),
Expand All @@ -371,7 +371,7 @@ async fn auth_test_uri(
uri.push_str("&tlsCertificateKeyFile=");
uri.push_str(
&percent_encoding::utf8_percent_encode(
cert_key_file_path,
cert_key_file_path.to_str().unwrap(),
percent_encoding::NON_ALPHANUMERIC,
)
.to_string(),
Expand Down

0 comments on commit 9cb7dc6

Please sign in to comment.