fix(onboarding): a scan the target runs is listed once among its applicable scans (#1043) #1046

Merged
sharang merged 1 commits from fix/applicable-scans-once into main 2026-11-17 16:46:33 +00:00
2 changed files with 85 additions and 12 deletions

No files matched your search

+75 -12
View File
@@ -1367,7 +1367,30 @@ fn parse_enum<T: for<'de> Deserialize<'de>>(s: &str) -> Option<T> {
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<ScanOptionDto>) -> Vec<ScanOptionDto> {
let runs: std::collections::HashSet<String> = 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 {
@@ -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;
}