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.5 KiB

7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::fs::File as FsFile;
  5. use std::io::Write;
  6. use std::path::Path;
  7. use command_runner::CommandRunner;
  8. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  9. use resources::Resource;
  10. pub struct AcmeCertChain<'a, C: 'a + CommandRunner> {
  11. domain: Cow<'a, str>,
  12. command_runner: &'a C
  13. }
  14. impl<'a, C: CommandRunner> AcmeCertChain<'a, C> {
  15. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  16. AcmeCertChain { domain, command_runner }
  17. }
  18. fn get_single_cert_path(&self) -> String {
  19. format!("/etc/ssl/local_certs/{}.crt", self.domain)
  20. }
  21. fn get_cert_chain_path(&self) -> String {
  22. format!("/etc/ssl/local_certs/{}.chained.crt", self.domain)
  23. }
  24. }
  25. impl<'a, C: CommandRunner> fmt::Display for AcmeCertChain<'a, C> {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "AcmeCertChain {}", self.domain)
  28. }
  29. }
  30. const DAYS_IN_SECONDS: u32 = 24*60*60;
  31. impl<'a, C: CommandRunner> Symbol for AcmeCertChain<'a, C> {
  32. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  33. if !Path::new(&self.get_cert_chain_path()).exists() {
  34. return Ok(false);
  35. }
  36. let stdout = try!(self.command_runner.get_output("openssl", &["x509", "-in", &self.get_cert_chain_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]));
  37. if stdout != format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes() {
  38. return Ok(false);
  39. }
  40. // FIXME: From my understanding, the -untrusted *.pem parameter shouldn't be necessary, but is necessary with openssl 1.1.0f-3
  41. Ok(self.command_runner.run_successfully("openssl", &["verify", "-untrusted", "/home/acme/lets_encrypt_x3_cross_signed.pem", &self.get_cert_chain_path()]).is_ok())
  42. }
  43. fn execute(&self) -> Result<(), Box<dyn Error>> {
  44. let output = try!(self.command_runner.get_output("cat", &[self.get_single_cert_path().as_ref(), "/home/acme/lets_encrypt_x3_cross_signed.pem"]));
  45. let mut file = try!(FsFile::create(self.get_cert_chain_path()));
  46. try!(file.write_all(&output));
  47. Ok(())
  48. }
  49. fn get_prerequisites(&self) -> Vec<Resource> {
  50. vec![ Resource::new("file", self.get_single_cert_path()) ]
  51. }
  52. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  53. Box::new(SymbolAction::new(runner, self))
  54. }
  55. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
  56. Box::new(OwnedSymbolAction::new(runner, *self))
  57. }
  58. }
  59. #[cfg(test)]
  60. mod test {
  61. }