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.

116 lines
2.8 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
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 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
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 SelfSignedTlsCert<'a, C: 'a + CommandRunner> {
  9. domain: Cow<'a, str>,
  10. command_runner: &'a C,
  11. }
  12. impl<'a, C: CommandRunner> SelfSignedTlsCert<'a, C> {
  13. pub fn new(domain: Cow<'a, str>, command_runner: &'a C) -> Self {
  14. SelfSignedTlsCert {
  15. domain,
  16. command_runner,
  17. }
  18. }
  19. fn get_key_path(&self) -> String {
  20. format!("/etc/ssl/private/{}.key", self.domain)
  21. }
  22. fn get_cert_path(&self) -> String {
  23. format!("/etc/ssl/local_certs/{}.chained.crt", self.domain)
  24. }
  25. }
  26. impl<'a, C: CommandRunner> fmt::Display for SelfSignedTlsCert<'a, C> {
  27. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  28. write!(f, "SelfSignedTlsCert {}", self.domain)
  29. }
  30. }
  31. const DAYS_IN_SECONDS: u32 = 24 * 60 * 60;
  32. impl<'a, C: CommandRunner> Symbol for SelfSignedTlsCert<'a, C> {
  33. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  34. if !Path::new(&self.get_cert_path()).exists() {
  35. return Ok(false);
  36. }
  37. let output = self.command_runner.run_with_args(
  38. "openssl",
  39. &[
  40. "x509",
  41. "-in",
  42. &self.get_cert_path(),
  43. "-noout",
  44. "-subject",
  45. "-checkend",
  46. &(30 * DAYS_IN_SECONDS).to_string(),
  47. ],
  48. )?;
  49. println!("{}", output.status.code().unwrap());
  50. match output.status.code() {
  51. Some(0) => Ok(
  52. output.stdout
  53. == format!(
  54. "subject=CN = {}\nCertificate will not expire\n",
  55. self.domain
  56. )
  57. .as_bytes(),
  58. ),
  59. Some(_) => {
  60. if output.stdout
  61. == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes()
  62. {
  63. Ok(false)
  64. } else {
  65. Err("Exit code non-zero, but wrong stdout".to_string().into())
  66. }
  67. }
  68. _ => Err("Apparently killed by signal".to_string().into()),
  69. }
  70. }
  71. fn execute(&self) -> Result<(), Box<dyn Error>> {
  72. self.command_runner.run_successfully(
  73. "openssl",
  74. &[
  75. "req",
  76. "-x509",
  77. "-sha256",
  78. "-days",
  79. "90",
  80. "-key",
  81. &self.get_key_path(),
  82. "-out",
  83. &self.get_cert_path(),
  84. "-subj",
  85. &format!("/CN={}", self.domain),
  86. ],
  87. )
  88. }
  89. fn get_prerequisites(&self) -> Vec<Resource> {
  90. vec![Resource::new("file", self.get_key_path())]
  91. }
  92. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  93. Box::new(SymbolAction::new(runner, self))
  94. }
  95. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  96. where
  97. Self: 'b,
  98. {
  99. Box::new(OwnedSymbolAction::new(runner, *self))
  100. }
  101. }
  102. #[cfg(test)]
  103. mod test {}