1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.fail;
25
26 import java.net.HttpURLConnection;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.hbase.HBaseTestingUtility;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.testclassification.LargeTests;
37 import org.apache.hadoop.hbase.thrift.generated.Hbase;
38 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39 import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
40 import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
41 import org.apache.thrift.protocol.TBinaryProtocol;
42 import org.apache.thrift.protocol.TProtocol;
43 import org.apache.thrift.transport.THttpClient;
44 import org.apache.thrift.transport.TTransportException;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
49 import org.junit.Rule;
50 import org.junit.rules.ExpectedException;
51
52 import com.google.common.base.Joiner;
53
54
55
56
57
58 @Category(LargeTests.class)
59
60 public class TestThriftHttpServer {
61
62 private static final Log LOG =
63 LogFactory.getLog(TestThriftHttpServer.class);
64
65 private static final HBaseTestingUtility TEST_UTIL =
66 new HBaseTestingUtility();
67
68 private Thread httpServerThread;
69 private volatile Exception httpServerException;
70
71 private Exception clientSideException;
72
73 private ThriftServer thriftServer;
74 private int port;
75
76 @BeforeClass
77 public static void setUpBeforeClass() throws Exception {
78 TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.thrift.http", true);
79 TEST_UTIL.getConfiguration().setBoolean("hbase.table.sanity.checks", false);
80 TEST_UTIL.startMiniCluster();
81
82
83 EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge());
84 }
85
86 @AfterClass
87 public static void tearDownAfterClass() throws Exception {
88 TEST_UTIL.shutdownMiniCluster();
89 EnvironmentEdgeManager.reset();
90 }
91
92 @Test
93 public void testExceptionThrownWhenMisConfigured() throws Exception {
94 Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
95 conf.set("hbase.thrift.security.qop", "privacy");
96 conf.setBoolean("hbase.thrift.ssl.enabled", false);
97
98 ThriftServerRunner runner = null;
99 ExpectedException thrown = ExpectedException.none();
100 try {
101 thrown.expect(IllegalArgumentException.class);
102 thrown.expectMessage("Thrift HTTP Server's QoP is privacy, " +
103 "but hbase.thrift.ssl.enabled is false");
104 runner = new ThriftServerRunner(conf);
105 fail("Thrift HTTP Server starts up even with wrong security configurations.");
106 } catch (Exception e) {
107 }
108
109 assertNull(runner);
110 }
111
112 private void startHttpServerThread(final String[] args) {
113 LOG.info("Starting HBase Thrift server with HTTP server: " + Joiner.on(" ").join(args));
114
115 httpServerException = null;
116 httpServerThread = new Thread(new Runnable() {
117 @Override
118 public void run() {
119 try {
120 thriftServer.doMain(args);
121 } catch (Exception e) {
122 httpServerException = e;
123 }
124 }
125 });
126 httpServerThread.setName(ThriftServer.class.getSimpleName() +
127 "-httpServer");
128 httpServerThread.start();
129 }
130
131 @Rule
132 public ExpectedException exception = ExpectedException.none();
133
134 @Test(timeout=600000)
135 public void testRunThriftServerWithHeaderBufferLength() throws Exception {
136
137
138 try {
139 runThriftServer(1024 * 63);
140 } catch (TTransportException tex) {
141 assertFalse(tex.getMessage().equals("HTTP Response code: 413"));
142 }
143
144
145 exception.expect(TTransportException.class);
146 exception.expectMessage("HTTP Response code: 413");
147 runThriftServer(1024 * 64);
148 }
149
150 @Test(timeout=600000)
151 public void testRunThriftServer() throws Exception {
152 runThriftServer(0);
153 }
154
155 @Test
156 public void testThriftServerHttpTraceForbiddenWhenOptionsDisabled() throws Exception {
157
158
159 checkHttpMethods("TRACE", HttpURLConnection.HTTP_FORBIDDEN);
160 }
161
162 @Test
163 public void testThriftServerHttpTraceForbiddenWhenOptionsEnabled() throws Exception {
164
165
166 TEST_UTIL.getConfiguration().setBoolean(ThriftServerRunner.THRIFT_HTTP_ALLOW_OPTIONS_METHOD,
167 true);
168 checkHttpMethods("TRACE", HttpURLConnection.HTTP_FORBIDDEN);
169 }
170
171 @Test
172 public void testThriftServerHttpOptionsForbiddenWhenOptionsDisabled() throws Exception {
173
174
175 TEST_UTIL.getConfiguration().unset(ThriftServerRunner.THRIFT_HTTP_ALLOW_OPTIONS_METHOD);
176 checkHttpMethods("OPTIONS", HttpURLConnection.HTTP_FORBIDDEN);
177 }
178
179 @Test
180 public void testThriftServerHttpOptionsOkWhenOptionsEnabled() throws Exception {
181 TEST_UTIL.getConfiguration().setBoolean(ThriftServerRunner.THRIFT_HTTP_ALLOW_OPTIONS_METHOD,
182 true);
183 checkHttpMethods("OPTIONS", HttpURLConnection.HTTP_OK);
184 }
185
186 private void waitThriftServerStartup() throws Exception{
187
188 HBaseTestingUtility.waitForHostPort(HConstants.LOCALHOST, port);
189 }
190
191 private void runThriftServer(int customHeaderSize) throws Exception {
192 List<String> args = new ArrayList<String>();
193 port = HBaseTestingUtility.randomFreePort();
194 args.add("-" + ThriftServer.PORT_OPTION);
195 args.add(String.valueOf(port));
196 args.add("start");
197
198 thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
199 startHttpServerThread(args.toArray(new String[args.size()]));
200
201 waitThriftServerStartup();
202
203 try {
204 talkToThriftServer(customHeaderSize);
205 } catch (Exception ex) {
206 clientSideException = ex;
207 } finally {
208 stopHttpServerThread();
209 }
210
211 if (clientSideException != null) {
212 LOG.error("Thrift client threw an exception " + clientSideException);
213 if (clientSideException instanceof TTransportException) {
214 throw clientSideException;
215 } else {
216 throw new Exception(clientSideException);
217 }
218 }
219 }
220
221 private static volatile boolean tableCreated = false;
222
223 private void talkToThriftServer(int customHeaderSize) throws Exception {
224 THttpClient httpClient = new THttpClient(
225 "http://"+ HConstants.LOCALHOST + ":" + port);
226 httpClient.open();
227
228 if (customHeaderSize > 0) {
229 StringBuilder sb = new StringBuilder();
230 for (int i = 0; i < customHeaderSize; i++) {
231 sb.append("a");
232 }
233 httpClient.setCustomHeader("User-Agent", sb.toString());
234 }
235
236 try {
237 TProtocol prot;
238 prot = new TBinaryProtocol(httpClient);
239 Hbase.Client client = new Hbase.Client(prot);
240 if (!tableCreated){
241 TestThriftServer.createTestTables(client);
242 tableCreated = true;
243 }
244 TestThriftServer.checkTableList(client);
245 } finally {
246 httpClient.close();
247 }
248 }
249
250 private void stopHttpServerThread() throws Exception {
251 LOG.debug("Stopping Thrift HTTP server");
252 thriftServer.stop();
253 httpServerThread.join();
254 if (httpServerException != null) {
255 LOG.error("Command-line invocation of HBase Thrift server threw an " +
256 "exception", httpServerException);
257 throw new Exception(httpServerException);
258 }
259 }
260
261 private void checkHttpMethods(String httpRequestMethod,
262 int httpExpectedResponse) throws Exception {
263 port = HBaseTestingUtility.randomFreePort();
264 thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
265 try {
266 startHttpServerThread(new String[] { "-port", String.valueOf(port), "start" });
267 waitThriftServerStartup();
268 final URL url = new URL("http://"+ HConstants.LOCALHOST + ":" + port);
269 final HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
270 httpConn.setRequestMethod(httpRequestMethod);
271 assertEquals(httpExpectedResponse, httpConn.getResponseCode());
272 } finally {
273 stopHttpServerThread();
274 }
275 }
276 }