147 lines
5.0 KiB
Rust
147 lines
5.0 KiB
Rust
//! Findings a scan no longer reports are closed, and reopened when reported
|
|
//! again. 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::finding_store::RunFindings;
|
|
use compliance_core::models::{
|
|
Finding, FindingStatus, ScanChangeKind, ScanType, Severity, StatusChange,
|
|
};
|
|
use mongodb::bson::doc;
|
|
|
|
const TENANT: &str = "resolve-findings";
|
|
const TARGET: &str = "target-1";
|
|
|
|
fn finding(fingerprint: &str, scanner: &str, file: &str) -> Finding {
|
|
let mut f = Finding::new(
|
|
TARGET.into(),
|
|
fingerprint.into(),
|
|
scanner.into(),
|
|
ScanType::Sast,
|
|
format!("issue {fingerprint}"),
|
|
"desc".into(),
|
|
Severity::High,
|
|
);
|
|
f.file_path = Some(file.into());
|
|
f
|
|
}
|
|
|
|
async fn row(db: &Database, fingerprint: &str) -> Finding {
|
|
db.findings()
|
|
.find_one(doc! { "fingerprint": fingerprint })
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
}
|
|
|
|
/// The reason a scan recorded on the last history entry of `f`.
|
|
fn last_scan_change(f: &Finding) -> Option<ScanChangeKind> {
|
|
f.status_history
|
|
.last()
|
|
.filter(|c| c.by == StatusChange::SCAN_ACTOR)
|
|
.and_then(|c| c.scan.as_ref())
|
|
.map(|s| s.kind)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_scan_closes_what_it_no_longer_finds_and_reopens_what_returns() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"resolve_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP resolve_findings: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let db = pool.for_tenant_id(TENANT).await.unwrap();
|
|
|
|
// Arrange: an earlier scan stored these.
|
|
let mut decided = finding("fp-false-positive", "semgrep", "src/c.c");
|
|
decided.status = FindingStatus::FalsePositive;
|
|
let mut other_target = finding("fp-other-target", "semgrep", "src/a.c");
|
|
other_target.repo_id = "target-2".into();
|
|
db.findings()
|
|
.insert_many([
|
|
finding("fp-kept", "semgrep", "src/a.c"),
|
|
finding("fp-fixed", "semgrep", "src/b.c"),
|
|
finding("fp-vendor", "semgrep", "Drivers/STM32F4xx_HAL_Driver/x.c"),
|
|
finding("fp-lint", "clippy", "src/lib.rs"),
|
|
decided,
|
|
other_target,
|
|
])
|
|
.await
|
|
.unwrap();
|
|
|
|
// A run in which no scanner completed closes nothing.
|
|
let mut idle = RunFindings::new("run-idle");
|
|
assert_eq!(idle.resolve_missing(&db, TARGET, &[]).await.unwrap(), 0);
|
|
|
|
// Act: semgrep completes and reports only fp-kept.
|
|
let mut run = RunFindings::new("run-2");
|
|
run.completed("semgrep");
|
|
let mut seen = vec![finding("fp-kept", "semgrep", "src/a.c")];
|
|
run.write_back(&db, &mut seen).await.unwrap();
|
|
let vendor = vec!["Drivers".to_string()];
|
|
let closed = run.resolve_missing(&db, TARGET, &vendor).await.unwrap();
|
|
|
|
// Assert: fixed and vendored closed, each with its reason; the rest kept.
|
|
assert_eq!(closed, 2);
|
|
let fixed = row(&db, "fp-fixed").await;
|
|
assert_eq!(fixed.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&fixed),
|
|
Some(ScanChangeKind::NoLongerFound)
|
|
);
|
|
let vendored = row(&db, "fp-vendor").await;
|
|
assert_eq!(vendored.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&vendored),
|
|
Some(ScanChangeKind::NowVendorCode)
|
|
);
|
|
assert_eq!(row(&db, "fp-kept").await.status, FindingStatus::Open);
|
|
assert_eq!(
|
|
row(&db, "fp-false-positive").await.status,
|
|
FindingStatus::FalsePositive,
|
|
"a person's decision stays"
|
|
);
|
|
assert_eq!(
|
|
row(&db, "fp-lint").await.status,
|
|
FindingStatus::Open,
|
|
"a scanner that did not complete closes nothing"
|
|
);
|
|
assert_eq!(
|
|
row(&db, "fp-other-target").await.status,
|
|
FindingStatus::Open,
|
|
"another target's findings are not touched"
|
|
);
|
|
|
|
// Act: the fixed finding comes back. A scanner that did not complete in
|
|
// this run does not reopen it; one that did, does.
|
|
let mut quiet = RunFindings::new("run-3");
|
|
quiet
|
|
.save_new(&db, &mut [finding("fp-fixed", "semgrep", "src/b.c")])
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(row(&db, "fp-fixed").await.status, FindingStatus::Resolved);
|
|
|
|
let mut again = RunFindings::new("run-4");
|
|
again.completed("semgrep");
|
|
again
|
|
.save_new(&db, &mut [finding("fp-fixed", "semgrep", "src/b.c")])
|
|
.await
|
|
.unwrap();
|
|
let reopened = row(&db, "fp-fixed").await;
|
|
assert_eq!(reopened.status, FindingStatus::Open);
|
|
assert_eq!(
|
|
last_scan_change(&reopened),
|
|
Some(ScanChangeKind::FoundAgain)
|
|
);
|
|
assert_eq!(reopened.status_history.len(), 2);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|