Skip to content

Commit

Permalink
Use context class loader when watching SSL resource
Browse files Browse the repository at this point in the history
Update `BundleContentProperty` to use a provided resource loader when
watching files.

Fixes gh-42468
  • Loading branch information
philwebb committed Oct 23, 2024
1 parent 4996721 commit dcbf009
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

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;

Expand Down Expand Up @@ -51,9 +51,10 @@ boolean hasValue() {
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);
}
Expand All @@ -68,9 +69,4 @@ Path toWatchPath() {
}
}

private Resource getResource() {
Assert.state(!isPemContent(), "Value contains PEM content");
return new ApplicationResourceLoader().getResource(this.value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private Set<Path> watchedPaths(String bundleName, List<BundleContentProperty> pr
try {
return properties.stream()
.filter(BundleContentProperty::hasValue)
.map(BundleContentProperty::toWatchPath)
.map((content) -> content.toWatchPath(this.resourceLoader))
.collect(Collectors.toSet());
}
catch (BundleContentNotWatchableException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@

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}.
Expand Down Expand Up @@ -72,7 +78,7 @@ void hasValueWhenHasEmptyValueReturnsFalse() {
@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");
}

Expand All @@ -81,13 +87,24 @@ void toWatchPathWhenPathReturnsPath() throws URISyntaxException {
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");
}

Expand Down

0 comments on commit dcbf009

Please sign in to comment.