refactor: define crypto source trait

This commit is contained in:
Jun Kurihara 2023-07-12 19:21:43 +09:00
commit db329e38b4
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
2 changed files with 27 additions and 3 deletions

View file

@ -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<PathBuf>,
}
#[async_trait]
impl CryptoSource for CryptoFileSource {
type Error = io::Error;
async fn read(&self) -> Result<CertsAndKeys, Self::Error> {
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,

View file

@ -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<CertsAndKeys, Self::Error>;
async fn read(&self) -> Result<CertsAndKeys, Self::Error>;
}