Improve rustls config creation error messages

This commit is contained in:
Joakim Frostegård 2023-03-15 23:57:27 +01:00
parent 45e28cc39a
commit 608957a54b

View file

@ -1,5 +1,7 @@
use std::{fs::File, io::BufReader, path::Path}; use std::{fs::File, io::BufReader, path::Path};
use anyhow::Context;
pub type RustlsConfig = rustls::ServerConfig; pub type RustlsConfig = rustls::ServerConfig;
pub fn create_rustls_config( pub fn create_rustls_config(
@ -7,7 +9,12 @@ pub fn create_rustls_config(
tls_private_key_path: &Path, tls_private_key_path: &Path,
) -> anyhow::Result<RustlsConfig> { ) -> anyhow::Result<RustlsConfig> {
let certs = { 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); let mut f = BufReader::new(f);
rustls_pemfile::certs(&mut f)? rustls_pemfile::certs(&mut f)?
@ -17,7 +24,12 @@ pub fn create_rustls_config(
}; };
let private_key = { 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); let mut f = BufReader::new(f);
rustls_pemfile::pkcs8_private_keys(&mut f)? rustls_pemfile::pkcs8_private_keys(&mut f)?
@ -29,7 +41,8 @@ pub fn create_rustls_config(
let tls_config = rustls::ServerConfig::builder() let tls_config = rustls::ServerConfig::builder()
.with_safe_defaults() .with_safe_defaults()
.with_no_client_auth() .with_no_client_auth()
.with_single_cert(certs, private_key)?; .with_single_cert(certs, private_key)
.with_context(|| "create rustls config")?;
Ok(tls_config) Ok(tls_config)
} }