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.

119 lines
3.0 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
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::error::Error;
  2. use std::fmt;
  3. use std::fs::File as FsFile;
  4. use std::io::Write;
  5. use std::path::{Path, PathBuf};
  6. use crate::command_runner::CommandRunner;
  7. use crate::resources::Resource;
  8. use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  9. pub struct AcmeCertChain<'a, D: AsRef<str>, R: AsRef<Path>, C: CommandRunner> {
  10. domain: D,
  11. command_runner: &'a C,
  12. root_cert: R,
  13. }
  14. impl<'a, D: AsRef<str>, R: AsRef<Path>, C: CommandRunner> AcmeCertChain<'a, D, R, C> {
  15. pub fn new(domain: D, command_runner: &'a C, root_cert: R) -> Self {
  16. AcmeCertChain {
  17. domain,
  18. command_runner,
  19. root_cert,
  20. }
  21. }
  22. fn get_single_cert_path(&self) -> PathBuf {
  23. format!("/etc/ssl/local_certs/{}.crt", self.domain.as_ref()).into()
  24. }
  25. fn get_cert_chain_path(&self) -> PathBuf {
  26. format!("/etc/ssl/local_certs/{}.chained.crt", self.domain.as_ref()).into()
  27. }
  28. }
  29. impl<'a, D: AsRef<str>, R: AsRef<Path>, C: CommandRunner> fmt::Display
  30. for AcmeCertChain<'a, D, R, C>
  31. {
  32. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  33. write!(f, "AcmeCertChain {}", self.domain.as_ref())
  34. }
  35. }
  36. const DAYS_IN_SECONDS: u32 = 24 * 60 * 60;
  37. impl<'a, D: AsRef<str>, R: AsRef<Path>, C: CommandRunner> Symbol for AcmeCertChain<'a, D, R, C> {
  38. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  39. if !self.get_cert_chain_path().exists() {
  40. return Ok(false);
  41. }
  42. let stdout = self.command_runner.get_output(
  43. "openssl",
  44. args![
  45. "x509",
  46. "-in",
  47. self.get_cert_chain_path(),
  48. "-noout",
  49. "-subject",
  50. "-checkend",
  51. (30 * DAYS_IN_SECONDS).to_string(),
  52. ],
  53. )?;
  54. if stdout
  55. != format!(
  56. "subject=CN = {}\nCertificate will not expire\n",
  57. self.domain.as_ref()
  58. )
  59. .as_bytes()
  60. {
  61. return Ok(false);
  62. }
  63. // FIXME: From my understanding, the -untrusted *.pem parameter shouldn't be necessary, but is necessary with openssl 1.1.0f-3
  64. Ok(
  65. self
  66. .command_runner
  67. .run_successfully(
  68. "openssl",
  69. args![
  70. "verify",
  71. "-untrusted",
  72. self.root_cert.as_ref(),
  73. self.get_cert_chain_path(),
  74. ],
  75. )
  76. .is_ok(),
  77. )
  78. }
  79. fn execute(&self) -> Result<(), Box<dyn Error>> {
  80. let output = self.command_runner.get_output(
  81. "cat",
  82. args![self.get_single_cert_path(), self.root_cert.as_ref(),],
  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(
  90. "file",
  91. self.get_single_cert_path().to_str().unwrap(),
  92. )]
  93. }
  94. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  95. Box::new(SymbolAction::new(runner, self))
  96. }
  97. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  98. where
  99. Self: 'b,
  100. {
  101. Box::new(OwnedSymbolAction::new(runner, *self))
  102. }
  103. }
  104. #[cfg(test)]
  105. mod test {}