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.

86 lines
2.0 KiB

7 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
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 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::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::path::Path;
  5. use command_runner::CommandRunner;
  6. use resources::Resource;
  7. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  8. pub struct TlsCsr<'a, C: 'a + CommandRunner> {
  9. domain: Cow<'a, str>,
  10. command_runner: &'a C,
  11. }
  12. impl<'a, C: CommandRunner> TlsCsr<'a, C> {
  13. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  14. TlsCsr {
  15. domain,
  16. command_runner,
  17. }
  18. }
  19. fn get_key_path(&self) -> String {
  20. format!("/etc/ssl/private/{}.key", self.domain)
  21. }
  22. fn get_csr_path(&self) -> String {
  23. format!("/etc/ssl/local_certs/{}.csr", self.domain)
  24. }
  25. }
  26. impl<'a, C: CommandRunner> fmt::Display for TlsCsr<'a, C> {
  27. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  28. write!(f, "TlsCsr {}", self.domain)
  29. }
  30. }
  31. impl<'a, C: CommandRunner> Symbol for TlsCsr<'a, C> {
  32. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  33. if !Path::new(&self.get_csr_path()).exists() {
  34. return Ok(false);
  35. }
  36. let output = self.command_runner.get_stderr(
  37. "openssl",
  38. &["req", "-in", &self.get_csr_path(), "-noout", "-verify"],
  39. )?;
  40. Ok(output == b"verify OK\n")
  41. }
  42. fn execute(&self) -> Result<(), Box<dyn Error>> {
  43. self.command_runner.run_successfully(
  44. "openssl",
  45. &[
  46. "req",
  47. "-new",
  48. "-sha256",
  49. "-key",
  50. &self.get_key_path(),
  51. "-out",
  52. &self.get_csr_path(),
  53. "-subj",
  54. &format!("/CN={}", self.domain),
  55. ],
  56. )?;
  57. Ok(())
  58. }
  59. fn get_prerequisites(&self) -> Vec<Resource> {
  60. vec![Resource::new("file", self.get_key_path())]
  61. }
  62. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  63. Box::new(SymbolAction::new(runner, self))
  64. }
  65. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  66. where
  67. Self: 'b,
  68. {
  69. Box::new(OwnedSymbolAction::new(runner, *self))
  70. }
  71. }
  72. #[cfg(test)]
  73. mod test {}