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.

126 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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 resources::Resource;
  9. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  10. pub struct AcmeCert<'a, C: 'a + CommandRunner> {
  11. domain: Cow<'a, str>,
  12. command_runner: &'a C,
  13. }
  14. impl<'a, C: CommandRunner> AcmeCert<'a, C> {
  15. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  16. AcmeCert {
  17. domain,
  18. command_runner,
  19. }
  20. }
  21. fn get_csr_path(&self) -> String {
  22. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  23. }
  24. fn get_cert_path(&self) -> String {
  25. format!("/etc/ssl/local_certs/{}.crt", self.domain)
  26. }
  27. }
  28. impl<'a, C: CommandRunner> fmt::Display for AcmeCert<'a, C> {
  29. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  30. write!(f, "AcmeCert {}", self.domain)
  31. }
  32. }
  33. const DAYS_IN_SECONDS: u32 = 24 * 60 * 60;
  34. impl<'a, C: CommandRunner> Symbol for AcmeCert<'a, C> {
  35. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  36. if !Path::new(&self.get_cert_path()).exists() {
  37. return Ok(false);
  38. }
  39. let output = try!(self.command_runner.run_with_args(
  40. "openssl",
  41. &[
  42. "x509",
  43. "-in",
  44. &self.get_cert_path(),
  45. "-noout",
  46. "-subject",
  47. "-checkend",
  48. &(30 * DAYS_IN_SECONDS).to_string()
  49. ]
  50. ));
  51. if output.status.success()
  52. && output.stdout
  53. == format!(
  54. "subject=CN = {}\nCertificate will not expire\n",
  55. self.domain
  56. )
  57. .as_bytes()
  58. {
  59. Ok(
  60. self
  61. .command_runner
  62. .run_successfully(
  63. "openssl",
  64. &[
  65. "verify",
  66. "--untrusted",
  67. "/home/acme/lets_encrypt_x3_cross_signed.pem",
  68. &self.get_cert_path(),
  69. ],
  70. )
  71. .is_ok(),
  72. )
  73. } else if output.status.code() == Some(1)
  74. && output.stdout
  75. == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes()
  76. {
  77. Ok(false)
  78. } else {
  79. Err(try!(String::from_utf8(output.stderr)).into())
  80. }
  81. }
  82. fn execute(&self) -> Result<(), Box<dyn Error>> {
  83. let output = try!(self.command_runner.get_output(
  84. "acme-tiny",
  85. &[
  86. "--account-key",
  87. "/home/acme/account.key",
  88. "--csr",
  89. &self.get_csr_path(),
  90. "--acme-dir",
  91. "/home/acme/challenges/"
  92. ]
  93. ));
  94. let mut file = try!(FsFile::create(self.get_cert_path()));
  95. try!(file.write_all(&output));
  96. Ok(())
  97. }
  98. fn get_prerequisites(&self) -> Vec<Resource> {
  99. vec![Resource::new("file", self.get_csr_path())]
  100. }
  101. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  102. Box::new(SymbolAction::new(runner, self))
  103. }
  104. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  105. where
  106. Self: 'b,
  107. {
  108. Box::new(OwnedSymbolAction::new(runner, *self))
  109. }
  110. }
  111. #[cfg(test)]
  112. mod test {}