Skip to content

Commit

Permalink
fix: entropy plugin exec and remove legacy plugin handling
Browse files Browse the repository at this point in the history
  • Loading branch information
j-lanson committed Nov 5, 2024
1 parent be226d7 commit 824d06d
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 131 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion config/Hipcheck.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
plugin "mitre/review" version="0.1.0" manifest="./plugins/review/plugin.kdl"
plugin "mitre/typo" version="0.1.0" manifest="./plugins/typo/plugin.kdl"
plugin "mitre/affiliation" version="0.1.0" manifest="./plugins/affiliation/plugin.kdl"
plugin "mitre/entropy" version="0.1.0"
plugin "mitre/entropy" version="0.1.0" manifest="./plugins/entropy/plugin.kdl"
plugin "mitre/churn" version="0.1.0" manifest="./plugins/churn/plugin.kdl"
}
patch {
Expand Down Expand Up @@ -41,6 +41,8 @@ analyze {

analysis "mitre/entropy" policy="(eq 0 (count (filter (gt 8.0) $)))" {
langs-file "./config/Langs.toml"
entropy-threshold 10.0
commit-percentage 0.0
}
analysis "mitre/churn" policy="(lte (divz (count (filter (gt 3) $)) (count $)) 0.02)" {
langs-file "./config/Langs.toml"
Expand Down
8 changes: 4 additions & 4 deletions config/Hipcheck.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#==============================================================================
# Risk
#
#
# Configuration related to the overall risk tolerance across all analyses.
#==============================================================================

Expand Down Expand Up @@ -101,7 +101,7 @@ weight = 1

# The threshold for the number of binary files present in the repository,
# over which a repository will be marked as failing this analysis.
#
#
# Default: 0
binary_file_threshold = 0

Expand Down Expand Up @@ -364,9 +364,9 @@ weight = 1
value_threshold = 3

# This is the month range in which the committer needs to have the above value_threshold
# committer must have commits counts more or equal to X (value_threshold) commits
# committer must have commits counts more or equal to X (value_threshold) commits
# since Y (trust_month_count_threshold) months ago
#
#
# Default: 3
trust_month_count_threshold = 3

Expand Down
1 change: 1 addition & 0 deletions hipcheck-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ proc-macro = true
[dependencies]
anyhow = "1.0.91"
convert_case = "0.6.0"
log = "0.4.22"
proc-macro2 = "1.0.89"
quote = "1.0.37"
syn = { version = "2.0.87", features = ["full", "printing"] }
2 changes: 1 addition & 1 deletion hipcheck-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub fn queries(_item: TokenStream) -> TokenStream {
};
agg.extend(out);
}
eprintln!(
log::info!(
"Auto-generating Plugin::queries() with {} detected queries",
q_lock.len()
);
Expand Down
66 changes: 7 additions & 59 deletions hipcheck/src/analysis/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

use crate::{
analysis::AnalysisProvider,
config::{
visit_leaves, Analysis, AnalysisTree, WeightTreeProvider, DEFAULT_QUERY, MITRE_PUBLISHER,
},
config::{visit_leaves, Analysis, AnalysisTree, WeightTreeProvider},
engine::HcEngine,
error::Result,
hc_error,
plugin::{QueryResult, MITRE_LEGACY_PLUGINS},
plugin::QueryResult,
policy_exprs::Executor,
shell::spinner_phase::SpinnerPhase,
};
use indextree::{Arena, NodeId};
#[cfg(test)]
use num_traits::identities::Zero;
use serde_json::Value;
use std::{collections::HashMap, default::Default};

#[cfg(test)]
Expand Down Expand Up @@ -54,25 +51,12 @@ pub struct PluginAnalysisResults {
}

impl PluginAnalysisResults {
pub fn get_legacy(&self, analysis: &str) -> Option<&PluginAnalysisResult> {
if MITRE_LEGACY_PLUGINS.contains(&analysis) {
let key = Analysis::legacy(analysis);
self.table.get(&key)
} else {
None
}
pub fn get_legacy(&self, _analysis: &str) -> Option<&PluginAnalysisResult> {
None
}
/// Get all results from non-legacy analyses.
pub fn plugin_results(&self) -> impl Iterator<Item = (&Analysis, &PluginAnalysisResult)> {
self.table.iter().filter_map(|(analysis, result)| {
if MITRE_LEGACY_PLUGINS.contains(&analysis.plugin.as_str())
&& analysis.publisher == MITRE_PUBLISHER
{
None
} else {
Some((analysis, result))
}
})
self.table.iter()
}
}

Expand All @@ -82,15 +66,7 @@ pub struct Score {
}

#[salsa::query_group(ScoringProviderStorage)]
pub trait ScoringProvider: HcEngine + AnalysisProvider + WeightTreeProvider {
fn wrapped_query(
&self,
publisher: String,
plugin: String,
query: String,
key: Value,
) -> Result<QueryResult>;
}
pub trait ScoringProvider: HcEngine + AnalysisProvider + WeightTreeProvider {}

#[cfg(test)]
fn normalize_st_internal(node: NodeId, tree: &mut Arena<ScoreTreeNode>) -> f64 {
Expand Down Expand Up @@ -214,34 +190,6 @@ pub struct ScoreTreeNode {
pub weight: f64,
}

fn wrapped_query(
db: &dyn ScoringProvider,
publisher: String,
plugin: String,
query: String,
key: Value,
) -> Result<QueryResult> {
if publisher == *MITRE_PUBLISHER && MITRE_LEGACY_PLUGINS.contains(&plugin.as_str()) {
if query != *DEFAULT_QUERY {
return Err(hc_error!("legacy analyses only have a default query"));
}
match plugin.as_str() {
ACTIVITY_PHASE => db.activity_analysis(),
AFFILIATION_PHASE => db.affiliation_analysis(),
BINARY_PHASE => db.binary_analysis(),
CHURN_PHASE => db.churn_analysis(),
ENTROPY_PHASE => db.entropy_analysis(),
IDENTITY_PHASE => db.identity_analysis(),
FUZZ_PHASE => db.fuzz_analysis(),
REVIEW_PHASE => db.review_analysis(),
TYPO_PHASE => db.typo_analysis(),
error => Err(hc_error!("Unrecognized legacy analysis '{}'", error)),
}
} else {
db.query(publisher, plugin, query, key)
}
}

pub fn score_results(_phase: &SpinnerPhase, db: &dyn ScoringProvider) -> Result<ScoringResults> {
// Scoring should be performed by the construction of a "score tree" where scores are the
// nodes and weights are the edges. The leaves are the analyses themselves, which either
Expand All @@ -261,7 +209,7 @@ pub fn score_results(_phase: &SpinnerPhase, db: &dyn ScoringProvider) -> Result<

for analysis in analysis_tree.get_analyses() {
// Perform query, passing target in JSON
let response = db.wrapped_query(
let response = db.query(
analysis.0.publisher.clone(),
analysis.0.plugin.clone(),
analysis.0.query.clone(),
Expand Down
14 changes: 0 additions & 14 deletions hipcheck/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ pub trait CommitConfigQuery: ConfigSource {
fn contributor_trust_month_count_threshold(&self) -> Result<u64>;
}

pub static MITRE_PUBLISHER: &str = "mitre";
pub static DEFAULT_QUERY: &str = "";

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -517,19 +516,6 @@ pub struct Analysis {
pub plugin: String,
pub query: String,
}
impl Analysis {
pub fn new(publisher: &str, plugin: &str, query: &str) -> Analysis {
Analysis {
publisher: publisher.to_owned(),
plugin: plugin.to_owned(),
query: query.to_owned(),
}
}

pub fn legacy(analysis: &str) -> Analysis {
Analysis::new(MITRE_PUBLISHER, analysis, DEFAULT_QUERY)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PoliciedAnalysis(pub Analysis, pub String);
Expand Down
12 changes: 6 additions & 6 deletions hipcheck/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn query(
// (with salsa memo-ization) to get the needed data, and resume our
// current query by providing the plugin the answer.
loop {
eprintln!("Query needs more info, recursing...");
log::trace!("Query needs more info, recursing...");
let answer = db
.query(
ar.publisher.clone(),
Expand All @@ -107,7 +107,7 @@ fn query(
ar.key.clone(),
)?
.value;
eprintln!("Got answer, resuming");
log::trace!("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 All @@ -134,7 +134,7 @@ pub fn async_query(
};
// Initiate the query. If remote closed or we got our response immediately,
// return
eprintln!("Querying: {query}, key: {key:?}");
log::trace!("Querying: {query}, key: {key:?}");
let mut ar = match p_handle.query(query, key).await? {
PluginResponse::RemoteClosed => {
return Err(hc_error!("Plugin channel closed unexpected"));
Expand All @@ -148,7 +148,7 @@ pub fn async_query(
// (with salsa memo-ization) to get the needed data, and resume our
// current query by providing the plugin the answer.
loop {
eprintln!("Awaiting result, now recursing");
log::trace!("Awaiting result, now recursing");
let answer = async_query(
Arc::clone(&core),
ar.publisher.clone(),
Expand All @@ -158,7 +158,7 @@ pub fn async_query(
)
.await?
.value;
eprintln!("Resuming query with answer {answer:?}");
log::trace!("Resuming query with answer {answer:?}");
ar = match p_handle.resume_query(ar, answer).await? {
PluginResponse::RemoteClosed => {
return Err(hc_error!("Plugin channel closed unexpected"));
Expand Down Expand Up @@ -196,7 +196,7 @@ impl HcEngineImpl {
// independent of Salsa.
pub fn new(executor: PluginExecutor, plugins: Vec<PluginWithConfig>) -> Result<Self> {
let runtime = RUNTIME.handle();
eprintln!("Starting HcPluginCore");
log::info!("Starting HcPluginCore");
let core = runtime.block_on(HcPluginCore::new(executor, plugins))?;
let mut engine = HcEngineImpl {
storage: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions hipcheck/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use crate::plugin::{get_plugin_key, manager::*, plugin_id::PluginId, types::
pub use arch::{get_current_arch, try_set_arch, Arch};
pub use download_manifest::{ArchiveFormat, DownloadManifest, HashAlgorithm, HashWithDigest};
pub use plugin_manifest::{PluginManifest, PluginName, PluginPublisher, PluginVersion};
pub use retrieval::{retrieve_plugins, MITRE_LEGACY_PLUGINS};
pub use retrieval::retrieve_plugins;
use serde_json::Value;
use std::collections::HashMap;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl ActivePlugin {
concerns: vec![],
};

eprintln!("Resuming query");
log::trace!("Resuming query");

Ok(self.channel.query(query).await?.into())
}
Expand Down
12 changes: 0 additions & 12 deletions hipcheck/src/plugin/retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::{
cache::plugin::HcPluginCache,
config::MITRE_PUBLISHER,
error::Error,
hc_error,
plugin::{
Expand All @@ -27,9 +26,6 @@ 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; 1] = ["entropy"];

/// determine all of the plugins that need to be run and locate download them, if they do not exist
pub fn retrieve_plugins(
policy_plugins: &[PolicyPlugin],
Expand All @@ -38,14 +34,6 @@ pub fn retrieve_plugins(
let mut required_plugins = HashSet::new();

for policy_plugin in policy_plugins.iter() {
// TODO: while the legacy passes are still integrated in the main codebase, we skip downloading them!
if policy_plugin.name.publisher.0.as_str() == MITRE_PUBLISHER
&& MITRE_LEGACY_PLUGINS
.iter()
.any(|x| *x == policy_plugin.name.name.0.as_str())
{
continue;
}
retrieve_plugin(
policy_plugin.get_plugin_id(),
&policy_plugin.manifest,
Expand Down
Loading

0 comments on commit 824d06d

Please sign in to comment.