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.
119 lines
3.0 KiB
119 lines
3.0 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::path::PathBuf;
|
|
|
|
use crate::command_runner::CommandRunner;
|
|
use crate::resources::Resource;
|
|
use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct SelfSignedTlsCert<'a, D: AsRef<str>, C: CommandRunner> {
|
|
domain: D,
|
|
command_runner: &'a C,
|
|
}
|
|
|
|
impl<'a, D: AsRef<str>, C: CommandRunner> SelfSignedTlsCert<'a, D, C> {
|
|
pub fn new(domain: D, command_runner: &'a C) -> Self {
|
|
SelfSignedTlsCert {
|
|
domain,
|
|
command_runner,
|
|
}
|
|
}
|
|
|
|
fn get_key_path(&self) -> PathBuf {
|
|
format!("/etc/ssl/private/{}.key", self.domain.as_ref()).into()
|
|
}
|
|
|
|
fn get_cert_path(&self) -> PathBuf {
|
|
format!("/etc/ssl/local_certs/{}.chained.crt", self.domain.as_ref()).into()
|
|
}
|
|
}
|
|
|
|
impl<'a, D: AsRef<str>, C: CommandRunner> fmt::Display for SelfSignedTlsCert<'a, D, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "SelfSignedTlsCert {}", self.domain.as_ref())
|
|
}
|
|
}
|
|
|
|
const DAYS_IN_SECONDS: u32 = 24 * 60 * 60;
|
|
|
|
impl<'a, D: AsRef<str>, C: CommandRunner> Symbol for SelfSignedTlsCert<'a, D, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
if !self.get_cert_path().exists() {
|
|
return Ok(false);
|
|
}
|
|
let output = self.command_runner.run_with_args(
|
|
"openssl",
|
|
args![
|
|
"x509",
|
|
"-in",
|
|
self.get_cert_path(),
|
|
"-noout",
|
|
"-subject",
|
|
"-checkend",
|
|
(30 * DAYS_IN_SECONDS).to_string(),
|
|
],
|
|
)?;
|
|
println!("{}", output.status.code().unwrap());
|
|
match output.status.code() {
|
|
Some(0) => Ok(
|
|
output.stdout
|
|
== format!(
|
|
"subject=CN = {}\nCertificate will not expire\n",
|
|
self.domain.as_ref()
|
|
)
|
|
.as_bytes(),
|
|
),
|
|
Some(_) => {
|
|
if output.stdout
|
|
== format!(
|
|
"subject=CN = {}\nCertificate will expire\n",
|
|
self.domain.as_ref()
|
|
)
|
|
.as_bytes()
|
|
{
|
|
Ok(false)
|
|
} else {
|
|
Err("Exit code non-zero, but wrong stdout".to_string().into())
|
|
}
|
|
}
|
|
_ => Err("Apparently killed by signal".to_string().into()),
|
|
}
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
self.command_runner.run_successfully(
|
|
"openssl",
|
|
args![
|
|
"req",
|
|
"-x509",
|
|
"-sha256",
|
|
"-days",
|
|
"90",
|
|
"-key",
|
|
self.get_key_path(),
|
|
"-out",
|
|
self.get_cert_path(),
|
|
"-subj",
|
|
format!("/CN={}", self.domain.as_ref()),
|
|
],
|
|
)
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![Resource::new("file", self.get_key_path().to_str().unwrap())]
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
|
|
where
|
|
Self: 'b,
|
|
{
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {}
|