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.io.IOException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.client.Table;
29 import org.apache.hadoop.hbase.filter.ParseFilter;
30 import org.apache.hadoop.hbase.security.UserProvider;
31 import org.apache.hadoop.hbase.util.ConnectionCache;
32 import org.apache.hadoop.security.UserGroupInformation;
33 import org.apache.hadoop.security.authorize.ProxyUsers;
34
35
36
37
38 @InterfaceAudience.Private
39 public class RESTServlet implements Constants {
40 private static final Log LOG = LogFactory.getLog(RESTServlet.class);
41 private static RESTServlet INSTANCE;
42 private final Configuration conf;
43 private final MetricsREST metrics = new MetricsREST();
44 private final ConnectionCache connectionCache;
45 private final UserGroupInformation realUser;
46
47 static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
48 static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
49 static final String HBASE_REST_SUPPORT_PROXYUSER = "hbase.rest.support.proxyuser";
50
51 UserGroupInformation getRealUser() {
52 return realUser;
53 }
54
55
56
57
58 public synchronized static RESTServlet getInstance() {
59 assert(INSTANCE != null);
60 return INSTANCE;
61 }
62
63
64
65
66
67
68
69 public synchronized static RESTServlet getInstance(Configuration conf,
70 UserProvider userProvider) throws IOException {
71 if (INSTANCE == null) {
72 INSTANCE = new RESTServlet(conf, userProvider);
73 }
74 return INSTANCE;
75 }
76
77 public synchronized static void stop() {
78 if (INSTANCE != null) {
79 INSTANCE.shutdown();
80 INSTANCE = null;
81 }
82 }
83
84
85
86
87
88
89
90 RESTServlet(final Configuration conf,
91 final UserProvider userProvider) throws IOException {
92 this.realUser = userProvider.getCurrent().getUGI();
93 this.conf = conf;
94 registerCustomFilter(conf);
95
96 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
97 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
98 connectionCache = new ConnectionCache(
99 conf, userProvider, cleanInterval, maxIdleTime);
100 if (supportsProxyuser()) {
101 ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
102 }
103 }
104
105 Admin getAdmin() throws IOException {
106 return connectionCache.getAdmin();
107 }
108
109
110
111
112 Table getTable(String tableName) throws IOException {
113 return connectionCache.getTable(tableName);
114 }
115
116 Configuration getConfiguration() {
117 return conf;
118 }
119
120 MetricsREST getMetrics() {
121 return metrics;
122 }
123
124
125
126
127
128
129 boolean isReadOnly() {
130 return getConfiguration().getBoolean("hbase.rest.readonly", false);
131 }
132
133 void setEffectiveUser(String effectiveUser) {
134 connectionCache.setEffectiveUser(effectiveUser);
135 }
136
137
138
139
140 void shutdown() {
141 if (connectionCache != null) connectionCache.shutdown();
142 }
143
144 boolean supportsProxyuser() {
145 return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
146 }
147
148 private void registerCustomFilter(Configuration conf) {
149 String[] filterList = conf.getStrings(Constants.CUSTOM_FILTERS);
150 if (filterList != null) {
151 for (String filterClass : filterList) {
152 String[] filterPart = filterClass.split(":");
153 if (filterPart.length != 2) {
154 LOG.warn(
155 "Invalid filter specification " + filterClass + " - skipping");
156 } else {
157 ParseFilter.registerFilter(filterPart[0], filterPart[1]);
158 }
159 }
160 }
161 }
162 }