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.
77 lines
2.2 KiB
77 lines
2.2 KiB
use std::{fs, path};
|
|
|
|
use crate::domain::tls::{Certificate, CertificateChain, PrivateKey};
|
|
use crate::error::unexpected::{self, Mappable};
|
|
use crate::util;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct StoredPKeyCert {
|
|
private_key: PrivateKey,
|
|
cert: Vec<Certificate>,
|
|
}
|
|
|
|
pub struct JSONFSStore {
|
|
dir_path: path::PathBuf,
|
|
}
|
|
|
|
impl JSONFSStore {
|
|
pub fn new(dir_path: &path::Path) -> unexpected::Result<Self> {
|
|
fs::create_dir_all(dir_path).or_unexpected()?;
|
|
Ok(Self {
|
|
dir_path: dir_path.into(),
|
|
})
|
|
}
|
|
|
|
fn certificate_path(&self, domain: &str) -> path::PathBuf {
|
|
let mut domain = domain.to_string();
|
|
domain.push_str(".json");
|
|
|
|
self.dir_path.join(domain)
|
|
}
|
|
}
|
|
|
|
impl super::Store for JSONFSStore {
|
|
fn set_certificate(
|
|
&self,
|
|
domain: &str,
|
|
key: PrivateKey,
|
|
certs: CertificateChain,
|
|
) -> Result<(), unexpected::Error> {
|
|
let to_store = StoredPKeyCert {
|
|
private_key: key,
|
|
cert: certs.into(),
|
|
};
|
|
|
|
let path = self.certificate_path(domain);
|
|
{
|
|
let cert_file =
|
|
fs::File::create(path.as_path()).or_unexpected_while("creating file")?;
|
|
serde_json::to_writer(cert_file, &to_store).or_unexpected_while("writing cert to file")
|
|
}
|
|
.map_unexpected_while(|| format!("path is {}", path.display()))
|
|
}
|
|
|
|
fn get_certificate(
|
|
&self,
|
|
domain: &str,
|
|
) -> unexpected::Result<Option<(PrivateKey, CertificateChain)>> {
|
|
let path = self.certificate_path(domain);
|
|
{
|
|
let file = match util::open_file(path.as_path()).or_unexpected_while("opening_file")? {
|
|
Some(file) => file,
|
|
None => return Ok(None),
|
|
};
|
|
|
|
let stored: StoredPKeyCert =
|
|
serde_json::from_reader(file).or_unexpected_while("parsing json")?;
|
|
|
|
unexpected::Result::<Option<(PrivateKey, CertificateChain)>>::Ok(Some((
|
|
stored.private_key,
|
|
stored.cert.into(),
|
|
)))
|
|
}
|
|
.map_unexpected_while(|| format!("path is {}", path.display()))
|
|
}
|
|
}
|
|
|