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.
126 lines
3.0 KiB
126 lines
3.0 KiB
use std::borrow::Cow;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::fs::File as FsFile;
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
|
|
use command_runner::CommandRunner;
|
|
use resources::Resource;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct AcmeCert<'a, C: 'a + CommandRunner> {
|
|
domain: Cow<'a, str>,
|
|
command_runner: &'a C,
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> AcmeCert<'a, C> {
|
|
pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
|
|
AcmeCert {
|
|
domain,
|
|
command_runner,
|
|
}
|
|
}
|
|
|
|
fn get_csr_path(&self) -> String {
|
|
format!("/etc/ssl/local_certs/{}.csr", self.domain)
|
|
}
|
|
|
|
fn get_cert_path(&self) -> String {
|
|
format!("/etc/ssl/local_certs/{}.crt", self.domain)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for AcmeCert<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "AcmeCert {}", self.domain)
|
|
}
|
|
}
|
|
|
|
const DAYS_IN_SECONDS: u32 = 24 * 60 * 60;
|
|
|
|
impl<'a, C: CommandRunner> Symbol for AcmeCert<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
if !Path::new(&self.get_cert_path()).exists() {
|
|
return Ok(false);
|
|
}
|
|
|
|
let output = try!(self.command_runner.run_with_args(
|
|
"openssl",
|
|
&[
|
|
"x509",
|
|
"-in",
|
|
&self.get_cert_path(),
|
|
"-noout",
|
|
"-subject",
|
|
"-checkend",
|
|
&(30 * DAYS_IN_SECONDS).to_string()
|
|
]
|
|
));
|
|
if output.status.success()
|
|
&& output.stdout
|
|
== format!(
|
|
"subject=CN = {}\nCertificate will not expire\n",
|
|
self.domain
|
|
)
|
|
.as_bytes()
|
|
{
|
|
Ok(
|
|
self
|
|
.command_runner
|
|
.run_successfully(
|
|
"openssl",
|
|
&[
|
|
"verify",
|
|
"--untrusted",
|
|
"/home/acme/lets_encrypt_x3_cross_signed.pem",
|
|
&self.get_cert_path(),
|
|
],
|
|
)
|
|
.is_ok(),
|
|
)
|
|
} else if output.status.code() == Some(1)
|
|
&& output.stdout
|
|
== format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes()
|
|
{
|
|
Ok(false)
|
|
} else {
|
|
Err(try!(String::from_utf8(output.stderr)).into())
|
|
}
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
let output = try!(self.command_runner.get_output(
|
|
"acme-tiny",
|
|
&[
|
|
"--account-key",
|
|
"/home/acme/account.key",
|
|
"--csr",
|
|
&self.get_csr_path(),
|
|
"--acme-dir",
|
|
"/home/acme/challenges/"
|
|
]
|
|
));
|
|
let mut file = try!(FsFile::create(self.get_cert_path()));
|
|
try!(file.write_all(&output));
|
|
Ok(())
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![Resource::new("file", self.get_csr_path())]
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
|
|
where
|
|
Self: 'b,
|
|
{
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {}
|