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.

70 lines
2.1 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
  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::Symbol;
  8. pub struct SelfSignedTlsCert<'a> {
  9. domain: Cow<'a, str>,
  10. command_runner: &'a CommandRunner
  11. }
  12. impl<'a> SelfSignedTlsCert<'a> {
  13. pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> SelfSignedTlsCert<'a> {
  14. SelfSignedTlsCert {
  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_cert_path(&self) -> String {
  23. format!("/etc/ssl/local_certs/{}.chained.crt", self.domain)
  24. }
  25. }
  26. impl<'a> fmt::Display for SelfSignedTlsCert<'a> {
  27. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  28. write!(f, "SelfSignedTlsCert {}", self.domain)
  29. }
  30. }
  31. const DAYS_IN_SECONDS: u32 = 24*60*60;
  32. impl<'a> Symbol for SelfSignedTlsCert<'a> {
  33. fn target_reached(&self) -> Result<bool, Box<Error>> {
  34. if !Path::new(&self.get_cert_path()).exists() {
  35. return Ok(false);
  36. }
  37. let output = try!(self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]));
  38. println!("{}", output.status.code().unwrap());
  39. match output.status.code() {
  40. Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()),
  41. Some(_) => if output.stdout == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes() {
  42. Ok(false)
  43. } else {
  44. Err("Exit code non-zero, but wrong stdout".to_string().into())
  45. },
  46. _ => Err("Apparently killed by signal".to_string().into())
  47. }
  48. }
  49. fn execute(&self) -> Result<(), Box<Error>> {
  50. self.command_runner.run_successfully("openssl", &["req", "-x509", "-sha256", "-days", "90", "-key", &self.get_key_path(), "-out", &self.get_cert_path(), "-subj", &format!("/CN={}", self.domain)])
  51. }
  52. fn get_prerequisites(&self) -> Vec<Resource> {
  53. vec![Resource::new("file", self.get_key_path())]
  54. }
  55. }
  56. #[cfg(test)]
  57. mod test {
  58. }