Skip to content

Commit

Permalink
chore: delete unused Salsa dbs in config.rs and clean up policy_exprs…
Browse files Browse the repository at this point in the history
… source

Signed-off-by: jlanson <[email protected]>
  • Loading branch information
j-lanson committed Dec 16, 2024
1 parent 855dc79 commit 4b11b5c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 271 deletions.
179 changes: 1 addition & 178 deletions hipcheck/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use crate::{
policy_exprs::{std_parse, Expr},
score::*,
util::fs as file,
BINARY_CONFIG_FILE, F64, LANGS_FILE, ORGS_FILE, TYPO_FILE,
F64,
};
use indextree::{Arena, NodeEdge, NodeId};
use num_traits::identities::Zero;
use pathbuf::pathbuf;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use smart_default::SmartDefault;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -472,47 +471,6 @@ pub trait RiskConfigQuery: ConfigSource {
fn risk_policy(&self) -> Result<Rc<Expr>>;
}

/// Query for accessing the languages analysis config
#[salsa::query_group(LanguagesConfigQueryStorage)]
pub trait LanguagesConfigQuery: ConfigSource {
/// Returns the langs file path relative to the config file
fn langs_file_rel(&self) -> Rc<String>;
/// Returns the langs file absolute path
fn langs_file(&self) -> Result<Rc<PathBuf>>;
}

/// Queries for accessing the practices analysis config
#[salsa::query_group(PracticesConfigQueryStorage)]
pub trait PracticesConfigQuery: ConfigSource {
/// Returns the binary formats file path relative to the
/// config file
fn binary_formats_file_rel(&self) -> Rc<String>;
/// Returns the binary formats file absolute path
fn binary_formats_file(&self) -> Result<Rc<PathBuf>>;
}

/// Queries for accessing the attacks analysis config
#[salsa::query_group(AttacksConfigQueryStorage)]
pub trait AttacksConfigQuery: CommitConfigQuery {
/// Returns the typo file path relative to the config file
fn typo_file_rel(&self) -> Rc<String>;
/// Returns the typo file absolute path
fn typo_file(&self) -> Result<Rc<PathBuf>>;
}

/// Queries for accessing the commit analysis config
#[salsa::query_group(CommitConfigQueryStorage)]
pub trait CommitConfigQuery: ConfigSource {
/// Returns the orgs file path relative to the config file
fn orgs_file_rel(&self) -> Rc<String>;
/// Returns the orgs file absolute path
fn orgs_file(&self) -> Result<Rc<PathBuf>>;
/// Returns the contributor trust analysis count threshold
fn contributor_trust_value_threshold(&self) -> Result<u64>;
/// Returns the contributor trust analysis month threshold
fn contributor_trust_month_count_threshold(&self) -> Result<u64>;
}

pub static DEFAULT_QUERY: &str = "";

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -875,138 +833,3 @@ fn risk_policy(db: &dyn RiskConfigQuery) -> Result<Rc<Expr>> {

Ok(Rc::new(expr))
}

fn langs_file_rel(_db: &dyn LanguagesConfigQuery) -> Rc<String> {
Rc::new(LANGS_FILE.to_string())
}

fn langs_file(db: &dyn LanguagesConfigQuery) -> Result<Rc<PathBuf>> {
if let Some(config_dir) = db.config_dir() {
return Ok(Rc::new(pathbuf![
config_dir.as_ref(),
db.langs_file_rel().as_ref()
]));
}

let options = vec!["mitre/churn", "mitre/entropy"];
let policy_file = db.policy();
for opt in options {
if let Some(langs_config) = policy_file.get_config(opt) {
if let Some(v_filepath) = langs_config.get("langs-file") {
let Value::String(filepath) = v_filepath else {
return Err(hc_error!("'langs-file' was not a string"));
};
return Ok(Rc::new(Path::new(&filepath).to_path_buf()));
}
};
}

Err(hc_error!("Cannot find path to languages config file in policy file. This file is necessary for running the linguist analysis."))
}

