153 lines
5.4 KiB
Rust
153 lines
5.4 KiB
Rust
//! #483: a group takes over what a person decided about its places one by
|
|
//! one, when they all carry the same decision; mixed decisions are listed per
|
|
//! place and leave the group open; a decision on the group itself is never
|
|
//! overridden. Runs against a live Mongo (`TEST_MONGODB_URI`) and skips
|
|
//! cleanly without one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
use compliance_agent::database::{Database, DatabasePool};
|
|
use compliance_agent::pipeline::dedup::compute_fingerprint;
|
|
use compliance_agent::pipeline::finding_store::RunFindings;
|
|
use compliance_agent::pipeline::grouping::{group_repetitive, GroupingPolicy};
|
|
use compliance_core::models::{Finding, FindingStatus, ScanChangeKind, ScanType, Severity};
|
|
use mongodb::bson::doc;
|
|
|
|
const PLACES: u32 = 6;
|
|
|
|
fn logging(target: &str, line: u32) -> Finding {
|
|
let file = "app/src/main/java/Main.java";
|
|
let mut f = Finding::new(
|
|
target.into(),
|
|
compute_fingerprint(&[target, "android_logging", file, &line.to_string()]),
|
|
"mobsfscan".into(),
|
|
ScanType::Sast,
|
|
"The app logs information".into(),
|
|
"The app logs information.".into(),
|
|
Severity::Low,
|
|
);
|
|
f.rule_id = Some("android_logging".into());
|
|
f.file_path = Some(file.into());
|
|
f.line_number = Some(line);
|
|
f
|
|
}
|
|
|
|
/// Store the single findings of `target` with the status `decide` gives each
|
|
/// line, then run a scan that groups them; the stored group.
|
|
async fn scan_after(
|
|
db: &Database,
|
|
target: &str,
|
|
decide: impl Fn(u32) -> FindingStatus,
|
|
) -> (Finding, RunFindings) {
|
|
let stored: Vec<Finding> = (1..=PLACES)
|
|
.map(|line| {
|
|
let mut f = logging(target, line);
|
|
f.status = decide(line);
|
|
f
|
|
})
|
|
.collect();
|
|
db.findings().insert_many(&stored).await.unwrap();
|
|
let mut findings: Vec<Finding> = (1..=PLACES).map(|line| logging(target, line)).collect();
|
|
group_repetitive(&mut findings, &GroupingPolicy::default());
|
|
let mut run = RunFindings::new("run-1");
|
|
run.completed("mobsfscan");
|
|
run.save_new(db, &mut findings).await.unwrap();
|
|
run.write_back(db, &mut findings).await.unwrap();
|
|
run.resolve_missing(db, target, &[]).await.unwrap();
|
|
let group = db
|
|
.findings()
|
|
.find_one(doc! { "repo_id": target, "occurrences.0": { "$exists": true } })
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
(group, run)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn groups_take_over_their_places_decisions() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"groupdec_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP group_decisions: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "group-decisions";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP group_decisions: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
|
|
// Every place marked a false positive: the group is one too, and its
|
|
// issue follows.
|
|
let (group, run) = scan_after(&db, "all-same", |_| FindingStatus::FalsePositive).await;
|
|
assert_eq!(group.status, FindingStatus::FalsePositive);
|
|
let change = group.status_history.last().expect("history");
|
|
assert_eq!(
|
|
change.scan.as_ref().map(|s| s.kind),
|
|
Some(ScanChangeKind::Inherited)
|
|
);
|
|
assert_eq!(
|
|
change.reason.as_deref(),
|
|
Some("inherited from 6 grouped findings")
|
|
);
|
|
assert!(run.status_changed().contains(&group.id.unwrap()));
|
|
assert!(group
|
|
.description
|
|
.contains("Main.java:1 [marked false_positive]"));
|
|
|
|
// Mixed: the group stays open and lists each place's decision.
|
|
let (group, _) = scan_after(&db, "mixed", |line| {
|
|
if line <= 3 {
|
|
FindingStatus::AcceptedRisk
|
|
} else {
|
|
FindingStatus::Open
|
|
}
|
|
})
|
|
.await;
|
|
assert_eq!(group.status, FindingStatus::Open);
|
|
let decisions: Vec<Option<FindingStatus>> = group
|
|
.occurrences
|
|
.iter()
|
|
.map(|o| o.decision.clone())
|
|
.collect();
|
|
assert_eq!(
|
|
decisions,
|
|
[
|
|
Some(FindingStatus::AcceptedRisk),
|
|
Some(FindingStatus::AcceptedRisk),
|
|
Some(FindingStatus::AcceptedRisk),
|
|
None,
|
|
None,
|
|
None
|
|
]
|
|
);
|
|
|
|
// A group stored before (#477) is set right on the next scan, but a
|
|
// person's decision about the group itself stays.
|
|
for (target, stored_status, expected) in [
|
|
("earlier-open", FindingStatus::Open, FindingStatus::Ignored),
|
|
(
|
|
"earlier-decided",
|
|
FindingStatus::AcceptedRisk,
|
|
FindingStatus::AcceptedRisk,
|
|
),
|
|
] {
|
|
let mut earlier: Vec<Finding> = (1..=PLACES).map(|line| logging(target, line)).collect();
|
|
group_repetitive(&mut earlier, &GroupingPolicy::default());
|
|
earlier[0].status = stored_status;
|
|
db.findings().insert_many(&earlier).await.unwrap();
|
|
let (group, _) = scan_after(&db, target, |_| FindingStatus::Ignored).await;
|
|
assert_eq!(group.status, expected, "{target}");
|
|
assert!(group
|
|
.occurrences
|
|
.iter()
|
|
.all(|o| o.decision == Some(FindingStatus::Ignored)));
|
|
}
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|