Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate components from vendored SBOMs #114

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
nativeCheckInputs = (previousAttrs.nativeCheckInputs or [ ]) ++ [ pkgs.rustfmt ];
checkPhase = "cargo fmt --check";
});
} // import ./nix/tests { inherit pkgs buildBom; };
} // import ./nix/tests { inherit pkgs buildBom passthruVendoredSbom; };

pre-commit = {
check.enable = true;
Expand Down
6 changes: 6 additions & 0 deletions nix/tests/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{ pkgs
, buildBom
, passthruVendoredSbom
}:

let
rustPassthru = pkg: pkgs.callPackage (passthruVendoredSbom.rust pkg) { };

buildtimeOptions = { includeBuildtimeDependencies = true; };

# This list cannot grow indefinitely because building a Bom requires all
Expand All @@ -24,6 +27,9 @@ let

{ name = "git-extra-paths"; drv = git; options = { extraPaths = [ poetry ]; }; }
{ name = "git-extra-paths-buildtime"; drv = git; options = buildtimeOptions // { extraPaths = [ poetry ]; }; }

{ name = "cloud-hypervisor"; drv = rustPassthru cloud-hypervisor; options = { }; }
{ name = "cloud-hypervisor"; drv = rustPassthru cloud-hypervisor; options = buildtimeOptions; }
];

cycloneDxSpec = pkgs.fetchFromGitHub {
Expand Down
25 changes: 23 additions & 2 deletions rust/transformer/src/cyclonedx.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::convert::Into;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -84,15 +85,35 @@ impl CycloneDXComponents {

/// Extend the `Components` with components read from multiple BOMs inside a directory.
pub fn extend_from_directory(&mut self, path: impl AsRef<Path>) -> Result<()> {
let mut m = BTreeMap::new();

// Insert the components from the original SBOM
for component in self.0 .0.clone() {
let key = component
.bom_ref
.clone()
.unwrap_or_else(|| component.name.to_string());
m.entry(key).or_insert(component);
}

// Add the components from the vendored SBOMs
for entry in fs::read_dir(&path)
.with_context(|| format!("Failed to read {:?}", path.as_ref()))?
.flatten()
{
let bom = CycloneDXBom::from_file(entry.path())?;
if let Some(component) = bom.components() {
self.0 .0.extend(component.0);
if let Some(components) = bom.components() {
for component in components.0 {
let key = component
.bom_ref
.clone()
.unwrap_or_else(|| component.name.to_string());
m.entry(key).or_insert(component);
}
}
}

self.0 .0 = m.into_values().collect();
Ok(())
}
}
Expand Down