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.
66 lines
1.9 KiB
66 lines
1.9 KiB
use std::borrow::{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 AcmeAccountKey<'a, C: 'a + CommandRunner> {
|
|
path: Cow<'a, Path>,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> AcmeAccountKey<'a, C> {
|
|
pub fn new(path: Cow<'a, Path>, command_runner: &'a C) -> Self {
|
|
AcmeAccountKey {
|
|
path: path,
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
|
|
fn get_path(&self) -> &Path {
|
|
self.path.borrow()
|
|
}
|
|
|
|
fn get_bytes(&self) -> u32 {
|
|
4096
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for AcmeAccountKey<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "AcmeAccountKey {}", self.get_path().display())
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> Symbol for AcmeAccountKey<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if !self.get_path().exists() {
|
|
return Ok(false);
|
|
}
|
|
let stdout = try!(self.command_runner.get_output("openssl", &["rsa".as_ref(), "-in".as_ref(), self.get_path().as_os_str(), "-noout".as_ref(), "-check".as_ref(), "-text".as_ref()]));
|
|
Ok(stdout.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes()))
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
self.command_runner.run_successfully("openssl", &["genrsa".as_ref(), "-out".as_ref(), self.get_path().as_os_str(), self.get_bytes().to_string().as_ref()])
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![ Resource::new("dir", self.get_path().parent().unwrap().to_string_lossy() ) ]
|
|
}
|
|
|
|
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 {
|
|
}
|