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.

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