Skip to content

SBOM: fix issue with unresolved artifacts and empty "components" #688

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

Merged
merged 2 commits into from
Feb 14, 2025
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 @@ -81,6 +81,7 @@ class SBOMFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
buildSucceeded
outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\) and exported as JSON \\(see build artifacts\\)\\."
outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)"
outputDoesNotContain "Could not generate an augmented SBOM"
validateExportedSBOM sbom
!file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists()
outputContains "Hello, native!"
Expand All @@ -101,6 +102,7 @@ class SBOMFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
buildSucceeded
outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\)."
outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)"
outputDoesNotContain "Could not generate an augmented SBOM"
!file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists()
outputContains "Hello, native!"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ private void generateAugmentedSBOMIfNeeded() throws IllegalArgumentException, Mo
}

var sbomGenerator = new SBOMGenerator(mavenProject, mavenSession, pluginManager, repositorySystem, mainClass, logger);
sbomGenerator.generate();
try {
sbomGenerator.generate();
} catch (MojoExecutionException e) {
/* Only throw exception for users that explicitly opt-in to using augmented SBOMs. */
if (optionWasSet) {
throw e;
}
logger.warn(String.format("Could not generate an augmented SBOM: %s. Fallback to generating a non-augmented SBOM.",
e.getCause().getMessage()));
}
}

private String consumeConfigurationNodeValue(String pluginKey, String... nodeNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Set<ArtifactAdapter> getArtifactAdapters() throws Exception {
return artifactsWithPackageNameMappings;
}

private Optional<ArtifactAdapter> resolvePackageNamesFromArtifact(Artifact artifact) throws ArtifactResolutionException, IOException {
private Optional<ArtifactAdapter> resolvePackageNamesFromArtifact(Artifact artifact) throws IOException {
File artifactFile = artifact.getFile();
if (artifactFile != null && artifactFile.exists()) {
return resolvePackageNamesFromArtifactFile(artifactFile, ArtifactAdapter.fromMavenArtifact(artifact));
Expand All @@ -124,7 +124,13 @@ private Optional<ArtifactAdapter> resolvePackageNamesFromArtifact(Artifact artif
.setArtifact(sourceArtifact)
.setRepositories(remoteRepositories);

ArtifactResult result = repositorySystem.resolveArtifact(repositorySystemSession, request);
ArtifactResult result;
try {
result = repositorySystem.resolveArtifact(repositorySystemSession, request);
} catch (ArtifactResolutionException e) {
return Optional.empty();
}

if (result != null && result.getArtifact() != null && result.getArtifact().getFile() != null) {
File sourceFile = result.getArtifact().getFile();
return resolvePackageNamesFromArtifactFile(sourceFile, ArtifactAdapter.fromEclipseArtifact(result.getArtifact()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,10 @@ private void augmentSBOM(Path baseSBOMPath, Set<ArtifactAdapter> artifacts) thro
JSONObject sbomJson = new JSONObject(Files.readString(baseSBOMPath));

JSONArray componentsArray = sbomJson.optJSONArray("components");
if (componentsArray == null) {
throw new RuntimeException(String.format("SBOM generated by %s:%s contained no components.", Plugin.groupId, Plugin.artifactId));
if (componentsArray != null) {
componentsArray.forEach(componentNode -> augmentComponentNode((JSONObject) componentNode, artifacts));
}

/* Augment the "components" */
componentsArray.forEach(componentNode -> augmentComponentNode((JSONObject) componentNode, artifacts));

/* Augment the main component in "metadata/component" */
JSONObject metadataNode = sbomJson.optJSONObject("metadata");
if (metadataNode != null && metadataNode.has("component")) {
Expand Down
Loading