diff --git a/compliance-agent/src/api/handlers/onboarding.rs b/compliance-agent/src/api/handlers/onboarding.rs index f21425c..87f4307 100644 --- a/compliance-agent/src/api/handlers/onboarding.rs +++ b/compliance-agent/src/api/handlers/onboarding.rs @@ -999,6 +999,30 @@ pub async fn detect_target( })) } +/// Why a scan is asked for (`POST /api/v1/targets/{id}/scan?reason=…`). +#[derive(Debug, Default, serde::Deserialize)] +pub struct ScanReason { + /// `new_content` after the customer created the target or uploaded + /// files (t72): it runs only when something changed, as a change-driven + /// Prüfung. Absent: the customer asks for a Prüfung, which always runs + /// and counts. + #[serde(default)] + pub reason: Option, +} + +/// The trigger of a scan the customer (`staff: false`) or support asked +/// for, for `reason`. Support's scans are never the customer's usage. +fn requested_trigger(staff: bool, reason: Option<&str>) -> compliance_core::models::ScanTrigger { + use compliance_core::models::ScanTrigger; + if staff { + ScanTrigger::Support + } else if reason == Some("new_content") { + ScanTrigger::NewContent + } else { + ScanTrigger::Manual + } +} + /// POST /api/v1/targets/{id}/scan — trigger a scan for the target. /// /// Dispatches to the unified pipeline when `UNIFIED_PIPELINE` is set (else the @@ -1008,6 +1032,7 @@ pub async fn trigger_target_scan( Extension(agent): AgentExt, tenant: TenantCtx, Path(id): Path, + Query(reason): Query, ) -> Result, StatusCode> { let oid = parse_oid(&id)?; let db = tenant_db(&agent, &tenant).await?; @@ -1024,12 +1049,7 @@ pub async fn trigger_target_scan( let agent_clone = (*agent).clone(); let tenant_id = tenant.0.tenant_id.clone(); - // A scan support starts is not the customer's usage. - let trigger = if tenant.0.staff.is_some() { - compliance_core::models::ScanTrigger::Support - } else { - compliance_core::models::ScanTrigger::Manual - }; + let trigger = requested_trigger(tenant.0.staff.is_some(), reason.reason.as_deref()); tokio::spawn(async move { // Always the unified target pipeline — this endpoint is about an // onboarded target by construction, independent of the global @@ -1045,6 +1065,22 @@ pub async fn trigger_target_scan( mod filter_tests { use super::*; + #[test] + fn a_scan_asked_for_new_content_is_change_driven() { + use compliance_core::models::ScanTrigger; + assert_eq!(requested_trigger(false, None), ScanTrigger::Manual); + assert_eq!( + requested_trigger(false, Some("new_content")), + ScanTrigger::NewContent + ); + assert_eq!(requested_trigger(false, Some("other")), ScanTrigger::Manual); + assert_eq!( + requested_trigger(true, Some("new_content")), + ScanTrigger::Support, + "support's scans are never the customer's usage" + ); + } + #[test] fn a_project_filter_matches_its_targets() { let filter = TargetFilter { diff --git a/compliance-agent/src/scan_gate.rs b/compliance-agent/src/scan_gate.rs index 6f44692..af12658 100644 --- a/compliance-agent/src/scan_gate.rs +++ b/compliance-agent/src/scan_gate.rs @@ -38,7 +38,10 @@ pub const SCHEDULED_SCAN_JITTER: Duration = Duration::from_secs(30); pub fn start_delay(trigger: &ScanTrigger) -> Duration { match trigger { ScanTrigger::Scheduled => rand::rng().random_range(Duration::ZERO..=SCHEDULED_SCAN_JITTER), - ScanTrigger::Webhook | ScanTrigger::Manual | ScanTrigger::Support => Duration::ZERO, + ScanTrigger::Webhook + | ScanTrigger::Manual + | ScanTrigger::Support + | ScanTrigger::NewContent => Duration::ZERO, } } @@ -187,11 +190,12 @@ mod tests { } #[test] - fn webhook_manual_and_support_scans_start_at_once() { + fn webhook_manual_support_and_new_content_scans_start_at_once() { for trigger in [ ScanTrigger::Webhook, ScanTrigger::Manual, ScanTrigger::Support, + ScanTrigger::NewContent, ] { assert_eq!(start_delay(&trigger), Duration::ZERO); } diff --git a/compliance-core/src/models/metering.rs b/compliance-core/src/models/metering.rs index 6d103db..5799346 100644 --- a/compliance-core/src/models/metering.rs +++ b/compliance-core/src/models/metering.rs @@ -48,7 +48,7 @@ impl ScanAttribution { match trigger { ScanTrigger::Manual => Some(Self::Request), ScanTrigger::Support => Some(Self::Support), - ScanTrigger::Scheduled | ScanTrigger::Webhook => None, + ScanTrigger::Scheduled | ScanTrigger::Webhook | ScanTrigger::NewContent => None, } } @@ -325,6 +325,11 @@ mod tests { ScanAttribution::detected(&ScanTrigger::Webhook, false), ScanAttribution::Rescan ); + assert_eq!( + ScanAttribution::detected(&ScanTrigger::NewContent, true), + ScanAttribution::Change, + "an upload is the customer's change" + ); } #[test] diff --git a/compliance-core/src/models/repository.rs b/compliance-core/src/models/repository.rs index df190fa..6c54c19 100644 --- a/compliance-core/src/models/repository.rs +++ b/compliance-core/src/models/repository.rs @@ -10,6 +10,10 @@ pub enum ScanTrigger { /// Started by BreakPilot support in the customer's tenant; not counted /// as the customer's usage. Support, + /// Started after the customer gave the target new content: created it, + /// or uploaded files (t72). Like a scheduled scan, it runs only when + /// something changed, and then it is a change-driven Prüfung. + NewContent, } impl ScanTrigger { @@ -18,7 +22,7 @@ impl ScanTrigger { pub fn started_by_person(&self) -> bool { match self { Self::Manual | Self::Support => true, - Self::Scheduled | Self::Webhook => false, + Self::Scheduled | Self::Webhook | Self::NewContent => false, } } } diff --git a/compliance-dashboard/src/components/artifact_upload.rs b/compliance-dashboard/src/components/artifact_upload.rs index 7ddfa59..6e8bfaa 100644 --- a/compliance-dashboard/src/components/artifact_upload.rs +++ b/compliance-dashboard/src/components/artifact_upload.rs @@ -6,8 +6,8 @@ use dioxus::prelude::*; use crate::i18n::texts::{common, kinds, onboarding as otx, targets as tx}; use crate::i18n::use_lang; use crate::infrastructure::onboarding::{ - is_file_kind, plc_format_for, set_archive_password, upload_target_artifact, ArchivePassword, - PasswordOutcome, UNSUPPORTED_ARCHIVE, + is_file_kind, plc_format_for, set_archive_password, trigger_new_content_scan, + upload_target_artifact, ArchivePassword, PasswordOutcome, UNSUPPORTED_ARCHIVE, }; /// The upload row for target `target_id`; `on_uploaded` runs once the file @@ -82,6 +82,7 @@ pub fn ArtifactUpload(target_id: String, on_uploaded: EventHandler<()>) -> Eleme onclick: move |_| { let Some(picked) = file() else { return; }; let (id, kind, pw) = (target_id.clone(), kind(), password()); + let scan_id = id.clone(); let format = (kind == "plc_project").then(|| plc_format.read().clone()); busy.set(true); spawn(async move { @@ -115,10 +116,21 @@ pub fn ArtifactUpload(target_id: String, on_uploaded: EventHandler<()>) -> Eleme } } }; + // New content starts its change-driven scan (t72); a + // failed start leaves the file for the next scan. + let scanning = match &result { + Ok(()) => trigger_new_content_scan(scan_id).await.is_ok(), + Err(_) => false, + }; busy.set(false); match result { Ok(()) => { - note.set(Some((true, tx::UPLOADED.get(lang).to_string()))); + let uploaded = if scanning { + tx::UPLOADED + } else { + tx::UPLOADED_NOT_SCANNED + }; + note.set(Some((true, uploaded.get(lang).to_string()))); file.set(None); password.set(String::new()); on_uploaded.call(()); diff --git a/compliance-dashboard/src/i18n/texts.rs b/compliance-dashboard/src/i18n/texts.rs index 2ec9bdf..25c2af6 100644 --- a/compliance-dashboard/src/i18n/texts.rs +++ b/compliance-dashboard/src/i18n/texts.rs @@ -1118,9 +1118,9 @@ pub mod targets { }; pub const UPLOAD_HINT: Text = Text { de: "Ein Archiv (zip, tar, tar.gz), ein SPS-Projekt oder ein Firmware-Image. Die Datei \ - wird sofort hochgeladen und bei der nächsten Prüfung geprüft.", + wird sofort hochgeladen und gleich danach geprüft.", en: "An archive (zip, tar, tar.gz), a PLC project or a firmware image. It is uploaded \ - at once and checked by the next scan.", + at once and scanned right after.", }; pub const UPLOAD: Text = Text { de: "Hochladen", @@ -1173,8 +1173,14 @@ pub mod targets { } } pub const UPLOADED: Text = Text { - de: "Hochgeladen. Starten Sie die Prüfung, um die Datei zu prüfen.", - en: "Uploaded. Start a scan to check the file.", + de: "Hochgeladen. Die Prüfung der neuen Datei startet.", + en: "Uploaded. The scan of the new file is starting.", + }; + pub const UPLOADED_NOT_SCANNED: Text = Text { + de: "Hochgeladen. Die Prüfung konnte nicht gestartet werden; die nächste \ + geplante Prüfung liest die Datei.", + en: "Uploaded. The scan could not be started; the next scheduled scan reads \ + the file.", }; pub const EVIDENCE_BUILDING: Text = Text { de: "Wird erstellt …", diff --git a/compliance-dashboard/src/infrastructure/onboarding.rs b/compliance-dashboard/src/infrastructure/onboarding.rs index fc02338..7763fef 100644 --- a/compliance-dashboard/src/infrastructure/onboarding.rs +++ b/compliance-dashboard/src/infrastructure/onboarding.rs @@ -606,7 +606,24 @@ pub async fn delete_target(id: String) -> Result Result { + let resp = super::agent_client::agent_request( + reqwest::Method::POST, + &format!("/api/v1/targets/{id}/scan?reason=new_content"), + ) + .await? + .send() + .await + .map_err(|e| ServerFnError::new(e.to_string()))?; + super::agent_client::json_or_error(resp).await +} + +/// Trigger a scan for a target: the customer asks for a Prüfung, which +/// always runs and counts (t72). #[server] pub async fn trigger_target_scan(id: String) -> Result { let resp = super::agent_client::agent_request( diff --git a/compliance-dashboard/src/pages/onboarding.rs b/compliance-dashboard/src/pages/onboarding.rs index aef7a15..8ecb63b 100644 --- a/compliance-dashboard/src/pages/onboarding.rs +++ b/compliance-dashboard/src/pages/onboarding.rs @@ -17,7 +17,7 @@ use crate::i18n::texts::{common, kinds, onboarding as tx}; use crate::i18n::{format_bytes, use_lang}; use crate::infrastructure::onboarding::{ create_target, detect_target, enable_target_scans, fetch_applicable_scans, is_file_kind, - plc_format_for, set_archive_password, trigger_target_scan, upload_target_artifact, + plc_format_for, set_archive_password, trigger_new_content_scan, upload_target_artifact, validate_artifact_ref, validate_target_name, ArchivePassword, ArtifactInputDto, PasswordOutcome, UNSUPPORTED_ARCHIVE, }; @@ -591,7 +591,7 @@ pub fn OnboardingPage() -> Element { return; } } - first_scan.set(match trigger_target_scan(id).await { + first_scan.set(match trigger_new_content_scan(id).await { Ok(_) => FirstScan::Started, Err(e) => FirstScan::Failed(e.to_string()), }); @@ -750,7 +750,7 @@ pub fn OnboardingPage() -> Element { let offers_optin = !source_only && !optin_scans(&applicable).is_empty(); scans.set(applicable); if upload_errors.read().is_empty() && !offers_optin { - first_scan.set(match trigger_target_scan(id).await { + first_scan.set(match trigger_new_content_scan(id).await { Ok(_) => FirstScan::Started, Err(e) => FirstScan::Failed(e.to_string()), }); diff --git a/docs/guide/scanning.md b/docs/guide/scanning.md index 0e79689..3270e74 100644 --- a/docs/guide/scanning.md +++ b/docs/guide/scanning.md @@ -76,11 +76,18 @@ New findings appear on the target's page while the scan is still running. Click **Prüfung starten** (Run scan), either in the row of the target in the **Prüfobjekte** list or at the top of the target's page. The scan starts in the background and the message "Prüfung gestartet. Den Fortschritt sehen Sie unter -Prüfläufe." appears. A new target is scanned once as soon as you create it in -the **Neues Prüfobjekt** wizard. +Prüfläufe." appears. A manual scan always scans in full, even if nothing changed. +### Scan after new content + +Creating a target in the wizard and uploading a file to a target start a scan +of the new content right away. Like a scheduled scan, it runs only when +something changed. Through the API, ask for it with +`POST /api/v1/targets/{id}/scan?reason=new_content`; without `reason`, the +scan is a Prüfung you request, which always runs in full. + ### Scheduled Scans Targets are scanned automatically on a recurring schedule: by default every 6