Skip to content

Commit d60d159

Browse files
committed
Remove rustls-pemfile from spin-trigger-http
This functionality is available from the more common rustls-pki-types crate which is already a depedency for other reasons. Signed-off-by: Lann Martin <[email protected]>
1 parent 8f455ee commit d60d159

File tree

3 files changed

+38
-55
lines changed

3 files changed

+38
-55
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/trigger-http/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ http-body-util = { workspace = true }
1616
hyper = { workspace = true }
1717
hyper-util = { workspace = true }
1818
rustls = { workspace = true }
19-
rustls-pemfile = { workspace = true }
2019
rustls-pki-types = { workspace = true }
2120
serde = { workspace = true }
2221
serde_json = { workspace = true }

crates/trigger-http/src/tls.rs

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use rustls_pemfile::private_key;
1+
use anyhow::Context;
2+
use rustls_pki_types::pem::PemObject;
23
use std::{
3-
fs, io,
44
path::{Path, PathBuf},
55
sync::Arc,
66
};
@@ -35,97 +35,81 @@ impl TlsConfig {
3535
// load_certs parse and return the certs from the provided file
3636
fn load_certs(
3737
path: impl AsRef<Path>,
38-
) -> io::Result<Vec<rustls_pki_types::CertificateDer<'static>>> {
39-
rustls_pemfile::certs(&mut io::BufReader::new(fs::File::open(path).map_err(
40-
|err| {
41-
io::Error::new(
42-
io::ErrorKind::InvalidInput,
43-
format!("failed to read cert file {:?}", err),
38+
) -> anyhow::Result<Vec<rustls_pki_types::CertificateDer<'static>>> {
39+
rustls_pki_types::CertificateDer::pem_file_iter(&path)
40+
.and_then(Iterator::collect)
41+
.with_context(|| {
42+
format!(
43+
"failed to load certificate(s) from '{}'",
44+
path.as_ref().display()
4445
)
45-
},
46-
)?))
47-
.collect()
46+
})
4847
}
4948

5049
// parse and return the first private key from the provided file
51-
fn load_key(path: impl AsRef<Path>) -> io::Result<rustls_pki_types::PrivateKeyDer<'static>> {
52-
private_key(&mut io::BufReader::new(fs::File::open(path).map_err(
53-
|err| {
54-
io::Error::new(
55-
io::ErrorKind::InvalidInput,
56-
format!("failed to read private key file {:?}", err),
57-
)
58-
},
59-
)?))
60-
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid private key"))
61-
.transpose()
62-
.ok_or_else(|| {
63-
io::Error::new(
64-
io::ErrorKind::InvalidInput,
65-
"private key file contains no private keys",
50+
fn load_key(path: impl AsRef<Path>) -> anyhow::Result<rustls_pki_types::PrivateKeyDer<'static>> {
51+
rustls_pki_types::PrivateKeyDer::from_pem_file(&path).with_context(|| {
52+
format!(
53+
"failed to load private key from '{}'",
54+
path.as_ref().display()
6655
)
67-
})?
56+
})
6857
}
6958

7059
#[cfg(test)]
7160
mod tests {
61+
use rustls_pki_types::pem;
62+
7263
use super::*;
7364

7465
const TESTDATA_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata");
7566

7667
#[test]
7768
fn test_read_non_existing_cert() {
7869
let path = Path::new(TESTDATA_DIR).join("non-existing-file.pem");
79-
80-
let certs = load_certs(path);
81-
assert!(certs.is_err());
82-
assert_eq!(certs.err().unwrap().to_string(), "failed to read cert file Os { code: 2, kind: NotFound, message: \"No such file or directory\" }");
70+
match load_certs(path).unwrap_err().downcast().unwrap() {
71+
pem::Error::Io(err) => assert_eq!(err.kind(), std::io::ErrorKind::NotFound),
72+
other => panic!("expected Error::Io error got {other:?}"),
73+
}
8374
}
8475

8576
#[test]
8677
fn test_read_invalid_cert() {
8778
let path = Path::new(TESTDATA_DIR).join("invalid-cert.pem");
88-
89-
let certs = load_certs(path);
90-
assert!(certs.is_err());
91-
assert_eq!(
92-
certs.err().unwrap().to_string(),
93-
"section end \"-----END CERTIFICATE-----\" missing"
94-
);
79+
match load_certs(path).unwrap_err().downcast().unwrap() {
80+
pem::Error::MissingSectionEnd { .. } => (),
81+
other => panic!("expected Error::MissingSectionEnd got {other:?}"),
82+
}
9583
}
9684

9785
#[test]
9886
fn test_read_valid_cert() {
9987
let path = Path::new(TESTDATA_DIR).join("valid-cert.pem");
100-
101-
let certs = load_certs(path);
102-
assert!(certs.is_ok());
103-
assert_eq!(certs.unwrap().len(), 2);
88+
let certs = load_certs(path).unwrap();
89+
assert_eq!(certs.len(), 2);
10490
}
10591

10692
#[test]
10793
fn test_read_non_existing_private_key() {
10894
let path = Path::new(TESTDATA_DIR).join("non-existing-file.pem");
109-
110-
let keys = load_key(path);
111-
assert!(keys.is_err());
112-
assert_eq!(keys.err().unwrap().to_string(), "failed to read private key file Os { code: 2, kind: NotFound, message: \"No such file or directory\" }");
95+
match load_key(path).unwrap_err().downcast().unwrap() {
96+
pem::Error::Io(err) => assert_eq!(err.kind(), std::io::ErrorKind::NotFound),
97+
other => panic!("expected Error::Io error got {other:?}"),
98+
}
11399
}
114100

115101
#[test]
116102
fn test_read_invalid_private_key() {
117103
let path = Path::new(TESTDATA_DIR).join("invalid-private-key.pem");
118-
119-
let keys = load_key(path);
120-
assert!(keys.is_err());
121-
assert_eq!(keys.err().unwrap().to_string(), "invalid private key");
104+
match load_key(path).unwrap_err().downcast().unwrap() {
105+
pem::Error::MissingSectionEnd { .. } => (),
106+
other => panic!("expected Error::MissingSectionEnd got {other:?}"),
107+
}
122108
}
123109

124110
#[test]
125111
fn test_read_valid_private_key() {
126112
let path = Path::new(TESTDATA_DIR).join("valid-private-key.pem");
127-
128-
let keys = load_key(path);
129-
assert!(keys.is_ok());
113+
load_key(path).unwrap();
130114
}
131115
}

0 commit comments

Comments
 (0)