1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.commons.cli.PosixParser;
31 import org.apache.commons.lang.ArrayUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.classification.InterfaceAudience;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.hbase.HBaseConfiguration;
37 import org.apache.hadoop.hbase.rest.filter.AuthFilter;
38 import org.apache.hadoop.hbase.rest.filter.GzipFilter;
39 import org.apache.hadoop.hbase.security.User;
40 import org.apache.hadoop.hbase.security.UserProvider;
41 import org.apache.hadoop.hbase.util.InfoServer;
42 import org.apache.hadoop.hbase.util.Strings;
43 import org.apache.hadoop.hbase.util.VersionInfo;
44 import org.apache.hadoop.net.DNS;
45 import org.apache.hadoop.security.UserGroupInformation;
46 import org.mortbay.jetty.Connector;
47 import org.mortbay.jetty.Server;
48 import org.mortbay.jetty.nio.SelectChannelConnector;
49 import org.mortbay.jetty.security.SslSelectChannelConnector;
50 import org.mortbay.jetty.servlet.Context;
51 import org.mortbay.jetty.servlet.FilterHolder;
52 import org.mortbay.jetty.servlet.ServletHolder;
53 import org.mortbay.thread.QueuedThreadPool;
54
55 import com.google.common.base.Preconditions;
56 import com.sun.jersey.api.json.JSONConfiguration;
57 import com.sun.jersey.spi.container.servlet.ServletContainer;
58
59
60
61
62
63
64
65
66
67
68 @InterfaceAudience.Private
69 public class RESTServer implements Constants {
70
71 private static void printUsageAndExit(Options options, int exitCode) {
72 HelpFormatter formatter = new HelpFormatter();
73 formatter.printHelp("bin/hbase rest start", "", options,
74 "\nTo run the REST server as a daemon, execute " +
75 "bin/hbase-daemon.sh start|stop rest [--infoport <port>] [-p <port>] [-ro]\n", true);
76 System.exit(exitCode);
77 }
78
79
80
81
82
83
84 public static void main(String[] args) throws Exception {
85 Log LOG = LogFactory.getLog("RESTServer");
86
87 VersionInfo.logVersion();
88 FilterHolder authFilter = null;
89 Configuration conf = HBaseConfiguration.create();
90 Class<? extends ServletContainer> containerClass = ServletContainer.class;
91 UserProvider userProvider = UserProvider.instantiate(conf);
92
93 if (userProvider.isHadoopSecurityEnabled() && userProvider.isHBaseSecurityEnabled()) {
94 String machineName = Strings.domainNamePointerToHostName(
95 DNS.getDefaultHost(conf.get(REST_DNS_INTERFACE, "default"),
96 conf.get(REST_DNS_NAMESERVER, "default")));
97 String keytabFilename = conf.get(REST_KEYTAB_FILE);
98 Preconditions.checkArgument(keytabFilename != null && !keytabFilename.isEmpty(),
99 REST_KEYTAB_FILE + " should be set if security is enabled");
100 String principalConfig = conf.get(REST_KERBEROS_PRINCIPAL);
101 Preconditions.checkArgument(principalConfig != null && !principalConfig.isEmpty(),
102 REST_KERBEROS_PRINCIPAL + " should be set if security is enabled");
103 userProvider.login(REST_KEYTAB_FILE, REST_KERBEROS_PRINCIPAL, machineName);
104 if (conf.get(REST_AUTHENTICATION_TYPE) != null) {
105 containerClass = RESTServletContainer.class;
106 authFilter = new FilterHolder();
107 authFilter.setClassName(AuthFilter.class.getName());
108 authFilter.setName("AuthenticationFilter");
109 }
110 }
111
112 UserGroupInformation realUser = userProvider.getCurrent().getUGI();
113 RESTServlet servlet = RESTServlet.getInstance(conf, realUser);
114
115 Options options = new Options();
116 options.addOption("p", "port", true, "Port to bind to [default: 8080]");
117 options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
118 "method requests [default: false]");
119 options.addOption(null, "infoport", true, "Port for web UI");
120
121 CommandLine commandLine = null;
122 try {
123 commandLine = new PosixParser().parse(options, args);
124 } catch (ParseException e) {
125 LOG.error("Could not parse: ", e);
126 printUsageAndExit(options, -1);
127 }
128
129
130 if (commandLine != null && commandLine.hasOption("port")) {
131 String val = commandLine.getOptionValue("port");
132 servlet.getConfiguration()
133 .setInt("hbase.rest.port", Integer.valueOf(val));
134 LOG.debug("port set to " + val);
135 }
136
137
138 if (commandLine != null && commandLine.hasOption("readonly")) {
139 servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
140 LOG.debug("readonly set to true");
141 }
142
143
144 if (commandLine != null && commandLine.hasOption("infoport")) {
145 String val = commandLine.getOptionValue("infoport");
146 servlet.getConfiguration()
147 .setInt("hbase.rest.info.port", Integer.valueOf(val));
148 LOG.debug("Web UI port set to " + val);
149 }
150
151 @SuppressWarnings("unchecked")
152 List<String> remainingArgs = commandLine != null ?
153 commandLine.getArgList() : new ArrayList<String>();
154 if (remainingArgs.size() != 1) {
155 printUsageAndExit(options, 1);
156 }
157
158 String command = remainingArgs.get(0);
159 if ("start".equals(command)) {
160
161 } else if ("stop".equals(command)) {
162 System.exit(1);
163 } else {
164 printUsageAndExit(options, 1);
165 }
166
167
168 ServletHolder sh = new ServletHolder(containerClass);
169 sh.setInitParameter(
170 "com.sun.jersey.config.property.resourceConfigClass",
171 ResourceConfig.class.getCanonicalName());
172 sh.setInitParameter("com.sun.jersey.config.property.packages",
173 "jetty");
174
175
176
177
178
179
180
181 ServletHolder shPojoMap = new ServletHolder(containerClass);
182 @SuppressWarnings("unchecked")
183 Map<String, String> shInitMap = sh.getInitParameters();
184 for (Entry<String, String> e : shInitMap.entrySet()) {
185 shPojoMap.setInitParameter(e.getKey(), e.getValue());
186 }
187 shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
188
189
190
191 Server server = new Server();
192
193 Connector connector = new SelectChannelConnector();
194 if(conf.getBoolean(REST_SSL_ENABLED, false)) {
195 SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
196 String keystore = conf.get(REST_SSL_KEYSTORE_STORE);
197 String password = conf.get(REST_SSL_KEYSTORE_PASSWORD);
198 String keyPassword = conf.get(REST_SSL_KEYSTORE_KEYPASSWORD, password);
199 sslConnector.setKeystore(keystore);
200 sslConnector.setPassword(password);
201 sslConnector.setKeyPassword(keyPassword);
202 connector = sslConnector;
203 }
204 connector.setPort(servlet.getConfiguration().getInt("hbase.rest.port", 8080));
205 connector.setHost(servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0"));
206
207 server.addConnector(connector);
208
209
210
211
212
213
214 int maxThreads = servlet.getConfiguration().getInt("hbase.rest.threads.max", 100);
215 int minThreads = servlet.getConfiguration().getInt("hbase.rest.threads.min", 2);
216 QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
217 threadPool.setMinThreads(minThreads);
218 server.setThreadPool(threadPool);
219
220 server.setSendServerVersion(false);
221 server.setSendDateHeader(false);
222 server.setStopAtShutdown(true);
223
224 Context context = new Context(server, "/", Context.SESSIONS);
225 context.addServlet(shPojoMap, "/status/cluster");
226 context.addServlet(sh, "/*");
227 if (authFilter != null) {
228 context.addFilter(authFilter, "/*", 1);
229 }
230
231
232 String[] filterClasses = servlet.getConfiguration().getStrings(FILTER_CLASSES,
233 ArrayUtils.EMPTY_STRING_ARRAY);
234 for (String filter : filterClasses) {
235 filter = filter.trim();
236 context.addFilter(Class.forName(filter), "/*", 0);
237 }
238
239
240 int port = conf.getInt("hbase.rest.info.port", 8085);
241 if (port >= 0) {
242 conf.setLong("startcode", System.currentTimeMillis());
243 String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
244 InfoServer infoServer = new InfoServer("rest", a, port, false, conf);
245 infoServer.setAttribute("hbase.conf", conf);
246 infoServer.start();
247 }
248
249
250 server.start();
251 server.join();
252 }
253 }