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.

57 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 TlsCsr<'a> {
  7. domain: Cow<'a, str>,
  8. command_runner: &'a CommandRunner
  9. }
  10. impl<'a> TlsCsr<'a> {
  11. pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> TlsCsr<'a> {
  12. TlsCsr {
  13. domain: domain,
  14. command_runner: command_runner
  15. }
  16. }
  17. fn get_key_path(&self) -> String {
  18. format!("/etc/ssl/private/{}.key", self.domain)
  19. }
  20. fn get_csr_path(&self) -> String {
  21. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  22. }
  23. }
  24. impl<'a> fmt::Display for TlsCsr<'a> {
  25. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  26. write!(f, "TlsCsr {}", self.domain)
  27. }
  28. }
  29. impl<'a> Symbol for TlsCsr<'a> {
  30. fn target_reached(&self) -> Result<bool, Box<Error>> {
  31. let result = self.command_runner.run_with_args("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]);
  32. match result {
  33. Err(e) => Err(Box::new(e)),
  34. Ok(output) => match output.status.code() {
  35. Some(0) => Ok(output.stderr == "verify OK\n".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 = try!(self.command_runner.run_with_args("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]).map_err(|e| Box::new(e)));
  43. Ok(())
  44. }
  45. }
  46. #[cfg(test)]
  47. mod test {
  48. }