Add sbom module: Spring Boot 4.2.0-RC1 tools jar-mode sbom command, CycloneDX in the jar, Actuator endpoint, licence check and OSV scan
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cfo2K4u4rF18QsPXPFnytf
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.ankurm.sbom;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SbomApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SbomApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# Only health is exposed over HTTP by default. The sbom endpoint has to be listed explicitly.
|
||||
management.endpoints.web.exposure.include=health,sbom
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.ankurm.sbom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.sbom.support.Transcript;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Starts the application and asks Actuator for its SBOM. The file is the one the CycloneDX plugin wrote into
|
||||
* target/classes/META-INF/sbom during generate-resources, so the test needs no jar.
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class ActuatorSbomTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
private final HttpClient http = HttpClient.newHttpClient();
|
||||
private final ObjectMapper json = new ObjectMapper();
|
||||
|
||||
private HttpResponse<String> get(String path) throws Exception {
|
||||
return http.send(HttpRequest.newBuilder(URI.create("http://localhost:" + port + path)).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorServesTheSbomTheBuildProduced() throws Exception {
|
||||
Transcript t = new Transcript("05-actuator-sbom");
|
||||
|
||||
JsonNode index = json.readTree(get("/actuator").body());
|
||||
t.line("GET /actuator -> links: %s", names(index.get("_links")));
|
||||
|
||||
HttpResponse<String> ids = get("/actuator/sbom");
|
||||
t.line("GET /actuator/sbom -> %d %s", ids.statusCode(), ids.body());
|
||||
|
||||
HttpResponse<String> app = get("/actuator/sbom/application");
|
||||
JsonNode served = json.readTree(app.body());
|
||||
t.line("GET /actuator/sbom/application -> %d, content-type %s", app.statusCode(),
|
||||
app.headers().firstValue("content-type").orElse("?"));
|
||||
t.line(" bomFormat %s, specVersion %s, components %d", served.get("bomFormat").asString(),
|
||||
served.get("specVersion").asString(), served.get("components").size());
|
||||
|
||||
JsonNode onDisk = json.readTree(Files.readString(Path.of("target/classes/META-INF/sbom/application.cdx.json")));
|
||||
t.line("same document as the file the build wrote: %s", served.equals(onDisk));
|
||||
|
||||
HttpResponse<String> unknown = get("/actuator/sbom/nope");
|
||||
t.line("GET /actuator/sbom/nope -> %d", unknown.statusCode());
|
||||
t.write();
|
||||
|
||||
assertThat(ids.body()).isEqualTo("{\"ids\":[\"application\"]}");
|
||||
assertThat(served).isEqualTo(onDisk);
|
||||
assertThat(unknown.statusCode()).isEqualTo(404);
|
||||
}
|
||||
|
||||
private static String names(JsonNode links) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
links.propertyNames().forEach(n -> sb.append(sb.isEmpty() ? "" : ", ").append(n));
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.ankurm.sbom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.sbom.support.Transcript;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
|
||||
/** Removes our one-line exposure setting to show what a stock application does. */
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
properties = "management.endpoints.web.exposure.include=health")
|
||||
class DefaultExposureTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Test
|
||||
void sbomIsNotExposedUnlessYouAskForIt() throws Exception {
|
||||
HttpClient http = HttpClient.newHttpClient();
|
||||
HttpResponse<String> r = http.send(
|
||||
HttpRequest.newBuilder(URI.create("http://localhost:" + port + "/actuator/sbom")).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
Transcript t = new Transcript("06-actuator-sbom-not-exposed");
|
||||
t.line("management.endpoints.web.exposure.include=health");
|
||||
t.line("GET /actuator/sbom -> %d", r.statusCode());
|
||||
t.write();
|
||||
assertThat(r.statusCode()).isEqualTo(404);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.ankurm.sbom.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Collects lines and writes them to output/NN-name.txt, so the test and the transcript cannot drift apart. */
|
||||
public class Transcript {
|
||||
|
||||
private final String name;
|
||||
private final List<String> lines = new ArrayList<>();
|
||||
|
||||
public Transcript(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void line(String format, Object... args) {
|
||||
lines.add(args.length == 0 ? format : String.format(format, args));
|
||||
}
|
||||
|
||||
public void write() {
|
||||
try {
|
||||
Path dir = Path.of("output");
|
||||
Files.createDirectories(dir);
|
||||
Files.write(dir.resolve(name + ".txt"), lines);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user