A library for writing host-specific, single-binary configuration management and deployment tools
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.

68 lines
1.8 KiB

use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::path::Path;
use command_runner::CommandRunner;
use resources::Resource;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct TlsCsr<'a> {
domain: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> TlsCsr<'a> {
pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> TlsCsr<'a> {
TlsCsr {
domain: domain,
command_runner: command_runner
}
}
fn get_key_path(&self) -> String {
format!("/etc/ssl/private/{}.key", self.domain)
}
fn get_csr_path(&self) -> String {
format!("/etc/ssl/local_certs/{}.csr", self.domain)
}
}
impl<'a> fmt::Display for TlsCsr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TlsCsr {}", self.domain)
}
}
impl<'a> Symbol for TlsCsr<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
if !Path::new(&self.get_csr_path()).exists() {
return Ok(false);
}
let output = try!(self.command_runner.get_stderr("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]));
Ok(output == b"verify OK\n")
}
fn execute(&self) -> Result<(), Box<Error>> {
try!(self.command_runner.run_successfully("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]));
Ok(())
}
fn get_prerequisites(&self) -> Vec<Resource> {
vec![Resource::new("file", self.get_key_path())]
}
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
#[cfg(test)]
mod test {
}