|
1 |
| -use rustls_pemfile::private_key; |
| 1 | +use anyhow::Context; |
| 2 | +use rustls_pki_types::pem::PemObject; |
2 | 3 | use std::{
|
3 |
| - fs, io, |
4 | 4 | path::{Path, PathBuf},
|
5 | 5 | sync::Arc,
|
6 | 6 | };
|
@@ -35,97 +35,81 @@ impl TlsConfig {
|
35 | 35 | // load_certs parse and return the certs from the provided file
|
36 | 36 | fn load_certs(
|
37 | 37 | 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() |
44 | 45 | )
|
45 |
| - }, |
46 |
| - )?)) |
47 |
| - .collect() |
| 46 | + }) |
48 | 47 | }
|
49 | 48 |
|
50 | 49 | // 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() |
66 | 55 | )
|
67 |
| - })? |
| 56 | + }) |
68 | 57 | }
|
69 | 58 |
|
70 | 59 | #[cfg(test)]
|
71 | 60 | mod tests {
|
| 61 | + use rustls_pki_types::pem; |
| 62 | + |
72 | 63 | use super::*;
|
73 | 64 |
|
74 | 65 | const TESTDATA_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata");
|
75 | 66 |
|
76 | 67 | #[test]
|
77 | 68 | fn test_read_non_existing_cert() {
|
78 | 69 | 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 | + } |
83 | 74 | }
|
84 | 75 |
|
85 | 76 | #[test]
|
86 | 77 | fn test_read_invalid_cert() {
|
87 | 78 | 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 | + } |
95 | 83 | }
|
96 | 84 |
|
97 | 85 | #[test]
|
98 | 86 | fn test_read_valid_cert() {
|
99 | 87 | 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); |
104 | 90 | }
|
105 | 91 |
|
106 | 92 | #[test]
|
107 | 93 | fn test_read_non_existing_private_key() {
|
108 | 94 | 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 | + } |
113 | 99 | }
|
114 | 100 |
|
115 | 101 | #[test]
|
116 | 102 | fn test_read_invalid_private_key() {
|
117 | 103 | 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 | + } |
122 | 108 | }
|
123 | 109 |
|
124 | 110 | #[test]
|
125 | 111 | fn test_read_valid_private_key() {
|
126 | 112 | 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(); |
130 | 114 | }
|
131 | 115 | }
|
0 commit comments