2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License. See accompanying LICENSE file.
14 package org
.apache
.hadoop
.hbase
.http
;
16 import java
.util
.List
;
18 import java
.io
.IOException
;
19 import java
.net
.HttpURLConnection
;
20 import java
.net
.HttpCookie
;
23 import javax
.net
.ssl
.HttpsURLConnection
;
24 import javax
.servlet
.Filter
;
25 import javax
.servlet
.FilterConfig
;
26 import javax
.servlet
.FilterChain
;
27 import javax
.servlet
.ServletRequest
;
28 import javax
.servlet
.ServletResponse
;
29 import javax
.servlet
.ServletException
;
30 import javax
.servlet
.http
.HttpServletResponse
;
31 import java
.security
.GeneralSecurityException
;
32 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
35 import org
.apache
.hadoop
.conf
.Configuration
;
36 import org
.apache
.hadoop
.fs
.FileUtil
;
37 import org
.apache
.hadoop
.net
.NetUtils
;
38 import org
.apache
.hadoop
.security
.authentication
.server
.AuthenticationFilter
;
39 import org
.apache
.hadoop
.security
.ssl
.KeyStoreTestUtil
;
40 import org
.apache
.hadoop
.security
.ssl
.SSLFactory
;
42 import org
.junit
.Assert
;
43 import org
.junit
.AfterClass
;
44 import org
.junit
.BeforeClass
;
45 import org
.junit
.ClassRule
;
46 import org
.junit
.Test
;
47 import org
.junit
.experimental
.categories
.Category
;
49 @Category({ MiscTests
.class, SmallTests
.class})
50 public class TestHttpCookieFlag
{
52 public static final HBaseClassTestRule CLASS_RULE
=
53 HBaseClassTestRule
.forClass(TestHttpCookieFlag
.class);
55 private static final String BASEDIR
= System
.getProperty("test.build.dir",
56 "target/test-dir") + "/" +
57 org
.apache
.hadoop
.hbase
.http
.TestHttpCookieFlag
.class.getSimpleName();
58 private static String keystoresDir
;
59 private static String sslConfDir
;
60 private static SSLFactory clientSslFactory
;
61 private static HttpServer server
;
63 public static class DummyAuthenticationFilter
implements Filter
{
66 public void init(FilterConfig filterConfig
) throws ServletException
{
70 public void doFilter(ServletRequest request
, ServletResponse response
,
71 FilterChain chain
) throws IOException
,
73 HttpServletResponse resp
= (HttpServletResponse
) response
;
74 boolean isHttps
= "https".equals(request
.getScheme());
75 AuthenticationFilter
.createAuthCookie(resp
, "token", null, null, -1,
77 chain
.doFilter(request
, resp
);
81 public void destroy() {
84 public static class DummyFilterInitializer
extends FilterInitializer
{
86 public void initFilter(FilterContainer container
, Configuration conf
) {
87 container
.addFilter("DummyAuth", DummyAuthenticationFilter
.class
93 public static void setUp() throws Exception
{
94 Configuration conf
= new Configuration();
95 conf
.set(HttpServer
.FILTER_INITIALIZERS_PROPERTY
,
96 DummyFilterInitializer
.class.getName());
97 conf
.setInt("hbase.http.max.threads", 19); /* acceptors=2 + selectors=16 + request=1 */
98 System
.setProperty("hadoop.log.dir", BASEDIR
); /* needed for /logs */
100 File base
= new File(BASEDIR
);
101 FileUtil
.fullyDelete(base
);
103 keystoresDir
= new File(BASEDIR
).getAbsolutePath();
104 sslConfDir
= KeyStoreTestUtil
.getClasspathDir(TestSSLHttpServer
.class);
106 KeyStoreTestUtil
.setupSSLConfig(keystoresDir
, sslConfDir
, conf
, false);
107 Configuration sslConf
= KeyStoreTestUtil
.getSslConfig();
109 clientSslFactory
= new SSLFactory(SSLFactory
.Mode
.CLIENT
, sslConf
);
110 clientSslFactory
.init();
112 server
= new HttpServer
.Builder()
114 .addEndpoint(new URI("http://localhost"))
115 .addEndpoint(new URI("https://localhost"))
117 .keyPassword(sslConf
.get("ssl.server.keystore.keypassword"))
118 .keyStore(sslConf
.get("ssl.server.keystore.location"),
119 sslConf
.get("ssl.server.keystore.password"),
120 sslConf
.get("ssl.server.keystore.type", "jks"))
121 .trustStore(sslConf
.get("ssl.server.truststore.location"),
122 sslConf
.get("ssl.server.truststore.password"),
123 sslConf
.get("ssl.server.truststore.type", "jks"))
125 server
.addPrivilegedServlet("echo", "/echo", TestHttpServer
.EchoServlet
.class);
130 public void testHttpCookie() throws IOException
{
131 URL base
= new URL("http://" + NetUtils
.getHostPortString(server
132 .getConnectorAddress(0)));
133 HttpURLConnection conn
= (HttpURLConnection
) new URL(base
,
134 "/echo").openConnection();
136 String header
= conn
.getHeaderField("Set-Cookie");
137 Assert
.assertTrue(header
!= null);
138 List
<HttpCookie
> cookies
= HttpCookie
.parse(header
);
139 Assert
.assertTrue(!cookies
.isEmpty());
140 Assert
.assertTrue(header
.contains("; HttpOnly"));
141 Assert
.assertTrue("token".equals(cookies
.get(0).getValue()));
145 public void testHttpsCookie() throws IOException
, GeneralSecurityException
{
146 URL base
= new URL("https://" + NetUtils
.getHostPortString(server
147 .getConnectorAddress(1)));
148 HttpsURLConnection conn
= (HttpsURLConnection
) new URL(base
,
149 "/echo").openConnection();
150 conn
.setSSLSocketFactory(clientSslFactory
.createSSLSocketFactory());
152 String header
= conn
.getHeaderField("Set-Cookie");
153 Assert
.assertTrue(header
!= null);
155 List
<HttpCookie
> cookies
= HttpCookie
.parse(header
);
156 Assert
.assertTrue(!cookies
.isEmpty());
157 Assert
.assertTrue(header
.contains("; HttpOnly"));
158 Assert
.assertTrue(cookies
.get(0).getSecure());
159 Assert
.assertTrue("token".equals(cookies
.get(0).getValue()));
163 public void testHttpsCookieDefaultServlets() throws Exception
{
164 HttpsURLConnection conn
= null;
166 URL base
= new URL("https://" + NetUtils
.getHostPortString(server
167 .getConnectorAddress(1)) + "/");
169 for (String servlet
: new String
[] { "static", "stacks", "logLevel", "jmx", "logs" }) {
170 conn
= (HttpsURLConnection
) new URL(base
,
171 "/" + servlet
).openConnection();
172 conn
.setSSLSocketFactory(clientSslFactory
.createSSLSocketFactory());
174 String header
= conn
.getHeaderField("Set-Cookie");
175 Assert
.assertTrue(header
!= null);
176 List
<HttpCookie
> cookies
= HttpCookie
.parse(header
);
177 Assert
.assertTrue(!cookies
.isEmpty());
178 Assert
.assertTrue(header
.contains("; HttpOnly"));
179 Assert
.assertTrue(cookies
.get(0).getSecure());
180 Assert
.assertTrue("token".equals(cookies
.get(0).getValue()));
185 public static void cleanup() throws Exception
{
187 FileUtil
.fullyDelete(new File(BASEDIR
));
188 KeyStoreTestUtil
.cleanupSSLConfig(keystoresDir
, sslConfDir
);
189 clientSslFactory
.destroy();