Skip to content

Commit

Permalink
CAMEL-19644 - camel-jbang - Add command to generate SBOM report (#11601)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Cosentino <[email protected]>
  • Loading branch information
oscerd authored Sep 28, 2023
1 parent 26097e2 commit 57b0c31
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public static void run(String... args) {
.addSubcommand("version", new CommandLine(new VersionCommand(main))
.addSubcommand("get", new CommandLine(new VersionGet(main)))
.addSubcommand("set", new CommandLine(new VersionSet(main)))
.addSubcommand("list", new CommandLine(new VersionList(main))));
.addSubcommand("list", new CommandLine(new VersionList(main))))
.addSubcommand("sbom", new CommandLine(new SBOMGenerator(main)));

commandLine.getCommandSpec().versionProvider(() -> {
CamelCatalog catalog = new DefaultCamelCatalog();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.dsl.jbang.core.commands;

import java.io.File;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.util.CamelCaseOrderedProperties;
import org.apache.camel.util.FileUtil;
import picocli.CommandLine;

@CommandLine.Command(name = "sbom",
description = "Generate a CycloneDX SBOM for a specific project")
public class SBOMGenerator extends Export {

protected static final String EXPORT_DIR = ".camel-jbang/export";

@CommandLine.Option(names = { "--output-directory" }, description = "Directory where the SBOM will be saved",
defaultValue = ".")
protected String outputDirectory;

@CommandLine.Option(names = { "--output-name" }, description = "Output name of the SBOM file",
defaultValue = "sbom")
protected String outputName;

@CommandLine.Option(names = { "--plugin-version" }, description = "The CycloneDX Maven Plugin version",
defaultValue = "2.7.9")
protected String pluginVersion = "2.7.9";

public SBOMGenerator(CamelJBangMain main) {
super(main);
}

@Override
public Integer doCall() throws Exception {
this.quiet = true; // be quiet and generate from fresh data to ensure the output is up-to-date
return super.doCall();
}

@Override
protected Integer export() throws Exception {
Integer answer = doExport();
if (answer == 0) {
File buildDir = new File(EXPORT_DIR);
String outputDirectoryParameter = "-DoutputDirectory=";
if (Paths.get(outputDirectory).isAbsolute()) {
outputDirectoryParameter += outputDirectory;
} else {
outputDirectoryParameter += "../../" + outputDirectory;
}
Process p = Runtime.getRuntime()
.exec("mvn org.cyclonedx:cyclonedx-maven-plugin:" + pluginVersion + ":makeAggregateBom "
+ outputDirectoryParameter
+ " -DoutputName="
+ outputName,
null,
buildDir);
boolean done = p.waitFor(60, TimeUnit.SECONDS);
if (!done) {
answer = 1;
}
if (p.exitValue() != 0) {
answer = p.exitValue();
}
// cleanup dir after complete
FileUtil.removeDir(buildDir);
}
return answer;
}

protected Integer doExport() throws Exception {
// read runtime and gav from profile if not configured
File profile = new File(getProfile() + ".properties");
if (profile.exists()) {
Properties prop = new CamelCaseOrderedProperties();
RuntimeUtil.loadProperties(prop, profile);
if (this.runtime == null) {
this.runtime = prop.getProperty("camel.jbang.runtime");
}
if (this.gav == null) {
this.gav = prop.getProperty("camel.jbang.gav");
}
// allow configuring versions from profile
this.javaVersion = prop.getProperty("camel.jbang.javaVersion", this.javaVersion);
this.camelVersion = prop.getProperty("camel.jbang.camelVersion", this.camelVersion);
this.kameletsVersion = prop.getProperty("camel.jbang.kameletsVersion", this.kameletsVersion);
this.localKameletDir = prop.getProperty("camel.jbang.localKameletDir", this.localKameletDir);
this.quarkusGroupId = prop.getProperty("camel.jbang.quarkusGroupId", this.quarkusGroupId);
this.quarkusArtifactId = prop.getProperty("camel.jbang.quarkusArtifactId", this.quarkusArtifactId);
this.quarkusVersion = prop.getProperty("camel.jbang.quarkusVersion", this.quarkusVersion);
this.springBootVersion = prop.getProperty("camel.jbang.springBootVersion", this.springBootVersion);
}

// use temporary export dir
exportDir = EXPORT_DIR;
if (gav == null) {
gav = "org.apache.camel:camel-jbang-export:1.0";
}
if (runtime == null) {
runtime = "camel-main";
}

if ("spring-boot".equals(runtime) || "camel-spring-boot".equals(runtime)) {
return export(new ExportSpringBoot(getMain()));
} else if ("quarkus".equals(runtime) || "camel-quarkus".equals(runtime)) {
return export(new ExportQuarkus(getMain()));
} else if ("main".equals(runtime) || "camel-main".equals(runtime)) {
return export(new ExportCamelMain(getMain()));
} else {
System.err.println("Unknown runtime: " + runtime);
return 1;
}
}
}

0 comments on commit 57b0c31

Please sign in to comment.