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.

82 lines
2.6 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 std::fs::File as FsFile;
  5. use std::io::{self, Write};
  6. use command_runner::CommandRunner;
  7. use symbols::Symbol;
  8. use resources::{FileResource, Resource};
  9. pub struct AcmeCert<'a> {
  10. domain: Cow<'a, str>,
  11. command_runner: &'a CommandRunner
  12. }
  13. impl<'a> AcmeCert<'a> {
  14. pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> AcmeCert<'a> {
  15. AcmeCert {
  16. domain: domain,
  17. command_runner: command_runner
  18. }
  19. }
  20. fn get_csr_path(&self) -> String {
  21. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  22. }
  23. fn get_cert_path(&self) -> String {
  24. format!("/etc/ssl/local_certs/{}.crt", self.domain)
  25. }
  26. }
  27. impl<'a> fmt::Display for AcmeCert<'a> {
  28. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  29. write!(f, "AcmeCert {}", self.domain)
  30. }
  31. }
  32. const DAYS_IN_SECONDS: u32 = 24*60*60;
  33. impl<'a> Symbol for AcmeCert<'a> {
  34. fn target_reached(&self) -> Result<bool, Box<Error>> {
  35. let file = FsFile::open(self.get_cert_path());
  36. // Check first if file exists to support dry-run mode where the acme user is not even created
  37. if let Err(e) = file {
  38. return if e.kind() == io::ErrorKind::NotFound {
  39. Ok(false)
  40. } else {
  41. Err(Box::new(e))
  42. };
  43. }
  44. // FIXME: check who signed it
  45. let result = self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]);
  46. match result {
  47. Err(e) => Err(Box::new(e)),
  48. Ok(output) => match output.status.code() {
  49. Some(0) => if output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes() {
  50. let result = try!(self.command_runner.run_with_args("openssl", &["verify", "--untrusted", "/home/acme/lets_encrypt_x3_cross_signed.pem", &self.get_cert_path()]).map_err(|e| Box::new(e)));
  51. Ok(result.status.code() == Some(0))
  52. } else { Ok(false) },
  53. Some(_) => Ok(false),
  54. _ => Err("Didn't work".to_string().into())
  55. }
  56. }
  57. }
  58. fn execute(&self) -> Result<(), Box<Error>> {
  59. let output = try!(self.command_runner.run_with_args("acme-tiny", &["--account-key", "/home/acme/account.key", "--csr", &self.get_csr_path(), "--acme-dir", "/home/acme/challenges/"]).map_err(|e| Box::new(e)));
  60. let mut file = try!(FsFile::create(self.get_cert_path()));
  61. try!(file.write_all(&output.stdout));
  62. Ok(())
  63. }
  64. fn get_prerequisites(&self) -> Vec<Box<Resource>> {
  65. vec![Box::new(FileResource { path: self.get_csr_path().into() })]
  66. }
  67. }
  68. #[cfg(test)]
  69. mod test {
  70. }