Skip to content

Commit

Permalink
Add resource hints for SBOM endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed May 29, 2024
1 parent c2fd48b commit d62d615
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
import java.util.Map;
import java.util.TreeSet;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.sbom.SbomEndpoint.SbomEndpointRuntimeHints;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
Expand All @@ -38,6 +42,7 @@
* @since 3.3.0
*/
@Endpoint(id = "sbom")
@ImportRuntimeHints(SbomEndpointRuntimeHints.class)
public class SbomEndpoint {

private static final List<String> DEFAULT_APPLICATION_SBOM_LOCATIONS = List.of("classpath:META-INF/sbom/bom.json",
Expand Down Expand Up @@ -141,4 +146,19 @@ private static String stripOptionalPrefix(String location) {
}
}

static class SbomEndpointRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
for (String defaultLocation : DEFAULT_APPLICATION_SBOM_LOCATIONS) {
hints.resources().registerPattern(stripClasspathPrefix(defaultLocation));
}
}

private String stripClasspathPrefix(String location) {
return location.substring("classpath:".length());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.actuate.sbom.SbomEndpoint.SbomEndpointRuntimeHints;
import org.springframework.boot.actuate.sbom.SbomEndpoint.Sboms;
import org.springframework.boot.actuate.sbom.SbomProperties.Sbom;
import org.springframework.context.support.GenericApplicationContext;
Expand Down Expand Up @@ -81,6 +84,14 @@ void shouldNotFailIfNonExistingOptionalLocationIsGiven() {
assertThat(createEndpoint().sbom("application")).isNull();
}

@Test
void shouldRegisterHints() {
RuntimeHints hints = new RuntimeHints();
new SbomEndpointRuntimeHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.resource().forResource("META-INF/sbom/bom.json")).accepts(hints);
assertThat(RuntimeHintsPredicates.resource().forResource("META-INF/sbom/application.cdx.json")).accepts(hints);
}

private Sbom sbom(String location) {
Sbom result = new Sbom();
result.setLocation(location);
Expand Down

0 comments on commit d62d615

Please sign in to comment.