1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.security.access;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.regex.Pattern;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.HTableDescriptor;
28 import org.apache.hadoop.hbase.MasterNotRunningException;
29 import org.apache.hadoop.hbase.NamespaceDescriptor;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.classification.InterfaceStability;
34 import org.apache.hadoop.hbase.client.Admin;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.client.Table;
38 import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
39 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
40 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos;
41 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.AccessControlService.BlockingInterface;
42 import org.apache.hadoop.hbase.util.Bytes;
43
44
45
46
47 @InterfaceAudience.Public
48 @InterfaceStability.Evolving
49 public class AccessControlClient {
50 public static final TableName ACL_TABLE_NAME =
51 TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "acl");
52
53 private static BlockingInterface getAccessControlServiceStub(Table ht)
54 throws IOException {
55 CoprocessorRpcChannel service = ht.coprocessorService(HConstants.EMPTY_START_ROW);
56 BlockingInterface protocol =
57 AccessControlProtos.AccessControlService.newBlockingStub(service);
58 return protocol;
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public static void grant(Configuration conf, final TableName tableName,
72 final String userName, final byte[] family, final byte[] qual,
73 final Permission.Action... actions) throws Throwable {
74
75
76 try (Connection connection = ConnectionFactory.createConnection(conf)) {
77 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
78 ProtobufUtil.grant(getAccessControlServiceStub(table), userName, tableName, family, qual,
79 actions);
80 }
81 }
82 }
83
84
85
86
87
88
89
90
91
92 public static void grant(Configuration conf, final String namespace,
93 final String userName, final Permission.Action... actions) throws Throwable {
94
95
96 try (Connection connection = ConnectionFactory.createConnection(conf)) {
97 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
98 ProtobufUtil.grant(getAccessControlServiceStub(table), userName, namespace, actions);
99 }
100 }
101 }
102
103
104
105
106 public static void grant(Configuration conf, final String userName,
107 final Permission.Action... actions) throws Throwable {
108
109
110 try (Connection connection = ConnectionFactory.createConnection(conf)) {
111 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
112 ProtobufUtil.grant(getAccessControlServiceStub(table), userName, actions);
113 }
114 }
115 }
116
117 public static boolean isAccessControllerRunning(Configuration conf)
118 throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
119
120
121 try (Connection connection = ConnectionFactory.createConnection(conf)) {
122 try (Admin admin = connection.getAdmin()) {
123 return admin.isTableAvailable(ACL_TABLE_NAME);
124 }
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138 public static void revoke(Configuration conf, final TableName tableName,
139 final String username, final byte[] family, final byte[] qualifier,
140 final Permission.Action... actions) throws Throwable {
141
142
143 try (Connection connection = ConnectionFactory.createConnection(conf)) {
144 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
145 ProtobufUtil.revoke(getAccessControlServiceStub(table), username, tableName, family,
146 qualifier, actions);
147 }
148 }
149 }
150
151
152
153
154
155
156
157
158
159 public static void revoke(Configuration conf, final String namespace,
160 final String userName, final Permission.Action... actions) throws Throwable {
161
162
163 try (Connection connection = ConnectionFactory.createConnection(conf)) {
164 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
165 ProtobufUtil.revoke(getAccessControlServiceStub(table), userName, namespace, actions);
166 }
167 }
168 }
169
170
171
172
173 public static void revoke(Configuration conf, final String userName,
174 final Permission.Action... actions) throws Throwable {
175
176
177 try (Connection connection = ConnectionFactory.createConnection(conf)) {
178 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
179 ProtobufUtil.revoke(getAccessControlServiceStub(table), userName, actions);
180 }
181 }
182 }
183
184
185
186
187
188
189
190
191 public static List<UserPermission> getUserPermissions(Configuration conf, String tableRegex)
192 throws Throwable {
193 try (Connection connection = ConnectionFactory.createConnection(conf)) {
194 return getUserPermissions(connection, tableRegex);
195 }
196 }
197
198
199
200
201
202
203
204
205 public static List<UserPermission> getUserPermissions(Connection connection, String tableRegex)
206 throws Throwable {
207 List<UserPermission> permList = new ArrayList<UserPermission>();
208
209
210 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
211 try (Admin admin = connection.getAdmin()) {
212 CoprocessorRpcChannel service = table.coprocessorService(HConstants.EMPTY_START_ROW);
213 BlockingInterface protocol =
214 AccessControlProtos.AccessControlService.newBlockingStub(service);
215 HTableDescriptor[] htds = null;
216 if (tableRegex == null || tableRegex.isEmpty()) {
217 permList = ProtobufUtil.getUserPermissions(protocol);
218 } else if (tableRegex.charAt(0) == '@') {
219 String namespace = tableRegex.substring(1);
220 permList = ProtobufUtil.getUserPermissions(protocol, Bytes.toBytes(namespace));
221 } else {
222 htds = admin.listTables(Pattern.compile(tableRegex), true);
223 for (HTableDescriptor hd : htds) {
224 permList.addAll(ProtobufUtil.getUserPermissions(protocol, hd.getTableName()));
225 }
226 }
227 }
228 }
229 return permList;
230 }
231 }