fix(git-source): managed targets get the type their code shows #458
No files matched your search
@@ -315,25 +315,34 @@ pub async fn update_target(
|
||||
if let Some(name) = req.name {
|
||||
set.insert("name", name);
|
||||
}
|
||||
// Both merge with what is stored, so the stored target is read once.
|
||||
let stored = if req.scan_config.is_some()
|
||||
|| req.artifacts.is_some()
|
||||
|| req.project_ids.is_some()
|
||||
|| req.target_type.is_some()
|
||||
{
|
||||
Some(
|
||||
db.onboarded_targets()
|
||||
.find_one(doc! { "_id": oid })
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(tt) = req.target_type {
|
||||
set.insert(
|
||||
"target_type",
|
||||
to_bson(&tt).map_err(|_| StatusCode::BAD_REQUEST)?,
|
||||
);
|
||||
// A person picked another type: the scans' detection no longer
|
||||
// changes it (the editor sends the type on every save, so only a
|
||||
// change counts).
|
||||
if stored.as_ref().is_some_and(|s| s.target_type != tt) {
|
||||
set.insert("type_chosen", true);
|
||||
}
|
||||
}
|
||||
// Both merge with what is stored, so the stored target is read once.
|
||||
let stored =
|
||||
if req.scan_config.is_some() || req.artifacts.is_some() || req.project_ids.is_some() {
|
||||
Some(
|
||||
db.onboarded_targets()
|
||||
.find_one(doc! { "_id": oid })
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let credential_error = |e: SecretError| {
|
||||
tracing::error!("storing a credential: {e}");
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
|
||||
@@ -1414,13 +1414,29 @@ impl PipelineOrchestrator {
|
||||
crate::pipeline::firmware_sbom::is_firmware_target(classification.suggested);
|
||||
if let (Some(oid), Ok(bson)) = (target.id, mongodb::bson::to_bson(&classification))
|
||||
{
|
||||
let mut set = doc! { "classification": bson };
|
||||
// A managed target was created before its repository had
|
||||
// code, so with a default type: the code decides it now.
|
||||
if let Some(detected) = target.detected_type(&classification) {
|
||||
let profile = compliance_core::models::default_compliance_profile(detected);
|
||||
if let (Ok(tt), Ok(profile)) = (
|
||||
mongodb::bson::to_bson(&detected),
|
||||
mongodb::bson::to_bson(&profile),
|
||||
) {
|
||||
tracing::info!(
|
||||
target_id,
|
||||
from = %target.target_type,
|
||||
to = %detected,
|
||||
"managed target typed by its code"
|
||||
);
|
||||
set.insert("target_type", tt);
|
||||
set.insert("compliance_profile", profile);
|
||||
}
|
||||
}
|
||||
let _ = self
|
||||
.db
|
||||
.onboarded_targets()
|
||||
.update_one(
|
||||
doc! { "_id": oid },
|
||||
doc! { "$set": { "classification": bson } },
|
||||
)
|
||||
.update_one(doc! { "_id": oid }, doc! { "$set": set })
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,6 +529,21 @@ pub struct ComplianceProfile {
|
||||
|
||||
/// The sensible default compliance scope for a target type. Firmware / PLC /
|
||||
/// embedded default to CRA + IEC 62443; software defaults to GDPR + SOC 2.
|
||||
impl OnboardedTarget {
|
||||
/// The type a scan's `classification` gives a managed target, when it
|
||||
/// should change: the target came from a managed git source (created
|
||||
/// before its repository had any code, so typed by default), no person
|
||||
/// chose its type, the classifier found evidence (not just the fallback
|
||||
/// to the current type), and the evidence names another type.
|
||||
pub fn detected_type(&self, classification: &Classification) -> Option<TargetType> {
|
||||
let detected = self.source_repo.is_some()
|
||||
&& !self.type_chosen
|
||||
&& !classification.candidates.is_empty()
|
||||
&& classification.suggested != self.target_type;
|
||||
detected.then_some(classification.suggested)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_compliance_profile(target_type: TargetType) -> ComplianceProfile {
|
||||
use ComplianceFramework::{Cra, Gdpr, Iec62443, Soc2};
|
||||
let frameworks = match target_type {
|
||||
@@ -598,6 +613,10 @@ pub struct OnboardedTarget {
|
||||
/// for a target a user onboarded.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub source_repo: Option<super::git_source::SourceRepo>,
|
||||
/// A person changed the target's type. A managed target's type otherwise
|
||||
/// follows what each scan detects (see [`OnboardedTarget::detected_type`]).
|
||||
#[serde(default)]
|
||||
pub type_chosen: bool,
|
||||
/// When the target was archived because its repository was deleted.
|
||||
/// Archived targets keep their findings but are no longer scanned.
|
||||
#[serde(
|
||||
@@ -642,6 +661,7 @@ impl OnboardedTarget {
|
||||
findings_count: 0,
|
||||
project_ids: Vec::new(),
|
||||
source_repo: None,
|
||||
type_chosen: false,
|
||||
archived_at: None,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
@@ -676,6 +696,55 @@ impl OnboardedTarget {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn classified(suggested: TargetType, evidence: bool) -> Classification {
|
||||
Classification {
|
||||
suggested,
|
||||
candidates: if evidence {
|
||||
vec![TargetTypeCandidate {
|
||||
target_type: suggested,
|
||||
confidence: 0.8,
|
||||
rationale: "Android manifest / gradle".into(),
|
||||
}]
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
facts: Vec::new(),
|
||||
detected_by: Vec::new(),
|
||||
detected_at: Utc::now(),
|
||||
confirmed: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_managed_targets_type_follows_its_code_until_a_person_picks_one() {
|
||||
let mut managed = OnboardedTarget::new("gt-mobile".into(), TargetType::BackendService);
|
||||
managed.source_repo = Some(super::super::git_source::SourceRepo {
|
||||
base_url: "https://kunde.code.example".into(),
|
||||
repo_id: 7,
|
||||
full_name: "kunde/gt-mobile".into(),
|
||||
});
|
||||
let android = classified(TargetType::AndroidApp, true);
|
||||
assert_eq!(
|
||||
managed.detected_type(&android),
|
||||
Some(TargetType::AndroidApp)
|
||||
);
|
||||
// An empty repository gives no evidence: the default stays.
|
||||
assert_eq!(
|
||||
managed.detected_type(&classified(TargetType::BackendService, false)),
|
||||
None
|
||||
);
|
||||
// Already right: nothing to change.
|
||||
managed.target_type = TargetType::AndroidApp;
|
||||
assert_eq!(managed.detected_type(&android), None);
|
||||
// A person's choice stands.
|
||||
managed.target_type = TargetType::DesktopApp;
|
||||
managed.type_chosen = true;
|
||||
assert_eq!(managed.detected_type(&android), None);
|
||||
// A target a person onboarded keeps the type they gave it.
|
||||
let onboarded = OnboardedTarget::new("fw".into(), TargetType::BackendService);
|
||||
assert_eq!(onboarded.detected_type(&android), None);
|
||||
}
|
||||
|
||||
fn sample_target() -> OnboardedTarget {
|
||||
let mut t = OnboardedTarget::new("acme-web".to_string(), TargetType::WebApp);
|
||||
t.artifacts.push(Artifact::git_repo(
|
||||
|
||||
Reference in new issue
Block a user