diff --git a/compliance-agent/src/pipeline/change.rs b/compliance-agent/src/pipeline/change.rs index 4758f18..5d431f2 100644 --- a/compliance-agent/src/pipeline/change.rs +++ b/compliance-agent/src/pipeline/change.rs @@ -44,6 +44,40 @@ pub fn upload_inputs(target: &OnboardedTarget) -> Option { Some(hex::encode(hasher.finalize())) } +/// Whether unchanged uploads let a scheduled scan be skipped: on unless +/// `UPLOAD_CHANGE_SKIP` is `false`, `0` or `off`, which scans a target with +/// uploaded files on every schedule. +pub fn upload_skip_enabled() -> bool { + switch_on(std::env::var("UPLOAD_CHANGE_SKIP").ok().as_deref()) +} + +/// Whether a switch's value leaves it on (the default). +fn switch_on(value: Option<&str>) -> bool { + !matches!( + value.map(|v| v.trim().to_ascii_lowercase()).as_deref(), + Some("false" | "0" | "off") + ) +} + +/// Whether the uploads of a git `target` let a scheduled scan that finds no +/// new commits be skipped: it has none, or (with [`upload_skip_enabled`]) +/// they are the same as at its last completed scan. A file uploaded to a git +/// target since then means the scan runs. +pub fn uploads_allow_git_skip(target: &OnboardedTarget) -> bool { + if !target.artifacts.iter().any(is_upload) { + return true; + } + upload_skip_enabled() && uploads_match(target) +} + +/// Whether the uploads of `target` are the ones its last completed scan read. +fn uploads_match(target: &OnboardedTarget) -> bool { + target + .last_scan + .as_ref() + .is_some_and(|last| last.inputs.is_some() && last.inputs == upload_inputs(target)) +} + /// Whether `artifact` is a file uploaded to the target (not a git repository /// or a live URL, which are fetched or probed). fn is_upload(artifact: &Artifact) -> bool { @@ -75,7 +109,7 @@ pub fn upload_unchanged(target: &OnboardedTarget) -> bool { let Some(last) = &target.last_scan else { return false; }; - last.build == build_id() && last.inputs.is_some() && last.inputs == upload_inputs(target) + last.build == build_id() && uploads_match(target) } #[cfg(test)] @@ -113,6 +147,32 @@ mod tests { assert!(!upload_unchanged(&t)); } + #[test] + fn the_skip_switch_is_on_unless_turned_off() { + assert!(switch_on(None)); + assert!(switch_on(Some("true"))); + assert!(!switch_on(Some("false"))); + assert!(!switch_on(Some(" OFF "))); + } + + #[test] + fn a_git_target_with_a_changed_upload_is_not_skipped() { + let mut repo = Artifact::git_repo("https://git.example/x.git", "main"); + repo.id = "g".into(); + let only_git = target(vec![repo.clone()]); + assert!(uploads_allow_git_skip(&only_git), "nothing uploaded"); + + let mut t = target(vec![repo, upload("a", Some("h1"))]); + t.last_scan = Some(LastScan { + build: build_id(), + inputs: upload_inputs(&t), + }); + assert!(uploads_match(&t)); + t.artifacts.push(upload("b", Some("h2"))); + assert!(!uploads_match(&t), "a new upload since the last scan"); + assert!(!uploads_allow_git_skip(&t)); + } + #[test] fn only_uploads_without_a_hash_take_the_one_ingest_computed() { let mut repo = Artifact::git_repo("https://git.example/x.git", "main"); diff --git a/compliance-agent/src/pipeline/orchestrator.rs b/compliance-agent/src/pipeline/orchestrator.rs index 7416da1..f475d2c 100644 --- a/compliance-agent/src/pipeline/orchestrator.rs +++ b/compliance-agent/src/pipeline/orchestrator.rs @@ -161,9 +161,10 @@ impl PipelineOrchestrator { // A scheduled or webhook scan of an unchanged repository has nothing // new to find; one a person started always runs (after a changed - // setting it can find something else), and a new Prüfwerk build scans - // once (#465). + // setting it can find something else), a new Prüfwerk build scans + // once (#465), and so does a file uploaded to the target since. if !trigger.started_by_person() + && repo.uploads_allow_skip && repo.last_scanned_build.as_deref() == Some(crate::pipeline::change::build_id().as_str()) && !GitOps::has_new_commits(&repo_path, repo.last_scanned_commit.as_deref())? @@ -1058,6 +1059,7 @@ impl PipelineOrchestrator { .any(|a| a.kind == ArtifactKind::GitRepo); if !trigger.started_by_person() && !has_git + && crate::pipeline::change::upload_skip_enabled() && crate::pipeline::change::upload_unchanged(target) { tracing::info!(target_id = %target_id, "Uploaded files unchanged, skipping scan"); @@ -1152,6 +1154,31 @@ impl PipelineOrchestrator { match target.code_artifact() { Some(code) if code.kind == ArtifactKind::GitRepo => { + // The code scan reads the repository; a source archive also + // uploaded to the target is not read, and the run says so. + for archive in target + .artifacts + .iter() + .filter(|a| a.kind == ArtifactKind::SourceArchive) + { + let name = archive + .display_name + .as_deref() + .unwrap_or(&archive.source_ref); + self.skip_stage( + scan_run_id, + SkippedStage::new( + "source_archive", + SkipCode::NotAnalysedYet, + format!( + "the code scan reads the target's git repository; the uploaded \ + archive {name} is not scanned (upload it as a target of its own)" + ), + ) + .about(name.to_string()), + ) + .await; + } let repo = RepoView::from_target(target, code, &self.secrets); let n = self.run_pipeline(&repo, scan_run_id, trigger).await?; self.finalize_target(target, &repo, n).await?; diff --git a/compliance-agent/src/pipeline/repo_view.rs b/compliance-agent/src/pipeline/repo_view.rs index c0a9afc..955dc6e 100644 --- a/compliance-agent/src/pipeline/repo_view.rs +++ b/compliance-agent/src/pipeline/repo_view.rs @@ -42,6 +42,9 @@ pub struct RepoView { /// Created by a managed git source (#439): its Gitea issues get the /// Prüfwerk/CRA labels and a link into Prüfwerk. pub managed: bool, + /// Whether the target's uploaded files let a scheduled scan without new + /// commits be skipped ([`crate::pipeline::change::uploads_allow_git_skip`]). + pub uploads_allow_skip: bool, } impl RepoView { @@ -70,6 +73,7 @@ impl RepoView { last_scanned_build: target.last_scan.as_ref().map(|l| l.build.clone()), findings_count: target.findings_count, managed: target.source_repo.is_some(), + uploads_allow_skip: crate::pipeline::change::uploads_allow_git_skip(target), }; if let Some(git) = &code.git { view.default_branch = git.default_branch.clone(); diff --git a/compliance-dashboard/src/i18n/texts.rs b/compliance-dashboard/src/i18n/texts.rs index c2d5de7..b4eeb1e 100644 --- a/compliance-dashboard/src/i18n/texts.rs +++ b/compliance-dashboard/src/i18n/texts.rs @@ -3123,6 +3123,12 @@ pub mod coverage { de: "braucht ein weiteres Artefakt", en: "needs another artifact", }; + pub const UPLOAD_BESIDE_GIT: Text = Text { + de: "Der Code-Scan liest das Git-Repository des Prüfobjekts; ein dazu hochgeladenes \ + Archiv wird nicht geprüft. Laden Sie es als eigenes Prüfobjekt hoch", + en: "the code scan reads the target's git repository; an archive uploaded to it is \ + not scanned. Upload it as a target of its own", + }; pub const IMAGE_NOT_ANALYSED: Text = Text { de: "Firmware-Images werden noch nicht entpackt: Prüfen Sie stattdessen die \ Firmware-Quellen", @@ -3148,7 +3154,9 @@ pub mod coverage { let r = raw.to_ascii_lowercase(); // Missing artifacts name the kind as `no artifact provided`; // scans no stage runs yet explain themselves in prose. - let text = if r.contains("http(s) live url") { + let text = if r.contains("uploaded archive") { + UPLOAD_BESIDE_GIT + } else if r.contains("http(s) live url") { NEEDS_WEB_URL } else if r.contains("firmware_image") { NEEDS_FIRMWARE @@ -3195,6 +3203,11 @@ mod coverage_tests { ), ("no live_url artifact provided", coverage::NEEDS_DEVICE), ("no git_repo artifact provided", coverage::NEEDS_CODE), + ( + "the code scan reads the target's git repository; the uploaded archive app.zip \ + is not scanned (upload it as a target of its own)", + coverage::UPLOAD_BESIDE_GIT, + ), ( "firmware images are not unpacked or analysed yet: scan the firmware sources \ instead", diff --git a/docs/guide/scanning.md b/docs/guide/scanning.md index bac698f..e9ead21 100644 --- a/docs/guide/scanning.md +++ b/docs/guide/scanning.md @@ -79,7 +79,12 @@ with no new commits since the last scan, or of uploaded files that are the same as at the last scan (compared by their SHA-256, so any newly uploaded file, even one with the same name, is scanned), does not scan again; the run says so under -**Nicht ausgeführt** and counts as no scan. After an update of Prüfwerk every +**Nicht ausgeführt** and counts as no scan. A git target to which a file was +uploaded since its last scan is scanned, even without new commits; its code +scan reads the repository, so an archive uploaded to a git target is listed +under **Nicht ausgeführt** as not scanned: upload it as a target of its own. +The administrator can turn the skipping of unchanged uploads off with +`UPLOAD_CHANGE_SKIP=false`. After an update of Prüfwerk every target is scanned once more, since new rules can find more in the same code. **Prüfung starten** always scans in full, also when a restart of Prüfwerk interrupted it and it is started again. The daily CVE check of the known