diff --git a/compliance-agent/src/pipeline/cve_issues.rs b/compliance-agent/src/pipeline/cve_issues.rs index d0b8608..8ebdcd8 100644 --- a/compliance-agent/src/pipeline/cve_issues.rs +++ b/compliance-agent/src/pipeline/cve_issues.rs @@ -238,8 +238,9 @@ const AUTOCLOSE_PER_SCAN: usize = 20; /// Close the open issues in `settled` (each with the VEX statement that /// settles its CVE) as `ISSUE_AUTOCLOSE` says: not at all, only counted, or /// at most [`AUTOCLOSE_PER_SCAN`] with a comment. Every Gitea request waits -/// for its instance's slot ([`crate::trackers::gitea_limit`]). Logs one line -/// per target with counts only. +/// for its instance's slot ([`crate::trackers::gitea_limit`]). Unless off, +/// logs one line per target and scan with counts only, also when there is +/// nothing to close, so "ran, nothing to close" shows in the log. /// /// # Returns /// @@ -252,9 +253,6 @@ async fn autoclose( target: &CveIssueTarget<'_>, settled: &[(&TrackerIssue, &VexAssessment)], ) -> Result { - if settled.is_empty() { - return Ok(0); - } let tenant_id = target.tenant_id; let target_id = target.target_id; match target.autoclose { @@ -943,11 +941,46 @@ mod tests { pool.drop_tenant("cve-issues-vex").await.ok(); } + /// Log lines written while the returned guard lives (on this thread; a + /// `#[tokio::test]` runs on one). + fn capture_logs() -> (Arc>>, tracing::subscriber::DefaultGuard) { + let buffer: Arc>> = Arc::default(); + let writer = buffer.clone(); + let subscriber = tracing_subscriber::fmt() + .with_ansi(false) + .with_max_level(tracing::Level::INFO) + .with_writer(move || LogWriter(writer.clone())) + .finish(); + (buffer, tracing::subscriber::set_default(subscriber)) + } + + struct LogWriter(Arc>>); + + impl std::io::Write for LogWriter { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + self.0.lock().unwrap().extend_from_slice(bytes); + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + /// The autoclose summary lines logged into `logs`. + fn summaries(logs: &Mutex>) -> Vec { + String::from_utf8_lossy(&logs.lock().unwrap()) + .lines() + .filter(|l| l.contains("CVE issues settled by VEX")) + .map(str::to_owned) + .collect() + } + #[tokio::test] async fn autoclose_off_and_dry_run_leave_the_issue_open() { let Some((pool, db)) = test_db("cve-issues-vex-off").await else { return; }; + let (logs, _guard) = capture_logs(); let calls: Calls = Arc::default(); let tracker = tracker_at(fake_gitea(calls.clone()).await); let mut okhttp = alert("okhttp", "3.12.0", "GHSA-3cqm-mf7h-prrj", "HIGH"); @@ -969,8 +1002,11 @@ mod tests { CveIssueCounts::default() ); assert_eq!(calls.lock().unwrap().len(), before); + // Off logs no summary. + assert!(summaries(&logs).is_empty()); - // Dry run, against a Gitea that refuses every write: only counted. + // Dry run, against a Gitea that refuses every write: only counted, + // and a target with nothing to close says so too. let writes: Calls = Arc::default(); let read_only = tracker_at(read_only_gitea(writes.clone()).await); let dry = vex_target(&db, &read_only, IssueAutoclose::DryRun); @@ -979,6 +1015,27 @@ mod tests { CveIssueCounts::default() ); assert!(writes.lock().unwrap().is_empty(), "{:?}", writes.lock()); + let other = CveIssueTarget { + target_id: "target-2", + ..vex_target(&db, &read_only, IssueAutoclose::DryRun) + }; + sync_cve_issues(&other, &[], true).await.unwrap(); + let lines = summaries(&logs); + assert_eq!(lines.len(), 2, "{lines:?}"); + assert!( + lines[0].contains("tenant_id=\"cve-issues-vex\"") + && lines[0].contains("target_id=\"target-1\"") + && lines[0].contains("would_close=1 not_affected=0 fixed=1"), + "{}", + lines[0] + ); + assert!( + lines[1].contains("target_id=\"target-2\"") && lines[1].contains("would_close=0"), + "{}", + lines[1] + ); + // No title or path in the line. + assert!(!lines[0].contains("okhttp"), "{}", lines[0]); assert_eq!( db.tracker_issues() .count_documents(doc! { "status": "open" })