CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 6s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 6m47s
294 lines
11 KiB
Rust
294 lines
11 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-test-data", "semgrep", "fixtures/dotnet/Program.cs"),
|
|
finding("fp-excluded", "semgrep", "docs/samples/app.py"),
|
|
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")
|
|
.with_test_data(vec!["fixtures".to_string()])
|
|
.with_excluded(vec!["docs/samples".to_string()]);
|
|
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, vendored, test data and excluded closed, each with its
|
|
// reason; the rest kept.
|
|
assert_eq!(closed, 4);
|
|
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)
|
|
);
|
|
let test_data = row(&db, "fp-test-data").await;
|
|
assert_eq!(test_data.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&test_data),
|
|
Some(ScanChangeKind::NowTestData)
|
|
);
|
|
let excluded = row(&db, "fp-excluded").await;
|
|
assert_eq!(excluded.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&excluded),
|
|
Some(ScanChangeKind::NowExcluded)
|
|
);
|
|
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();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn only_what_was_left_out_is_closed_for_a_stage_without_a_seen_set() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"leftout_{}",
|
|
&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: PLC findings in an excluded path, in test data and elsewhere,
|
|
// and one of another scanner in the excluded path.
|
|
let mut decided = finding("fp-plc-decided", "plc-control-logic", "legacy/Old.st");
|
|
decided.status = FindingStatus::AcceptedRisk;
|
|
db.findings()
|
|
.insert_many([
|
|
finding("fp-plc-excluded", "plc-control-logic", "legacy/Old.st"),
|
|
finding("fp-plc-fixture", "plc-control-logic", "fixtures/Pump.xml"),
|
|
finding("fp-plc-kept", "plc-control-logic", "src/Main.st"),
|
|
finding("fp-other-scanner", "semgrep", "legacy/tool.py"),
|
|
decided,
|
|
])
|
|
.await
|
|
.unwrap();
|
|
|
|
// Act
|
|
let mut run = RunFindings::new("run-plc")
|
|
.with_excluded(vec!["legacy".to_string()])
|
|
.with_test_data(vec!["fixtures".to_string()]);
|
|
let closed = run
|
|
.resolve_left_out(&db, TARGET, Some("plc-control-logic"))
|
|
.await
|
|
.unwrap();
|
|
|
|
// Assert: only the left-out PLC findings close, each with its reason;
|
|
// what the scan did not see otherwise is not closed as fixed.
|
|
assert_eq!(closed, 2);
|
|
assert_eq!(run.status_changed().len(), 2);
|
|
let excluded = row(&db, "fp-plc-excluded").await;
|
|
assert_eq!(excluded.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&excluded),
|
|
Some(ScanChangeKind::NowExcluded)
|
|
);
|
|
let fixture = row(&db, "fp-plc-fixture").await;
|
|
assert_eq!(
|
|
last_scan_change(&fixture),
|
|
Some(ScanChangeKind::NowTestData)
|
|
);
|
|
assert_eq!(row(&db, "fp-plc-kept").await.status, FindingStatus::Open);
|
|
assert_eq!(
|
|
row(&db, "fp-other-scanner").await.status,
|
|
FindingStatus::Open
|
|
);
|
|
assert_eq!(
|
|
row(&db, "fp-plc-decided").await.status,
|
|
FindingStatus::AcceptedRisk,
|
|
"a person's decision stays"
|
|
);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn left_out_findings_close_whichever_scanner_stored_them() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"leftoutall_{}",
|
|
&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 findings of scanners that do not run
|
|
// now (no C, no BitBake left in the tree), one in the test data that this
|
|
// run still reports, and one outside the left-out paths.
|
|
db.findings()
|
|
.insert_many([
|
|
finding("fp-cppcheck", "cppcheck", "fixtures/firmware/sensors.c"),
|
|
finding("fp-recipe", "yocto-config", "legacy/image.bb"),
|
|
finding("fp-reported", "gitleaks", "fixtures/app/.env.local"),
|
|
finding("fp-kept", "cppcheck", "src/main.c"),
|
|
])
|
|
.await
|
|
.unwrap();
|
|
|
|
// Act: only gitleaks completes, and it reports fp-reported again.
|
|
let mut run = RunFindings::new("run-all")
|
|
.with_excluded(vec!["legacy".to_string()])
|
|
.with_test_data(vec!["fixtures".to_string()]);
|
|
run.completed("gitleaks");
|
|
let mut seen = vec![finding(
|
|
"fp-reported",
|
|
"gitleaks",
|
|
"fixtures/app/.env.local",
|
|
)];
|
|
run.write_back(&db, &mut seen).await.unwrap();
|
|
run.resolve_missing(&db, TARGET, &[]).await.unwrap();
|
|
let closed = run.resolve_left_out(&db, TARGET, None).await.unwrap();
|
|
|
|
// Assert: the left-out findings close with their reason, although their
|
|
// scanner did not run; one this run reported, and one outside, stay.
|
|
assert_eq!(closed, 2);
|
|
let cppcheck = row(&db, "fp-cppcheck").await;
|
|
assert_eq!(cppcheck.status, FindingStatus::Resolved);
|
|
assert_eq!(
|
|
last_scan_change(&cppcheck),
|
|
Some(ScanChangeKind::NowTestData)
|
|
);
|
|
let recipe = row(&db, "fp-recipe").await;
|
|
assert_eq!(recipe.status, FindingStatus::Resolved);
|
|
assert_eq!(last_scan_change(&recipe), Some(ScanChangeKind::NowExcluded));
|
|
assert_eq!(
|
|
row(&db, "fp-reported").await.status,
|
|
FindingStatus::Open,
|
|
"a finding this run reported is not closed"
|
|
);
|
|
assert_eq!(row(&db, "fp-kept").await.status, FindingStatus::Open);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|