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.

59 lines
1.6 KiB

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