From db329e38b4575f29213a9fdb946925fa8dd0b1e8 Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Wed, 12 Jul 2023 19:21:43 +0900 Subject: [PATCH] refactor: define crypto source trait --- src/cert_file_reader.rs | 26 +++++++++++++++++++++++++- src/certs.rs | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cert_file_reader.rs b/src/cert_file_reader.rs index 53a736e..2a800b3 100644 --- a/src/cert_file_reader.rs +++ b/src/cert_file_reader.rs @@ -1,4 +1,8 @@ -use crate::{certs::CertsAndKeys, log::*}; +use crate::{ + certs::{CertsAndKeys, CryptoSource}, + log::*, +}; +use async_trait::async_trait; use rustls::{Certificate, PrivateKey}; use std::{ fs::File, @@ -6,6 +10,26 @@ use std::{ path::PathBuf, }; +/// Crypto-related file reader implementing certs::CryptoRead trait +pub struct CryptoFileSource { + /// tls settings in file + pub tls_cert_path: PathBuf, + pub tls_cert_key_path: PathBuf, + pub client_ca_cert_path: Option, +} + +#[async_trait] +impl CryptoSource for CryptoFileSource { + type Error = io::Error; + async fn read(&self) -> Result { + read_certs_and_keys( + &self.tls_cert_path, + &self.tls_cert_key_path, + self.client_ca_cert_path.as_ref(), + ) + } +} + /// Read certificates and private keys from file pub(crate) fn read_certs_and_keys( cert_path: &PathBuf, diff --git a/src/certs.rs b/src/certs.rs index 3008900..da51e14 100644 --- a/src/certs.rs +++ b/src/certs.rs @@ -11,7 +11,7 @@ pub struct CertsAndKeys { #[async_trait] // Trait to read certs and keys anywhere from KVS, file, sqlite, etc. -pub trait ReadCerts { +pub trait CryptoSource { type Error; - async fn read_crypto_source(&self) -> Result; + async fn read(&self) -> Result; }