use std::borrow::Cow; use std::error::Error; use std::fmt; use std::path::Path; use command_runner::CommandRunner; use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct TlsKey<'a, C: 'a + CommandRunner> { domain: Cow<'a, str>, command_runner: &'a C } impl<'a, C: CommandRunner> TlsKey<'a, C> { pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self { TlsKey { domain: domain, command_runner: command_runner } } fn get_path(&self) -> String { format!("/etc/ssl/private/{}.key", self.domain) } fn get_bytes(&self) -> u32 { 4096 } } impl<'a, C: CommandRunner> fmt::Display for TlsKey<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "TlsKey {}", self.domain) } } impl<'a, C: CommandRunner> Symbol for TlsKey<'a, C> { fn target_reached(&self) -> Result> { if !Path::new(&self.get_path()).exists() { return Ok(false); } let output = try!(self.command_runner.get_output("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"])); Ok(output.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())) } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]) } fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'b>(self: Box, runner: &'b SymbolRunner) -> Box where Self: 'b { Box::new(OwnedSymbolAction::new(runner, *self)) } } #[cfg(test)] mod test { }