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.

58 lines
1.5 KiB

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