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.

66 lines
1.3 KiB

7 years ago
4 years ago
7 years ago
4 years ago
5 years ago
4 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
4 years ago
7 years ago
4 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::error::Error;
  5. use std::path::Path;
  6. #[derive(Debug)]
  7. pub struct Key<C, P> {
  8. file_path: P,
  9. command_runner: C,
  10. bytes: u32,
  11. }
  12. impl<C, P> Key<C, P> {
  13. pub const fn new(command_runner: C, file_path: P) -> Self {
  14. Self {
  15. file_path,
  16. command_runner,
  17. bytes: 4096,
  18. }
  19. }
  20. }
  21. #[async_trait(?Send)]
  22. impl<C: CommandRunner, P: AsRef<Path>> Symbol for Key<C, P> {
  23. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  24. if !self.file_path.as_ref().exists() {
  25. return Ok(false);
  26. }
  27. let stdout = self
  28. .command_runner
  29. .get_output(
  30. "openssl",
  31. args![
  32. "rsa",
  33. "-in",
  34. self.file_path.as_ref(),
  35. "-noout",
  36. "-check",
  37. "-text",
  38. ],
  39. )
  40. .await?;
  41. // FIXME check bytes
  42. Ok(stdout.ends_with(b"RSA key ok\n"))
  43. }
  44. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  45. self
  46. .command_runner
  47. .run_successfully(
  48. "openssl",
  49. args![
  50. "genrsa",
  51. "-out",
  52. self.file_path.as_ref(),
  53. self.bytes.to_string(),
  54. ],
  55. )
  56. .await
  57. }
  58. }
  59. #[cfg(test)]
  60. mod test {}