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.

78 lines
2.5 KiB

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