Skip to content

Commit 3b67266

Browse files
committed
Improved error handling for HTTPS initialization #530
1 parent e65cbfd commit 3b67266

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ List of changes for this repo, including `atomic-cli`, `atomic-server` and `atom
44
By far most changes relate to `atomic-server`, so if not specified, assume the changes are relevant only for the server.
55
Changes to JS assets are not included here, but in [`atomic-data-browser`'s CHANGELOG](https://github.com/atomicdata-dev/atomic-data-browser/blob/main/CHANGELOG.md).
66

7+
## UNRELEASED
8+
9+
- Improved error handling for HTTPS initialization #530
10+
711
## [v0.34.0] - 2022-10-31
812

913
- Add parent parameter to search endpoint which scopes a search to only the descendants of the given resource. #226

server/src/https.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ pub async fn cert_init_server(config: &crate::config::Config) -> AtomicServerRes
3838

3939
let running_server = init_server.bind(&address_clone)?.run();
4040

41-
tx.send(running_server.handle()).unwrap();
41+
tx.send(running_server.handle())
42+
.expect("Error sending handle during HTTPS init");
4243

4344
running_server.await
4445
})
4546
});
4647

47-
let handle = rx.recv().unwrap();
48+
let handle = rx.recv().expect("Error receiving handle during HTTPS init");
4849

4950
let agent = ureq::builder()
5051
.timeout(std::time::Duration::from_secs(2))
@@ -197,27 +198,35 @@ pub fn get_https_config(config: &crate::config::Config) -> Result<rustls::Server
197198
// rustls::NoClientAuth::new()
198199
let cert_file =
199200
&mut BufReader::new(File::open(config.cert_path.clone()).expect("No HTTPS TLS key found."));
200-
let key_file = &mut BufReader::new(File::open(&config.key_path).unwrap());
201+
let key_file =
202+
&mut BufReader::new(File::open(&config.key_path).expect("Could not open config key path"));
201203
let mut cert_chain = Vec::new();
202204

203-
for bytes in certs(cert_file).unwrap() {
205+
for bytes in certs(cert_file)? {
204206
let certificate = rustls::Certificate(bytes);
205207
cert_chain.push(certificate);
206208
}
207-
// let first_cert = cert_chain.first().unwrap().to_owned();
208-
let mut keys = pkcs8_private_keys(key_file).unwrap();
209+
let mut keys = pkcs8_private_keys(key_file)?;
209210
if keys.is_empty() {
210211
panic!("No key found. Consider deleting the `.https` directory and restart to create new keys.")
211212
}
212-
let a = https_config
213+
Ok(https_config
213214
.with_single_cert(cert_chain, rustls::PrivateKey(keys.remove(0)))
214-
.unwrap();
215-
Ok(a)
215+
.expect("Unable to create HTTPS config from certificates"))
216216
}
217217

218218
fn certs_created_at_path(config: &crate::config::Config) -> PathBuf {
219219
// ~/.config/atomic/https
220-
let mut path = config.cert_path.parent().unwrap().to_path_buf();
220+
let mut path = config
221+
.cert_path
222+
.parent()
223+
.unwrap_or_else(|| {
224+
panic!(
225+
"Cannot open parent dit of HTTPS certs {:?}",
226+
config.cert_path
227+
)
228+
})
229+
.to_path_buf();
221230
path.push("certs_created_at");
222231
path
223232
}

0 commit comments

Comments
 (0)