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.

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