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

revert(SBOMER-156): reverts component tree structure in manifest #768

Merged
merged 1 commit into from
Sep 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.cyclonedx.model.Bom;
import org.cyclonedx.model.Component;
import org.cyclonedx.model.Dependency;
import org.cyclonedx.model.Metadata;
import org.cyclonedx.model.Property;
import org.jboss.sbomer.core.errors.ApplicationException;
import org.jboss.sbomer.core.features.sbom.config.SyftImageConfig;
Expand Down Expand Up @@ -124,7 +125,7 @@ public Bom adjust(Bom bom) {
cleanupComponent(bom.getComponents().get(i));
}

createComponentTree(bom);
adjustComponents(bom);

// Populate dependencies section with components
adjustDependencies(bom);
Expand Down Expand Up @@ -197,39 +198,33 @@ private void filterComponents(List<Component> components) {

/**
* <p>
* Creates a component tree in the main {@link Bom#getComponents()} list.
* Ensures that the main component is present in the {@link Bom#getComponents()} list.
* </p>
*
*
* <p>
* The result of this adjustment is that there will be onlys asingle {@link Component} in the component list with
* all other components listed in this particular component's {@link Component#getComponents()} list. This
* effectiveky creates a tree with a single root being the container image component itself.
* At the same time it cleans up the main compontn available in the {@link Metadata#getComponent()}.
* </p>
*
* @param bom
*/
private void createComponentTree(Bom bom) {
private void adjustComponents(Bom bom) {
Component mainComponent = bom.getMetadata().getComponent();

// Create a new component out of the current main component which will replace it.
Component matadataComponent = new Component();
matadataComponent.setType(mainComponent.getType());
matadataComponent.setName(mainComponent.getName());
matadataComponent.setPurl(mainComponent.getPurl());
matadataComponent.setProperties(mainComponent.getProperties());
Component metadataComponent = new Component();
metadataComponent.setType(mainComponent.getType());
metadataComponent.setName(mainComponent.getName());
metadataComponent.setPurl(mainComponent.getPurl());
metadataComponent.setProperties(mainComponent.getProperties());

// Set main component
bom.getMetadata().setComponent(matadataComponent);
bom.getMetadata().setComponent(metadataComponent);

// Clear any properties from the root component. These are now in the metadata component.
mainComponent.setProperties(null);
// Create component tree
mainComponent.setComponents(new ArrayList<>());
mainComponent.getComponents().addAll(bom.getComponents());

// Remove any components and add our root component
bom.setComponents(new ArrayList<>());
bom.getComponents().add(mainComponent);
// Set the main component
bom.getComponents().add(0, mainComponent);
}

/**
Expand Down Expand Up @@ -385,13 +380,6 @@ private void adjustDependencies(Bom bom) {
// as a product dependency
if (dependencies.size() > 1) {
for (Dependency dependency : dependencies.subList(1, dependencies.size())) {

boolean found = dependencies.stream().anyMatch(d -> d.getRef().equals(dependency.getRef()));

if (!found) {
dependencies.add(dependency);
}

productDependency.addDependency(SbomUtils.createDependency(dependency.getRef()));
}
}
Expand All @@ -414,7 +402,12 @@ private void populateDependencies(List<Dependency> dependencies, List<Component>
}

components.forEach(component -> {
dependencies.add(SbomUtils.createDependency(component.getBomRef()));
boolean found = dependencies.stream().anyMatch(d -> d.getRef().equals(component.getBomRef()));

if (!found) {
dependencies.add(SbomUtils.createDependency(component.getBomRef()));
}

populateDependencies(dependencies, component.getComponents());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ void removeAllRpms() throws IOException {
"pkg:oci/console-ui-rhel9@sha256%3Aee4e27734a21cc6b8a8597ef2af32822ad0b4677dbde0a794509f55cbaff5ab3?arch=amd64&os=linux&tag=2.7.0-8.1718294415",
adjusted.getMetadata().getComponent().getPurl());
assertEquals("amq-streams/console-ui-rhel9", adjusted.getMetadata().getComponent().getName());
assertEquals(1, adjusted.getComponents().size());
assertEquals(32, adjusted.getComponents().get(0).getComponents().size());
assertEquals(33, adjusted.getComponents().size());
assertEquals(33, adjusted.getDependencies().size());

assertEquals(0, SbomUtils.validate(SbomUtils.toJsonNode(adjusted)).size());
}

Expand All @@ -59,8 +57,7 @@ void removeAllRpmsLeavePrefix() throws IOException {

Bom adjusted = adjuster.adjust(bom);

assertEquals(1, adjusted.getComponents().size());
assertEquals(23, adjusted.getComponents().get(0).getComponents().size());
assertEquals(24, adjusted.getComponents().size());
assertEquals(24, adjusted.getDependencies().size());
assertEquals(0, SbomUtils.validate(SbomUtils.toJsonNode(adjusted)).size());
}
Expand All @@ -74,8 +71,7 @@ void preserveRpms() throws IOException {

Bom adjusted = adjuster.adjust(bom);

assertEquals(1, adjusted.getComponents().size());
assertEquals(191, adjusted.getComponents().get(0).getComponents().size());
assertEquals(192, adjusted.getComponents().size());
assertEquals(192, adjusted.getDependencies().size());
assertEquals(0, SbomUtils.validate(SbomUtils.toJsonNode(adjusted)).size());
}
Expand All @@ -89,8 +85,7 @@ void preserveRpmsWithPrefix() throws IOException {

Bom adjusted = adjuster.adjust(bom);

assertEquals(1, adjusted.getComponents().size());
assertEquals(182, adjusted.getComponents().get(0).getComponents().size());
assertEquals(183, adjusted.getComponents().size());
assertEquals(183, adjusted.getDependencies().size());
assertEquals(0, SbomUtils.validate(SbomUtils.toJsonNode(adjusted)).size());
}
Expand All @@ -111,13 +106,11 @@ void shouldLeaveOnlyArchAndEpochQualifiers() throws IOException {

Bom adjusted = adjuster.adjust(bom);

// 11th component before adjustment becomes 11th element nested under a root component.
assertEquals(
"pkg:rpm/redhat/[email protected]?arch=x86_64",
adjusted.getComponents().get(0).getComponents().get(10).getPurl());
// 11th component before adjustment becomes 12th after adding main component
assertEquals("pkg:rpm/redhat/[email protected]?arch=x86_64", adjusted.getComponents().get(11).getPurl());
assertEquals(
"pkg:rpm/redhat/[email protected]_2.1?arch=x86_64&epoch=1",
adjusted.getComponents().get(0).getComponents().get(21).getPurl());
adjusted.getComponents().get(22).getPurl());
assertEquals(0, SbomUtils.validate(SbomUtils.toJsonNode(adjusted)).size());
}

Expand Down Expand Up @@ -147,9 +140,7 @@ void generatedDependenciesShouldHaveProperDependsOn() throws IOException {

Bom adjusted = adjuster.adjust(bom);

assertEquals(1, bom.getComponents().size());
// It is one less after filtering, because the "operating system" component does not have a purl.
assertEquals(191, bom.getComponents().get(0).getComponents().size());
assertEquals(192, bom.getComponents().size());
// Main component + all of it's components
assertEquals(192, adjusted.getDependencies().size());
// Main component dependencies
Expand Down
Loading