This commit is contained in:
Adrian Heine 2017-04-09 16:07:33 +02:00
parent 1bb22db692
commit 7d629e38d8
14 changed files with 478 additions and 58 deletions

57
src/symbols/tls/csr.rs Normal file
View file

@ -0,0 +1,57 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct TlsCsr<'a> {
domain: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> TlsCsr<'a> {
pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> TlsCsr<'a> {
TlsCsr {
domain: domain,
command_runner: command_runner
}
}
fn get_key_path(&self) -> String {
format!("/etc/ssl/private/{}.key", self.domain)
}
fn get_csr_path(&self) -> String {
format!("/etc/ssl/local_certs/{}.csr", self.domain)
}
}
impl<'a> fmt::Display for TlsCsr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TlsCsr {}", self.domain)
}
}
impl<'a> Symbol for TlsCsr<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let result = self.command_runner.run_with_args("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]);
match result {
Err(e) => Err(Box::new(e)),
Ok(output) => match output.status.code() {
Some(0) => Ok(output.stderr == "verify OK\n".as_bytes()),
Some(_) => Ok(false),
_ => Err("Didn't work".to_string().into())
}
}
}
fn execute(&self) -> Result<(), Box<Error>> {
let output = try!(self.command_runner.run_with_args("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]).map_err(|e| Box::new(e)));
Ok(())
}
}
#[cfg(test)]
mod test {
}

60
src/symbols/tls/key.rs Normal file
View file

@ -0,0 +1,60 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct TlsKey<'a> {
domain: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> TlsKey<'a> {
pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> TlsKey<'a> {
TlsKey {
domain: domain,
command_runner: command_runner
}
}
fn get_path(&self) -> String {
format!("/etc/ssl/private/{}.key", self.domain)
}
fn get_bytes(&self) -> u32 {
4096
}
}
impl<'a> fmt::Display for TlsKey<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TlsKey {}", self.domain)
}
}
impl<'a> Symbol for TlsKey<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let result = self.command_runner.run_with_args("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"]);
match result {
Err(e) => Err(Box::new(e)),
Ok(output) => match output.status.code() {
Some(0) => Ok(output.stdout.starts_with(format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())),
Some(_) => Ok(false),
_ => Err("Didn't work".to_string().into())
}
}
}
fn execute(&self) -> Result<(), Box<Error>> {
let output = self.command_runner.run_with_args("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]);
match output {
Err(e) => Err(Box::new(e)),
Ok(_) => Ok(())
}
}
}
#[cfg(test)]
mod test {
}

7
src/symbols/tls/mod.rs Normal file
View file

@ -0,0 +1,7 @@
mod csr;
mod key;
mod self_signed_cert;
pub use self::csr::TlsCsr;
pub use self::key::TlsKey;
pub use self::self_signed_cert::SelfSignedTlsCert;

View file

@ -0,0 +1,62 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct SelfSignedTlsCert<'a> {
domain: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> SelfSignedTlsCert<'a> {
pub fn new(domain: Cow<'a, str>, command_runner: &'a CommandRunner) -> SelfSignedTlsCert<'a> {
SelfSignedTlsCert {
domain: domain,
command_runner: command_runner
}
}
fn get_key_path(&self) -> String {
format!("/etc/ssl/private/{}.key", self.domain)
}
fn get_cert_path(&self) -> String {
format!("/etc/ssl/local_certs/{}.crt", self.domain)
}
}
impl<'a> fmt::Display for SelfSignedTlsCert<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SelfSignedTlsCert {}", self.domain)
}
}
const DAYS_IN_SECONDS: u32 = 24*60*60;
impl<'a> Symbol for SelfSignedTlsCert<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let result = self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]);
match result {
Err(e) => Err(Box::new(e)),
Ok(output) => match output.status.code() {
Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()),
Some(_) => Ok(false),
_ => Err("Didn't work".to_string().into())
}
}
}
fn execute(&self) -> Result<(), Box<Error>> {
let output = self.command_runner.run_with_args("openssl", &["req", "-x509", "-sha256", "-days", "90", "-key", &self.get_key_path(), "-out", &self.get_cert_path(), "-subj", &format!("/CN={}", self.domain)]);
match output {
Err(e) => Err(Box::new(e)),
Ok(_) => Ok(())
}
}
}
#[cfg(test)]
mod test {
}