From f7fc28426990888a0b2f586b8c5ab60eb6a32180 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 19 Jul 2020 11:15:17 +0300 Subject: feat: check that TLS cert and key files exist, with error message --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 3 ++- src/main.rs | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 7e7d8c2..ad8b7b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,6 +179,7 @@ dependencies = [ "serde", "serde_yaml", "structopt", + "thiserror", "tokio", "warp", ] @@ -1199,6 +1200,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.1.43" diff --git a/Cargo.toml b/Cargo.toml index ffcdfb2..f0cd5f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ tokio = { version = "0.2", features = ["macros"] } serde_yaml = "0.8" serde = { version = "1", features = ["derive"] } structopt = "0.3" -anyhow = "1" \ No newline at end of file +anyhow = "1" +thiserror = "1" diff --git a/src/main.rs b/src/main.rs index c6e22ab..d74a148 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,17 @@ struct Opt { config: PathBuf, } +#[derive(Debug, thiserror::Error)] +enum EwwwError { + #[error("TLS certificate {0} does not exist")] + TlsCertNotFound(PathBuf), + + #[error("TLS key {0} does not exist")] + TlsKeyNotFound(PathBuf), +} + + + #[tokio::main] async fn main() { let opt = Opt::from_args(); @@ -37,5 +48,17 @@ async fn main() { fn read_config(filename: &Path) -> anyhow::Result { let config = std::fs::read_to_string(filename)?; let config: Config = serde_yaml::from_str(&config)?; + check_config(&config)?; Ok(config) } + +fn check_config(config: &Config) -> anyhow::Result<()> { + if !config.tls_cert.exists() { + return Err(EwwwError::TlsCertNotFound(config.tls_cert.clone()).into()); + } + if !config.tls_key.exists() { + return Err(EwwwError::TlsKeyNotFound(config.tls_key.clone()).into()); + + } + Ok(()) +} -- cgit v1.2.1