1818package org .apache .hadoop .hbase .http ;
1919
2020import static org .junit .Assert .assertEquals ;
21+ import static org .junit .Assert .assertNotEquals ;
2122
2223import java .io .ByteArrayOutputStream ;
2324import java .io .File ;
2627import java .net .URI ;
2728import java .net .URL ;
2829import java .security .GeneralSecurityException ;
30+ import java .security .KeyPair ;
31+ import java .security .KeyStore ;
32+ import java .security .cert .X509Certificate ;
2933import javax .net .ssl .HttpsURLConnection ;
3034import org .apache .hadoop .conf .Configuration ;
3135import org .apache .hadoop .fs .FileUtil ;
3741import org .apache .hadoop .hbase .testclassification .MiscTests ;
3842import org .apache .hadoop .io .IOUtils ;
3943import org .apache .hadoop .net .NetUtils ;
44+ import org .apache .hadoop .security .ssl .FileBasedKeyStoresFactory ;
4045import org .apache .hadoop .security .ssl .SSLFactory ;
4146import org .junit .AfterClass ;
4247import org .junit .BeforeClass ;
4651import org .slf4j .Logger ;
4752import org .slf4j .LoggerFactory ;
4853
54+ import org .apache .hbase .thirdparty .org .eclipse .jetty .server .AbstractConnector ;
55+ import org .apache .hbase .thirdparty .org .eclipse .jetty .server .ConnectionFactory ;
56+ import org .apache .hbase .thirdparty .org .eclipse .jetty .server .SslConnectionFactory ;
57+ import org .apache .hbase .thirdparty .org .eclipse .jetty .util .ssl .SslContextFactory ;
58+
4959/**
5060 * This testcase issues SSL certificates configures the HttpServer to serve HTTPS using the created
5161 * certficates and calls an echo servlet using the corresponding HTTPS URL.
@@ -65,6 +75,7 @@ public class TestSSLHttpServer extends HttpServerFunctionalTest {
6575 private static String sslConfDir ;
6676 private static SSLFactory clientSslFactory ;
6777 private static HBaseCommonTestingUtil HTU ;
78+ private static long reloadInterval ;
6879
6980 @ BeforeClass
7081 public static void setup () throws Exception {
@@ -74,6 +85,9 @@ public static void setup() throws Exception {
7485
7586 serverConf .setInt (HttpServer .HTTP_MAX_THREADS , TestHttpServer .MAX_THREADS );
7687 serverConf .setBoolean (ServerConfigurationKeys .HBASE_SSL_ENABLED_KEY , true );
88+ reloadInterval = 1000 ;
89+ serverConf .setLong (FileBasedKeyStoresFactory .SSL_STORES_RELOAD_INTERVAL_TPL_KEY ,
90+ reloadInterval );
7791
7892 keystoresDir = new File (HTU .getDataTestDir ("keystore" ).toString ());
7993 keystoresDir .mkdirs ();
@@ -131,6 +145,45 @@ public void testSecurityHeaders() throws IOException, GeneralSecurityException {
131145 conn .getHeaderField ("Content-Security-Policy" ));
132146 }
133147
148+ @ Test (timeout = 60000 )
149+ public void testReloadKeyStore () throws Exception {
150+ String serverKS = keystoresDir + "/serverKS.jks" ;
151+ String serverPassword = "serverP" ;
152+
153+ KeyStore oldKeyStore = KeyStoreTestUtil .loadKeyStore (serverKS , serverPassword .toCharArray ());
154+
155+ KeyPair sKP = KeyStoreTestUtil .generateKeyPair ("RSA" );
156+ X509Certificate sCert =
157+ KeyStoreTestUtil .generateCertificate ("CN=localhost, O=server" , sKP , 30 , "SHA1withRSA" );
158+ KeyStoreTestUtil .createKeyStore (serverKS , serverPassword , "server" , sKP .getPrivate (), sCert );
159+ KeyStore newKeyStore = KeyStoreTestUtil .loadKeyStore (serverKS , serverPassword .toCharArray ());
160+
161+ Thread .sleep ((reloadInterval + 1000 ));
162+
163+ for (AbstractConnector connector : server .getServerConnectors ()) {
164+ if (connector != null ) {
165+ for (ConnectionFactory connectionFactory : connector .getConnectionFactories ()) {
166+ if (connectionFactory instanceof SslConnectionFactory ) {
167+ SslContextFactory sslContextFactory =
168+ ((SslConnectionFactory ) connectionFactory ).getSslContextFactory ();
169+ KeyStore currentKeyStore = sslContextFactory .getKeyStore ();
170+
171+ assertNotEquals (currentKeyStore .getCertificate ("server" ),
172+ oldKeyStore .getCertificate ("server" ));
173+ assertNotEquals (currentKeyStore .getKey ("server" , serverPassword .toCharArray ()),
174+ oldKeyStore .getKey ("server" , serverPassword .toCharArray ()));
175+
176+ assertEquals (currentKeyStore .getCertificate ("server" ),
177+ newKeyStore .getCertificate ("server" ));
178+ assertEquals (currentKeyStore .getKey ("server" , serverPassword .toCharArray ()),
179+ newKeyStore .getKey ("server" , serverPassword .toCharArray ()));
180+
181+ }
182+ }
183+ }
184+ }
185+ }
186+
134187 private static String readOut (URL url ) throws Exception {
135188 HttpsURLConnection conn = (HttpsURLConnection ) url .openConnection ();
136189 conn .setSSLSocketFactory (clientSslFactory .createSSLSocketFactory ());
0 commit comments