Skip to content

Commit 99986a2

Browse files
Polish SSL internals
1 parent d3f177b commit 99986a2

File tree

4 files changed

+26
-34
lines changed

4 files changed

+26
-34
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/BundleContentProperty.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,29 @@ boolean hasValue() {
5151
return StringUtils.hasText(this.value);
5252
}
5353

54-
private URL toUrl() throws FileNotFoundException {
55-
Assert.state(!isPemContent(), "Value contains PEM content");
56-
return ResourceUtils.getURL(this.value);
57-
}
58-
5954
Path toWatchPath() {
6055
return toPath();
6156
}
6257

6358
private Path toPath() {
6459
try {
6560
URL url = toUrl();
66-
Assert.state(isFileUrl(url), () -> "Vaule '%s' is not a file URL".formatted(url));
61+
Assert.state(isFileUrl(url), () -> "Value '%s' is not a file URL".formatted(url));
6762
return Path.of(url.toURI()).toAbsolutePath();
6863
}
6964
catch (Exception ex) {
70-
throw new IllegalStateException("Unable to convert '%s' property to a path".formatted(this.name), ex);
65+
throw new IllegalStateException("Unable to convert value of property '%s' to a path".formatted(this.name),
66+
ex);
7167
}
7268
}
7369

70+
private URL toUrl() throws FileNotFoundException {
71+
Assert.state(!isPemContent(), "Value contains PEM content");
72+
return ResourceUtils.getURL(this.value);
73+
}
74+
7475
private boolean isFileUrl(URL url) {
7576
return "file".equalsIgnoreCase(url.getProtocol());
7677
}
78+
7779
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ssl/BundleContentPropertyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void hasValueWhenHasEmptyValueReturnsFalse() {
7474
void toWatchPathWhenNotPathThrowsException() {
7575
BundleContentProperty property = new BundleContentProperty("name", PEM_TEXT);
7676
assertThatIllegalStateException().isThrownBy(property::toWatchPath)
77-
.withMessage("Unable to convert 'name' property to a path");
77+
.withMessage("Unable to convert value of property 'name' to a path");
7878
}
7979

8080
@Test

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemContent.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,6 @@ static PemContent load(String content) throws IOException {
126126
}
127127
}
128128

129-
/**
130-
* Load {@link PemContent} from the given {@link URL}.
131-
* @param url the URL to load content from
132-
* @return the loaded PEM content
133-
* @throws IOException on IO error
134-
*/
135-
public static PemContent load(URL url) throws IOException {
136-
Assert.notNull(url, "Url must not be null");
137-
try (InputStream in = url.openStream()) {
138-
return load(in);
139-
}
140-
}
141-
142129
/**
143130
* Load {@link PemContent} from the given {@link Path}.
144131
* @param path a path to load the content from
@@ -152,6 +139,13 @@ public static PemContent load(Path path) throws IOException {
152139
}
153140
}
154141

142+
private static PemContent load(URL url) throws IOException {
143+
Assert.notNull(url, "Url must not be null");
144+
try (InputStream in = url.openStream()) {
145+
return load(in);
146+
}
147+
}
148+
155149
private static PemContent load(InputStream in) throws IOException {
156150
return of(StreamUtils.copyToString(in, StandardCharsets.UTF_8));
157151
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ssl/pem/PemContentTests.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void getCertificateWhenNoCertificatesThrowsException() {
4646

4747
@Test
4848
void getCertificateReturnsCertificates() throws Exception {
49-
PemContent content = PemContent.load(getClass().getResource("/test-cert-chain.pem"));
49+
PemContent content = PemContent.load(contentFromClasspath("/test-cert-chain.pem"));
5050
List<X509Certificate> certificates = content.getCertificates();
5151
assertThat(certificates).isNotNull();
5252
assertThat(certificates).hasSize(2);
@@ -64,7 +64,7 @@ void getPrivateKeyWhenNoKeyThrowsException() {
6464
@Test
6565
void getPrivateKeyReturnsPrivateKey() throws Exception {
6666
PemContent content = PemContent
67-
.load(getClass().getResource("/org/springframework/boot/web/server/pkcs8/dsa.key"));
67+
.load(contentFromClasspath("/org/springframework/boot/web/server/pkcs8/dsa.key"));
6868
PrivateKey privateKey = content.getPrivateKey();
6969
assertThat(privateKey).isNotNull();
7070
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
@@ -117,30 +117,22 @@ void loadWithStringWhenContentIsPemContentReturnsContent() throws Exception {
117117
@Test
118118
void loadWithStringWhenClasspathLocationReturnsContent() throws IOException {
119119
String actual = PemContent.load("classpath:test-cert.pem").toString();
120-
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
120+
String expected = contentFromClasspath("test-cert.pem");
121121
assertThat(actual).isEqualTo(expected);
122122
}
123123

124124
@Test
125125
void loadWithStringWhenFileLocationReturnsContent() throws IOException {
126126
String actual = PemContent.load("src/test/resources/test-cert.pem").toString();
127-
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
128-
assertThat(actual).isEqualTo(expected);
129-
}
130-
131-
@Test
132-
void loadWithUrlReturnsContent() throws Exception {
133-
ClassPathResource resource = new ClassPathResource("test-cert.pem");
134-
String expected = resource.getContentAsString(StandardCharsets.UTF_8);
135-
String actual = PemContent.load(resource.getURL()).toString();
127+
String expected = contentFromClasspath("test-cert.pem");
136128
assertThat(actual).isEqualTo(expected);
137129
}
138130

139131
@Test
140132
void loadWithPathReturnsContent() throws IOException {
141133
Path path = Path.of("src/test/resources/test-cert.pem");
142134
String actual = PemContent.load(path).toString();
143-
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
135+
String expected = contentFromClasspath("test-cert.pem");
144136
assertThat(actual).isEqualTo(expected);
145137
}
146138

@@ -154,4 +146,8 @@ void ofReturnsContent() {
154146
assertThat(PemContent.of("test")).hasToString("test");
155147
}
156148

149+
private static String contentFromClasspath(String path) throws IOException {
150+
return new ClassPathResource(path).getContentAsString(StandardCharsets.UTF_8);
151+
}
152+
157153
}

0 commit comments

Comments
 (0)