1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.http;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.io.PrintStream;
24 import java.net.BindException;
25 import java.net.InetSocketAddress;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import javax.servlet.Filter;
38 import javax.servlet.FilterChain;
39 import javax.servlet.FilterConfig;
40 import javax.servlet.ServletContext;
41 import javax.servlet.ServletException;
42 import javax.servlet.ServletRequest;
43 import javax.servlet.ServletResponse;
44 import javax.servlet.http.HttpServlet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletRequestWrapper;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.hadoop.HadoopIllegalArgumentException;
52 import org.apache.hadoop.hbase.classification.InterfaceAudience;
53 import org.apache.hadoop.hbase.classification.InterfaceStability;
54 import org.apache.hadoop.conf.Configuration;
55 import org.apache.hadoop.fs.CommonConfigurationKeys;
56 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
57 import org.apache.hadoop.hbase.http.conf.ConfServlet;
58 import org.apache.hadoop.hbase.http.jmx.JMXJsonServlet;
59 import org.apache.hadoop.hbase.http.log.LogLevel;
60 import org.apache.hadoop.hbase.util.Threads;
61 import org.apache.hadoop.metrics.MetricsServlet;
62 import org.apache.hadoop.security.SecurityUtil;
63 import org.apache.hadoop.security.UserGroupInformation;
64 import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
65 import org.apache.hadoop.security.authorize.AccessControlList;
66 import org.apache.hadoop.util.ReflectionUtils;
67 import org.apache.hadoop.util.Shell;
68 import org.mortbay.io.Buffer;
69 import org.mortbay.jetty.Connector;
70 import org.mortbay.jetty.Handler;
71 import org.mortbay.jetty.MimeTypes;
72 import org.mortbay.jetty.RequestLog;
73 import org.mortbay.jetty.Server;
74 import org.mortbay.jetty.handler.ContextHandler;
75 import org.mortbay.jetty.handler.ContextHandlerCollection;
76 import org.mortbay.jetty.handler.HandlerCollection;
77 import org.mortbay.jetty.handler.RequestLogHandler;
78 import org.mortbay.jetty.nio.SelectChannelConnector;
79 import org.mortbay.jetty.security.SslSocketConnector;
80 import org.mortbay.jetty.servlet.Context;
81 import org.mortbay.jetty.servlet.DefaultServlet;
82 import org.mortbay.jetty.servlet.FilterHolder;
83 import org.mortbay.jetty.servlet.FilterMapping;
84 import org.mortbay.jetty.servlet.ServletHandler;
85 import org.mortbay.jetty.servlet.ServletHolder;
86 import org.mortbay.jetty.webapp.WebAppContext;
87 import org.mortbay.thread.QueuedThreadPool;
88 import org.mortbay.util.MultiException;
89
90 import com.google.common.base.Preconditions;
91 import com.google.common.collect.Lists;
92 import com.sun.jersey.spi.container.servlet.ServletContainer;
93
94
95
96
97
98
99
100
101
102 @InterfaceAudience.Private
103 @InterfaceStability.Evolving
104 public class HttpServer implements FilterContainer {
105 public static final Log LOG = LogFactory.getLog(HttpServer.class);
106 private static final String EMPTY_STRING = "";
107
108 static final String FILTER_INITIALIZERS_PROPERTY
109 = "hbase.http.filter.initializers";
110 static final String HTTP_MAX_THREADS = "hbase.http.max.threads";
111
112 public static final String HTTP_UI_AUTHENTICATION = "hbase.security.authentication.ui";
113 static final String HTTP_AUTHENTICATION_PREFIX = "hbase.security.authentication.";
114 static final String HTTP_SPNEGO_AUTHENTICATION_PREFIX = HTTP_AUTHENTICATION_PREFIX
115 + "spnego.";
116 static final String HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX = "kerberos.principal";
117 public static final String HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY =
118 HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX;
119 static final String HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX = "kerberos.keytab";
120 public static final String HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY =
121 HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX;
122 static final String HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX = "kerberos.name.rules";
123 public static final String HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY =
124 HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX;
125 static final String HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX =
126 "signature.secret.file";
127 public static final String HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY =
128 HTTP_AUTHENTICATION_PREFIX + HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX;
129
130
131
132 public static final String CONF_CONTEXT_ATTRIBUTE = "hbase.conf";
133 public static final String ADMINS_ACL = "admins.acl";
134 public static final String BIND_ADDRESS = "bind.address";
135 public static final String SPNEGO_FILTER = "SpnegoFilter";
136 public static final String NO_CACHE_FILTER = "NoCacheFilter";
137 public static final String APP_DIR = "webapps";
138
139 private final AccessControlList adminsAcl;
140
141 protected final Server webServer;
142 protected String appDir;
143 protected String logDir;
144
145 private static class ListenerInfo {
146
147
148
149
150 private final boolean isManaged;
151 private final Connector listener;
152 private ListenerInfo(boolean isManaged, Connector listener) {
153 this.isManaged = isManaged;
154 this.listener = listener;
155 }
156 }
157
158 private final List<ListenerInfo> listeners = Lists.newArrayList();
159
160 protected final WebAppContext webAppContext;
161 protected final boolean findPort;
162 protected final Map<Context, Boolean> defaultContexts =
163 new HashMap<Context, Boolean>();
164 protected final List<String> filterNames = new ArrayList<String>();
165 static final String STATE_DESCRIPTION_ALIVE = " - alive";
166 static final String STATE_DESCRIPTION_NOT_LIVE = " - not live";
167
168
169
170
171 public static class Builder {
172 private ArrayList<URI> endpoints = Lists.newArrayList();
173 private Connector connector;
174 private Configuration conf;
175 private String[] pathSpecs;
176 private AccessControlList adminsAcl;
177 private boolean securityEnabled = false;
178 private String usernameConfKey;
179 private String keytabConfKey;
180 private boolean needsClientAuth;
181
182 private String hostName;
183 private String appDir = APP_DIR;
184 private String logDir;
185 private boolean findPort;
186
187 private String trustStore;
188 private String trustStorePassword;
189 private String trustStoreType;
190
191 private String keyStore;
192 private String keyStorePassword;
193 private String keyStoreType;
194
195
196 private String keyPassword;
197
198 private String kerberosNameRulesKey;
199 private String signatureSecretFileKey;
200
201 @Deprecated
202 private String name;
203 @Deprecated
204 private String bindAddress;
205 @Deprecated
206 private int port = -1;
207
208
209
210
211
212
213
214
215
216
217
218 public Builder addEndpoint(URI endpoint) {
219 endpoints.add(endpoint);
220 return this;
221 }
222
223
224
225
226
227
228 public Builder hostName(String hostName) {
229 this.hostName = hostName;
230 return this;
231 }
232
233 public Builder trustStore(String location, String password, String type) {
234 this.trustStore = location;
235 this.trustStorePassword = password;
236 this.trustStoreType = type;
237 return this;
238 }
239
240 public Builder keyStore(String location, String password, String type) {
241 this.keyStore = location;
242 this.keyStorePassword = password;
243 this.keyStoreType = type;
244 return this;
245 }
246
247 public Builder keyPassword(String password) {
248 this.keyPassword = password;
249 return this;
250 }
251
252
253
254
255
256 public Builder needsClientAuth(boolean value) {
257 this.needsClientAuth = value;
258 return this;
259 }
260
261
262
263
264 @Deprecated
265 public Builder setName(String name){
266 this.name = name;
267 return this;
268 }
269
270
271
272
273 @Deprecated
274 public Builder setBindAddress(String bindAddress){
275 this.bindAddress = bindAddress;
276 return this;
277 }
278
279
280
281
282 @Deprecated
283 public Builder setPort(int port) {
284 this.port = port;
285 return this;
286 }
287
288 public Builder setFindPort(boolean findPort) {
289 this.findPort = findPort;
290 return this;
291 }
292
293 public Builder setConf(Configuration conf) {
294 this.conf = conf;
295 return this;
296 }
297
298 public Builder setConnector(Connector connector) {
299 this.connector = connector;
300 return this;
301 }
302
303 public Builder setPathSpec(String[] pathSpec) {
304 this.pathSpecs = pathSpec;
305 return this;
306 }
307
308 public Builder setACL(AccessControlList acl) {
309 this.adminsAcl = acl;
310 return this;
311 }
312
313 public Builder setSecurityEnabled(boolean securityEnabled) {
314 this.securityEnabled = securityEnabled;
315 return this;
316 }
317
318 public Builder setUsernameConfKey(String usernameConfKey) {
319 this.usernameConfKey = usernameConfKey;
320 return this;
321 }
322
323 public Builder setKeytabConfKey(String keytabConfKey) {
324 this.keytabConfKey = keytabConfKey;
325 return this;
326 }
327
328 public Builder setKerberosNameRulesKey(String kerberosNameRulesKey) {
329 this.kerberosNameRulesKey = kerberosNameRulesKey;
330 return this;
331 }
332
333 public Builder setSignatureSecretFileKey(String signatureSecretFileKey) {
334 this.signatureSecretFileKey = signatureSecretFileKey;
335 return this;
336 }
337
338 public Builder setAppDir(String appDir) {
339 this.appDir = appDir;
340 return this;
341 }
342
343 public Builder setLogDir(String logDir) {
344 this.logDir = logDir;
345 return this;
346 }
347
348 public HttpServer build() throws IOException {
349
350
351 if (this.name == null) {
352 throw new HadoopIllegalArgumentException("name is not set");
353 }
354
355
356 if (bindAddress != null && port != -1) {
357 try {
358 endpoints.add(0, new URI("http", "", bindAddress, port, "", "", ""));
359 } catch (URISyntaxException e) {
360 throw new HadoopIllegalArgumentException("Invalid endpoint: "+ e);
361 }
362 }
363
364 if (endpoints.size() == 0 && connector == null) {
365 throw new HadoopIllegalArgumentException("No endpoints specified");
366 }
367
368 if (hostName == null) {
369 hostName = endpoints.size() == 0 ? connector.getHost() : endpoints.get(
370 0).getHost();
371 }
372
373 if (this.conf == null) {
374 conf = new Configuration();
375 }
376
377 HttpServer server = new HttpServer(this);
378
379 if (this.securityEnabled) {
380 server.initSpnego(conf, hostName, usernameConfKey, keytabConfKey, kerberosNameRulesKey,
381 signatureSecretFileKey);
382 }
383
384 if (connector != null) {
385 server.addUnmanagedListener(connector);
386 }
387
388 for (URI ep : endpoints) {
389 Connector listener = null;
390 String scheme = ep.getScheme();
391 if ("http".equals(scheme)) {
392 listener = HttpServer.createDefaultChannelConnector();
393 } else if ("https".equals(scheme)) {
394 SslSocketConnector c = new SslSocketConnectorSecure();
395 c.setNeedClientAuth(needsClientAuth);
396 c.setKeyPassword(keyPassword);
397
398 if (keyStore != null) {
399 c.setKeystore(keyStore);
400 c.setKeystoreType(keyStoreType);
401 c.setPassword(keyStorePassword);
402 }
403
404 if (trustStore != null) {
405 c.setTruststore(trustStore);
406 c.setTruststoreType(trustStoreType);
407 c.setTrustPassword(trustStorePassword);
408 }
409 listener = c;
410
411 } else {
412 throw new HadoopIllegalArgumentException(
413 "unknown scheme for endpoint:" + ep);
414 }
415 listener.setHost(ep.getHost());
416 listener.setPort(ep.getPort() == -1 ? 0 : ep.getPort());
417 server.addManagedListener(listener);
418 }
419
420 server.loadListeners();
421 return server;
422
423 }
424
425 }
426
427
428 @Deprecated
429 public HttpServer(String name, String bindAddress, int port, boolean findPort
430 ) throws IOException {
431 this(name, bindAddress, port, findPort, new Configuration());
432 }
433
434 @Deprecated
435 public HttpServer(String name, String bindAddress, int port,
436 boolean findPort, Configuration conf, Connector connector) throws IOException {
437 this(name, bindAddress, port, findPort, conf, null, connector, null);
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 @Deprecated
455 public HttpServer(String name, String bindAddress, int port,
456 boolean findPort, Configuration conf, String[] pathSpecs) throws IOException {
457 this(name, bindAddress, port, findPort, conf, null, null, pathSpecs);
458 }
459
460
461
462
463
464
465
466
467
468
469 @Deprecated
470 public HttpServer(String name, String bindAddress, int port,
471 boolean findPort, Configuration conf) throws IOException {
472 this(name, bindAddress, port, findPort, conf, null, null, null);
473 }
474
475 @Deprecated
476 public HttpServer(String name, String bindAddress, int port,
477 boolean findPort, Configuration conf, AccessControlList adminsAcl)
478 throws IOException {
479 this(name, bindAddress, port, findPort, conf, adminsAcl, null, null);
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494 @Deprecated
495 public HttpServer(String name, String bindAddress, int port,
496 boolean findPort, Configuration conf, AccessControlList adminsAcl,
497 Connector connector) throws IOException {
498 this(name, bindAddress, port, findPort, conf, adminsAcl, connector, null);
499 }
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 @Deprecated
516 public HttpServer(String name, String bindAddress, int port,
517 boolean findPort, Configuration conf, AccessControlList adminsAcl,
518 Connector connector, String[] pathSpecs) throws IOException {
519 this(new Builder().setName(name)
520 .addEndpoint(URI.create("http://" + bindAddress + ":" + port))
521 .setFindPort(findPort).setConf(conf).setACL(adminsAcl)
522 .setConnector(connector).setPathSpec(pathSpecs));
523 }
524
525 private HttpServer(final Builder b) throws IOException {
526 this.appDir = b.appDir;
527 this.logDir = b.logDir;
528 final String appDir = getWebAppsPath(b.name);
529 this.webServer = new Server();
530 this.adminsAcl = b.adminsAcl;
531 this.webAppContext = createWebAppContext(b.name, b.conf, adminsAcl, appDir);
532 this.findPort = b.findPort;
533 initializeWebServer(b.name, b.hostName, b.conf, b.pathSpecs);
534 }
535
536 private void initializeWebServer(String name, String hostName,
537 Configuration conf, String[] pathSpecs)
538 throws FileNotFoundException, IOException {
539
540 Preconditions.checkNotNull(webAppContext);
541
542 int maxThreads = conf.getInt(HTTP_MAX_THREADS, -1);
543
544
545 QueuedThreadPool threadPool = maxThreads == -1 ? new QueuedThreadPool()
546 : new QueuedThreadPool(maxThreads);
547 threadPool.setDaemon(true);
548 webServer.setThreadPool(threadPool);
549
550 ContextHandlerCollection contexts = new ContextHandlerCollection();
551 RequestLog requestLog = HttpRequestLog.getRequestLog(name);
552
553 if (requestLog != null) {
554 RequestLogHandler requestLogHandler = new RequestLogHandler();
555 requestLogHandler.setRequestLog(requestLog);
556 HandlerCollection handlers = new HandlerCollection();
557 handlers.setHandlers(new Handler[] { requestLogHandler, contexts });
558 webServer.setHandler(handlers);
559 } else {
560 webServer.setHandler(contexts);
561 }
562
563 final String appDir = getWebAppsPath(name);
564
565 webServer.addHandler(webAppContext);
566
567 addDefaultApps(contexts, appDir, conf);
568
569 addGlobalFilter("safety", QuotingInputFilter.class.getName(), null);
570 Map<String, String> params = new HashMap<String, String>();
571 params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode", "DENY"));
572 addGlobalFilter("clickjackingprevention",
573 ClickjackingPreventionFilter.class.getName(), params);
574 final FilterInitializer[] initializers = getFilterInitializers(conf);
575 if (initializers != null) {
576 conf = new Configuration(conf);
577 conf.set(BIND_ADDRESS, hostName);
578 for (FilterInitializer c : initializers) {
579 c.initFilter(this, conf);
580 }
581 }
582
583 addDefaultServlets();
584
585 if (pathSpecs != null) {
586 for (String path : pathSpecs) {
587 LOG.info("adding path spec: " + path);
588 addFilterPathMapping(path, webAppContext);
589 }
590 }
591 }
592
593 private void addUnmanagedListener(Connector connector) {
594 listeners.add(new ListenerInfo(false, connector));
595 }
596
597 private void addManagedListener(Connector connector) {
598 listeners.add(new ListenerInfo(true, connector));
599 }
600
601 private static WebAppContext createWebAppContext(String name,
602 Configuration conf, AccessControlList adminsAcl, final String appDir) {
603 WebAppContext ctx = new WebAppContext();
604 ctx.setDisplayName(name);
605 ctx.setContextPath("/");
606 ctx.setWar(appDir + "/" + name);
607 ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, conf);
608 ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
609 addNoCacheFilter(ctx);
610 return ctx;
611 }
612
613 private static void addNoCacheFilter(WebAppContext ctxt) {
614 defineFilter(ctxt, NO_CACHE_FILTER, NoCacheFilter.class.getName(),
615 Collections.<String, String> emptyMap(), new String[] { "/*" });
616 }
617
618
619
620
621
622
623 public Connector createBaseListener(Configuration conf) throws IOException {
624 return HttpServer.createDefaultChannelConnector();
625 }
626
627 @InterfaceAudience.Private
628 public static Connector createDefaultChannelConnector() {
629 SelectChannelConnector ret = new SelectChannelConnector();
630 ret.setLowResourceMaxIdleTime(10000);
631 ret.setAcceptQueueSize(128);
632 ret.setResolveNames(false);
633 ret.setUseDirectBuffers(false);
634 if(Shell.WINDOWS) {
635
636
637
638
639 ret.setReuseAddress(false);
640 }
641 ret.setHeaderBufferSize(1024*64);
642 return ret;
643 }
644
645
646 private static FilterInitializer[] getFilterInitializers(Configuration conf) {
647 if (conf == null) {
648 return null;
649 }
650
651 Class<?>[] classes = conf.getClasses(FILTER_INITIALIZERS_PROPERTY);
652 if (classes == null) {
653 return null;
654 }
655
656 FilterInitializer[] initializers = new FilterInitializer[classes.length];
657 for(int i = 0; i < classes.length; i++) {
658 initializers[i] = (FilterInitializer)ReflectionUtils.newInstance(
659 classes[i], conf);
660 }
661 return initializers;
662 }
663
664
665
666
667
668
669 protected void addDefaultApps(ContextHandlerCollection parent,
670 final String appDir, Configuration conf) throws IOException {
671
672 String logDir = this.logDir;
673 if (logDir == null) {
674 logDir = System.getProperty("hadoop.log.dir");
675 }
676 if (logDir != null) {
677 Context logContext = new Context(parent, "/logs");
678 logContext.setResourceBase(logDir);
679 logContext.addServlet(AdminAuthorizedServlet.class, "/*");
680 if (conf.getBoolean(
681 ServerConfigurationKeys.HBASE_JETTY_LOGS_SERVE_ALIASES,
682 ServerConfigurationKeys.DEFAULT_HBASE_JETTY_LOGS_SERVE_ALIASES)) {
683 @SuppressWarnings("unchecked")
684 Map<String, String> params = logContext.getInitParams();
685 params.put(
686 "org.mortbay.jetty.servlet.Default.aliases", "true");
687 }
688 logContext.setDisplayName("logs");
689 setContextAttributes(logContext, conf);
690 addNoCacheFilter(webAppContext);
691 defaultContexts.put(logContext, true);
692 }
693
694 Context staticContext = new Context(parent, "/static");
695 staticContext.setResourceBase(appDir + "/static");
696 staticContext.addServlet(DefaultServlet.class, "/*");
697 staticContext.setDisplayName("static");
698 setContextAttributes(staticContext, conf);
699 defaultContexts.put(staticContext, true);
700 }
701
702 private void setContextAttributes(Context context, Configuration conf) {
703 context.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, conf);
704 context.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
705 }
706
707
708
709
710 protected void addDefaultServlets() {
711
712 addServlet("stacks", "/stacks", StackServlet.class);
713 addServlet("logLevel", "/logLevel", LogLevel.Servlet.class);
714 addServlet("metrics", "/metrics", MetricsServlet.class);
715 addServlet("jmx", "/jmx", JMXJsonServlet.class);
716 addServlet("conf", "/conf", ConfServlet.class);
717 }
718
719 public void addContext(Context ctxt, boolean isFiltered)
720 throws IOException {
721 webServer.addHandler(ctxt);
722 addNoCacheFilter(webAppContext);
723 defaultContexts.put(ctxt, isFiltered);
724 }
725
726
727
728
729
730
731
732
733 protected void addContext(String pathSpec, String dir, boolean isFiltered) throws IOException {
734 if (0 == webServer.getHandlers().length) {
735 throw new RuntimeException("Couldn't find handler");
736 }
737 WebAppContext webAppCtx = new WebAppContext();
738 webAppCtx.setContextPath(pathSpec);
739 webAppCtx.setWar(dir);
740 addContext(webAppCtx, true);
741 }
742
743
744
745
746
747
748
749 public void setAttribute(String name, Object value) {
750 webAppContext.setAttribute(name, value);
751 }
752
753
754
755
756
757
758 public void addJerseyResourcePackage(final String packageName,
759 final String pathSpec) {
760 LOG.info("addJerseyResourcePackage: packageName=" + packageName
761 + ", pathSpec=" + pathSpec);
762 final ServletHolder sh = new ServletHolder(ServletContainer.class);
763 sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
764 "com.sun.jersey.api.core.PackagesResourceConfig");
765 sh.setInitParameter("com.sun.jersey.config.property.packages", packageName);
766 webAppContext.addServlet(sh, pathSpec);
767 }
768
769
770
771
772
773
774
775 public void addServlet(String name, String pathSpec,
776 Class<? extends HttpServlet> clazz) {
777 addInternalServlet(name, pathSpec, clazz, false);
778 addFilterPathMapping(pathSpec, webAppContext);
779 }
780
781
782
783
784
785
786
787
788
789
790
791 public void addInternalServlet(String name, String pathSpec,
792 Class<? extends HttpServlet> clazz) {
793 addInternalServlet(name, pathSpec, clazz, false);
794 }
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809 public void addInternalServlet(String name, String pathSpec,
810 Class<? extends HttpServlet> clazz, boolean requireAuth) {
811 ServletHolder holder = new ServletHolder(clazz);
812 if (name != null) {
813 holder.setName(name);
814 }
815 webAppContext.addServlet(holder, pathSpec);
816
817 if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
818 LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
819 ServletHandler handler = webAppContext.getServletHandler();
820 FilterMapping fmap = new FilterMapping();
821 fmap.setPathSpec(pathSpec);
822 fmap.setFilterName(SPNEGO_FILTER);
823 fmap.setDispatches(Handler.ALL);
824 handler.addFilterMapping(fmap);
825 }
826 }
827
828 @Override
829 public void addFilter(String name, String classname,
830 Map<String, String> parameters) {
831
832 final String[] USER_FACING_URLS = { "*.html", "*.jsp" };
833 defineFilter(webAppContext, name, classname, parameters, USER_FACING_URLS);
834 LOG.info("Added filter " + name + " (class=" + classname
835 + ") to context " + webAppContext.getDisplayName());
836 final String[] ALL_URLS = { "/*" };
837 for (Map.Entry<Context, Boolean> e : defaultContexts.entrySet()) {
838 if (e.getValue()) {
839 Context ctx = e.getKey();
840 defineFilter(ctx, name, classname, parameters, ALL_URLS);
841 LOG.info("Added filter " + name + " (class=" + classname
842 + ") to context " + ctx.getDisplayName());
843 }
844 }
845 filterNames.add(name);
846 }
847
848 @Override
849 public void addGlobalFilter(String name, String classname,
850 Map<String, String> parameters) {
851 final String[] ALL_URLS = { "/*" };
852 defineFilter(webAppContext, name, classname, parameters, ALL_URLS);
853 for (Context ctx : defaultContexts.keySet()) {
854 defineFilter(ctx, name, classname, parameters, ALL_URLS);
855 }
856 LOG.info("Added global filter '" + name + "' (class=" + classname + ")");
857 }
858
859
860
861
862 public static void defineFilter(Context ctx, String name,
863 String classname, Map<String,String> parameters, String[] urls) {
864
865 FilterHolder holder = new FilterHolder();
866 holder.setName(name);
867 holder.setClassName(classname);
868 holder.setInitParameters(parameters);
869 FilterMapping fmap = new FilterMapping();
870 fmap.setPathSpecs(urls);
871 fmap.setDispatches(Handler.ALL);
872 fmap.setFilterName(name);
873 ServletHandler handler = ctx.getServletHandler();
874 handler.addFilter(holder, fmap);
875 }
876
877
878
879
880
881
882 protected void addFilterPathMapping(String pathSpec,
883 Context webAppCtx) {
884 ServletHandler handler = webAppCtx.getServletHandler();
885 for(String name : filterNames) {
886 FilterMapping fmap = new FilterMapping();
887 fmap.setPathSpec(pathSpec);
888 fmap.setFilterName(name);
889 fmap.setDispatches(Handler.ALL);
890 handler.addFilterMapping(fmap);
891 }
892 }
893
894
895
896
897
898
899 public Object getAttribute(String name) {
900 return webAppContext.getAttribute(name);
901 }
902
903 public WebAppContext getWebAppContext(){
904 return this.webAppContext;
905 }
906
907 public String getWebAppsPath(String appName) throws FileNotFoundException {
908 return getWebAppsPath(this.appDir, appName);
909 }
910
911
912
913
914
915
916
917 protected String getWebAppsPath(String webapps, String appName) throws FileNotFoundException {
918 URL url = getClass().getClassLoader().getResource(webapps + "/" + appName);
919 if (url == null)
920 throw new FileNotFoundException(webapps + "/" + appName
921 + " not found in CLASSPATH");
922 String urlString = url.toString();
923 return urlString.substring(0, urlString.lastIndexOf('/'));
924 }
925
926
927
928
929
930 @Deprecated
931 public int getPort() {
932 return webServer.getConnectors()[0].getLocalPort();
933 }
934
935
936
937
938
939
940
941 public InetSocketAddress getConnectorAddress(int index) {
942 Preconditions.checkArgument(index >= 0);
943 if (index > webServer.getConnectors().length)
944 return null;
945
946 Connector c = webServer.getConnectors()[index];
947 if (c.getLocalPort() == -1) {
948
949 return null;
950 }
951
952 return new InetSocketAddress(c.getHost(), c.getLocalPort());
953 }
954
955
956
957
958 public void setThreads(int min, int max) {
959 QueuedThreadPool pool = (QueuedThreadPool) webServer.getThreadPool();
960 pool.setMinThreads(min);
961 pool.setMaxThreads(max);
962 }
963
964 private void initSpnego(Configuration conf, String hostName,
965 String usernameConfKey, String keytabConfKey, String kerberosNameRuleKey,
966 String signatureSecretKeyFileKey) throws IOException {
967 Map<String, String> params = new HashMap<String, String>();
968 String principalInConf = getOrEmptyString(conf, usernameConfKey);
969 if (!principalInConf.isEmpty()) {
970 params.put(HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX, SecurityUtil.getServerPrincipal(
971 principalInConf, hostName));
972 }
973 String httpKeytab = getOrEmptyString(conf, keytabConfKey);
974 if (!httpKeytab.isEmpty()) {
975 params.put(HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX, httpKeytab);
976 }
977 String kerberosNameRule = getOrEmptyString(conf, kerberosNameRuleKey);
978 if (!kerberosNameRule.isEmpty()) {
979 params.put(HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX, kerberosNameRule);
980 }
981 String signatureSecretKeyFile = getOrEmptyString(conf, signatureSecretKeyFileKey);
982 if (!signatureSecretKeyFile.isEmpty()) {
983 params.put(HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX,
984 signatureSecretKeyFile);
985 }
986 params.put(AuthenticationFilter.AUTH_TYPE, "kerberos");
987
988
989 if (isMissing(params.get(HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX)) ||
990 isMissing(params.get(HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX))) {
991 throw new IllegalArgumentException(usernameConfKey + " and "
992 + keytabConfKey + " are both required in the configuration "
993 + "to enable SPNEGO/Kerberos authentication for the Web UI");
994 }
995
996 addGlobalFilter(SPNEGO_FILTER, AuthenticationFilter.class.getName(), params);
997 }
998
999
1000
1001
1002 private boolean isMissing(String value) {
1003 if (null == value) {
1004 return true;
1005 }
1006 return value.trim().isEmpty();
1007 }
1008
1009
1010
1011
1012
1013 private String getOrEmptyString(Configuration conf, String key) {
1014 if (null == key) {
1015 return EMPTY_STRING;
1016 }
1017 final String value = conf.get(key.trim());
1018 return null == value ? EMPTY_STRING : value;
1019 }
1020
1021
1022
1023
1024 public void start() throws IOException {
1025 try {
1026 try {
1027 openListeners();
1028 webServer.start();
1029 } catch (IOException ex) {
1030 LOG.info("HttpServer.start() threw a non Bind IOException", ex);
1031 throw ex;
1032 } catch (MultiException ex) {
1033 LOG.info("HttpServer.start() threw a MultiException", ex);
1034 throw ex;
1035 }
1036
1037 Handler[] handlers = webServer.getHandlers();
1038 for (int i = 0; i < handlers.length; i++) {
1039 if (handlers[i].isFailed()) {
1040 throw new IOException(
1041 "Problem in starting http server. Server handlers failed");
1042 }
1043 }
1044
1045 Throwable unavailableException = webAppContext.getUnavailableException();
1046 if (unavailableException != null) {
1047
1048
1049 webServer.stop();
1050 throw new IOException("Unable to initialize WebAppContext",
1051 unavailableException);
1052 }
1053 } catch (IOException e) {
1054 throw e;
1055 } catch (InterruptedException e) {
1056 throw (IOException) new InterruptedIOException(
1057 "Interrupted while starting HTTP server").initCause(e);
1058 } catch (Exception e) {
1059 throw new IOException("Problem starting http server", e);
1060 }
1061 }
1062
1063 private void loadListeners() {
1064 for (ListenerInfo li : listeners) {
1065 webServer.addConnector(li.listener);
1066 }
1067 }
1068
1069
1070
1071
1072
1073 void openListeners() throws Exception {
1074 for (ListenerInfo li : listeners) {
1075 Connector listener = li.listener;
1076 if (!li.isManaged || li.listener.getLocalPort() != -1) {
1077
1078 continue;
1079 }
1080 int port = listener.getPort();
1081 while (true) {
1082
1083
1084 try {
1085 listener.close();
1086 listener.open();
1087 LOG.info("Jetty bound to port " + listener.getLocalPort());
1088 break;
1089 } catch (BindException ex) {
1090 if (port == 0 || !findPort) {
1091 BindException be = new BindException("Port in use: "
1092 + listener.getHost() + ":" + listener.getPort());
1093 be.initCause(ex);
1094 throw be;
1095 }
1096 }
1097
1098 listener.setPort(++port);
1099 Thread.sleep(100);
1100 }
1101 }
1102 }
1103
1104
1105
1106
1107 public void stop() throws Exception {
1108 MultiException exception = null;
1109 for (ListenerInfo li : listeners) {
1110 if (!li.isManaged) {
1111 continue;
1112 }
1113
1114 try {
1115 li.listener.close();
1116 } catch (Exception e) {
1117 LOG.error(
1118 "Error while stopping listener for webapp"
1119 + webAppContext.getDisplayName(), e);
1120 exception = addMultiException(exception, e);
1121 }
1122 }
1123
1124 try {
1125
1126 webAppContext.clearAttributes();
1127 webAppContext.stop();
1128 } catch (Exception e) {
1129 LOG.error("Error while stopping web app context for webapp "
1130 + webAppContext.getDisplayName(), e);
1131 exception = addMultiException(exception, e);
1132 }
1133
1134 try {
1135 webServer.stop();
1136 } catch (Exception e) {
1137 LOG.error("Error while stopping web server for webapp "
1138 + webAppContext.getDisplayName(), e);
1139 exception = addMultiException(exception, e);
1140 }
1141
1142 if (exception != null) {
1143 exception.ifExceptionThrow();
1144 }
1145
1146 }
1147
1148 private MultiException addMultiException(MultiException exception, Exception e) {
1149 if(exception == null){
1150 exception = new MultiException();
1151 }
1152 exception.add(e);
1153 return exception;
1154 }
1155
1156 public void join() throws InterruptedException {
1157 webServer.join();
1158 }
1159
1160
1161
1162
1163
1164 public boolean isAlive() {
1165 return webServer != null && webServer.isStarted();
1166 }
1167
1168
1169
1170
1171
1172 @Override
1173 public String toString() {
1174 if (listeners.size() == 0) {
1175 return "Inactive HttpServer";
1176 } else {
1177 StringBuilder sb = new StringBuilder("HttpServer (")
1178 .append(isAlive() ? STATE_DESCRIPTION_ALIVE : STATE_DESCRIPTION_NOT_LIVE).append("), listening at:");
1179 for (ListenerInfo li : listeners) {
1180 Connector l = li.listener;
1181 sb.append(l.getHost()).append(":").append(l.getPort()).append("/,");
1182 }
1183 return sb.toString();
1184 }
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 public static boolean isInstrumentationAccessAllowed(
1203 ServletContext servletContext, HttpServletRequest request,
1204 HttpServletResponse response) throws IOException {
1205 Configuration conf =
1206 (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE);
1207
1208 boolean access = true;
1209 boolean adminAccess = conf.getBoolean(
1210 CommonConfigurationKeys.HADOOP_SECURITY_INSTRUMENTATION_REQUIRES_ADMIN,
1211 false);
1212 if (adminAccess) {
1213 access = hasAdministratorAccess(servletContext, request, response);
1214 }
1215 return access;
1216 }
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228 public static boolean hasAdministratorAccess(
1229 ServletContext servletContext, HttpServletRequest request,
1230 HttpServletResponse response) throws IOException {
1231 Configuration conf =
1232 (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE);
1233
1234 if (!conf.getBoolean(
1235 CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) {
1236 return true;
1237 }
1238
1239 String remoteUser = request.getRemoteUser();
1240 if (remoteUser == null) {
1241 response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
1242 "Unauthenticated users are not " +
1243 "authorized to access this page.");
1244 return false;
1245 }
1246
1247 if (servletContext.getAttribute(ADMINS_ACL) != null &&
1248 !userHasAdministratorAccess(servletContext, remoteUser)) {
1249 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User "
1250 + remoteUser + " is unauthorized to access this page.");
1251 return false;
1252 }
1253
1254 return true;
1255 }
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 public static boolean userHasAdministratorAccess(ServletContext servletContext,
1267 String remoteUser) {
1268 AccessControlList adminsAcl = (AccessControlList) servletContext
1269 .getAttribute(ADMINS_ACL);
1270 UserGroupInformation remoteUserUGI =
1271 UserGroupInformation.createRemoteUser(remoteUser);
1272 return adminsAcl != null && adminsAcl.isUserAllowed(remoteUserUGI);
1273 }
1274
1275
1276
1277
1278
1279
1280
1281 public static class StackServlet extends HttpServlet {
1282 private static final long serialVersionUID = -6284183679759467039L;
1283
1284 @Override
1285 public void doGet(HttpServletRequest request, HttpServletResponse response)
1286 throws ServletException, IOException {
1287 if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
1288 request, response)) {
1289 return;
1290 }
1291 response.setContentType("text/plain; charset=UTF-8");
1292 try (PrintStream out = new PrintStream(
1293 response.getOutputStream(), false, "UTF-8")) {
1294 Threads.printThreadInfo(out, "");
1295 out.flush();
1296 }
1297 ReflectionUtils.logThreadInfo(LOG, "jsp requested", 1);
1298 }
1299 }
1300
1301
1302
1303
1304
1305
1306 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
1307 public static class QuotingInputFilter implements Filter {
1308 private FilterConfig config;
1309
1310 public static class RequestQuoter extends HttpServletRequestWrapper {
1311 private final HttpServletRequest rawRequest;
1312 public RequestQuoter(HttpServletRequest rawRequest) {
1313 super(rawRequest);
1314 this.rawRequest = rawRequest;
1315 }
1316
1317
1318
1319
1320 @SuppressWarnings("unchecked")
1321 @Override
1322 public Enumeration<String> getParameterNames() {
1323 return new Enumeration<String>() {
1324 private Enumeration<String> rawIterator =
1325 rawRequest.getParameterNames();
1326 @Override
1327 public boolean hasMoreElements() {
1328 return rawIterator.hasMoreElements();
1329 }
1330
1331 @Override
1332 public String nextElement() {
1333 return HtmlQuoting.quoteHtmlChars(rawIterator.nextElement());
1334 }
1335 };
1336 }
1337
1338
1339
1340
1341 @Override
1342 public String getParameter(String name) {
1343 return HtmlQuoting.quoteHtmlChars(rawRequest.getParameter
1344 (HtmlQuoting.unquoteHtmlChars(name)));
1345 }
1346
1347 @Override
1348 public String[] getParameterValues(String name) {
1349 String unquoteName = HtmlQuoting.unquoteHtmlChars(name);
1350 String[] unquoteValue = rawRequest.getParameterValues(unquoteName);
1351 if (unquoteValue == null) {
1352 return null;
1353 }
1354 String[] result = new String[unquoteValue.length];
1355 for(int i=0; i < result.length; ++i) {
1356 result[i] = HtmlQuoting.quoteHtmlChars(unquoteValue[i]);
1357 }
1358 return result;
1359 }
1360
1361 @SuppressWarnings("unchecked")
1362 @Override
1363 public Map<String, String[]> getParameterMap() {
1364 Map<String, String[]> result = new HashMap<String,String[]>();
1365 Map<String, String[]> raw = rawRequest.getParameterMap();
1366 for (Map.Entry<String,String[]> item: raw.entrySet()) {
1367 String[] rawValue = item.getValue();
1368 String[] cookedValue = new String[rawValue.length];
1369 for(int i=0; i< rawValue.length; ++i) {
1370 cookedValue[i] = HtmlQuoting.quoteHtmlChars(rawValue[i]);
1371 }
1372 result.put(HtmlQuoting.quoteHtmlChars(item.getKey()), cookedValue);
1373 }
1374 return result;
1375 }
1376
1377
1378
1379
1380
1381 @Override
1382 public StringBuffer getRequestURL(){
1383 String url = rawRequest.getRequestURL().toString();
1384 return new StringBuffer(HtmlQuoting.quoteHtmlChars(url));
1385 }
1386
1387
1388
1389
1390
1391 @Override
1392 public String getServerName() {
1393 return HtmlQuoting.quoteHtmlChars(rawRequest.getServerName());
1394 }
1395 }
1396
1397 @Override
1398 public void init(FilterConfig config) throws ServletException {
1399 this.config = config;
1400 }
1401
1402 @Override
1403 public void destroy() {
1404 }
1405
1406 @Override
1407 public void doFilter(ServletRequest request,
1408 ServletResponse response,
1409 FilterChain chain
1410 ) throws IOException, ServletException {
1411 HttpServletRequestWrapper quoted =
1412 new RequestQuoter((HttpServletRequest) request);
1413 HttpServletResponse httpResponse = (HttpServletResponse) response;
1414
1415 String mime = inferMimeType(request);
1416 if (mime == null) {
1417 httpResponse.setContentType("text/plain; charset=utf-8");
1418 } else if (mime.startsWith("text/html")) {
1419
1420
1421
1422
1423 httpResponse.setContentType("text/html; charset=utf-8");
1424 } else if (mime.startsWith("application/xml")) {
1425 httpResponse.setContentType("text/xml; charset=utf-8");
1426 }
1427 chain.doFilter(quoted, httpResponse);
1428 }
1429
1430
1431
1432
1433
1434 private String inferMimeType(ServletRequest request) {
1435 String path = ((HttpServletRequest)request).getRequestURI();
1436 ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
1437 MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
1438 Buffer mimeBuffer = mimes.getMimeByExtension(path);
1439 return (mimeBuffer == null) ? null : mimeBuffer.toString();
1440 }
1441
1442 }
1443
1444 }