Skip to content

Commit

Permalink
improved source error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Oct 27, 2022
1 parent 33a958d commit 3535f0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion shotover-proxy/src/config/topology.rs
Expand Up @@ -70,7 +70,10 @@ impl Topology {
sources_list.append(
&mut source_config
.get_source(chain, trigger_shutdown_rx.clone())
.await?,
.await
.map_err(|e| {
e.context(format!("Failed to initialize source {source_name}"))
})?,
);
} else {
return Err(anyhow!("Could not find the [{}] chain from \
Expand Down
27 changes: 26 additions & 1 deletion shotover-proxy/src/tls.rs
@@ -1,7 +1,8 @@
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Ok, Result};
use openssl::ssl::Ssl;
use openssl::ssl::{SslAcceptor, SslConnector, SslFiletype, SslMethod};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
use tokio::io::{AsyncRead, AsyncWrite};
Expand All @@ -23,8 +24,26 @@ pub struct TlsAcceptor {
acceptor: Arc<SslAcceptor>,
}

pub fn check_file_field(field_name: &str, file_path: &str) -> Result<()> {
if Path::new(file_path).exists() {
Ok(())
} else {
Err(anyhow!(
"configured {field_name} does not exist {file_path}"
))
}
}

impl TlsAcceptor {
pub fn new(tls_config: TlsAcceptorConfig) -> Result<TlsAcceptor> {
// openssl's errors are really bad so we do our own checks so we can provide reasonable errors
check_file_field(
"certificate_authority_path",
&tls_config.certificate_authority_path,
)?;
check_file_field("private_key_path", &tls_config.private_key_path)?;
check_file_field("certificate_path", &tls_config.certificate_path)?;

let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
builder.set_ca_file(tls_config.certificate_authority_path)?;
builder.set_private_key_file(tls_config.private_key_path, SslFiletype::PEM)?;
Expand Down Expand Up @@ -65,14 +84,20 @@ pub struct TlsConnector {

impl TlsConnector {
pub fn new(tls_config: TlsConnectorConfig) -> Result<TlsConnector> {
check_file_field(
"certificate_authority_path",
&tls_config.certificate_authority_path,
)?;
let mut builder = SslConnector::builder(SslMethod::tls())?;
builder.set_ca_file(tls_config.certificate_authority_path)?;

if let Some(private_key_path) = tls_config.private_key_path {
check_file_field("private_key_path", &private_key_path)?;
builder.set_private_key_file(private_key_path, SslFiletype::PEM)?;
}

if let Some(certificate_path) = tls_config.certificate_path {
check_file_field("certificate_path", &certificate_path)?;
builder.set_certificate_chain_file(certificate_path)?;
}

Expand Down

0 comments on commit 3535f0f

Please sign in to comment.