fn binary_formats_file_rel(_db: &dyn PracticesConfigQuery) -> Rc<String> {
Rc::new(BINARY_CONFIG_FILE.to_string())
}

fn binary_formats_file(db: &dyn PracticesConfigQuery) -> Result<Rc<PathBuf>> {
if let Some(config_dir) = db.config_dir() {
return Ok(Rc::new(pathbuf![
config_dir.as_ref(),
db.binary_formats_file_rel().as_ref()
]));
}

let policy_file = db.policy();
if let Some(binary_config) = policy_file.get_config("mitre/binary") {
if let Some(v_filepath) = binary_config.get("binary-file") {
let Value::String(filepath) = v_filepath else {
return Err(hc_error!("'binary-file' was not a string"));
};
return Ok(Rc::new(Path::new(&filepath).to_path_buf()));
}
};

Err(hc_error!("Cannot find path to binary config file in policy file. This file is necessary for running the binary analysis."))
}

fn typo_file_rel(_db: &dyn AttacksConfigQuery) -> Rc<String> {
Rc::new(TYPO_FILE.to_string())
}

fn typo_file(db: &dyn AttacksConfigQuery) -> Result<Rc<PathBuf>> {
if let Some(config_dir) = db.config_dir() {
return Ok(Rc::new(pathbuf![
config_dir.as_ref(),
db.typo_file_rel().as_ref()
]));
}

let policy_file = db.policy();
if let Some(typo_config) = policy_file.get_config("mitre/typo") {
if let Some(v_filepath) = typo_config.get("typo-file") {
let Value::String(filepath) = v_filepath else {
return Err(hc_error!("'typo-file' was not a string"));
};
return Ok(Rc::new(Path::new(&filepath).to_path_buf()));
}
};

Err(hc_error!("Cannot find path to typo config file in policy file. This file is necessary for running the typo analysis."))
}

fn orgs_file_rel(_db: &dyn CommitConfigQuery) -> Rc<String> {
Rc::new(ORGS_FILE.to_string())
}

fn orgs_file(db: &dyn CommitConfigQuery) -> Result<Rc<PathBuf>> {
if let Some(config_dir) = db.config_dir() {
return Ok(Rc::new(pathbuf![
config_dir.as_ref(),
db.orgs_file_rel().as_ref()
]));
}

let policy_file = db.policy();
if let Some(affiliation_config) = policy_file.get_config("mitre/affiliation") {
if let Some(v_filepath) = affiliation_config.get("orgs-file") {
let Value::String(filepath) = v_filepath else {
return Err(hc_error!("'orgs-file' was not a string"));
};
return Ok(Rc::new(Path::new(&filepath).to_path_buf()));
}
};

Err(hc_error!("Cannot find path to orgs config file in policy file. This file is necessary for running the affiliation analysis."))
}

fn contributor_trust_value_threshold(db: &dyn CommitConfigQuery) -> Result<u64> {
let policy_file = db.policy();
if let Some(trust_config) = policy_file.get_config("mitre/contributor-trust") {
if let Some(v_threshold) = trust_config.get("value-threshold") {
let Value::Number(threshold) = v_threshold else {
return Err(hc_error!("'value-threshold' was not a number"));
};
return threshold
.as_u64()
.ok_or_else(|| hc_error!("'value-threshold' was too large to be a u64"));
}
};

Err(hc_error!("Cannot find config for contributor trust value in policy file. This file is necessary for running the commit trust analysis."))
}

