001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http; 019 020import java.io.ByteArrayOutputStream; 021import java.io.File; 022import java.io.InputStream; 023import java.net.URI; 024import java.net.URL; 025import javax.net.ssl.HttpsURLConnection; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileUtil; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseConfiguration; 030import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil; 031import org.apache.hadoop.hbase.testclassification.MiscTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.apache.hadoop.io.IOUtils; 034import org.apache.hadoop.net.NetUtils; 035import org.apache.hadoop.security.ssl.SSLFactory; 036import org.junit.AfterClass; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.slf4j.Logger; 042import org.slf4j.LoggerFactory; 043 044/** 045 * This testcase issues SSL certificates configures the HttpServer to serve 046 * HTTPS using the created certficates and calls an echo servlet using the 047 * corresponding HTTPS URL. 048 */ 049@Category({MiscTests.class, SmallTests.class}) 050public class TestSSLHttpServer extends HttpServerFunctionalTest { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestSSLHttpServer.class); 055 056 private static final String BASEDIR = System.getProperty("test.build.dir", 057 "target/test-dir") + "/" + TestSSLHttpServer.class.getSimpleName(); 058 059 private static final Logger LOG = LoggerFactory.getLogger(TestSSLHttpServer.class); 060 private static Configuration conf; 061 private static HttpServer server; 062 private static URL baseUrl; 063 private static String keystoresDir; 064 private static String sslConfDir; 065 private static SSLFactory clientSslFactory; 066 067 @BeforeClass 068 public static void setup() throws Exception { 069 conf = new Configuration(); 070 conf.setInt(HttpServer.HTTP_MAX_THREADS, TestHttpServer.MAX_THREADS); 071 072 File base = new File(BASEDIR); 073 FileUtil.fullyDelete(base); 074 base.mkdirs(); 075 keystoresDir = new File(BASEDIR).getAbsolutePath(); 076 sslConfDir = KeyStoreTestUtil.getClasspathDir(TestSSLHttpServer.class); 077 078 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false); 079 Configuration sslConf = new Configuration(false); 080 sslConf.addResource("ssl-server.xml"); 081 sslConf.addResource("ssl-client.xml"); 082 083 clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, sslConf); 084 clientSslFactory.init(); 085 086 server = new HttpServer.Builder() 087 .setName("test") 088 .addEndpoint(new URI("https://localhost")) 089 .setConf(conf) 090 .keyPassword(HBaseConfiguration.getPassword(sslConf, "ssl.server.keystore.keypassword", 091 null)) 092 .keyStore(sslConf.get("ssl.server.keystore.location"), 093 HBaseConfiguration.getPassword(sslConf, "ssl.server.keystore.password", null), 094 sslConf.get("ssl.server.keystore.type", "jks")) 095 .trustStore(sslConf.get("ssl.server.truststore.location"), 096 HBaseConfiguration.getPassword(sslConf, "ssl.server.truststore.password", null), 097 sslConf.get("ssl.server.truststore.type", "jks")).build(); 098 server.addServlet("echo", "/echo", TestHttpServer.EchoServlet.class); 099 server.start(); 100 baseUrl = new URL("https://" 101 + NetUtils.getHostPortString(server.getConnectorAddress(0))); 102 LOG.info("HTTP server started: " + baseUrl); 103 } 104 105 @AfterClass 106 public static void cleanup() throws Exception { 107 server.stop(); 108 FileUtil.fullyDelete(new File(BASEDIR)); 109 KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir); 110 clientSslFactory.destroy(); 111 } 112 113 @Test 114 public void testEcho() throws Exception { 115 assertEquals("a:b\nc:d\n", readOut(new URL(baseUrl, "/echo?a=b&c=d"))); 116 assertEquals("a:b\nc<:d\ne:>\n", readOut(new URL(baseUrl, 117 "/echo?a=b&c<=d&e=>"))); 118 } 119 120 private static String readOut(URL url) throws Exception { 121 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 122 conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory()); 123 InputStream in = conn.getInputStream(); 124 ByteArrayOutputStream out = new ByteArrayOutputStream(); 125 IOUtils.copyBytes(in, out, 1024); 126 return out.toString(); 127 } 128 129}