1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.http.log;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.net.BindException;
28 import java.net.SocketException;
29 import java.net.URI;
30 import java.security.PrivilegedExceptionAction;
31 import java.util.Properties;
32 import javax.net.ssl.SSLException;
33
34 import org.apache.commons.io.FileUtils;
35 import org.apache.hadoop.HadoopIllegalArgumentException;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.CommonConfigurationKeys;
38 import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
39 import org.apache.hadoop.fs.FileUtil;
40 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
41 import org.apache.hadoop.hbase.http.HttpConfig;
42 import org.apache.hadoop.hbase.http.HttpServer;
43 import org.apache.hadoop.hbase.http.log.LogLevel.CLI;
44 import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
45 import org.apache.hadoop.hbase.testclassification.MiscTests;
46 import org.apache.hadoop.hbase.testclassification.SmallTests;
47 import org.apache.hadoop.hdfs.DFSConfigKeys;
48 import org.apache.hadoop.minikdc.MiniKdc;
49 import org.apache.hadoop.net.NetUtils;
50 import org.apache.hadoop.security.UserGroupInformation;
51 import org.apache.hadoop.security.authorize.AccessControlList;
52 import org.apache.hadoop.security.ssl.SSLFactory;
53 import org.apache.hadoop.test.GenericTestUtils;
54 import org.apache.log4j.Level;
55 import org.apache.log4j.LogManager;
56 import org.apache.log4j.Logger;
57
58 import org.junit.AfterClass;
59 import org.junit.BeforeClass;
60 import org.junit.Test;
61 import org.junit.experimental.categories.Category;
62
63
64
65
66 @Category({MiscTests.class, SmallTests.class})
67 public class TestLogLevel {
68 private static File BASEDIR;
69 private static String keystoresDir;
70 private static String sslConfDir;
71 private static Configuration serverConf;
72 private static Configuration clientConf;
73 private static Configuration sslConf;
74 private static final String logName = TestLogLevel.class.getName();
75 private static final Logger log = LogManager.getLogger(logName);
76 private final static String PRINCIPAL = "loglevel.principal";
77 private final static String KEYTAB = "loglevel.keytab";
78
79 private static MiniKdc kdc;
80 private static HBaseCommonTestingUtility htu = new HBaseCommonTestingUtility();
81
82 private static final String LOCALHOST = "localhost";
83 private static final String clientPrincipal = "client/" + LOCALHOST;
84 private static String HTTP_PRINCIPAL = "HTTP/" + LOCALHOST;
85
86 private static final File KEYTAB_FILE = new File(
87 htu.getDataTestDir("keytab").toUri().getPath());
88
89 @BeforeClass
90 public static void setUp() throws Exception {
91 BASEDIR = new File(htu.getDataTestDir().toUri().getPath());
92
93 FileUtil.fullyDelete(BASEDIR);
94 if (!BASEDIR.mkdirs()) {
95 throw new Exception("unable to create the base directory for testing");
96 }
97 serverConf = new Configuration();
98 clientConf = new Configuration();
99
100 setupSSL(BASEDIR);
101
102 kdc = setupMiniKdc();
103
104 kdc.createPrincipal(KEYTAB_FILE, clientPrincipal, HTTP_PRINCIPAL);
105 }
106
107
108
109
110
111 static private MiniKdc setupMiniKdc() throws Exception {
112 Properties conf = MiniKdc.createConf();
113 conf.put(MiniKdc.DEBUG, true);
114 MiniKdc kdc = null;
115 File dir = null;
116
117
118 boolean bindException;
119 int numTries = 0;
120 do {
121 try {
122 bindException = false;
123 dir = new File(htu.getDataTestDir("kdc").toUri().getPath());
124 kdc = new MiniKdc(conf, dir);
125 kdc.start();
126 } catch (BindException e) {
127 FileUtils.deleteDirectory(dir);
128 numTries++;
129 if (numTries == 3) {
130 log.error("Failed setting up MiniKDC. Tried " + numTries + " times.");
131 throw e;
132 }
133 log.error("BindException encountered when setting up MiniKdc. Trying again.");
134 bindException = true;
135 }
136 } while (bindException);
137 return kdc;
138 }
139
140 static private void setupSSL(File base) throws Exception {
141 Configuration conf = new Configuration();
142 conf.set(DFSConfigKeys.DFS_HTTP_POLICY_KEY, HttpConfig.Policy.HTTPS_ONLY.name());
143 conf.set(DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY, "localhost:0");
144 conf.set(DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY, "localhost:0");
145
146 keystoresDir = base.getAbsolutePath();
147 sslConfDir = KeyStoreTestUtil.getClasspathDir(TestLogLevel.class);
148 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false);
149
150 sslConf = getSslConfig();
151 }
152
153
154
155
156
157
158 private static Configuration getSslConfig() {
159 Configuration sslConf = new Configuration(false);
160 String sslServerConfFile = "ssl-server.xml";
161 String sslClientConfFile = "ssl-client.xml";
162 sslConf.addResource(sslServerConfFile);
163 sslConf.addResource(sslClientConfFile);
164 sslConf.set(SSLFactory.SSL_SERVER_CONF_KEY, sslServerConfFile);
165 sslConf.set(SSLFactory.SSL_CLIENT_CONF_KEY, sslClientConfFile);
166 return sslConf;
167 }
168
169 @AfterClass
170 public static void tearDown() {
171 if (kdc != null) {
172 kdc.stop();
173 }
174
175 FileUtil.fullyDelete(BASEDIR);
176 }
177
178
179
180
181
182 @Test(timeout=120000)
183 public void testCommandOptions() throws Exception {
184 final String className = this.getClass().getName();
185
186 assertFalse(validateCommand(new String[] {"-foo" }));
187
188 assertFalse(validateCommand(new String[] {}));
189 assertFalse(validateCommand(new String[] {"-getlevel" }));
190 assertFalse(validateCommand(new String[] {"-setlevel" }));
191 assertFalse(validateCommand(new String[] {"-getlevel", "foo.bar:8080" }));
192
193
194 assertTrue(validateCommand(
195 new String[] {"-getlevel", "foo.bar:8080", className }));
196 assertTrue(validateCommand(
197 new String[] {"-setlevel", "foo.bar:8080", className, "DEBUG" }));
198 assertTrue(validateCommand(
199 new String[] {"-getlevel", "foo.bar:8080", className }));
200 assertTrue(validateCommand(
201 new String[] {"-setlevel", "foo.bar:8080", className, "DEBUG" }));
202
203
204 assertFalse(validateCommand(
205 new String[] {"-getlevel", "foo.bar:8080", className, "blah" }));
206 assertFalse(validateCommand(
207 new String[] {"-setlevel", "foo.bar:8080", className, "DEBUG", "blah" }));
208 assertFalse(validateCommand(
209 new String[] {
210 "-getlevel", "foo.bar:8080", className, "-setlevel", "foo.bar:8080", className }));
211 }
212
213
214
215
216
217
218
219 private boolean validateCommand(String[] args) {
220 CLI cli = new CLI(clientConf);
221 try {
222 cli.parseArguments(args);
223 } catch (HadoopIllegalArgumentException e) {
224 return false;
225 } catch (Exception e) {
226
227
228 return true;
229 }
230 return true;
231 }
232
233
234
235
236
237
238
239
240
241 private HttpServer createServer(String protocol, boolean isSpnego)
242 throws Exception {
243 HttpServer.Builder builder = new HttpServer.Builder()
244 .setName("..")
245 .addEndpoint(new URI(protocol + "://localhost:0"))
246 .setFindPort(true)
247 .setConf(serverConf);
248 if (isSpnego) {
249
250
251
252 builder.setSecurityEnabled(true)
253 .setUsernameConfKey(PRINCIPAL)
254 .setKeytabConfKey(KEYTAB)
255 .setACL(new AccessControlList("client"));
256 }
257
258
259 if (protocol.equals(LogLevel.PROTOCOL_HTTPS)) {
260 builder = builder.keyPassword(sslConf.get("ssl.server.keystore.keypassword"))
261 .keyStore(sslConf.get("ssl.server.keystore.location"),
262 sslConf.get("ssl.server.keystore.password"),
263 sslConf.get("ssl.server.keystore.type", "jks"))
264 .trustStore(sslConf.get("ssl.server.truststore.location"),
265 sslConf.get("ssl.server.truststore.password"),
266 sslConf.get("ssl.server.truststore.type", "jks"));
267 }
268
269 HttpServer server = builder.build();
270 server.start();
271 return server;
272 }
273
274 private void testDynamicLogLevel(final String bindProtocol, final String connectProtocol,
275 final boolean isSpnego) throws Exception {
276 testDynamicLogLevel(bindProtocol, connectProtocol, isSpnego, Level.DEBUG.toString());
277 }
278
279
280
281
282
283
284
285
286
287 private void testDynamicLogLevel(final String bindProtocol, final String connectProtocol,
288 final boolean isSpnego, final String newLevel) throws Exception {
289 if (!LogLevel.isValidProtocol(bindProtocol)) {
290 throw new Exception("Invalid server protocol " + bindProtocol);
291 }
292 if (!LogLevel.isValidProtocol(connectProtocol)) {
293 throw new Exception("Invalid client protocol " + connectProtocol);
294 }
295 Level oldLevel = log.getEffectiveLevel();
296 assertNotEquals("Get default Log Level which shouldn't be ERROR.",
297 Level.ERROR, oldLevel);
298
299
300 if (isSpnego) {
301 serverConf.set(PRINCIPAL, HTTP_PRINCIPAL);
302 serverConf.set(KEYTAB, KEYTAB_FILE.getAbsolutePath());
303 serverConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
304 serverConf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, true);
305 UserGroupInformation.setConfiguration(serverConf);
306 } else {
307 serverConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "simple");
308 serverConf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false);
309 UserGroupInformation.setConfiguration(serverConf);
310 }
311
312 final HttpServer server = createServer(bindProtocol, isSpnego);
313
314 final String authority = NetUtils.getHostPortString(server.getConnectorAddress(0));
315
316 String keytabFilePath = KEYTAB_FILE.getAbsolutePath();
317
318 UserGroupInformation clientUGI = UserGroupInformation.
319 loginUserFromKeytabAndReturnUGI(clientPrincipal, keytabFilePath);
320 try {
321 clientUGI.doAs(new PrivilegedExceptionAction<Void>() {
322 @Override public Void run() throws Exception {
323
324 getLevel(connectProtocol, authority);
325 setLevel(connectProtocol, authority, newLevel);
326 return null;
327 }
328 });
329 } finally {
330 server.stop();
331 }
332
333
334 GenericTestUtils.setLogLevel(log, oldLevel);
335 }
336
337
338
339
340
341
342
343
344
345 private void getLevel(String protocol, String authority) throws Exception {
346 String[] getLevelArgs = {"-getlevel", authority, logName, "-protocol", protocol};
347 CLI cli = new CLI(clientConf);
348 cli.run(getLevelArgs);
349 }
350
351
352
353
354
355
356
357
358
359 private void setLevel(String protocol, String authority, String newLevel)
360 throws Exception {
361 String[] setLevelArgs = {"-setlevel", authority, logName, newLevel, "-protocol", protocol};
362 CLI cli = new CLI(clientConf);
363 cli.run(setLevelArgs);
364
365 assertEquals("new level not equal to expected: ", newLevel.toUpperCase(),
366 log.getEffectiveLevel().toString());
367 }
368
369
370
371
372
373
374 @Test(timeout=60000)
375 public void testInfoLogLevel() throws Exception {
376 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTP, true, "INFO");
377 }
378
379
380
381
382
383
384 @Test(timeout=60000)
385 public void testErrorLogLevel() throws Exception {
386 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTP, true, "ERROR");
387 }
388
389
390
391
392
393
394
395 @Test(timeout=60000)
396 public void testLogLevelByHttp() throws Exception {
397 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTP, false);
398 try {
399 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTPS, false);
400 fail("A HTTPS Client should not have succeeded in connecting to a HTTP server");
401 } catch (SSLException e) {
402 GenericTestUtils.assertExceptionContains("Unrecognized SSL message", e);
403 }
404 }
405
406
407
408
409
410
411
412 @Test(timeout=60000)
413 public void testLogLevelByHttpWithSpnego() throws Exception {
414 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTP, true);
415 try {
416 testDynamicLogLevel(LogLevel.PROTOCOL_HTTP, LogLevel.PROTOCOL_HTTPS, true);
417 fail("A HTTPS Client should not have succeeded in connecting to a HTTP server");
418 } catch (SSLException e) {
419 GenericTestUtils.assertExceptionContains("Unrecognized SSL message", e);
420 }
421 }
422
423
424
425
426
427
428
429 @Test(timeout=60000)
430 public void testLogLevelByHttps() throws Exception {
431 testDynamicLogLevel(LogLevel.PROTOCOL_HTTPS, LogLevel.PROTOCOL_HTTPS, false);
432 try {
433 testDynamicLogLevel(LogLevel.PROTOCOL_HTTPS, LogLevel.PROTOCOL_HTTP, false);
434 fail("A HTTP Client should not have succeeded in connecting to a HTTPS server");
435 } catch (SocketException e) {
436 GenericTestUtils.assertExceptionContains("Unexpected end of file from server", e);
437 }
438 }
439
440
441
442
443
444
445
446 @Test(timeout=60000)
447 public void testLogLevelByHttpsWithSpnego() throws Exception {
448 testDynamicLogLevel(LogLevel.PROTOCOL_HTTPS, LogLevel.PROTOCOL_HTTPS, true);
449 try {
450 testDynamicLogLevel(LogLevel.PROTOCOL_HTTPS, LogLevel.PROTOCOL_HTTP, true);
451 fail("A HTTP Client should not have succeeded in connecting to a HTTPS server");
452 } catch (SocketException e) {
453 GenericTestUtils.assertExceptionContains("Unexpected end of file from server", e);
454 }
455 }
456 }