fn contributor_trust_month_count_threshold(db: &dyn CommitConfigQuery) -> Result<u64> {
let policy_file = db.policy();
if let Some(trust_config) = policy_file.get_config("mitre/contributor-trust") {
if let Some(v_threshold) = trust_config.get("month-count-threshold") {
let Value::Number(threshold) = v_threshold else {
return Err(hc_error!("'month-count-threshold' was not a number"));
};
return threshold
.as_u64()
.ok_or_else(|| hc_error!("'month-count-threshold' was too large to be a u64"));
}
};

Err(hc_error!("Cannot find config for contributor trust month threshold in policy file. This file is necessary for running the commit trust analysis."))
}
6 changes: 0 additions & 6 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,6 @@ fn print_missing() {
/// An `f64` that is never `NaN`.
type F64 = ordered_float::NotNan<f64>;

// Global variables for toml files per issue 157 config updates
const LANGS_FILE: &str = "Langs.toml";
const BINARY_CONFIG_FILE: &str = "Binary.toml";
const TYPO_FILE: &str = "Typos.toml";
const ORGS_FILE: &str = "Orgs.toml";

// Constants for exiting with error codes.
/// Indicates the program failed.
const EXIT_FAILURE: i32 = 1;
Expand Down
43 changes: 13 additions & 30 deletions hipcheck/src/policy_exprs/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

use crate::policy_exprs::{
expr::{
ArrayType as ExprArrayType, FuncReturnType, Function, FunctionDef, FunctionType, Op,
OpInfo, PrimitiveType, ReturnableType, Type, TypeChecker, Typed,
ArrayType as ExprArrayType, Function, FunctionDef, FunctionType, Op, PrimitiveType,
ReturnableType, Type, TypeChecker, Typed,
},
pass::ExprMutator,
Array as StructArray, Error, Expr, ExprVisitor, Function as StructFunction, Ident,
Lambda as StructLambda, Primitive, Result, F64,
Array as StructArray, Error, Expr, Function as StructFunction, Ident, Lambda as StructLambda,
Primitive, Result, F64,
};
use itertools::Itertools as _;
use jiff::{Span, Zoned};
use std::{
cmp::{Ordering, PartialEq},
collections::HashMap,
ops::Not as _,
};
use std::{cmp::Ordering, collections::HashMap, ops::Not as _};
use Expr::*;
use Primitive::*;

Expand Down Expand Up @@ -80,7 +76,7 @@ fn ty_filter(args: &[Type]) -> Result<ReturnableType> {
ReturnableType::Primitive(PrimitiveType::Bool) | ReturnableType::Unknown => {
Ok(ReturnableType::Array(arr_ty))
}
a => Err(Error::BadFuncArgType {
_ => Err(Error::BadFuncArgType {
name: "".to_owned(),
idx: 0,
expected: "a bool-returning lambda".to_owned(),
Expand Down Expand Up @@ -110,7 +106,7 @@ fn ty_higher_order_bool_fn(args: &[Type]) -> Result<ReturnableType> {
ReturnableType::Primitive(PrimitiveType::Bool) | ReturnableType::Unknown => {
Ok(ReturnableType::Primitive(PrimitiveType::Bool))
}
a => Err(Error::BadFuncArgType {
_ => Err(Error::BadFuncArgType {
name: "".to_owned(),
idx: 0,
expected: "a bool-returning lambda".to_owned(),
Expand Down Expand Up @@ -180,7 +176,6 @@ fn ty_divz(args: &[Type]) -> Result<ReturnableType> {
let opt_ty_1 = expect_primitive_at(args, 0)?;
let opt_ty_2 = expect_primitive_at(args, 1)?;
use PrimitiveType::*;
use ReturnableType::*;

let (bad, idx) = match (opt_ty_1, opt_ty_2) {
(None | Some(Int | Float), None | Some(Int | Float)) => return Ok(Float.into()),
Expand Down Expand Up @@ -265,13 +260,12 @@ fn ty_comp(args: &[Type]) -> Result<ReturnableType> {
})
}

fn ty_count(args: &[Type]) -> Result<ReturnableType> {
fn ty_count(_args: &[Type]) -> Result<ReturnableType> {
Ok(PrimitiveType::Int.into())
}

fn ty_avg(args: &[Type]) -> Result<ReturnableType> {
use PrimitiveType::*;
use ReturnableType::*;
let arr_ty = expect_array_at(args, 0)?;
match arr_ty {
None | Some(Int) | Some(Float) => Ok(Float.into()),
Expand All @@ -286,11 +280,10 @@ fn ty_avg(args: &[Type]) -> Result<ReturnableType> {

fn ty_duration(args: &[Type]) -> Result<ReturnableType> {
use PrimitiveType::*;
use ReturnableType::*;
let opt_ty_1 = expect_primitive_at(args, 0)?;
let opt_ty_2 = expect_primitive_at(args, 1)?;
match opt_ty_1 {
None | (Some(DateTime)) => (),
None | Some(DateTime) => (),
Some(got) => {
return Err(Error::BadFuncArgType {
name: "".to_owned(),
Expand All @@ -301,7 +294,7 @@ fn ty_duration(args: &[Type]) -> Result<ReturnableType> {
}
}
match opt_ty_2 {
None | (Some(DateTime)) => (),
None | Some(DateTime) => (),
Some(got) => {
return Err(Error::BadFuncArgType {
name: "".to_owned(),
Expand All @@ -316,9 +309,8 @@ fn ty_duration(args: &[Type]) -> Result<ReturnableType> {

fn ty_bool_unary(args: &[Type]) -> Result<ReturnableType> {
use PrimitiveType::*;
use ReturnableType::*;
match expect_primitive_at(args, 0)? {
None | (Some(Bool)) => Ok(PrimitiveType::Bool.into()),
None | Some(Bool) => Ok(PrimitiveType::Bool.into()),
Some(got) => Err(Error::BadFuncArgType {
name: "".to_owned(),
idx: 0,
Expand All @@ -330,11 +322,10 @@ fn ty_bool_unary(args: &[Type]) -> Result<ReturnableType> {

fn ty_bool_binary(args: &[Type]) -> Result<ReturnableType> {
use PrimitiveType::*;
use ReturnableType::*;
let opt_ty_1 = expect_primitive_at(args, 0)?;
let opt_ty_2 = expect_primitive_at(args, 1)?;
match opt_ty_1 {
None | (Some(Bool)) => (),
None | Some(Bool) => (),
Some(got) => {
return Err(Error::BadFuncArgType {
name: "".to_owned(),
Expand All @@ -345,7 +336,7 @@ fn ty_bool_binary(args: &[Type]) -> Result<ReturnableType> {
}
}
match opt_ty_2 {
None | (Some(Bool)) => (),
None | Some(Bool) => (),
Some(got) => {
return Err(Error::BadFuncArgType {
name: "".to_owned(),
Expand All @@ -369,8 +360,6 @@ impl Env<'_> {

/// Create the standard environment.
pub fn std() -> Self {
use FuncReturnType::*;
use PrimitiveType::*;
let mut env = Env::empty();

// Comparison functions.
Expand Down Expand Up @@ -821,7 +810,6 @@ fn add(env: &Env, args: &[Expr]) -> Result<Expr> {
.map_err(|err| Error::Datetime(err.to_string()))?,
)),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand All @@ -847,7 +835,6 @@ fn sub(env: &Env, args: &[Expr]) -> Result<Expr> {
.map_err(|err| Error::Datetime(err.to_string()))?,
)),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand All @@ -872,7 +859,6 @@ fn divz(env: &Env, args: &[Expr]) -> Result<Expr> {
Float(arg_1 / arg_2)
}),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand All @@ -889,7 +875,6 @@ fn duration(env: &Env, args: &[Expr]) -> Result<Expr> {
.map_err(|err| Error::Datetime(err.to_string()))?,
)),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand All @@ -901,7 +886,6 @@ fn and(env: &Env, args: &[Expr]) -> Result<Expr> {
let op = |arg_1, arg_2| match (arg_1, arg_2) {
(Bool(arg_1), Bool(arg_2)) => Ok(Bool(arg_1 && arg_2)),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand All @@ -913,7 +897,6 @@ fn or(env: &Env, args: &[Expr]) -> Result<Expr> {
let op = |arg_1, arg_2| match (arg_1, arg_2) {
(Bool(arg_1), Bool(arg_2)) => Ok(Bool(arg_1 || arg_2)),
(_, _) => Err(Error::BadType(name)),
_ => unreachable!(),
};

binary_primitive_op(name, env, args, op)
Expand Down
Loading

0 comments on commit 4b11b5c

Please sign in to comment.