wireman_core/client/
tls.rs

1#![allow(clippy::module_name_repetitions)]
2use crate::error::{Error, Result};
3use tonic::transport::{Certificate, ClientTlsConfig};
4
5/// The TLS config of the grpc client.
6#[derive(Debug, Clone)]
7pub struct TlsConfig(pub(super) ClientTlsConfig);
8
9impl TlsConfig {
10    /// Create a new `TlsConfig` with native certificate.
11    #[must_use]
12    pub fn native() -> Self {
13        Self(ClientTlsConfig::new().with_enabled_roots())
14    }
15
16    /// Create a new `TlsConfig` with a custom certificate.
17    ///
18    /// # Errors
19    ///
20    /// Errors if root certificates cannot be read from path.
21    pub fn custom(cert_path: String) -> Result<Self> {
22        let pem = std::fs::read_to_string(cert_path).map_err(Error::LoadTLSCertificateError)?;
23        let ca = Certificate::from_pem(pem);
24        let tls = ClientTlsConfig::new().ca_certificate(ca);
25
26        Ok(Self(tls))
27    }
28}