Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add churn plugin #523

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ members = [
"plugins/entropy",
"plugins/linguist",
"plugins/review",
"plugins/binary"
, "plugins/identity"]
"plugins/binary",
"plugins/identity",
"plugins/churn"
]

# Make sure Hipcheck is run with `cargo run`.
#
Expand Down
13 changes: 3 additions & 10 deletions hipcheck/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ fn default_query_explanation(
let core = db.core();
let key = get_plugin_key(publisher.as_str(), plugin.as_str());
let Some(p_handle) = core.plugins.get(&key) else {
return Err(hc_error!(
"Plugin '{}' not found",
key,
));
return Err(hc_error!("Plugin '{}' not found", key,));
};
Ok(p_handle.get_default_query_explanation().cloned())
}
Expand All @@ -90,16 +87,12 @@ fn query(
};
// Initiate the query. If remote closed or we got our response immediately,
// return
eprintln!("Querying {plugin}::{query} with key {key:?}");
let mut ar = match runtime.block_on(p_handle.query(query, key))? {
PluginResponse::RemoteClosed => {
return Err(hc_error!("Plugin channel closed unexpected"));
}
PluginResponse::Completed(v) => return Ok(v),
PluginResponse::AwaitingResult(a) => {
eprintln!("awaiting result: {:?}", a);
a
}
PluginResponse::AwaitingResult(a) => a,
};
// Otherwise, the plugin needs more data to continue. Recursively query
// (with salsa memo-ization) to get the needed data, and resume our
Expand All @@ -114,7 +107,7 @@ fn query(
ar.key.clone(),
)?
.value;
eprintln!("Got answer {answer:?}, resuming");
eprintln!("Got answer, resuming");
ar = match runtime.block_on(p_handle.resume_query(ar, answer))? {
PluginResponse::RemoteClosed => {
return Err(hc_error!("Plugin channel closed unexpected"));
Expand Down
2 changes: 1 addition & 1 deletion hipcheck/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl ActivePlugin {
concerns: vec![],
};

eprintln!("Resuming query with answer {query:?}");
eprintln!("Resuming query");

Ok(self.channel.query(query).await?.into())
}
Expand Down
3 changes: 1 addition & 2 deletions hipcheck/src/plugin/retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ use xz2::read::XzDecoder;
use super::get_current_arch;

/// The plugins currently are not delegated via the `plugin` system and are still part of `hipcheck` core
pub const MITRE_LEGACY_PLUGINS: [&str; 6] = [
pub const MITRE_LEGACY_PLUGINS: [&str; 5] = [
"activity",
"entropy",
"affiliation",
"binary",
"churn",
"typo",
];

Expand Down
21 changes: 21 additions & 0 deletions plugins/churn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "churn"
version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
publish = false

[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
hipcheck-sdk = { version = "0.1.0", path = "../../sdk/rust", features = ["macros"] }
log = "0.4.22"
salsa = "0.16.1"
schemars = "0.8.21"
serde = "1.0.213"
serde_json = "1.0.132"
tokio = { version = "1.41.0", features = ["rt"] }
toml = "0.8.19"

[dev-dependencies]
hipcheck-sdk = { path = "../../sdk/rust", features = ["mock_engine"] }
pathbuf = "1.0.0"
14 changes: 14 additions & 0 deletions plugins/churn/plugin.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
publisher "mitre"
name "churn"
version "0.1.0"
license "Apache-2.0"
entrypoint {
on arch="aarch64-apple-darwin" "./target/debug/churn"
on arch="x86_64-apple-darwin" "./target/debug/churn"
on arch="x86_64-unknown-linux-gnu" "./target/debug/churn"
on arch="x86_64-pc-windows-msvc" "./target/debug/churn"
}

dependencies {
plugin "mitre/git" version="0.1.0" manifest="./plugins/git/plugin.kdl"
}
160 changes: 160 additions & 0 deletions plugins/churn/src/error/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: Apache-2.0

//! A duplicate of the `anyhow::Context` extension trait intended to
//! make error propagation less verbose.

use crate::error::{Error, Introspect};
use std::error::Error as StdError;

/// Functions for adding context to an error result
///
/// The `Context` trait is based around the `Error` type defined in
/// this crate. Aside from the changed method names (collision
/// avoidance), it is a duplicate of the `anyhow::Context` trait.
/// Like its `anyhow` counterpart, this trait is sealed.
pub trait Context<T>: sealed::Sealed {
/// Add context to an error
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Introspect + 'static;

/// Lazily add context to an error
fn with_context<C, F>(self, context_fn: F) -> Result<T, Error>
where
C: Introspect + 'static,
F: FnOnce() -> C;
}

// `Context` is implemented only for those result types encountered
// when entering or traversing the query system: `Result<T, Error>`
// and `Result<T, E>` for dynamic error types `E`.

impl<T> Context<T> for Result<T, Error> {
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Introspect + 'static,
{
self.map_err(|err| err.context(context))
}

fn with_context<C, F>(self, context_fn: F) -> Result<T, Error>
where
C: Introspect + 'static,
F: FnOnce() -> C,
{
self.map_err(|err| err.context(context_fn()))
}
}

impl<T, E> Context<T> for Result<T, E>
where
E: StdError + Send + Sync + 'static,
{
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Introspect + 'static,
{
self.map_err(|err| Error::from(err).context(context))
}

fn with_context<C, F>(self, context_fn: F) -> Result<T, Error>
where
C: Introspect + 'static,
F: FnOnce() -> C,
{
self.map_err(|err| Error::from(err).context(context_fn()))
}
}

// Restricts implementations of `Context` only to those contained in
// this module
mod sealed {
use super::{Error, StdError};

pub trait Sealed {}

impl<T> Sealed for Result<T, Error> {}

impl<T, E> Sealed for Result<T, E> where E: StdError + 'static {}
}

#[cfg(test)]
mod tests {
//! Tests to ensure `Context` produces output correctly.

use crate::error::Error;
use std::{io, io::ErrorKind};

// Message source root error with no context
#[test]
fn debug_behavior_msg_no_context() {
let error = Error::msg("error message");
let debug = format!("{:?}", error);
let expected = "error message".to_string();
assert_eq!(expected, debug);
}

// Message source root error with a single context message
#[test]
fn debug_behavior_msg_single_context() {
let error = Error::msg("error message").context("context");
let debug = format!("{:?}", error);
let expected = "context\n\nCaused by: \n 0: error message".to_string();
assert_eq!(expected, debug);
}

// Message source root error with multiple context messages
#[test]
fn debug_behavior_msg_multiple_context() {
let error = Error::msg("error message")
.context("context 1")
.context("context 2");
let debug = format!("{:?}", error);
let expected =
"context 2\n\nCaused by: \n 0: context 1\n 1: error message".to_string();
assert_eq!(expected, debug);
}

// Dynamic error source with no context
#[test]
fn debug_behavior_std_no_context() {
let error = Error::from(io::Error::new(
ErrorKind::ConnectionRefused,
"connection refused",
));

let debug = format!("{:?}", error);
let expected = "connection refused".to_string();
assert_eq!(expected, debug);
}

// Dynamic error source with a single context message
#[test]
fn debug_behavior_std_single_context() {
let error = Error::from(io::Error::new(
ErrorKind::ConnectionRefused,
"connection refused",
))
.context("context");

let debug = format!("{:?}", error);
let expected = "context\n\nCaused by: \n 0: connection refused".to_string();
assert_eq!(expected, debug);
}

// Dynamic error source with multiple context messages
#[test]
fn debug_behavior_std_multiple_context() {
let error = Error::from(io::Error::new(
ErrorKind::ConnectionRefused,
"connection refused",
))
.context("context 1")
.context("context 2");

let debug = format!("{:?}", error);
let expected =
"context 2\n\nCaused by: \n 0: context 1\n 1: connection refused".to_string();
assert_eq!(expected, debug);
}
}
Loading