Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved source/tls error reporting #888

Merged
merged 4 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
7 changes: 5 additions & 2 deletions shotover-proxy/src/server.rs
Expand Up @@ -266,8 +266,11 @@ impl<C: Codec + 'static> TcpCodecListener<C> {
{
Ok(_) => info!("source {} was shutdown", self.source_name),
Err(e) => error!(
"source {} encountered an error when flushing the chain for shutdown: {}",
self.source_name, e
"{:?}",
e.context(format!(
"source {} encountered an error when flushing the chain for shutdown",
self.source_name,
))
),
}
}
Expand Down
25 changes: 25 additions & 0 deletions shotover-proxy/src/tls.rs
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, 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