|
| 1 | +package com.example.sslreload; |
| 2 | + |
| 3 | +import com.google.common.io.CharStreams; |
| 4 | +import com.google.common.io.Files; |
| 5 | +import com.google.common.io.Resources; |
| 6 | +import io.dropwizard.Configuration; |
| 7 | +import io.dropwizard.testing.ConfigOverride; |
| 8 | +import io.dropwizard.testing.ResourceHelpers; |
| 9 | +import io.dropwizard.testing.junit.DropwizardAppRule; |
| 10 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.ClassRule; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.rules.TemporaryFolder; |
| 17 | + |
| 18 | +import javax.net.ssl.HttpsURLConnection; |
| 19 | +import javax.net.ssl.SSLContext; |
| 20 | +import javax.net.ssl.TrustManager; |
| 21 | +import javax.net.ssl.X509TrustManager; |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.net.URL; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.security.cert.CertificateException; |
| 28 | +import java.security.cert.X509Certificate; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Java6Assertions.assertThat; |
| 31 | + |
| 32 | +public class SslReloadAppTest { |
| 33 | + @ClassRule |
| 34 | + public static final TemporaryFolder FOLDER = new TemporaryFolder(); |
| 35 | + |
| 36 | + private static final X509TrustManager TRUST_ALL = new X509TrustManager() { |
| 37 | + @Override |
| 38 | + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public X509Certificate[] getAcceptedIssuers() { |
| 49 | + return new X509Certificate[0]; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + private static File keystore; |
| 54 | + |
| 55 | + @Rule |
| 56 | + public final DropwizardAppRule<Configuration> rule = |
| 57 | + new DropwizardAppRule<>(SslReloadApp.class, ResourceHelpers.resourceFilePath("sslreload/config.yml"), |
| 58 | + ConfigOverride.config("server.applicationConnectors[0].keyStorePath", keystore.getAbsolutePath()), |
| 59 | + ConfigOverride.config("server.adminConnectors[0].keyStorePath", keystore.getAbsolutePath())); |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void setupClass() throws IOException { |
| 63 | + keystore = FOLDER.newFile("keystore.jks"); |
| 64 | + final byte[] keystoreBytes = Resources.toByteArray(Resources.getResource("sslreload/keystore.jks")); |
| 65 | + Files.write(keystoreBytes, keystore); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void after() throws IOException { |
| 70 | + // Reset keystore to known good keystore |
| 71 | + final byte[] keystoreBytes = Resources.toByteArray(Resources.getResource("sslreload/keystore.jks")); |
| 72 | + Files.write(keystoreBytes, keystore); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void reloadCertificateChangesTheServerCertificate() throws Exception { |
| 77 | + // Copy over our new keystore that has our new certificate to the current |
| 78 | + // location of our keystore |
| 79 | + final byte[] keystore2Bytes = Resources.toByteArray(Resources.getResource("sslreload/keystore2.jks")); |
| 80 | + Files.write(keystore2Bytes, keystore); |
| 81 | + |
| 82 | + // Get the bytes for the first certificate, and trigger a reload |
| 83 | + byte[] firstCertBytes = certBytes(200, "Reloaded certificate configuration\n"); |
| 84 | + |
| 85 | + // Get the bytes from our newly reloaded certificate |
| 86 | + byte[] secondCertBytes = certBytes(200, "Reloaded certificate configuration\n"); |
| 87 | + |
| 88 | + // Get the bytes from the reloaded certificate, but it should be the same |
| 89 | + // as the second cert because we didn't change anything! |
| 90 | + byte[] thirdCertBytes = certBytes(200, "Reloaded certificate configuration\n"); |
| 91 | + |
| 92 | + assertThat(firstCertBytes).isNotEqualTo(secondCertBytes); |
| 93 | + assertThat(secondCertBytes).isEqualTo(thirdCertBytes); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void badReloadDoesNotChangeTheServerCertificate() throws Exception { |
| 98 | + // This keystore has a different password than what jetty has been configured with |
| 99 | + // the password is "password2" |
| 100 | + final byte[] badKeystore = Resources.toByteArray(Resources.getResource("sslreload/keystore-diff-pwd.jks")); |
| 101 | + Files.write(badKeystore, keystore); |
| 102 | + |
| 103 | + // Get the bytes for the first certificate. The reload should fail |
| 104 | + byte[] firstCertBytes = certBytes(500, "Keystore was tampered with, or password was incorrect"); |
| 105 | + |
| 106 | + // Issue another request. The returned certificate should be the same as before |
| 107 | + byte[] secondCertBytes = certBytes(500, "Keystore was tampered with, or password was incorrect"); |
| 108 | + |
| 109 | + // And just to triple check, a third request will continue with |
| 110 | + // the same original certificate |
| 111 | + byte[] thirdCertBytes = certBytes(500, "Keystore was tampered with, or password was incorrect"); |
| 112 | + |
| 113 | + assertThat(firstCertBytes) |
| 114 | + .isEqualTo(secondCertBytes) |
| 115 | + .isEqualTo(thirdCertBytes); |
| 116 | + } |
| 117 | + |
| 118 | + /** Issues a POST against the reload ssl admin task, asserts that the code and content |
| 119 | + * are as expected, and finally returns the server certificate */ |
| 120 | + private byte[] certBytes(int code, String content) throws Exception { |
| 121 | + final URL url = new URL("https://localhost:" + rule.getAdminPort() + "/tasks/reload-ssl"); |
| 122 | + final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); |
| 123 | + try { |
| 124 | + postIt(conn); |
| 125 | + |
| 126 | + assertThat(conn.getResponseCode()).isEqualTo(code); |
| 127 | + if (code == 200) { |
| 128 | + assertThat(CharStreams.toString(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) |
| 129 | + .isEqualTo(content); |
| 130 | + } else { |
| 131 | + assertThat(CharStreams.toString(new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8))) |
| 132 | + .contains(content); |
| 133 | + } |
| 134 | + |
| 135 | + // The certificates are self signed, so are the only cert in the chain. |
| 136 | + // Thus, we return the one and only certificate. |
| 137 | + return conn.getServerCertificates()[0].getEncoded(); |
| 138 | + } finally { |
| 139 | + conn.disconnect(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** Configure SSL and POST request parameters */ |
| 144 | + private void postIt(HttpsURLConnection conn) throws Exception { |
| 145 | + final SSLContext sslCtx = SSLContext.getInstance("TLS"); |
| 146 | + sslCtx.init(null, new TrustManager[]{TRUST_ALL}, null); |
| 147 | + |
| 148 | + conn.setHostnameVerifier(new NoopHostnameVerifier()); |
| 149 | + conn.setSSLSocketFactory(sslCtx.getSocketFactory()); |
| 150 | + |
| 151 | + // Make it a POST |
| 152 | + conn.setDoOutput(true); |
| 153 | + conn.getOutputStream().write(new byte[]{}); |
| 154 | + } |
| 155 | +} |
0 commit comments