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.

60 lines
1.5 KiB

7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use command_runner::CommandRunner;
  5. use symbols::Symbol;
  6. pub struct AcmeAccountKey<'a> {
  7. path: Cow<'a, str>,
  8. command_runner: &'a CommandRunner
  9. }
  10. impl<'a> AcmeAccountKey<'a> {
  11. pub fn new(path: Cow<'a, str>, command_runner: &'a CommandRunner) -> AcmeAccountKey<'a> {
  12. AcmeAccountKey {
  13. path: path,
  14. command_runner: command_runner
  15. }
  16. }
  17. fn get_path(&self) -> String {
  18. self.path.clone().into_owned()
  19. }
  20. fn get_bytes(&self) -> u32 {
  21. 4096
  22. }
  23. }
  24. impl<'a> fmt::Display for AcmeAccountKey<'a> {
  25. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  26. write!(f, "AcmeAccountKey {}", self.path)
  27. }
  28. }
  29. impl<'a> Symbol for AcmeAccountKey<'a> {
  30. fn target_reached(&self) -> Result<bool, Box<Error>> {
  31. let result = self.command_runner.run_with_args("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"]);
  32. match result {
  33. Err(e) => Err(Box::new(e)),
  34. Ok(output) => match output.status.code() {
  35. Some(0) => Ok(output.stdout.starts_with(format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())),
  36. Some(_) => Ok(false),
  37. _ => Err("Didn't work".to_string().into())
  38. }
  39. }
  40. }
  41. fn execute(&self) -> Result<(), Box<Error>> {
  42. let output = self.command_runner.run_with_args("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]);
  43. match output {
  44. Err(e) => Err(Box::new(e)),
  45. Ok(_) => Ok(())
  46. }
  47. }
  48. }
  49. #[cfg(test)]
  50. mod test {
  51. }