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.

129 lines
3.1 KiB

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