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.

65 lines
1.8 KiB

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
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::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 TlsCsr<'a, C: 'a + CommandRunner> {
  9. domain: Cow<'a, str>,
  10. command_runner: &'a C
  11. }
  12. impl<'a, C: CommandRunner> TlsCsr<'a, C> {
  13. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  14. TlsCsr { domain, command_runner }
  15. }
  16. fn get_key_path(&self) -> String {
  17. format!("/etc/ssl/private/{}.key", self.domain)
  18. }
  19. fn get_csr_path(&self) -> String {
  20. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  21. }
  22. }
  23. impl<'a, C: CommandRunner> fmt::Display for TlsCsr<'a, C> {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. write!(f, "TlsCsr {}", self.domain)
  26. }
  27. }
  28. impl<'a, C: CommandRunner> Symbol for TlsCsr<'a, C> {
  29. fn target_reached(&self) -> Result<bool, Box<Error>> {
  30. if !Path::new(&self.get_csr_path()).exists() {
  31. return Ok(false);
  32. }
  33. let output = try!(self.command_runner.get_stderr("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]));
  34. Ok(output == b"verify OK\n")
  35. }
  36. fn execute(&self) -> Result<(), Box<Error>> {
  37. try!(self.command_runner.run_successfully("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]));
  38. Ok(())
  39. }
  40. fn get_prerequisites(&self) -> Vec<Resource> {
  41. vec![Resource::new("file", self.get_key_path())]
  42. }
  43. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  44. Box::new(SymbolAction::new(runner, self))
  45. }
  46. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  47. Box::new(OwnedSymbolAction::new(runner, *self))
  48. }
  49. }
  50. #[cfg(test)]
  51. mod test {
  52. }