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.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.hbase.HBaseConfiguration;
37 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
38 import org.apache.hadoop.hbase.http.HttpServer;
39 import org.apache.hadoop.hbase.http.InfoServer;
40 import org.apache.hadoop.hbase.jetty.SslSelectChannelConnectorSecure;
41 import org.apache.hadoop.hbase.rest.filter.AuthFilter;
42 import org.apache.hadoop.hbase.rest.filter.RestCsrfPreventionFilter;
43 import org.apache.hadoop.hbase.security.UserProvider;
44 import org.apache.hadoop.hbase.util.DNS;
45 import org.apache.hadoop.hbase.util.HttpServerUtil;
46 import org.apache.hadoop.hbase.util.Pair;
47 import org.apache.hadoop.hbase.util.Strings;
48 import org.apache.hadoop.hbase.util.VersionInfo;
49 import org.apache.hadoop.util.StringUtils;
50 import org.mortbay.jetty.Connector;
51 import org.mortbay.jetty.Server;
52 import org.mortbay.jetty.nio.SelectChannelConnector;
53 import org.mortbay.jetty.servlet.Context;
54 import org.mortbay.jetty.servlet.FilterHolder;
55 import org.mortbay.jetty.servlet.ServletHolder;
56 import org.mortbay.thread.QueuedThreadPool;
57
58 import com.google.common.base.Preconditions;
59 import com.sun.jersey.api.json.JSONConfiguration;
60 import com.sun.jersey.spi.container.servlet.ServletContainer;
61
62
63
64
65
66
67
68
69
70
71 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
72 public class RESTServer implements Constants {
73 static Log LOG = LogFactory.getLog("RESTServer");
74
75 static String REST_CSRF_ENABLED_KEY = "hbase.rest.csrf.enabled";
76 static boolean REST_CSRF_ENABLED_DEFAULT = false;
77 static boolean restCSRFEnabled = false;
78 static String REST_CSRF_CUSTOM_HEADER_KEY ="hbase.rest.csrf.custom.header";
79 static String REST_CSRF_CUSTOM_HEADER_DEFAULT = "X-XSRF-HEADER";
80 static String REST_CSRF_METHODS_TO_IGNORE_KEY = "hbase.rest.csrf.methods.to.ignore";
81 static String REST_CSRF_METHODS_TO_IGNORE_DEFAULT = "GET,OPTIONS,HEAD,TRACE";
82
83 static String REST_HTTP_ALLOW_OPTIONS_METHOD = "hbase.rest.http.allow.options.method";
84
85
86 private static boolean REST_HTTP_ALLOW_OPTIONS_METHOD_DEFAULT = false;
87
88 private static void printUsageAndExit(Options options, int exitCode) {
89 HelpFormatter formatter = new HelpFormatter();
90 formatter.printHelp("bin/hbase rest start", "", options,
91 "\nTo run the REST server as a daemon, execute " +
92 "bin/hbase-daemon.sh start|stop rest [--infoport <port>] [-p <port>] [-ro]\n", true);
93 System.exit(exitCode);
94 }
95
96
97
98
99
100
101
102
103
104
105 private static List<String> getTrimmedStringList(Configuration conf,
106 String name, String defaultValue) {
107 String valueString = conf.get(name, defaultValue);
108 if (valueString == null) {
109 return new ArrayList<>();
110 }
111 return new ArrayList<>(StringUtils.getTrimmedStringCollection(valueString));
112 }
113
114 static String REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY = "hbase.rest-csrf.browser-useragents-regex";
115 static void addCSRFFilter(Context context, Configuration conf) {
116 restCSRFEnabled = conf.getBoolean(REST_CSRF_ENABLED_KEY, REST_CSRF_ENABLED_DEFAULT);
117 if (restCSRFEnabled) {
118 String[] urls = { "/*" };
119 Map<String, String> restCsrfParams = RestCsrfPreventionFilter
120 .getFilterParams(conf, "hbase.rest-csrf.");
121 HttpServer.defineFilter(context, "csrf", RestCsrfPreventionFilter.class.getName(),
122 restCsrfParams, urls);
123 }
124 }
125
126
127 private static Pair<FilterHolder, Class<? extends ServletContainer>> loginServerPrincipal(
128 UserProvider userProvider, Configuration conf) throws Exception {
129 Class<? extends ServletContainer> containerClass = ServletContainer.class;
130 if (userProvider.isHadoopSecurityEnabled() && userProvider.isHBaseSecurityEnabled()) {
131 String machineName = Strings.domainNamePointerToHostName(
132 DNS.getDefaultHost(conf.get(REST_DNS_INTERFACE, "default"),
133 conf.get(REST_DNS_NAMESERVER, "default")));
134 String keytabFilename = conf.get(REST_KEYTAB_FILE);
135 Preconditions.checkArgument(keytabFilename != null && !keytabFilename.isEmpty(),
136 REST_KEYTAB_FILE + " should be set if security is enabled");
137 String principalConfig = conf.get(REST_KERBEROS_PRINCIPAL);
138 Preconditions.checkArgument(principalConfig != null && !principalConfig.isEmpty(),
139 REST_KERBEROS_PRINCIPAL + " should be set if security is enabled");
140 userProvider.login(REST_KEYTAB_FILE, REST_KERBEROS_PRINCIPAL, machineName);
141 if (conf.get(REST_AUTHENTICATION_TYPE) != null) {
142 containerClass = RESTServletContainer.class;
143 FilterHolder authFilter = new FilterHolder();
144 authFilter.setClassName(AuthFilter.class.getName());
145 authFilter.setName("AuthenticationFilter");
146 return new Pair<FilterHolder, Class<? extends ServletContainer>>(authFilter,containerClass);
147 }
148 }
149 return new Pair<FilterHolder, Class<? extends ServletContainer>>(null, containerClass);
150 }
151
152 private static void parseCommandLine(String[] args, RESTServlet servlet) {
153 Options options = new Options();
154 options.addOption("p", "port", true, "Port to bind to [default: " + DEFAULT_LISTEN_PORT + "]");
155 options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
156 "method requests [default: false]");
157 options.addOption(null, "infoport", true, "Port for web UI");
158
159 CommandLine commandLine = null;
160 try {
161 commandLine = new PosixParser().parse(options, args);
162 } catch (ParseException e) {
163 LOG.error("Could not parse: ", e);
164 printUsageAndExit(options, -1);
165 }
166
167
168 if (commandLine != null && commandLine.hasOption("port")) {
169 String val = commandLine.getOptionValue("port");
170 servlet.getConfiguration().setInt("hbase.rest.port", Integer.parseInt(val));
171 if (LOG.isDebugEnabled()) {
172 LOG.debug("port set to " + val);
173 }
174 }
175
176
177 if (commandLine != null && commandLine.hasOption("readonly")) {
178 servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
179 if (LOG.isDebugEnabled()) {
180 LOG.debug("readonly set to true");
181 }
182 }
183
184
185 if (commandLine != null && commandLine.hasOption("infoport")) {
186 String val = commandLine.getOptionValue("infoport");
187 servlet.getConfiguration().setInt("hbase.rest.info.port", Integer.parseInt(val));
188 if (LOG.isDebugEnabled()) {
189 LOG.debug("Web UI port set to " + val);
190 }
191 }
192
193 @SuppressWarnings("unchecked")
194 List<String> remainingArgs = commandLine != null ?
195 commandLine.getArgList() : new ArrayList<String>();
196 if (remainingArgs.size() != 1) {
197 printUsageAndExit(options, 1);
198 }
199
200 String command = remainingArgs.get(0);
201 if ("start".equals(command)) {
202
203 } else if ("stop".equals(command)) {
204 System.exit(1);
205 } else {
206 printUsageAndExit(options, 1);
207 }
208 }
209
210
211
212
213
214
215 public static void main(String[] args) throws Exception {
216 VersionInfo.logVersion();
217 Configuration conf = HBaseConfiguration.create();
218 UserProvider userProvider = UserProvider.instantiate(conf);
219 Pair<FilterHolder, Class<? extends ServletContainer>> pair = loginServerPrincipal(
220 userProvider, conf);
221 FilterHolder authFilter = pair.getFirst();
222 Class<? extends ServletContainer> containerClass = pair.getSecond();
223 RESTServlet servlet = RESTServlet.getInstance(conf, userProvider);
224
225 parseCommandLine(args, servlet);
226
227
228 ServletHolder sh = new ServletHolder(containerClass);
229 sh.setInitParameter(
230 "com.sun.jersey.config.property.resourceConfigClass",
231 ResourceConfig.class.getCanonicalName());
232 sh.setInitParameter("com.sun.jersey.config.property.packages",
233 "jetty");
234
235
236
237
238
239
240
241 ServletHolder shPojoMap = new ServletHolder(containerClass);
242 @SuppressWarnings("unchecked")
243 Map<String, String> shInitMap = sh.getInitParameters();
244 for (Entry<String, String> e : shInitMap.entrySet()) {
245 shPojoMap.setInitParameter(e.getKey(), e.getValue());
246 }
247 shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
248
249
250
251 Server server = new Server();
252
253 Connector connector = new SelectChannelConnector();
254 if(conf.getBoolean(REST_SSL_ENABLED, false)) {
255 SslSelectChannelConnectorSecure sslConnector = new SslSelectChannelConnectorSecure();
256 String keystore = conf.get(REST_SSL_KEYSTORE_STORE);
257 String password = HBaseConfiguration.getPassword(conf,
258 REST_SSL_KEYSTORE_PASSWORD, null);
259 String keyPassword = HBaseConfiguration.getPassword(conf,
260 REST_SSL_KEYSTORE_KEYPASSWORD, password);
261 sslConnector.setKeystore(keystore);
262 sslConnector.setPassword(password);
263 sslConnector.setKeyPassword(keyPassword);
264 connector = sslConnector;
265 }
266 connector.setPort(servlet.getConfiguration().getInt("hbase.rest.port", DEFAULT_LISTEN_PORT));
267 connector.setHost(servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0"));
268 connector.setHeaderBufferSize(65536);
269
270 server.addConnector(connector);
271
272
273
274
275
276
277 int maxThreads = servlet.getConfiguration().getInt("hbase.rest.threads.max", 100);
278 int minThreads = servlet.getConfiguration().getInt("hbase.rest.threads.min", 2);
279 QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
280 threadPool.setMinThreads(minThreads);
281 server.setThreadPool(threadPool);
282
283 server.setSendServerVersion(false);
284 server.setSendDateHeader(false);
285 server.setStopAtShutdown(true);
286
287 Context context = new Context(server, "/", Context.SESSIONS);
288 context.addServlet(shPojoMap, "/status/cluster");
289 context.addServlet(sh, "/*");
290 if (authFilter != null) {
291 context.addFilter(authFilter, "/*", 1);
292 }
293
294
295 String[] filterClasses = servlet.getConfiguration().getStrings(FILTER_CLASSES,
296 ArrayUtils.EMPTY_STRING_ARRAY);
297 for (String filter : filterClasses) {
298 filter = filter.trim();
299 context.addFilter(Class.forName(filter), "/*", 0);
300 }
301 addCSRFFilter(context, conf);
302 HttpServerUtil.constrainHttpMethods(context, servlet.getConfiguration()
303 .getBoolean(REST_HTTP_ALLOW_OPTIONS_METHOD, REST_HTTP_ALLOW_OPTIONS_METHOD_DEFAULT));
304
305
306 int port = conf.getInt("hbase.rest.info.port", 8085);
307 if (port >= 0) {
308 conf.setLong("startcode", System.currentTimeMillis());
309 String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
310 InfoServer infoServer = new InfoServer("rest", a, port, false, conf);
311 infoServer.setAttribute("hbase.conf", conf);
312 infoServer.start();
313 }
314
315 server.start();
316 server.join();
317 }
318 }