From 4ff2c199262d0515b96fffd0e29da20b3204cd9a Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:06:56 +0100 Subject: [PATCH] fix(onboarding): a scan the target runs is listed once among its applicable scans (#1043) A scan has one rule per artifact it can read; the endpoint listed each, so a target with only a supplier SBOM showed its SBOM and CVE scans three times, twice as blocked ('braucht Quellcode', 'Firmware-Images werden noch nicht entpackt'). Once one rule runs a scan, the endpoint now lists it once, as running; a scan none runs keeps every reason why not. The scan plan still binds each rule to its artifact. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9 --- .../src/api/handlers/onboarding.rs | 87 ++++++++++++++++--- .../tests/integration/api/onboarding.rs | 10 +++ 2 files changed, 85 insertions(+), 12 deletions(-) diff --git a/compliance-agent/src/api/handlers/onboarding.rs b/compliance-agent/src/api/handlers/onboarding.rs index 2f3861c..c825cda 100644 --- a/compliance-agent/src/api/handlers/onboarding.rs +++ b/compliance-agent/src/api/handlers/onboarding.rs @@ -1367,7 +1367,30 @@ fn parse_enum Deserialize<'de>>(s: &str) -> Option { serde_json::from_value(serde_json::Value::String(s.to_string())).ok() } +/// `scans`, each scan the target runs listed once (#1043): one scan may +/// have several rules, one per artifact it can read (code, a firmware image, +/// a supplier SBOM). Once one of them runs, the others are no reason it +/// cannot, and read as if it did not. A scan none of them runs keeps every +/// reason why not. +fn listed_once(scans: Vec) -> Vec { + let runs: std::collections::HashSet = scans + .iter() + .filter(|s| s.blocked_reason.is_none()) + .map(|s| s.scan.clone()) + .collect(); + let mut listed = std::collections::HashSet::new(); + scans + .into_iter() + .filter(|s| { + !runs.contains(&s.scan) || (s.blocked_reason.is_none() && listed.insert(s.scan.clone())) + }) + .collect() +} + /// The scans that apply to a target, with why and what each one needs. +/// +/// A scan the target runs is listed once; a scan it cannot run is listed +/// with each reason why not. #[utoipa::path( get, path = "/api/v1/targets/{id}/applicable-scans", @@ -1395,18 +1418,20 @@ pub async fn applicable_scans_for_target( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .ok_or(StatusCode::NOT_FOUND)?; - let scans = applicable_scans(&target) - .into_iter() - .map(|o| ScanOptionDto { - scan: o.scan.to_string(), - default_on: o.default_on, - // A scan nothing runs yet reads as blocked to every client, so the - // wizard neither pre-selects nor offers it. - blocked_reason: o.unrunnable_reason(), - rationale: o.rationale, - required_artifact: o.required_artifact.map(|k| k.to_string()), - }) - .collect(); + let scans = listed_once( + applicable_scans(&target) + .into_iter() + .map(|o| ScanOptionDto { + scan: o.scan.to_string(), + default_on: o.default_on, + // A scan nothing runs yet reads as blocked to every client, so the + // wizard neither pre-selects nor offers it. + blocked_reason: o.unrunnable_reason(), + rationale: o.rationale, + required_artifact: o.required_artifact.map(|k| k.to_string()), + }) + .collect(), + ); Ok(Json(ApiResponse { data: ApplicableScansResponse { @@ -1641,6 +1666,44 @@ pub async fn trigger_target_scan( mod filter_tests { use super::*; + fn option(scan: &str, blocked: Option<&str>) -> ScanOptionDto { + ScanOptionDto { + scan: scan.into(), + default_on: blocked.is_none(), + rationale: String::new(), + required_artifact: None, + blocked_reason: blocked.map(String::from), + } + } + + #[test] + fn a_scan_the_target_runs_is_listed_once() { + let listed = listed_once(vec![ + option("sast", Some("no git_repo artifact provided")), + option("sbom", Some("no git_repo artifact provided")), + option("sbom", Some("firmware images are not unpacked yet")), + option("sbom", None), + option("cve", Some("no git_repo artifact provided")), + option("cve", None), + option("cve", None), + ]); + let rows: Vec<(&str, bool)> = listed + .iter() + .map(|o| (o.scan.as_str(), o.blocked_reason.is_none())) + .collect(); + // A scan nothing runs keeps its reason; one that runs is there once. + assert_eq!(rows, [("sast", false), ("sbom", true), ("cve", true)]); + } + + #[test] + fn a_scan_nothing_runs_keeps_every_reason() { + let listed = listed_once(vec![ + option("sbom", Some("no git_repo artifact provided")), + option("sbom", Some("firmware images are not unpacked yet")), + ]); + assert_eq!(listed.len(), 2); + } + #[test] fn a_supplier_needs_a_name_and_an_https_csaf_provider() { let input = |name: &str, provider: Option<&str>| SupplierInput { diff --git a/compliance-agent/tests/integration/api/onboarding.rs b/compliance-agent/tests/integration/api/onboarding.rs index c76c7ea..990e1e2 100644 --- a/compliance-agent/tests/integration/api/onboarding.rs +++ b/compliance-agent/tests/integration/api/onboarding.rs @@ -354,6 +354,16 @@ async fn a_supplier_sbom_is_kept_and_brings_its_scans() { }) }; assert!(runs("sbom") && runs("cve"), "{body}"); + // Each once: not also as blocked by the rules that need code (#1043). + let listed = |scan: &str| { + body["data"]["scans"] + .as_array() + .unwrap() + .iter() + .filter(|s| s["scan"] == scan) + .count() + }; + assert_eq!((listed("sbom"), listed("cve")), (1, 1), "{body}"); server.cleanup().await; } -- 2.54.1