Skip to content

Commit

Permalink
Polish SSL internals
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Nov 2, 2023
1 parent d3f177b commit 99986a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,29 @@ boolean hasValue() {
return StringUtils.hasText(this.value);
}

private URL toUrl() throws FileNotFoundException {
Assert.state(!isPemContent(), "Value contains PEM content");
return ResourceUtils.getURL(this.value);
}

Path toWatchPath() {
return toPath();
}

private Path toPath() {
try {
URL url = toUrl();
Assert.state(isFileUrl(url), () -> "Vaule '%s' is not a file URL".formatted(url));
Assert.state(isFileUrl(url), () -> "Value '%s' is not a file URL".formatted(url));
return Path.of(url.toURI()).toAbsolutePath();
}
catch (Exception ex) {
throw new IllegalStateException("Unable to convert '%s' property to a path".formatted(this.name), ex);
throw new IllegalStateException("Unable to convert value of property '%s' to a path".formatted(this.name),
ex);
}
}

private URL toUrl() throws FileNotFoundException {
Assert.state(!isPemContent(), "Value contains PEM content");
return ResourceUtils.getURL(this.value);
}

private boolean isFileUrl(URL url) {
return "file".equalsIgnoreCase(url.getProtocol());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void hasValueWhenHasEmptyValueReturnsFalse() {
void toWatchPathWhenNotPathThrowsException() {
BundleContentProperty property = new BundleContentProperty("name", PEM_TEXT);
assertThatIllegalStateException().isThrownBy(property::toWatchPath)
.withMessage("Unable to convert 'name' property to a path");
.withMessage("Unable to convert value of property 'name' to a path");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,6 @@ static PemContent load(String content) throws IOException {
}
}

/**
* Load {@link PemContent} from the given {@link URL}.
* @param url the URL to load content from
* @return the loaded PEM content
* @throws IOException on IO error
*/
public static PemContent load(URL url) throws IOException {
Assert.notNull(url, "Url must not be null");
try (InputStream in = url.openStream()) {
return load(in);
}
}

/**
* Load {@link PemContent} from the given {@link Path}.
* @param path a path to load the content from
Expand All @@ -152,6 +139,13 @@ public static PemContent load(Path path) throws IOException {
}
}

private static PemContent load(URL url) throws IOException {
Assert.notNull(url, "Url must not be null");
try (InputStream in = url.openStream()) {
return load(in);
}
}

private static PemContent load(InputStream in) throws IOException {
return of(StreamUtils.copyToString(in, StandardCharsets.UTF_8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void getCertificateWhenNoCertificatesThrowsException() {

@Test
void getCertificateReturnsCertificates() throws Exception {
PemContent content = PemContent.load(getClass().getResource("/test-cert-chain.pem"));
PemContent content = PemContent.load(contentFromClasspath("/test-cert-chain.pem"));
List<X509Certificate> certificates = content.getCertificates();
assertThat(certificates).isNotNull();
assertThat(certificates).hasSize(2);
Expand All @@ -64,7 +64,7 @@ void getPrivateKeyWhenNoKeyThrowsException() {
@Test
void getPrivateKeyReturnsPrivateKey() throws Exception {
PemContent content = PemContent
.load(getClass().getResource("/org/springframework/boot/web/server/pkcs8/dsa.key"));
.load(contentFromClasspath("/org/springframework/boot/web/server/pkcs8/dsa.key"));
PrivateKey privateKey = content.getPrivateKey();
assertThat(privateKey).isNotNull();
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
Expand Down Expand Up @@ -117,30 +117,22 @@ void loadWithStringWhenContentIsPemContentReturnsContent() throws Exception {
@Test
void loadWithStringWhenClasspathLocationReturnsContent() throws IOException {
String actual = PemContent.load("classpath:test-cert.pem").toString();
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
String expected = contentFromClasspath("test-cert.pem");
assertThat(actual).isEqualTo(expected);
}

@Test
void loadWithStringWhenFileLocationReturnsContent() throws IOException {
String actual = PemContent.load("src/test/resources/test-cert.pem").toString();
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
assertThat(actual).isEqualTo(expected);
}

@Test
void loadWithUrlReturnsContent() throws Exception {
ClassPathResource resource = new ClassPathResource("test-cert.pem");
String expected = resource.getContentAsString(StandardCharsets.UTF_8);
String actual = PemContent.load(resource.getURL()).toString();
String expected = contentFromClasspath("test-cert.pem");
assertThat(actual).isEqualTo(expected);
}

@Test
void loadWithPathReturnsContent() throws IOException {
Path path = Path.of("src/test/resources/test-cert.pem");
String actual = PemContent.load(path).toString();
String expected = new ClassPathResource("test-cert.pem").getContentAsString(StandardCharsets.UTF_8);
String expected = contentFromClasspath("test-cert.pem");
assertThat(actual).isEqualTo(expected);
}

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

private static String contentFromClasspath(String path) throws IOException {
return new ClassPathResource(path).getContentAsString(StandardCharsets.UTF_8);
}

}

0 comments on commit 99986a2

Please sign in to comment.