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