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.3 KiB

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