CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m9s
CI / Deploy MCP (push) Successful in 5m21s
CI / Deploy Dashboard (push) Successful in 7m11s
CI / Deploy Agent (push) Successful in 9m19s
126 lines
4.4 KiB
Rust
126 lines
4.4 KiB
Rust
//! The SBOM of a machine (#1026): its parts, in-house and bought in, with
|
|
//! their suppliers and components, valid against CycloneDX 1.6.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
mod common;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use compliance_core::models::{SbomEntry, VulnRef};
|
|
use compliance_core::sbom_docs::{self, MachinePart};
|
|
|
|
/// An SBOM entry of target `target`.
|
|
fn entry(target: &str, name: &str, version: &str, purl: Option<&str>) -> SbomEntry {
|
|
let mut e = SbomEntry::new(target.into(), name.into(), version.into(), "npm".into());
|
|
e.purl = purl.map(String::from);
|
|
e
|
|
}
|
|
|
|
/// A controller made in-house (`t1`) and a drive bought in (`t2`), sharing
|
|
/// lodash; the drive's vendor blob has no purl.
|
|
fn machine() -> (Vec<MachinePart<'static>>, Vec<SbomEntry>) {
|
|
let parts = vec![
|
|
MachinePart {
|
|
id: "t1",
|
|
name: "Steuerung",
|
|
kind: "firmware",
|
|
supplier: "Example Maschinenbau",
|
|
version: None,
|
|
},
|
|
// Named like the machine: the refs must still be unique.
|
|
MachinePart {
|
|
id: "t2",
|
|
name: "Packing Line",
|
|
kind: "firmware",
|
|
supplier: "Example Drives GmbH",
|
|
version: Some("4.2.1"),
|
|
},
|
|
];
|
|
let mut lodash = entry("t1", "lodash", "4.17.20", Some("pkg:npm/lodash@4.17.20"));
|
|
lodash.known_vulnerabilities = vec![VulnRef {
|
|
id: "CVE-2021-23337".into(),
|
|
source: "osv".into(),
|
|
severity: Some("high".into()),
|
|
url: None,
|
|
exploitation: None,
|
|
aliases: Vec::new(),
|
|
}];
|
|
let entries = vec![
|
|
lodash,
|
|
entry("t1", "zlib", "1.2.11", Some("pkg:generic/zlib@1.2.11")),
|
|
entry("t2", "lodash", "4.17.20", Some("pkg:npm/lodash@4.17.20")),
|
|
entry("t2", "vendor-blob", "2.0", None),
|
|
];
|
|
(parts, entries)
|
|
}
|
|
|
|
#[test]
|
|
fn a_machines_sbom_names_its_parts_with_their_suppliers_and_is_valid() {
|
|
let (parts, entries) = machine();
|
|
|
|
let doc = sbom_docs::machine_cyclonedx("Packing Line", "p1", &parts, &entries);
|
|
|
|
let errors = common::cyclonedx_errors(&doc);
|
|
assert!(errors.is_empty(), "{errors:#?}");
|
|
assert_eq!(doc["metadata"]["component"]["type"], "device");
|
|
assert_eq!(doc["metadata"]["component"]["name"], "Packing Line");
|
|
let components = doc["components"].as_array().unwrap();
|
|
// The parts first, each with its supplier; the drive with its version.
|
|
assert_eq!(components[0]["name"], "Steuerung");
|
|
assert_eq!(components[0]["supplier"]["name"], "Example Maschinenbau");
|
|
assert!(components[0]["version"].is_string());
|
|
assert_eq!(components[1]["supplier"]["name"], "Example Drives GmbH");
|
|
assert_eq!(components[1]["version"], "4.2.1");
|
|
// lodash, in both parts, is listed once.
|
|
let lodash = components
|
|
.iter()
|
|
.filter(|c| c["purl"] == "pkg:npm/lodash@4.17.20")
|
|
.count();
|
|
assert_eq!(lodash, 1);
|
|
assert_eq!(components.len(), 2 + 3);
|
|
}
|
|
|
|
#[test]
|
|
fn every_ref_of_a_machines_sbom_is_unique_and_every_dependency_resolves() {
|
|
let (parts, entries) = machine();
|
|
|
|
let doc = sbom_docs::machine_cyclonedx("Packing Line", "p1", &parts, &entries);
|
|
|
|
let machine_ref = doc["metadata"]["component"]["bom-ref"].as_str().unwrap();
|
|
let mut refs = HashSet::from([machine_ref]);
|
|
for c in doc["components"].as_array().unwrap() {
|
|
assert!(refs.insert(c["bom-ref"].as_str().unwrap()), "{c}");
|
|
}
|
|
let depends = |r: &str| -> Vec<&str> {
|
|
doc["dependencies"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|d| d["ref"] == r)
|
|
.and_then(|d| d["dependsOn"].as_array())
|
|
.map(|list| list.iter().map(|v| v.as_str().unwrap()).collect())
|
|
.unwrap_or_default()
|
|
};
|
|
for d in doc["dependencies"].as_array().unwrap() {
|
|
assert!(refs.contains(d["ref"].as_str().unwrap()), "{d}");
|
|
for on in d["dependsOn"].as_array().into_iter().flatten() {
|
|
assert!(refs.contains(on.as_str().unwrap()), "{on}");
|
|
}
|
|
}
|
|
// The machine depends on its parts, each part on its own components.
|
|
assert_eq!(
|
|
depends(machine_ref),
|
|
["urn:pruefwerk:target:t1", "urn:pruefwerk:target:t2"]
|
|
);
|
|
assert_eq!(
|
|
depends("urn:pruefwerk:target:t2"),
|
|
["npm:vendor-blob@2.0", "pkg:npm/lodash@4.17.20"]
|
|
);
|
|
// A vulnerability points at the shared component.
|
|
assert_eq!(
|
|
doc["vulnerabilities"][0]["affects"][0]["ref"],
|
|
"pkg:npm/lodash@4.17.20"
|
|
);
|
|
}
|