From c16e91f98160dc8ba5928067c124c861cdb44918 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Wed, 4 Nov 2026 09:32:36 +0000 Subject: [PATCH] fix(plc): test data and excluded paths leave the PLC analysis too (#828) --- .../src/pipeline/finding_store.rs | 65 ++++++++++++++++++ compliance-agent/src/pipeline/orchestrator.rs | 26 +++++++ compliance-agent/tests/resolve_findings.rs | 67 +++++++++++++++++++ docs/guide/plc.md | 3 + 4 files changed, 161 insertions(+) diff --git a/compliance-agent/src/pipeline/finding_store.rs b/compliance-agent/src/pipeline/finding_store.rs index d8e09d0..1035902 100644 --- a/compliance-agent/src/pipeline/finding_store.rs +++ b/compliance-agent/src/pipeline/finding_store.rs @@ -782,6 +782,71 @@ impl RunFindings { Ok(closed) } + /// Close the undecided findings of `scanner` on target `target_id` whose + /// file lies in the excluded paths or the test data this run left out + /// ([`Self::with_excluded`], [`Self::with_test_data`]), each with its + /// reason. For a stage that stores its findings without this object (the + /// PLC analysis), so nothing it no longer sees is closed as fixed; only + /// what was left out. + /// + /// # Returns + /// + /// How many findings were closed. + /// + /// # Errors + /// + /// A database error. + pub async fn resolve_left_out( + &mut self, + db: &Database, + target_id: &str, + scanner: &str, + ) -> Result { + if self.excluded.is_empty() && self.test_data.is_empty() { + return Ok(0); + } + let rows: Vec = db + .findings() + .clone_with_type::() + .find(doc! { + "repo_id": target_id, + "scanner": scanner, + "status": { "$in": UNDECIDED.to_vec() }, + }) + .projection(doc! { "file_path": 1 }) + .await? + .try_collect() + .await?; + let (mut excluded, mut test_data) = (Vec::new(), Vec::new()); + for row in &rows { + let (Ok(id), Ok(file)) = (row.get_object_id("_id"), row.get_str("file_path")) else { + continue; + }; + if crate::pipeline::vendor::in_vendor_dir(file, &self.excluded) { + excluded.push(id); + } else if crate::pipeline::vendor::in_vendor_dir(file, &self.test_data) { + test_data.push(id); + } + } + self.set_status( + db, + &excluded, + &UNDECIDED, + FindingStatus::Resolved, + ScanChangeKind::NowExcluded, + ) + .await?; + self.set_status( + db, + &test_data, + &UNDECIDED, + FindingStatus::Resolved, + ScanChangeKind::NowTestData, + ) + .await?; + Ok(u32::try_from(excluded.len() + test_data.len()).unwrap_or(u32::MAX)) + } + /// Move the findings `ids` whose status is still one of `from` to `to`, /// with a history entry saying this run did it and why. The `from` guard /// leaves alone a finding a person changed in the meantime. diff --git a/compliance-agent/src/pipeline/orchestrator.rs b/compliance-agent/src/pipeline/orchestrator.rs index 2f22ea1..3983bba 100644 --- a/compliance-agent/src/pipeline/orchestrator.rs +++ b/compliance-agent/src/pipeline/orchestrator.rs @@ -1440,10 +1440,18 @@ impl PipelineOrchestrator { // CODESYS projects in their native format: their control logic is not // read, and the run says so (#165). let mut native = std::collections::BTreeSet::new(); + // What the target's settings exclude and the test data leave each tree + // first, as in a code scan (#820, #825). + let mut left_out = LeftOut::default(); for a in &sources { let Some(path) = ingest_set.get(&a.id).and_then(|ia| ia.working_path.clone()) else { continue; }; + let pruned = self + .prune_left_out(&path, &target.scan_config.exclude_paths, scan_run_id) + .await; + left_out.excluded.extend(pruned.excluded); + left_out.test_data.extend(pruned.test_data); native.extend(crate::pipeline::plc::native_projects(&path)); analysed.push(( path.clone(), @@ -1544,6 +1552,24 @@ impl PipelineOrchestrator { "Refreshed control_refs on {refreshed_count} existing PLC findings" ); } + // Findings an earlier scan stored in what is now left out close with + // that reason, and their issues follow. + let mut left_out_run = crate::pipeline::finding_store::RunFindings::new(scan_run_id) + .with_excluded(left_out.excluded) + .with_test_data(left_out.test_data); + let closed = left_out_run + .resolve_left_out(&self.db, target_id, "plc-control-logic") + .await?; + if closed > 0 { + tracing::info!(target_id, closed, "PLC findings in left-out paths resolved"); + crate::pipeline::issue_sync::IssueSync { + db: self.db.clone(), + config: self.config.clone(), + secrets: self.secrets.clone(), + net_guard: self.net_guard, + } + .spawn(left_out_run.status_changed().to_vec()); + } if !all_sbom.is_empty() { if let Err(e) = self diff --git a/compliance-agent/tests/resolve_findings.rs b/compliance-agent/tests/resolve_findings.rs index 0babedc..d093ab1 100644 --- a/compliance-agent/tests/resolve_findings.rs +++ b/compliance-agent/tests/resolve_findings.rs @@ -161,3 +161,70 @@ async fn a_scan_closes_what_it_no_longer_finds_and_reopens_what_returns() { 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, "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(); +} diff --git a/docs/guide/plc.md b/docs/guide/plc.md index 7ee4434..a97d5d4 100644 --- a/docs/guide/plc.md +++ b/docs/guide/plc.md @@ -127,6 +127,9 @@ my-plc-project/ A native `.project` or `.projectarchive` without an export is listed on the scan run under **SPS-Projekt (natives Format)**, so a missing export never reads as clean control logic. +- Test data (`fixtures/`, `testdata/` and similar) and the paths excluded under + **Von der Prüfung ausgenommen** are left out of the control-logic analysis as + well; findings an earlier scan reported there are closed with that reason. - CODESYS's built-in Git integration, which stores an exported representation, works too — as long as the committed form is PLCopen XML / textual.