1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hbase.http; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.InputStream; 23 import java.net.URI; 24 import java.net.URL; 25 26 import javax.net.ssl.HttpsURLConnection; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.hadoop.conf.Configuration; 31 import org.apache.hadoop.fs.FileUtil; 32 import org.apache.hadoop.hbase.HBaseConfiguration; 33 import org.apache.hadoop.hbase.testclassification.SmallTests; 34 import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil; 35 import org.apache.hadoop.io.IOUtils; 36 import org.apache.hadoop.net.NetUtils; 37 import org.apache.hadoop.security.ssl.SSLFactory; 38 import org.junit.AfterClass; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 import org.junit.experimental.categories.Category; 42 43 /** 44 * This testcase issues SSL certificates configures the HttpServer to serve 45 * HTTPS using the created certficates and calls an echo servlet using the 46 * corresponding HTTPS URL. 47 */ 48 @Category(SmallTests.class) 49 public class TestSSLHttpServer extends HttpServerFunctionalTest { 50 private static final String BASEDIR = System.getProperty("test.build.dir", 51 "target/test-dir") + "/" + TestSSLHttpServer.class.getSimpleName(); 52 53 private static final Log LOG = LogFactory.getLog(TestSSLHttpServer.class); 54 private static Configuration conf; 55 private static HttpServer server; 56 private static URL baseUrl; 57 private static String keystoresDir; 58 private static String sslConfDir; 59 private static SSLFactory clientSslFactory; 60 61 @BeforeClass setup()62 public static void setup() throws Exception { 63 conf = new Configuration(); 64 conf.setInt(HttpServer.HTTP_MAX_THREADS, 10); 65 66 File base = new File(BASEDIR); 67 FileUtil.fullyDelete(base); 68 base.mkdirs(); 69 keystoresDir = new File(BASEDIR).getAbsolutePath(); 70 sslConfDir = KeyStoreTestUtil.getClasspathDir(TestSSLHttpServer.class); 71 72 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false); 73 Configuration sslConf = new Configuration(false); 74 sslConf.addResource("ssl-server.xml"); 75 sslConf.addResource("ssl-client.xml"); 76 77 clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, sslConf); 78 clientSslFactory.init(); 79 80 server = new HttpServer.Builder() 81 .setName("test") 82 .addEndpoint(new URI("https://localhost")) 83 .setConf(conf) 84 .keyPassword(HBaseConfiguration.getPassword(sslConf, "ssl.server.keystore.keypassword", 85 null)) 86 .keyStore(sslConf.get("ssl.server.keystore.location"), 87 HBaseConfiguration.getPassword(sslConf, "ssl.server.keystore.password", null), 88 sslConf.get("ssl.server.keystore.type", "jks")) 89 .trustStore(sslConf.get("ssl.server.truststore.location"), 90 HBaseConfiguration.getPassword(sslConf, "ssl.server.truststore.password", null), 91 sslConf.get("ssl.server.truststore.type", "jks")).build(); 92 server.addServlet("echo", "/echo", TestHttpServer.EchoServlet.class); 93 server.start(); 94 baseUrl = new URL("https://" 95 + NetUtils.getHostPortString(server.getConnectorAddress(0))); 96 LOG.info("HTTP server started: " + baseUrl); 97 } 98 99 @AfterClass cleanup()100 public static void cleanup() throws Exception { 101 if (server != null) server.stop(); 102 FileUtil.fullyDelete(new File(BASEDIR)); 103 KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir); 104 clientSslFactory.destroy(); 105 } 106 107 @Test testEcho()108 public void testEcho() throws Exception { 109 assertEquals("a:b\nc:d\n", readOut(new URL(baseUrl, "/echo?a=b&c=d"))); 110 assertEquals("a:b\nc<:d\ne:>\n", readOut(new URL(baseUrl, 111 "/echo?a=b&c<=d&e=>"))); 112 } 113 readOut(URL url)114 private static String readOut(URL url) throws Exception { 115 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 116 conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory()); 117 InputStream in = conn.getInputStream(); 118 ByteArrayOutputStream out = new ByteArrayOutputStream(); 119 IOUtils.copyBytes(in, out, 1024); 120 return out.toString(); 121 } 122 123 } 124