Use context class loader when watching SSL resource
Update `BundleContentProperty` to use a provided resource loader when watching files. Fixes gh-42468
This commit is contained in:
parent
499672184c
commit
dcbf0096d8
|
@ -18,9 +18,9 @@ package org.springframework.boot.autoconfigure.ssl;
|
|||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.springframework.boot.io.ApplicationResourceLoader;
|
||||
import org.springframework.boot.ssl.pem.PemContent;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
@ -51,9 +51,10 @@ record BundleContentProperty(String name, String value) {
|
|||
return StringUtils.hasText(this.value);
|
||||
}
|
||||
|
||||
Path toWatchPath() {
|
||||
Path toWatchPath(ResourceLoader resourceLoader) {
|
||||
try {
|
||||
Resource resource = getResource();
|
||||
Assert.state(!isPemContent(), "Value contains PEM content");
|
||||
Resource resource = resourceLoader.getResource(this.value);
|
||||
if (!resource.isFile()) {
|
||||
throw new BundleContentNotWatchableException(this);
|
||||
}
|
||||
|
@ -68,9 +69,4 @@ record BundleContentProperty(String name, String value) {
|
|||
}
|
||||
}
|
||||
|
||||
private Resource getResource() {
|
||||
Assert.state(!isPemContent(), "Value contains PEM content");
|
||||
return new ApplicationResourceLoader().getResource(this.value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ class SslPropertiesBundleRegistrar implements SslBundleRegistrar {
|
|||
try {
|
||||
return properties.stream()
|
||||
.filter(BundleContentProperty::hasValue)
|
||||
.map(BundleContentProperty::toWatchPath)
|
||||
.map((content) -> content.toWatchPath(this.resourceLoader))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
catch (BundleContentNotWatchableException ex) {
|
||||
|
|
|
@ -22,9 +22,15 @@ import java.nio.file.Path;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.io.ApplicationResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* Tests for {@link BundleContentProperty}.
|
||||
|
@ -72,7 +78,7 @@ class BundleContentPropertyTests {
|
|||
@Test
|
||||
void toWatchPathWhenNotPathThrowsException() {
|
||||
BundleContentProperty property = new BundleContentProperty("name", PEM_TEXT);
|
||||
assertThatIllegalStateException().isThrownBy(property::toWatchPath)
|
||||
assertThatIllegalStateException().isThrownBy(() -> property.toWatchPath(new ApplicationResourceLoader()))
|
||||
.withMessage("Unable to convert value of property 'name' to a path");
|
||||
}
|
||||
|
||||
|
@ -81,13 +87,24 @@ class BundleContentPropertyTests {
|
|||
URL resource = getClass().getResource("keystore.jks");
|
||||
Path file = Path.of(resource.toURI()).toAbsolutePath();
|
||||
BundleContentProperty property = new BundleContentProperty("name", file.toString());
|
||||
assertThat(property.toWatchPath()).isEqualTo(file);
|
||||
assertThat(property.toWatchPath(new ApplicationResourceLoader())).isEqualTo(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toWatchPathUsesResourceLoader() throws URISyntaxException {
|
||||
URL resource = getClass().getResource("keystore.jks");
|
||||
Path file = Path.of(resource.toURI()).toAbsolutePath();
|
||||
BundleContentProperty property = new BundleContentProperty("name", file.toString());
|
||||
ResourceLoader resourceLoader = spy(new ApplicationResourceLoader());
|
||||
assertThat(property.toWatchPath(resourceLoader)).isEqualTo(file);
|
||||
then(resourceLoader).should(atLeastOnce()).getResource(file.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowBundleContentNotWatchableExceptionIfContentIsNotWatchable() {
|
||||
BundleContentProperty property = new BundleContentProperty("name", "https://example.com/");
|
||||
assertThatExceptionOfType(BundleContentNotWatchableException.class).isThrownBy(property::toWatchPath)
|
||||
assertThatExceptionOfType(BundleContentNotWatchableException.class)
|
||||
.isThrownBy(() -> property.toWatchPath(new ApplicationResourceLoader()))
|
||||
.withMessageContaining("Only 'file:' resources are watchable");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue