From 0822ad39392172dffd6d2c9b756e229302631411 Mon Sep 17 00:00:00 2001 From: AbooMinister25 Date: Thu, 11 Jul 2024 15:42:51 -0400 Subject: [PATCH] Remove thiserror and use color-eyre in its stead --- Cargo.lock | 93 ++++++++++++++++++++++++++++++++++++-------- Cargo.toml | 2 +- src/client.rs | 14 +++---- src/client/models.rs | 5 +-- src/error.rs | 49 ----------------------- src/main.rs | 9 +++-- src/scanner.rs | 14 +++---- 7 files changed, 97 insertions(+), 89 deletions(-) delete mode 100644 src/error.rs diff --git a/Cargo.lock b/Cargo.lock index 6790fef..bd1d3dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.22.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" dependencies = [ "gimli", ] @@ -82,9 +82,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.72" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", @@ -150,9 +150,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytemuck" -version = "1.16.0" +version = "1.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" +checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" [[package]] name = "byteorder" @@ -234,6 +234,33 @@ dependencies = [ "libloading", ] +[[package]] +name = "color-eyre" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "constant_time_eq" version = "0.3.0" @@ -357,6 +384,7 @@ dependencies = [ name = "dragonfly-client-rs" version = "0.1.0" dependencies = [ + "color-eyre", "figment", "flate2", "log", @@ -365,7 +393,6 @@ dependencies = [ "reqwest", "serde", "tar", - "thiserror", "threadpool", "tracing", "tracing-subscriber", @@ -399,6 +426,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fastrand" version = "2.1.0" @@ -544,9 +581,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.29.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "glob" @@ -700,6 +737,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.2.6" @@ -923,9 +966,9 @@ dependencies = [ [[package]] name = "object" -version = "0.35.0" +version = "0.32.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" dependencies = [ "memchr", ] @@ -986,6 +1029,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "parking_lot" version = "0.12.3" @@ -1647,9 +1696,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.13" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" dependencies = [ "serde", "serde_spanned", @@ -1668,9 +1717,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.13" +version = "0.22.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" +checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1" dependencies = [ "indexmap", "serde", @@ -1738,6 +1787,16 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.2.0" @@ -2092,9 +2151,9 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" -version = "0.6.9" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86c949fede1d13936a99f14fafd3e76fd642b556dd2ce96287fbe2e0151bfac6" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 18b49ec..16c992c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +color-eyre = "0.6.3" figment = { version = "0.10.19", features = ["env", "toml"] } flate2 = "1.0.30" log = "0.4.21" @@ -12,7 +13,6 @@ parking_lot = "0.12.3" reqwest = { version = "0.12.4", features = ["blocking", "json", "gzip"] } serde = { version = "1.0.203", features = ["derive"] } tar = "0.4.40" -thiserror = "1.0.61" threadpool = "1.8.1" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/src/client.rs b/src/client.rs index 9693095..0e08aa5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,7 +4,8 @@ mod models; pub use methods::*; pub use models::*; -use crate::{error::DragonflyError, APP_CONFIG}; +use crate::APP_CONFIG; +use color_eyre::Result; use flate2::read::GzDecoder; use parking_lot::RwLock; use reqwest::{blocking::Client, Url}; @@ -38,7 +39,7 @@ pub struct DragonflyClient { } impl DragonflyClient { - pub fn new() -> Result { + pub fn new() -> Result { let client = Client::builder().gzip(true).build()?; let auth_response = fetch_access_token(&client)?; @@ -105,7 +106,7 @@ impl DragonflyClient { } /// Update the global ruleset. Waits for a write lock. - pub fn update_rules(&self) -> Result<(), DragonflyError> { + pub fn update_rules(&self) -> Result<()> { let response = fetch_rules( self.get_http_client(), &self.authentication_state.read().access_token, @@ -150,10 +151,7 @@ impl DragonflyClient { } } -pub fn fetch_tarball( - http_client: &Client, - download_url: &Url, -) -> Result { +pub fn fetch_tarball(http_client: &Client, download_url: &Url) -> Result { let response = http_client.get(download_url.clone()).send()?; let decompressed = GzDecoder::new(response); @@ -165,7 +163,7 @@ pub fn fetch_tarball( Ok(tar::Archive::new(cursor)) } -pub fn fetch_zipfile(http_client: &Client, download_url: &Url) -> Result { +pub fn fetch_zipfile(http_client: &Client, download_url: &Url) -> Result { let response = http_client.get(download_url.to_string()).send()?; let mut cursor = Cursor::new(Vec::new()); diff --git a/src/client/models.rs b/src/client/models.rs index 82b020f..2a88f85 100644 --- a/src/client/models.rs +++ b/src/client/models.rs @@ -1,11 +1,10 @@ +use color_eyre::Result; use serde::Serialize; use serde::{self, Deserialize}; use std::collections::HashMap; use std::fmt::Display; use yara::{Compiler, Rules}; -use crate::error::DragonflyError; - #[derive(Debug, Serialize, PartialEq)] pub struct SubmitJobResultsSuccess { pub name: String, @@ -53,7 +52,7 @@ pub struct RulesResponse { impl RulesResponse { /// Compile the rules from the response - pub fn compile(&self) -> Result { + pub fn compile(&self) -> Result { let rules_str = self .rules .values() diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 5f34ea3..0000000 --- a/src/error.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::io; - -use figment::Error as ConfigError; -use thiserror::Error; -use zip::result::ZipError; - -#[allow(clippy::module_name_repetitions)] -#[derive(Error, Debug)] -pub enum DragonflyError { - #[error("Yara Error: {source:#?}")] - YaraError { - #[from] - source: yara::YaraError, - }, - - #[error("Yara Error: {source:#?}")] - GenericYaraError { - #[from] - source: yara::Error, - }, - - #[error("HTTP Error: {source:#?}")] - HTTPError { - #[from] - source: reqwest::Error, - }, - - #[error("IO Error: {source:#?}")] - IOError { - #[from] - source: io::Error, - }, - - #[error("Zipfile Error: {source:#?}")] - ZipError { - #[from] - source: ZipError, - }, - - #[error("Configuration Error: {source:#?}")] - ConfigError { - #[from] - source: ConfigError, - }, - - #[allow(dead_code)] - #[error("Download too large: '{0:#?}'")] - DownloadTooLarge(String), -} diff --git a/src/main.rs b/src/main.rs index 305aa84..61af5a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ mod app_config; mod client; -mod error; mod exts; mod scanner; mod utils; @@ -8,7 +7,7 @@ mod utils; use std::{sync::Arc, time::Duration}; use client::DragonflyClient; -use error::DragonflyError; +use color_eyre::eyre::Result; use reqwest::blocking::Client; use threadpool::ThreadPool; use tracing::{debug, error, info, span, trace, Level}; @@ -30,7 +29,7 @@ fn scanner( job: &Job, rules: &Rules, commit_hash: &str, -) -> Result { +) -> Result { let distribution_scan_results = scan_all_distributions(http_client, rules, job)?; let package_scan_result = PackageScanResults::new( @@ -76,7 +75,9 @@ fn runner(client: &DragonflyClient, job: Job) { } } -fn main() -> Result<(), DragonflyError> { +fn main() -> Result<()> { + color_eyre::install()?; + let default_env_filter = EnvFilter::builder() .parse("warn,dragonfly_client_rs=info") .unwrap(); diff --git a/src/scanner.rs b/src/scanner.rs index 5922f80..ee2bfff 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -4,12 +4,12 @@ use std::{ path::{Path, PathBuf}, }; +use color_eyre::Result; use reqwest::{blocking::Client, Url}; use yara::Rules; use crate::{ client::{fetch_tarball, fetch_zipfile, Job, SubmitJobResultsSuccess, TarballType, ZipType}, - error::DragonflyError, exts::RuleExt, utils::create_inspector_url, }; @@ -40,12 +40,12 @@ impl FileScanResult { /// Scan an archive format using Yara rules. trait Scan { - fn scan(&mut self, rules: &Rules) -> Result, DragonflyError>; + fn scan(&mut self, rules: &Rules) -> Result>; } impl Scan for TarballType { /// Scan a tarball against the given rule set - fn scan(&mut self, rules: &Rules) -> Result, DragonflyError> { + fn scan(&mut self, rules: &Rules) -> Result> { let file_scan_results = self .entries()? .filter_map(Result::ok) @@ -62,7 +62,7 @@ impl Scan for TarballType { impl Scan for ZipType { /// Scan a zipfile against the given rule set - fn scan(&mut self, rules: &Rules) -> Result, DragonflyError> { + fn scan(&mut self, rules: &Rules) -> Result> { let mut file_scan_results = Vec::new(); for idx in 0..self.len() { let mut file = self.by_index(idx)?; @@ -82,7 +82,7 @@ struct Distribution { } impl Distribution { - fn scan(&mut self, rules: &Rules) -> Result { + fn scan(&mut self, rules: &Rules) -> Result { let results = self.file.scan(rules)?; Ok(DistributionScanResults::new( @@ -224,7 +224,7 @@ pub fn scan_all_distributions( http_client: &Client, rules: &Rules, job: &Job, -) -> Result, DragonflyError> { +) -> Result> { let mut distribution_scan_results = Vec::with_capacity(job.distributions.len()); for distribution in &job.distributions { let download_url: Url = distribution.parse().unwrap(); @@ -254,7 +254,7 @@ fn scan_file( file: &mut impl Read, path: &Path, rules: &Rules, -) -> Result { +) -> Result { let mut buffer = Vec::new(); file.read_to_end(&mut buffer)?;