use std::borrow::Cow; use std::error::Error; use std::ffi::OsStr; use std::fmt; use std::path::PathBuf; use crate::command_runner::CommandRunner; use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct TlsKey<'a, C: 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, command_runner, } } fn get_path(&self) -> PathBuf { ["/etc/ssl/private", &format!("{}.key", self.domain)] .iter() .collect() } fn get_bytes(&self) -> u32 { 4096 } } impl fmt::Display for TlsKey<'_, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "TlsKey {}", self.domain) } } impl Symbol for TlsKey<'_, C> { fn target_reached(&self) -> Result> { if !self.get_path().exists() { return Ok(false); } let stdout = self.command_runner.get_output( "openssl", &[ OsStr::new("rsa"), "-in".as_ref(), self.get_path().as_ref(), "-noout".as_ref(), "-check".as_ref(), "-text".as_ref(), ], )?; Ok(stdout.ends_with("RSA key ok\n".as_bytes())) } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully( "openssl", &[ OsStr::new("genrsa"), "-out".as_ref(), self.get_path().as_ref(), self.get_bytes().to_string().as_ref(), ], ) } fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'b>(self: Box, runner: &'b dyn SymbolRunner) -> Box where Self: 'b, { Box::new(OwnedSymbolAction::new(runner, *self)) } } #[cfg(test)] mod test {}