use std::borrow::Cow; use std::error::Error; use std::fmt; use command_runner::CommandRunner; use symbols::Symbol; 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> { let result = self.command_runner.run_with_args("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]); match result { Err(e) => Err(Box::new(e)), Ok(output) => match output.status.code() { Some(0) => Ok(output.stderr == "verify OK\n".as_bytes()), Some(_) => Ok(false), _ => Err("Didn't work".to_string().into()) } } } fn execute(&self) -> Result<(), Box> { let output = try!(self.command_runner.run_with_args("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]).map_err(|e| Box::new(e))); Ok(()) } } #[cfg(test)] mod test { }