You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
use std::{fs, path};
|
|
|
|
use crate::domain::tls::{CertificateChain, PrivateKey};
|
|
use crate::error::unexpected::{self, Mappable};
|
|
use crate::util;
|
|
|
|
pub struct DirectFSStore {
|
|
key_file_path: path::PathBuf,
|
|
cert_file_path: path::PathBuf,
|
|
}
|
|
|
|
impl DirectFSStore {
|
|
pub fn new(key_file_path: &path::Path, cert_file_path: &path::Path) -> Self {
|
|
Self {
|
|
key_file_path: key_file_path.into(),
|
|
cert_file_path: cert_file_path.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl super::Store for DirectFSStore {
|
|
fn set_certificate(
|
|
&self,
|
|
_domain: &str,
|
|
key: PrivateKey,
|
|
cert: CertificateChain,
|
|
) -> unexpected::Result<()> {
|
|
fs::write(&self.key_file_path, key.to_string()).map_unexpected_while(|| {
|
|
format!("writing private key to {}", &self.key_file_path.display())
|
|
})?;
|
|
|
|
fs::write(&self.cert_file_path, cert.to_string()).map_unexpected_while(|| {
|
|
format!("writing certificate to {}", &self.cert_file_path.display())
|
|
})?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn get_certificate(
|
|
&self,
|
|
_domain: &str,
|
|
) -> unexpected::Result<Option<(PrivateKey, CertificateChain)>> {
|
|
let key: Option<PrivateKey> =
|
|
util::parse_file(&self.key_file_path).map_unexpected_while(|| {
|
|
format!("reading private key from {}", &self.key_file_path.display())
|
|
})?;
|
|
|
|
let certs: Option<CertificateChain> = util::parse_file(&self.cert_file_path)
|
|
.map_unexpected_while(|| {
|
|
format!(
|
|
"reading certificate from {}",
|
|
&self.cert_file_path.display()
|
|
)
|
|
})?;
|
|
|
|
match (key, certs) {
|
|
(None, None) => Ok(None),
|
|
(Some(key), Some(certs)) => Ok(Some((key, certs))),
|
|
_ =>
|
|
Err(unexpected::Error::from(format!(
|
|
"private key file {} and cert file {} are in inconsistent state, one exists but the other doesn't",
|
|
&self.key_file_path.display(),
|
|
&self.cert_file_path.display(),
|
|
).as_str()))
|
|
}
|
|
}
|
|
}
|
|
|