A library for writing host-specific, single-binary configuration management and deployment tools
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.

59 lines
1.8 KiB

6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::path::Path;
  5. use command_runner::CommandRunner;
  6. use resources::Resource;
  7. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  8. pub struct AcmeAccountKey<'a, C: 'a + CommandRunner> {
  9. path: Cow<'a, Path>,
  10. command_runner: &'a C
  11. }
  12. impl<'a, C: CommandRunner> AcmeAccountKey<'a, C> {
  13. pub fn new(path: Cow<'a, Path>, command_runner: &'a C) -> Self {
  14. AcmeAccountKey { path, command_runner }
  15. }
  16. fn get_bytes(&self) -> u32 {
  17. 4096
  18. }
  19. }
  20. impl<'a, C: CommandRunner> fmt::Display for AcmeAccountKey<'a, C> {
  21. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  22. write!(f, "AcmeAccountKey {}", self.path.display())
  23. }
  24. }
  25. impl<'a, C: CommandRunner> Symbol for AcmeAccountKey<'a, C> {
  26. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  27. if !self.path.exists() {
  28. return Ok(false);
  29. }
  30. let stdout = try!(self.command_runner.get_output("openssl", &["rsa".as_ref(), "-in".as_ref(), self.path.as_os_str(), "-noout".as_ref(), "-check".as_ref(), "-text".as_ref()]));
  31. Ok(stdout.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes()))
  32. }
  33. fn execute(&self) -> Result<(), Box<dyn Error>> {
  34. self.command_runner.run_successfully("openssl", &["genrsa".as_ref(), "-out".as_ref(), self.path.as_os_str(), self.get_bytes().to_string().as_ref()])
  35. }
  36. fn get_prerequisites(&self) -> Vec<Resource> {
  37. vec![ Resource::new("dir", self.path.parent().unwrap().to_string_lossy() ) ]
  38. }
  39. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  40. Box::new(SymbolAction::new(runner, self))
  41. }
  42. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
  43. Box::new(OwnedSymbolAction::new(runner, *self))
  44. }
  45. }
  46. #[cfg(test)]
  47. mod test {
  48. }