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.

75 lines
1.7 KiB

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
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 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
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 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,
  15. 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<dyn Error>> {
  32. if !Path::new(&self.get_path()).exists() {
  33. return Ok(false);
  34. }
  35. let output = self.command_runner.get_output(
  36. "openssl",
  37. &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"],
  38. )?;
  39. Ok(output.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes()))
  40. }
  41. fn execute(&self) -> Result<(), Box<dyn Error>> {
  42. self.command_runner.run_successfully(
  43. "openssl",
  44. &[
  45. "genrsa",
  46. "-out",
  47. &self.get_path(),
  48. &self.get_bytes().to_string(),
  49. ],
  50. )
  51. }
  52. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  53. Box::new(SymbolAction::new(runner, self))
  54. }
  55. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  56. where
  57. Self: 'b,
  58. {
  59. Box::new(OwnedSymbolAction::new(runner, *self))
  60. }
  61. }
  62. #[cfg(test)]
  63. mod test {}