From 608957a54bd4438eb1743b57d7119829a4bfe498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 15 Mar 2023 23:57:27 +0100 Subject: [PATCH] Improve rustls config creation error messages --- aquatic_common/src/rustls_config.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/aquatic_common/src/rustls_config.rs b/aquatic_common/src/rustls_config.rs index b852ec1..e6df59d 100644 --- a/aquatic_common/src/rustls_config.rs +++ b/aquatic_common/src/rustls_config.rs @@ -1,5 +1,7 @@ use std::{fs::File, io::BufReader, path::Path}; +use anyhow::Context; + pub type RustlsConfig = rustls::ServerConfig; pub fn create_rustls_config( @@ -7,7 +9,12 @@ pub fn create_rustls_config( tls_private_key_path: &Path, ) -> anyhow::Result { let certs = { - let f = File::open(tls_certificate_path)?; + let f = File::open(tls_certificate_path).with_context(|| { + format!( + "open tls certificate file at {}", + tls_certificate_path.to_string_lossy() + ) + })?; let mut f = BufReader::new(f); rustls_pemfile::certs(&mut f)? @@ -17,7 +24,12 @@ pub fn create_rustls_config( }; let private_key = { - let f = File::open(tls_private_key_path)?; + let f = File::open(tls_private_key_path).with_context(|| { + format!( + "open tls private key file at {}", + tls_private_key_path.to_string_lossy() + ) + })?; let mut f = BufReader::new(f); rustls_pemfile::pkcs8_private_keys(&mut f)? @@ -29,7 +41,8 @@ pub fn create_rustls_config( let tls_config = rustls::ServerConfig::builder() .with_safe_defaults() .with_no_client_auth() - .with_single_cert(certs, private_key)?; + .with_single_cert(certs, private_key) + .with_context(|| "create rustls config")?; Ok(tls_config) }