View Javadoc

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.testclassification.SmallTests;
33  import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
34  import org.apache.hadoop.io.IOUtils;
35  import org.apache.hadoop.net.NetUtils;
36  import org.apache.hadoop.security.ssl.SSLFactory;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  /**
43   * This testcase issues SSL certificates configures the HttpServer to serve
44   * HTTPS using the created certficates and calls an echo servlet using the
45   * corresponding HTTPS URL.
46   */
47  @Category(SmallTests.class)
48  public class TestSSLHttpServer extends HttpServerFunctionalTest {
49    private static final String BASEDIR = System.getProperty("test.build.dir",
50        "target/test-dir") + "/" + TestSSLHttpServer.class.getSimpleName();
51  
52    private static final Log LOG = LogFactory.getLog(TestSSLHttpServer.class);
53    private static Configuration conf;
54    private static HttpServer server;
55    private static URL baseUrl;
56    private static String keystoresDir;
57    private static String sslConfDir;
58    private static SSLFactory clientSslFactory;
59  
60    @BeforeClass
61    public static void setup() throws Exception {
62      conf = new Configuration();
63      conf.setInt(HttpServer.HTTP_MAX_THREADS, 10);
64  
65      File base = new File(BASEDIR);
66      FileUtil.fullyDelete(base);
67      base.mkdirs();
68      keystoresDir = new File(BASEDIR).getAbsolutePath();
69      sslConfDir = KeyStoreTestUtil.getClasspathDir(TestSSLHttpServer.class);
70  
71      KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false);
72      Configuration sslConf = new Configuration(false);
73      sslConf.addResource("ssl-server.xml");
74      sslConf.addResource("ssl-client.xml");
75  
76      clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, sslConf);
77      clientSslFactory.init();
78  
79      server = new HttpServer.Builder()
80          .setName("test")
81          .addEndpoint(new URI("https://localhost"))
82          .setConf(conf)
83          .keyPassword(sslConf.get("ssl.server.keystore.keypassword"))
84          .keyStore(sslConf.get("ssl.server.keystore.location"),
85              sslConf.get("ssl.server.keystore.password"),
86              sslConf.get("ssl.server.keystore.type", "jks"))
87          .trustStore(sslConf.get("ssl.server.truststore.location"),
88              sslConf.get("ssl.server.truststore.password"),
89              sslConf.get("ssl.server.truststore.type", "jks")).build();
90      server.addServlet("echo", "/echo", TestHttpServer.EchoServlet.class);
91      server.start();
92      baseUrl = new URL("https://"
93          + NetUtils.getHostPortString(server.getConnectorAddress(0)));
94      LOG.info("HTTP server started: " + baseUrl);
95    }
96  
97    @AfterClass
98    public static void cleanup() throws Exception {
99      if (server != null) server.stop();
100     FileUtil.fullyDelete(new File(BASEDIR));
101     KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir);
102     clientSslFactory.destroy();
103   }
104 
105   @Test
106   public void testEcho() throws Exception {
107     assertEquals("a:b\nc:d\n", readOut(new URL(baseUrl, "/echo?a=b&c=d")));
108     assertEquals("a:b\nc<:d\ne:>\n", readOut(new URL(baseUrl,
109         "/echo?a=b&c<=d&e=>")));
110   }
111 
112   private static String readOut(URL url) throws Exception {
113     HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
114     conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
115     InputStream in = conn.getInputStream();
116     ByteArrayOutputStream out = new ByteArrayOutputStream();
117     IOUtils.copyBytes(in, out, 1024);
118     return out.toString();
119   }
120 
121 }