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.

116 lines
2.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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
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 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 {
  17. domain,
  18. 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, C: CommandRunner> fmt::Display for AcmeCertChain<'a, C> {
  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, C: CommandRunner> Symbol for AcmeCertChain<'a, C> {
  35. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  36. if !Path::new(&self.get_cert_chain_path()).exists() {
  37. return Ok(false);
  38. }
  39. let stdout = self.command_runner.get_output(
  40. "openssl",
  41. &[
  42. "x509",
  43. "-in",
  44. &self.get_cert_chain_path(),
  45. "-noout",
  46. "-subject",
  47. "-checkend",
  48. &(30 * DAYS_IN_SECONDS).to_string(),
  49. ],
  50. )?;
  51. if stdout
  52. != format!(
  53. "subject=CN = {}\nCertificate will not expire\n",
  54. self.domain
  55. )
  56. .as_bytes()
  57. {
  58. return Ok(false);
  59. }
  60. // FIXME: From my understanding, the -untrusted *.pem parameter shouldn't be necessary, but is necessary with openssl 1.1.0f-3
  61. Ok(
  62. self
  63. .command_runner
  64. .run_successfully(
  65. "openssl",
  66. &[
  67. "verify",
  68. "-untrusted",
  69. "/home/acme/lets_encrypt_x3_cross_signed.pem",
  70. &self.get_cert_chain_path(),
  71. ],
  72. )
  73. .is_ok(),
  74. )
  75. }
  76. fn execute(&self) -> Result<(), Box<dyn Error>> {
  77. let output = self.command_runner.get_output(
  78. "cat",
  79. &[
  80. self.get_single_cert_path().as_ref(),
  81. "/home/acme/lets_encrypt_x3_cross_signed.pem",
  82. ],
  83. )?;
  84. let mut file = FsFile::create(self.get_cert_chain_path())?;
  85. file.write_all(&output)?;
  86. Ok(())
  87. }
  88. fn get_prerequisites(&self) -> Vec<Resource> {
  89. vec![Resource::new("file", self.get_single_cert_path())]
  90. }
  91. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  92. Box::new(SymbolAction::new(runner, self))
  93. }
  94. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  95. where
  96. Self: 'b,
  97. {
  98. Box::new(OwnedSymbolAction::new(runner, *self))
  99. }
  100. }
  101. #[cfg(test)]
  102. mod test {}