aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2023-03-30 19:16:55 -0700
committerFranck Cuny <franck@fcuny.net>2023-03-30 19:16:55 -0700
commitcdfe1f02e8e7848c500fb83f952add7158c9368e (patch)
tree3b5b006f755fca1b494aef626ad71fd5811ebbb3
parentadd a flag to check expired certificate (diff)
downloadx-cdfe1f02e8e7848c500fb83f952add7158c9368e.tar.gz
use anyhow for error handling
-rw-r--r--src/x509-info/Cargo.lock7
-rw-r--r--src/x509-info/Cargo.toml1
-rw-r--r--src/x509-info/src/client.rs6
3 files changed, 11 insertions, 3 deletions
diff --git a/src/x509-info/Cargo.lock b/src/x509-info/Cargo.lock
index 005aee6..e24318b 100644
--- a/src/x509-info/Cargo.lock
+++ b/src/x509-info/Cargo.lock
@@ -12,6 +12,12 @@ dependencies = [
]
[[package]]
+name = "anyhow"
+version = "1.0.70"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
+
+[[package]]
name = "asn1-rs"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -857,6 +863,7 @@ checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
name = "x509-info"
version = "0.1.0"
dependencies = [
+ "anyhow",
"chrono",
"clap",
"rustls",
diff --git a/src/x509-info/Cargo.toml b/src/x509-info/Cargo.toml
index f6b4e3e..e358a44 100644
--- a/src/x509-info/Cargo.toml
+++ b/src/x509-info/Cargo.toml
@@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+anyhow = "1.0.70"
chrono = {version = "0.4.20", features = ["clock"], default-features = false }
clap = {version = "4.0.18", features = ["derive", "cargo"]}
rustls = {version = "0.20.7", features = ["dangerous_configuration"]}
diff --git a/src/x509-info/src/client.rs b/src/x509-info/src/client.rs
index 344e300..8728f93 100644
--- a/src/x509-info/src/client.rs
+++ b/src/x509-info/src/client.rs
@@ -1,7 +1,7 @@
+use anyhow::{anyhow, Error};
use rustls::{
Certificate, ClientConfig, ClientConnection, OwnedTrustAnchor, RootCertStore, ServerName,
};
-use std::error::Error;
use std::io::Write;
use std::sync::Arc;
@@ -31,7 +31,7 @@ pub fn get_certificates(
domain: String,
port: u16,
insecure: bool,
-) -> Result<Vec<Certificate>, Box<dyn Error>> {
+) -> Result<Vec<Certificate>, Error> {
let mut tcp_stream = std::net::TcpStream::connect(format!("{}:{}", domain, port))?;
let config = if insecure {
@@ -69,6 +69,6 @@ pub fn get_certificates(
match conn.peer_certificates() {
Some(c) => Ok(c.to_vec()),
- None => Err("no certificate found")?,
+ None => Err(anyhow!("no certificate found for {}", domain))?,
}
}