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.

62 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use command_runner::CommandRunner;
  5. use resources::{Resource, FileResource};
  6. use symbols::Symbol;
  7. pub struct TlsCsr<'a> {
  8. domain: Cow<'a, str>,
  9. command_runner: &'a CommandRunner
  10. }
  11. impl<'a> TlsCsr<'a> {
  12. pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> TlsCsr<'a> {
  13. TlsCsr {
  14. domain: domain,
  15. command_runner: command_runner
  16. }
  17. }
  18. fn get_key_path(&self) -> String {
  19. format!("/etc/ssl/private/{}.key", self.domain)
  20. }
  21. fn get_csr_path(&self) -> String {
  22. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  23. }
  24. }
  25. impl<'a> fmt::Display for TlsCsr<'a> {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "TlsCsr {}", self.domain)
  28. }
  29. }
  30. impl<'a> Symbol for TlsCsr<'a> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. let result = self.command_runner.run_with_args("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]);
  33. match result {
  34. Err(e) => Err(Box::new(e)),
  35. Ok(output) => match output.status.code() {
  36. Some(0) => Ok(output.stderr == "verify OK\n".as_bytes()),
  37. Some(_) => Ok(false),
  38. _ => Err("Didn't work".to_string().into())
  39. }
  40. }
  41. }
  42. fn execute(&self) -> Result<(), Box<Error>> {
  43. 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)));
  44. Ok(())
  45. }
  46. fn get_prerequisites(&self) -> Vec<Box<Resource>> {
  47. vec![Box::new(FileResource { path: self.get_key_path().into() })]
  48. }
  49. }
  50. #[cfg(test)]
  51. mod test {
  52. }