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.

62 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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 symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  7. pub struct TlsKey<'a, C: 'a + CommandRunner> {
  8. domain: Cow<'a, str>,
  9. command_runner: &'a C
  10. }
  11. impl<'a, C: CommandRunner> TlsKey<'a, C> {
  12. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  13. TlsKey {
  14. domain: domain,
  15. command_runner: command_runner
  16. }
  17. }
  18. fn get_path(&self) -> String {
  19. format!("/etc/ssl/private/{}.key", self.domain)
  20. }
  21. fn get_bytes(&self) -> u32 {
  22. 4096
  23. }
  24. }
  25. impl<'a, C: CommandRunner> fmt::Display for TlsKey<'a, C> {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "TlsKey {}", self.domain)
  28. }
  29. }
  30. impl<'a, C: CommandRunner> Symbol for TlsKey<'a, C> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. if !Path::new(&self.get_path()).exists() {
  33. return Ok(false);
  34. }
  35. let output = try!(self.command_runner.get_output("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"]));
  36. Ok(output.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes()))
  37. }
  38. fn execute(&self) -> Result<(), Box<Error>> {
  39. self.command_runner.run_successfully("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()])
  40. }
  41. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  42. Box::new(SymbolAction::new(runner, self))
  43. }
  44. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  45. Box::new(OwnedSymbolAction::new(runner, *self))
  46. }
  47. }
  48. #[cfg(test)]
  49. mod test {
  50. }