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.

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