From 16896bd7fae9ce13431b27f0283dd2e0187ddbad Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 11 Feb 2025 10:24:13 -0500 Subject: [PATCH] Change the interfaces to take Options, to simplify users --- src/builder.rs | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 92a19ba..e5c53e0 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::time::Duration; use reqwest::Certificate; @@ -16,7 +17,7 @@ pub struct Builder { facts: Option, groups: Option, proxy: Option, - ssl_cert: Option, + ssl_cert_path: Option, timeout: Option, } @@ -30,28 +31,28 @@ impl Builder { facts: None, groups: None, proxy: None, - ssl_cert: None, + ssl_cert_path: None, timeout: None, } } - pub fn set_distinct_id(mut self, distinct_id: impl Into) -> Self { - self.distinct_id = Some(distinct_id.into()); + pub fn set_distinct_id(mut self, distinct_id: Option) -> Self { + self.distinct_id = distinct_id; self } - pub fn set_device_id(mut self, device_id: impl Into) -> Self { - self.device_id = Some(device_id.into()); + pub fn set_device_id(mut self, device_id: Option) -> Self { + self.device_id = device_id; self } - pub fn set_facts(mut self, facts: Map) -> Self { - self.facts = Some(facts); + pub fn set_facts(mut self, facts: Option) -> Self { + self.facts = facts; self } - pub fn set_groups(mut self, groups: Map) -> Self { - self.groups = Some(groups); + pub fn set_groups(mut self, groups: Option) -> Self { + self.groups = groups; self } @@ -66,8 +67,8 @@ impl Builder { self } - pub fn set_endpoint(mut self, endpoint: impl Into) -> Self { - self.endpoint = Some(endpoint.into()); + pub fn set_endpoint(mut self, endpoint: Option) -> Self { + self.endpoint = endpoint; self } @@ -87,7 +88,7 @@ impl Builder { /// let cli = Cli { no_telemetry: false, }; /// /// let (recorder, worker) = builder!() - /// .set_enable_reporting(!cli.no_telemetry) + /// .set_enable_reporting(cli.no_telemetry) /// .build() /// .await /// .unwrap(); @@ -98,22 +99,21 @@ impl Builder { self } - pub fn set_timeout(mut self, duration: impl Into) -> Self { - self.timeout = Some(duration.into()); + pub fn set_timeout(mut self, duration: Option) -> Self { + self.timeout = duration; self } - #[tracing::instrument(skip(self))] - pub async fn try_set_ssl_cert_file( - &mut self, - ssl_cert_file: impl AsRef + std::fmt::Debug, - ) -> Result<&mut Self, TransportsError> { - self.ssl_cert = Some(read_cert_file(&ssl_cert_file).await?); - Ok(self) + /// Set the path to a certificate bundle. + /// + /// Note: certificate paths that are invalid or can't be parsed are ignored. + pub async fn set_ssl_cert_path(mut self, ssl_cert_path: Option) -> Self { + self.ssl_cert_path = ssl_cert_path; + self } - pub fn set_proxy(mut self, proxy: Url) -> Self { - self.proxy = Some(proxy); + pub fn set_proxy(mut self, proxy: Option) -> Self { + self.proxy = proxy; self } @@ -129,12 +129,23 @@ impl Builder { snapshotter: S, ) -> Result<(Recorder, Worker), TransportsError> { let transport = if self.enable_reporting { + let certs = if let Some(path) = self.ssl_cert_path.take() { + read_cert_file(&path) + .await + .inspect_err(|e| { + tracing::warn!(?path, %e, "Failed to parse the TLS certificates"); + }) + .ok() + } else { + None + }; + crate::transport::Transports::try_new( self.endpoint.take(), self.timeout .take() .unwrap_or_else(|| Duration::from_secs(3)), - self.ssl_cert.take(), + certs, self.proxy.take(), ) .await?