Polish SSL internals

This commit is contained in:
Scott Frederick
2023-11-02 13:53:08 -05:00
parent d3f177be71
commit 99986a2fdd
4 changed files with 26 additions and 34 deletions

View File

@@ -51,11 +51,6 @@ record BundleContentProperty(String name, String value) {
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();
}
@@ -63,15 +58,22 @@ record BundleContentProperty(String name, String value) {
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());
}
}

View File

@@ -74,7 +74,7 @@ class BundleContentPropertyTests {
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

View File

@@ -126,19 +126,6 @@ public final class PemContent {
}
}
/**
* 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
@@ -152,6 +139,13 @@ public final class PemContent {
}
}
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));
}

View File

@@ -46,7 +46,7 @@ class PemContentTests {
@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);
@@ -64,7 +64,7 @@ class PemContentTests {
@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");
@@ -117,22 +117,14 @@ class PemContentTests {
@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);
}
@@ -140,7 +132,7 @@ class PemContentTests {
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);
}
@@ -154,4 +146,8 @@ class PemContentTests {
assertThat(PemContent.of("test")).hasToString("test");
}
private static String contentFromClasspath(String path) throws IOException {
return new ClassPathResource(path).getContentAsString(StandardCharsets.UTF_8);
}
}