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

6 years ago
7 years ago
7 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
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 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::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::path::Path;
  5. use command_runner::CommandRunner;
  6. use resources::Resource;
  7. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  8. pub struct AcmeAccountKey<'a, C: 'a + CommandRunner> {
  9. path: Cow<'a, Path>,
  10. command_runner: &'a C,
  11. }
  12. impl<'a, C: CommandRunner> AcmeAccountKey<'a, C> {
  13. pub fn new(path: Cow<'a, Path>, command_runner: &'a C) -> Self {
  14. AcmeAccountKey {
  15. path,
  16. command_runner,
  17. }
  18. }
  19. fn get_bytes(&self) -> u32 {
  20. 4096
  21. }
  22. }
  23. impl<'a, C: CommandRunner> fmt::Display for AcmeAccountKey<'a, C> {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. write!(f, "AcmeAccountKey {}", self.path.display())
  26. }
  27. }
  28. impl<'a, C: CommandRunner> Symbol for AcmeAccountKey<'a, C> {
  29. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  30. if !self.path.exists() {
  31. return Ok(false);
  32. }
  33. let stdout = try!(self.command_runner.get_output(
  34. "openssl",
  35. &[
  36. "rsa".as_ref(),
  37. "-in".as_ref(),
  38. self.path.as_os_str(),
  39. "-noout".as_ref(),
  40. "-check".as_ref(),
  41. "-text".as_ref()
  42. ]
  43. ));
  44. Ok(stdout.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes()))
  45. }
  46. fn execute(&self) -> Result<(), Box<dyn Error>> {
  47. self.command_runner.run_successfully(
  48. "openssl",
  49. &[
  50. "genrsa".as_ref(),
  51. "-out".as_ref(),
  52. self.path.as_os_str(),
  53. self.get_bytes().to_string().as_ref(),
  54. ],
  55. )
  56. }
  57. fn get_prerequisites(&self) -> Vec<Resource> {
  58. vec![Resource::new(
  59. "dir",
  60. self.path.parent().unwrap().to_string_lossy(),
  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 {}