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.

75 lines
2.4 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
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 SelfSignedTlsCert<'a, C: 'a + CommandRunner> {
  9. domain: Cow<'a, str>,
  10. command_runner: &'a C
  11. }
  12. impl<'a, C: CommandRunner> SelfSignedTlsCert<'a, C> {
  13. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  14. SelfSignedTlsCert { domain, command_runner }
  15. }
  16. fn get_key_path(&self) -> String {
  17. format!("/etc/ssl/private/{}.key", self.domain)
  18. }
  19. fn get_cert_path(&self) -> String {
  20. format!("/etc/ssl/local_certs/{}.chained.crt", self.domain)
  21. }
  22. }
  23. impl<'a, C: CommandRunner> fmt::Display for SelfSignedTlsCert<'a, C> {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. write!(f, "SelfSignedTlsCert {}", self.domain)
  26. }
  27. }
  28. const DAYS_IN_SECONDS: u32 = 24*60*60;
  29. impl<'a, C: CommandRunner> Symbol for SelfSignedTlsCert<'a, C> {
  30. fn target_reached(&self) -> Result<bool, Box<Error>> {
  31. if !Path::new(&self.get_cert_path()).exists() {
  32. return Ok(false);
  33. }
  34. 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()]));
  35. println!("{}", output.status.code().unwrap());
  36. match output.status.code() {
  37. Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()),
  38. Some(_) => if output.stdout == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes() {
  39. Ok(false)
  40. } else {
  41. Err("Exit code non-zero, but wrong stdout".to_string().into())
  42. },
  43. _ => Err("Apparently killed by signal".to_string().into())
  44. }
  45. }
  46. fn execute(&self) -> Result<(), Box<Error>> {
  47. 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)])
  48. }
  49. fn get_prerequisites(&self) -> Vec<Resource> {
  50. vec![Resource::new("file", self.get_key_path())]
  51. }
  52. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  53. Box::new(SymbolAction::new(runner, self))
  54. }
  55. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  56. Box::new(OwnedSymbolAction::new(runner, *self))
  57. }
  58. }
  59. #[cfg(test)]
  60. mod test {
  61. }