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.

68 lines
1.4 KiB

7 years ago
4 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use std::borrow::Borrow;
  5. use std::error::Error;
  6. use std::path::Path;
  7. #[derive(Debug)]
  8. pub struct Csr<C, D, K, P> {
  9. command_runner: C,
  10. domain: D,
  11. key_path: K,
  12. csr_path: P,
  13. }
  14. impl<C, D, K, P> Csr<C, D, K, P> {
  15. pub const fn new(command_runner: C, domain: D, key_path: K, csr_path: P) -> Self {
  16. Self {
  17. command_runner,
  18. domain,
  19. key_path,
  20. csr_path,
  21. }
  22. }
  23. }
  24. #[async_trait(?Send)]
  25. impl<C: CommandRunner, D: Borrow<str>, K: Borrow<Path>, P: Borrow<Path>> Symbol
  26. for Csr<C, D, K, P>
  27. {
  28. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  29. if !self.csr_path.borrow().exists() {
  30. return Ok(false);
  31. }
  32. let output = self
  33. .command_runner
  34. .get_stderr(
  35. "openssl",
  36. args!["req", "-in", self.csr_path.borrow(), "-noout", "-verify",],
  37. )
  38. .await?;
  39. Ok(output == b"verify OK\n")
  40. }
  41. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  42. self
  43. .command_runner
  44. .run_successfully(
  45. "openssl",
  46. args![
  47. "req",
  48. "-new",
  49. "-sha256",
  50. "-key",
  51. self.key_path.borrow(),
  52. "-out",
  53. self.csr_path.borrow(),
  54. "-subj",
  55. format!("/CN={}", self.domain.borrow()),
  56. ],
  57. )
  58. .await
  59. }
  60. }
  61. #[cfg(test)]
  62. mod test {}