use std::borrow::Cow; use std::error::Error; use std::fmt; use std::path::Path; use command_runner::CommandRunner; use resources::Resource; use symbols::Symbol; pub struct SelfSignedTlsCert<'a> { domain: Cow<'a, str>, command_runner: &'a CommandRunner } impl<'a> SelfSignedTlsCert<'a> { pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> SelfSignedTlsCert<'a> { SelfSignedTlsCert { domain: domain, command_runner: command_runner } } fn get_key_path(&self) -> String { format!("/etc/ssl/private/{}.key", self.domain) } fn get_cert_path(&self) -> String { format!("/etc/ssl/local_certs/{}.chained.crt", self.domain) } } impl<'a> fmt::Display for SelfSignedTlsCert<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "SelfSignedTlsCert {}", self.domain) } } const DAYS_IN_SECONDS: u32 = 24*60*60; impl<'a> Symbol for SelfSignedTlsCert<'a> { fn target_reached(&self) -> Result> { 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()])); println!("{}", output.status.code().unwrap()); match output.status.code() { Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()), Some(_) => if output.stdout == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes() { Ok(false) } else { Err("Exit code non-zero, but wrong stdout".to_string().into()) }, _ => Err("Apparently killed by signal".to_string().into()) } } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully("openssl", &["req", "-x509", "-sha256", "-days", "90", "-key", &self.get_key_path(), "-out", &self.get_cert_path(), "-subj", &format!("/CN={}", self.domain)]) } fn get_prerequisites(&self) -> Vec { vec![Resource::new("file", self.get_key_path())] } } #[cfg(test)] mod test { }