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.

60 lines
1.1 KiB

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