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.

85 lines
2.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 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::error::Error;
  2. use std::fmt;
  3. use std::path::Path;
  4. use crate::command_runner::CommandRunner;
  5. use crate::resources::Resource;
  6. use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  7. pub struct AcmeAccountKey<'a, P: AsRef<Path>, C: CommandRunner> {
  8. path: P,
  9. command_runner: &'a C,
  10. }
  11. impl<'a, P: AsRef<Path>, C: CommandRunner> AcmeAccountKey<'a, P, C> {
  12. pub fn new(path: P, command_runner: &'a C) -> Self {
  13. AcmeAccountKey {
  14. path,
  15. command_runner,
  16. }
  17. }
  18. fn get_bytes(&self) -> u32 {
  19. 4096
  20. }
  21. }
  22. impl<P: AsRef<Path>, C: CommandRunner> fmt::Display for AcmeAccountKey<'_, P, C> {
  23. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  24. write!(f, "AcmeAccountKey {}", self.path.as_ref().display())
  25. }
  26. }
  27. impl<P: AsRef<Path>, C: CommandRunner> Symbol for AcmeAccountKey<'_, P, C> {
  28. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  29. if !self.path.as_ref().exists() {
  30. return Ok(false);
  31. }
  32. let stdout = self.command_runner.get_output(
  33. "openssl",
  34. args![
  35. "rsa",
  36. "-in",
  37. self.path.as_ref(),
  38. "-noout",
  39. "-check",
  40. "-text",
  41. ],
  42. )?;
  43. Ok(stdout.ends_with("RSA key ok\n".as_bytes()))
  44. }
  45. fn execute(&self) -> Result<(), Box<dyn Error>> {
  46. self.command_runner.run_successfully(
  47. "openssl",
  48. args![
  49. "genrsa",
  50. "-out",
  51. self.path.as_ref(),
  52. self.get_bytes().to_string(),
  53. ],
  54. )
  55. }
  56. fn get_prerequisites(&self) -> Vec<Resource> {
  57. if let Some(parent) = self.path.as_ref().parent() {
  58. vec![Resource::new("dir", parent.to_str().unwrap())]
  59. } else {
  60. vec![]
  61. }
  62. }
  63. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  64. Box::new(SymbolAction::new(runner, self))
  65. }
  66. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  67. where
  68. Self: 'b,
  69. {
  70. Box::new(OwnedSymbolAction::new(runner, *self))
  71. }
  72. }
  73. #[cfg(test)]
  74. mod test {}