Skip to content

Commit cf723b8

Browse files
rudsbergdnestoro
authored andcommitted
SBOM: fallback if augmented SBOM generation fails
1 parent 94561ac commit cf723b8

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

native-maven-plugin/src/functionalTest/groovy/org/graalvm/buildtools/maven/SBOMFunctionalTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class SBOMFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
8181
buildSucceeded
8282
outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\) and exported as JSON \\(see build artifacts\\)\\."
8383
outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)"
84+
outputDoesNotContain "Could not generate an augmented SBOM"
8485
validateExportedSBOM sbom
8586
!file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists()
8687
outputContains "Hello, native!"
@@ -101,6 +102,7 @@ class SBOMFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
101102
buildSucceeded
102103
outputContainsPattern".*CycloneDX SBOM with \\d+ component\\(s\\) is embedded in binary \\(.*?\\)."
103104
outputDoesNotContain "Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM)"
105+
outputDoesNotContain "Could not generate an augmented SBOM"
104106
!file(String.format("target/%s", SBOMGenerator.SBOM_FILENAME)).exists()
105107
outputContains "Hello, native!"
106108
}

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeCompileNoForkMojo.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,16 @@ private void generateAugmentedSBOMIfNeeded() throws IllegalArgumentException, Mo
177177
}
178178

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

183192
private String consumeConfigurationNodeValue(String pluginKey, String... nodeNames) {

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/sbom/SBOMGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,17 @@ private static void deleteFileIfExists(Path sbomPath) {
237237
private void augmentSBOM(Path baseSBOMPath, Set<ArtifactAdapter> artifacts) throws IOException {
238238
JSONObject sbomJson = new JSONObject(Files.readString(baseSBOMPath));
239239

240+
JSONArray componentsArray = sbomJson.optJSONArray("components");
241+
if (componentsArray != null) {
242+
componentsArray.forEach(componentNode -> augmentComponentNode((JSONObject) componentNode, artifacts));
243+
}
244+
240245
/* Augment the main component in "metadata/component" */
241246
JSONObject metadataNode = sbomJson.optJSONObject("metadata");
242247
if (metadataNode != null && metadataNode.has("component")) {
243248
augmentComponentNode(metadataNode.getJSONObject("component"), artifacts);
244249
}
245250

246-
JSONArray componentsArray = sbomJson.optJSONArray("components");
247-
if (componentsArray != null) {
248-
componentsArray.forEach(componentNode -> augmentComponentNode((JSONObject) componentNode, artifacts));
249-
}
250-
251251
/* Save the augmented SBOM back to the file */
252252
Files.writeString(baseSBOMPath, sbomJson.toString(2));
253253
}

0 commit comments

Comments
 (0)