Skip to content

Commit

Permalink
refactor to remove expect
Browse files Browse the repository at this point in the history
  • Loading branch information
anil-db committed Oct 3, 2024
1 parent b8e49db commit ed0cf4f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/vector-core/src/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ fn tls_connector(settings: &MaybeTlsSettings) -> Result<ConnectConfiguration> {
.context(TlsBuildConnectorSnafu)?;
let tls_setting = settings.tls().cloned();
if let Some(tls_setting) = &tls_setting {
tls_setting.apply_connect_configuration(&mut configure)?;
tls_setting
.apply_connect_configuration(&mut configure)
.context(SetSniSnafu)?;
}
Ok(configure)
}
11 changes: 7 additions & 4 deletions lib/vector-core/src/tls/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use super::{
AddCertToStoreSnafu, AddExtraChainCertSnafu, CaStackPushSnafu, DerExportSnafu,
EncodeAlpnProtocolsSnafu, FileOpenFailedSnafu, FileReadFailedSnafu, MaybeTls, NewCaStackSnafu,
NewStoreBuilderSnafu, ParsePkcs12Snafu, Pkcs12Snafu, PrivateKeyParseSnafu, Result,
SetAlpnProtocolsSnafu, SetCertificateSnafu, SetPrivateKeySnafu, SetSniSnafu,
SetVerifyCertSnafu, TlsError, TlsIdentitySnafu, X509ParseSnafu,
SetAlpnProtocolsSnafu, SetCertificateSnafu, SetPrivateKeySnafu, SetVerifyCertSnafu, TlsError,
TlsIdentitySnafu, X509ParseSnafu,
};

pub const PEM_START_MARKER: &str = "-----BEGIN ";
Expand Down Expand Up @@ -343,12 +343,15 @@ impl TlsSettings {
Ok(())
}

pub fn apply_connect_configuration(&self, connection: &mut ConnectConfiguration) -> Result<()> {
pub fn apply_connect_configuration(
&self,
connection: &mut ConnectConfiguration,
) -> std::result::Result<(), openssl::error::ErrorStack> {
connection.set_verify_hostname(self.verify_hostname);
if let Some(server_name) = &self.server_name {
// Prevent native TLS lib from inferring default SNI using domain name from url.
connection.set_use_server_name_indication(false);
connection.set_hostname(server_name).context(SetSniSnafu)?;
connection.set_hostname(server_name)?;
}
Ok(())
}
Expand Down
16 changes: 3 additions & 13 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use hyper_openssl::HttpsConnector;
use hyper_proxy::ProxyConnector;
use rand::Rng;
use serde_with::serde_as;
use snafu::Error;
use snafu::{ResultExt, Snafu};
use std::{
fmt,
Expand Down Expand Up @@ -205,19 +204,10 @@ pub fn build_tls_connector(
let settings = tls_settings.tls().cloned();
https.set_callback(move |c, _uri| {
if let Some(settings) = &settings {
match settings.apply_connect_configuration(c) {
Ok(()) => (),
Err(error) => {
error
.source()
.expect("was expecting to have a source in tlsError variant returned from apply_connect_configuration(). See SetSniSnafu.")
.downcast_ref::<openssl::error::ErrorStack>()
.expect("was expecting the source error to be of type openssl::error::ErrorStack from apply_connect_configuration(). See SetSniSnafu.");
}
}
settings.apply_connect_configuration(c)
} else {
Ok(())
}

Ok(())
});
Ok(https)
}
Expand Down

0 comments on commit ed0cf4f

Please sign in to comment.