Skip to content

Commit 857e39a

Browse files
committed
remove use of unwrap from load tls certs/key fns
Signed-off-by: Rajat Jindal <[email protected]>
1 parent 9bef9d2 commit 857e39a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

crates/trigger-http/src/tls.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ pub fn load_key(path: impl AsRef<Path>) -> io::Result<rustls_pki_types::PrivateK
5656
},
5757
)?))
5858
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid private key"))
59-
.map(|keys| keys.unwrap())
59+
.transpose()
60+
.ok_or_else(|| {
61+
io::Error::new(
62+
io::ErrorKind::InvalidInput,
63+
"private key file contains no private keys",
64+
)
65+
})?
6066
}
6167

6268
#[cfg(test)]

crates/trigger/src/runtime_config/client_tls.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::Context;
22
use rustls_pemfile::private_key;
33
use std::io;
4-
use std::io::Cursor;
54
use std::{
65
fs,
76
path::{Path, PathBuf},
@@ -21,13 +20,16 @@ pub struct ClientTlsOpts {
2120
// load_certs parse and return the certs from the provided file
2221
pub fn load_certs(
2322
path: impl AsRef<Path>,
24-
) -> anyhow::Result<Vec<rustls_pki_types::CertificateDer<'static>>> {
25-
let contents = fs::read_to_string(path).expect("Should have been able to read the file");
26-
let mut custom_root_ca_cursor = Cursor::new(contents);
27-
28-
Ok(rustls_pemfile::certs(&mut custom_root_ca_cursor)
29-
.map(|certs| certs.unwrap())
30-
.collect())
23+
) -> io::Result<Vec<rustls_pki_types::CertificateDer<'static>>> {
24+
rustls_pemfile::certs(&mut io::BufReader::new(fs::File::open(path).map_err(
25+
|err| {
26+
io::Error::new(
27+
io::ErrorKind::InvalidInput,
28+
format!("failed to read cert file {:?}", err),
29+
)
30+
},
31+
)?))
32+
.collect::<io::Result<Vec<rustls_pki_types::CertificateDer<'static>>>>()
3133
}
3234

3335
// load_keys parse and return the first private key from the provided file
@@ -38,5 +40,11 @@ pub fn load_key(
3840
fs::File::open(path).context("loading private key")?,
3941
))
4042
.map_err(|_| anyhow::anyhow!("invalid input"))
41-
.map(|keys| keys.unwrap())
43+
.transpose()
44+
.ok_or_else(|| {
45+
io::Error::new(
46+
io::ErrorKind::InvalidInput,
47+
"private key file contains no private keys",
48+
)
49+
})?
4250
}

0 commit comments

Comments
 (0)