1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.security.access;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.DataInput;
23 import java.io.DataInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.TreeMap;
32 import java.util.TreeSet;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.AuthUtil;
38 import org.apache.hadoop.hbase.Cell;
39 import org.apache.hadoop.hbase.CellUtil;
40 import org.apache.hadoop.hbase.HColumnDescriptor;
41 import org.apache.hadoop.hbase.HConstants;
42 import org.apache.hadoop.hbase.HTableDescriptor;
43 import org.apache.hadoop.hbase.NamespaceDescriptor;
44 import org.apache.hadoop.hbase.TableName;
45 import org.apache.hadoop.hbase.Tag;
46 import org.apache.hadoop.hbase.TagType;
47 import org.apache.hadoop.hbase.classification.InterfaceAudience;
48 import org.apache.hadoop.hbase.client.Connection;
49 import org.apache.hadoop.hbase.client.ConnectionFactory;
50 import org.apache.hadoop.hbase.client.Delete;
51 import org.apache.hadoop.hbase.client.Get;
52 import org.apache.hadoop.hbase.client.Put;
53 import org.apache.hadoop.hbase.client.Result;
54 import org.apache.hadoop.hbase.client.ResultScanner;
55 import org.apache.hadoop.hbase.client.Scan;
56 import org.apache.hadoop.hbase.client.Table;
57 import org.apache.hadoop.hbase.exceptions.DeserializationException;
58 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
59 import org.apache.hadoop.hbase.filter.QualifierFilter;
60 import org.apache.hadoop.hbase.filter.RegexStringComparator;
61 import org.apache.hadoop.hbase.master.MasterServices;
62 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
63 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos;
64 import org.apache.hadoop.hbase.regionserver.BloomType;
65 import org.apache.hadoop.hbase.regionserver.InternalScanner;
66 import org.apache.hadoop.hbase.regionserver.Region;
67 import org.apache.hadoop.hbase.security.User;
68 import org.apache.hadoop.hbase.util.Bytes;
69 import org.apache.hadoop.hbase.util.Pair;
70 import org.apache.hadoop.io.Text;
71
72 import com.google.common.collect.ArrayListMultimap;
73 import com.google.common.collect.ListMultimap;
74 import com.google.common.collect.Lists;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 @InterfaceAudience.Private
100 public class AccessControlLists {
101
102 public static final TableName ACL_TABLE_NAME =
103 TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "acl");
104 public static final byte[] ACL_GLOBAL_NAME = ACL_TABLE_NAME.getName();
105
106 public static final String ACL_LIST_FAMILY_STR = "l";
107 public static final byte[] ACL_LIST_FAMILY = Bytes.toBytes(ACL_LIST_FAMILY_STR);
108
109 public static final byte ACL_TAG_TYPE = TagType.ACL_TAG_TYPE;
110
111 public static final char NAMESPACE_PREFIX = '@';
112
113
114
115
116 public static final char ACL_KEY_DELIMITER = ',';
117
118 private static Log LOG = LogFactory.getLog(AccessControlLists.class);
119
120
121
122
123
124
125 static void createACLTable(MasterServices master) throws IOException {
126 master.createTable(new HTableDescriptor(ACL_TABLE_NAME)
127 .addFamily(new HColumnDescriptor(ACL_LIST_FAMILY)
128 .setMaxVersions(1)
129 .setInMemory(true)
130 .setBlockCacheEnabled(true)
131 .setBlocksize(8 * 1024)
132 .setBloomFilterType(BloomType.NONE)
133 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
134
135
136 .setCacheDataInL1(true)),
137 null,
138 HConstants.NO_NONCE,
139 HConstants.NO_NONCE);
140 }
141
142
143
144
145
146
147
148 static void addUserPermission(Configuration conf, UserPermission userPerm)
149 throws IOException {
150 Permission.Action[] actions = userPerm.getActions();
151 byte[] rowKey = userPermissionRowKey(userPerm);
152 Put p = new Put(rowKey);
153 byte[] key = userPermissionKey(userPerm);
154
155 if ((actions == null) || (actions.length == 0)) {
156 String msg = "No actions associated with user '" + Bytes.toString(userPerm.getUser()) + "'";
157 LOG.warn(msg);
158 throw new IOException(msg);
159 }
160
161 byte[] value = new byte[actions.length];
162 for (int i = 0; i < actions.length; i++) {
163 value[i] = actions[i].code();
164 }
165 p.addImmutable(ACL_LIST_FAMILY, key, value);
166 if (LOG.isDebugEnabled()) {
167 LOG.debug("Writing permission with rowKey "+
168 Bytes.toString(rowKey)+" "+
169 Bytes.toString(key)+": "+Bytes.toStringBinary(value)
170 );
171 }
172
173 try (Connection connection = ConnectionFactory.createConnection(conf)) {
174 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
175 table.put(p);
176 }
177 }
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 static void removeUserPermission(Configuration conf, UserPermission userPerm)
194 throws IOException {
195 Delete d = new Delete(userPermissionRowKey(userPerm));
196 byte[] key = userPermissionKey(userPerm);
197
198 if (LOG.isDebugEnabled()) {
199 LOG.debug("Removing permission "+ userPerm.toString());
200 }
201 d.addColumns(ACL_LIST_FAMILY, key);
202
203 try (Connection connection = ConnectionFactory.createConnection(conf)) {
204 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
205 table.delete(d);
206 }
207 }
208 }
209
210
211
212
213 static void removeTablePermissions(Configuration conf, TableName tableName)
214 throws IOException{
215 Delete d = new Delete(tableName.getName());
216
217 if (LOG.isDebugEnabled()) {
218 LOG.debug("Removing permissions of removed table "+ tableName);
219 }
220
221 try (Connection connection = ConnectionFactory.createConnection(conf)) {
222 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
223 table.delete(d);
224 }
225 }
226 }
227
228
229
230
231 static void removeNamespacePermissions(Configuration conf, String namespace)
232 throws IOException{
233 Delete d = new Delete(Bytes.toBytes(toNamespaceEntry(namespace)));
234
235 if (LOG.isDebugEnabled()) {
236 LOG.debug("Removing permissions of removed namespace "+ namespace);
237 }
238
239 try (Connection connection = ConnectionFactory.createConnection(conf)) {
240 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
241 table.delete(d);
242 }
243 }
244 }
245
246
247
248
249 static void removeTablePermissions(Configuration conf, TableName tableName, byte[] column)
250 throws IOException{
251
252 if (LOG.isDebugEnabled()) {
253 LOG.debug("Removing permissions of removed column " + Bytes.toString(column) +
254 " from table "+ tableName);
255 }
256
257 try (Connection connection = ConnectionFactory.createConnection(conf)) {
258 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
259 Scan scan = new Scan();
260 scan.addFamily(ACL_LIST_FAMILY);
261
262 String columnName = Bytes.toString(column);
263 scan.setFilter(new QualifierFilter(CompareOp.EQUAL, new RegexStringComparator(
264 String.format("(%s%s%s)|(%s%s)$",
265 ACL_KEY_DELIMITER, columnName, ACL_KEY_DELIMITER,
266 ACL_KEY_DELIMITER, columnName))));
267
268 Set<byte[]> qualifierSet = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
269 ResultScanner scanner = table.getScanner(scan);
270 try {
271 for (Result res : scanner) {
272 for (byte[] q : res.getFamilyMap(ACL_LIST_FAMILY).navigableKeySet()) {
273 qualifierSet.add(q);
274 }
275 }
276 } finally {
277 scanner.close();
278 }
279
280 if (qualifierSet.size() > 0) {
281 Delete d = new Delete(tableName.getName());
282 for (byte[] qualifier : qualifierSet) {
283 d.addColumns(ACL_LIST_FAMILY, qualifier);
284 }
285 table.delete(d);
286 }
287 }
288 }
289 }
290
291 static byte[] userPermissionRowKey(UserPermission userPerm) {
292 byte[] row;
293 if(userPerm.hasNamespace()) {
294 row = Bytes.toBytes(toNamespaceEntry(userPerm.getNamespace()));
295 } else if(userPerm.isGlobal()) {
296 row = ACL_GLOBAL_NAME;
297 } else {
298 row = userPerm.getTableName().getName();
299 }
300 return row;
301 }
302
303
304
305
306
307
308
309 static byte[] userPermissionKey(UserPermission userPerm) {
310 byte[] qualifier = userPerm.getQualifier();
311 byte[] family = userPerm.getFamily();
312 byte[] key = userPerm.getUser();
313
314 if (family != null && family.length > 0) {
315 key = Bytes.add(key, Bytes.add(new byte[]{ACL_KEY_DELIMITER}, family));
316 if (qualifier != null && qualifier.length > 0) {
317 key = Bytes.add(key, Bytes.add(new byte[]{ACL_KEY_DELIMITER}, qualifier));
318 }
319 }
320
321 return key;
322 }
323
324
325
326
327
328 static boolean isAclRegion(Region region) {
329 return ACL_TABLE_NAME.equals(region.getTableDesc().getTableName());
330 }
331
332
333
334
335 static boolean isAclTable(HTableDescriptor desc) {
336 return ACL_TABLE_NAME.equals(desc.getTableName());
337 }
338
339
340
341
342
343
344
345
346
347 static Map<byte[], ListMultimap<String,TablePermission>> loadAll(Region aclRegion)
348 throws IOException {
349
350 if (!isAclRegion(aclRegion)) {
351 throw new IOException("Can only load permissions from "+ACL_TABLE_NAME);
352 }
353
354 Map<byte[], ListMultimap<String, TablePermission>> allPerms =
355 new TreeMap<byte[], ListMultimap<String, TablePermission>>(Bytes.BYTES_RAWCOMPARATOR);
356
357
358
359 Scan scan = new Scan();
360 scan.addFamily(ACL_LIST_FAMILY);
361
362 InternalScanner iScanner = null;
363 try {
364 iScanner = aclRegion.getScanner(scan);
365
366 while (true) {
367 List<Cell> row = new ArrayList<Cell>();
368
369 boolean hasNext = iScanner.next(row);
370 ListMultimap<String,TablePermission> perms = ArrayListMultimap.create();
371 byte[] entry = null;
372 for (Cell kv : row) {
373 if (entry == null) {
374 entry = CellUtil.cloneRow(kv);
375 }
376 Pair<String,TablePermission> permissionsOfUserOnTable =
377 parsePermissionRecord(entry, kv);
378 if (permissionsOfUserOnTable != null) {
379 String username = permissionsOfUserOnTable.getFirst();
380 TablePermission permissions = permissionsOfUserOnTable.getSecond();
381 perms.put(username, permissions);
382 }
383 }
384 if (entry != null) {
385 allPerms.put(entry, perms);
386 }
387 if (!hasNext) {
388 break;
389 }
390 }
391 } finally {
392 if (iScanner != null) {
393 iScanner.close();
394 }
395 }
396
397 return allPerms;
398 }
399
400
401
402
403
404 static Map<byte[], ListMultimap<String,TablePermission>> loadAll(
405 Configuration conf) throws IOException {
406 Map<byte[], ListMultimap<String,TablePermission>> allPerms =
407 new TreeMap<byte[], ListMultimap<String,TablePermission>>(Bytes.BYTES_RAWCOMPARATOR);
408
409
410
411 Scan scan = new Scan();
412 scan.addFamily(ACL_LIST_FAMILY);
413
414 ResultScanner scanner = null;
415
416 try (Connection connection = ConnectionFactory.createConnection(conf)) {
417 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
418 scanner = table.getScanner(scan);
419 try {
420 for (Result row : scanner) {
421 ListMultimap<String,TablePermission> resultPerms = parsePermissions(row.getRow(), row);
422 allPerms.put(row.getRow(), resultPerms);
423 }
424 } finally {
425 if (scanner != null) scanner.close();
426 }
427 }
428 }
429
430 return allPerms;
431 }
432
433 static ListMultimap<String, TablePermission> getTablePermissions(Configuration conf,
434 TableName tableName) throws IOException {
435 return getPermissions(conf, tableName != null ? tableName.getName() : null);
436 }
437
438 static ListMultimap<String, TablePermission> getNamespacePermissions(Configuration conf,
439 String namespace) throws IOException {
440 return getPermissions(conf, Bytes.toBytes(toNamespaceEntry(namespace)));
441 }
442
443
444
445
446
447
448
449
450
451
452 static ListMultimap<String, TablePermission> getPermissions(Configuration conf,
453 byte[] entryName) throws IOException {
454 if (entryName == null) entryName = ACL_GLOBAL_NAME;
455
456
457 ListMultimap<String, TablePermission> perms = ArrayListMultimap.create();
458
459 try (Connection connection = ConnectionFactory.createConnection(conf)) {
460 try (Table table = connection.getTable(ACL_TABLE_NAME)) {
461 Get get = new Get(entryName);
462 get.addFamily(ACL_LIST_FAMILY);
463 Result row = table.get(get);
464 if (!row.isEmpty()) {
465 perms = parsePermissions(entryName, row);
466 } else {
467 LOG.info("No permissions found in " + ACL_TABLE_NAME + " for acl entry "
468 + Bytes.toString(entryName));
469 }
470 }
471 }
472
473 return perms;
474 }
475
476
477
478
479
480 static List<UserPermission> getUserTablePermissions(
481 Configuration conf, TableName tableName) throws IOException {
482 return getUserPermissions(conf, tableName == null ? null : tableName.getName());
483 }
484
485 static List<UserPermission> getUserNamespacePermissions(
486 Configuration conf, String namespace) throws IOException {
487 return getUserPermissions(conf, Bytes.toBytes(toNamespaceEntry(namespace)));
488 }
489
490 static List<UserPermission> getUserPermissions(
491 Configuration conf, byte[] entryName)
492 throws IOException {
493 ListMultimap<String,TablePermission> allPerms = getPermissions(
494 conf, entryName);
495
496 List<UserPermission> perms = new ArrayList<UserPermission>();
497
498 for (Map.Entry<String, TablePermission> entry : allPerms.entries()) {
499 UserPermission up = new UserPermission(Bytes.toBytes(entry.getKey()),
500 entry.getValue().getTableName(), entry.getValue().getFamily(),
501 entry.getValue().getQualifier(), entry.getValue().getActions());
502 perms.add(up);
503 }
504 return perms;
505 }
506
507 private static ListMultimap<String, TablePermission> parsePermissions(
508 byte[] entryName, Result result) {
509 ListMultimap<String, TablePermission> perms = ArrayListMultimap.create();
510 if (result != null && result.size() > 0) {
511 for (Cell kv : result.rawCells()) {
512
513 Pair<String,TablePermission> permissionsOfUserOnTable =
514 parsePermissionRecord(entryName, kv);
515
516 if (permissionsOfUserOnTable != null) {
517 String username = permissionsOfUserOnTable.getFirst();
518 TablePermission permissions = permissionsOfUserOnTable.getSecond();
519 perms.put(username, permissions);
520 }
521 }
522 }
523 return perms;
524 }
525
526 private static Pair<String, TablePermission> parsePermissionRecord(
527 byte[] entryName, Cell kv) {
528
529 byte[] family = CellUtil.cloneFamily(kv);
530
531 if (!Bytes.equals(family, ACL_LIST_FAMILY)) {
532 return null;
533 }
534
535 byte[] key = CellUtil.cloneQualifier(kv);
536 byte[] value = CellUtil.cloneValue(kv);
537 if (LOG.isDebugEnabled()) {
538 LOG.debug("Read acl: kv ["+
539 Bytes.toStringBinary(key)+": "+
540 Bytes.toStringBinary(value)+"]");
541 }
542
543
544
545 String username = Bytes.toString(key);
546
547
548 if(isNamespaceEntry(entryName)) {
549 return new Pair<String, TablePermission>(username,
550 new TablePermission(Bytes.toString(fromNamespaceEntry(entryName)), value));
551 }
552
553
554
555 int idx = username.indexOf(ACL_KEY_DELIMITER);
556 byte[] permFamily = null;
557 byte[] permQualifier = null;
558 if (idx > 0 && idx < username.length()-1) {
559 String remainder = username.substring(idx+1);
560 username = username.substring(0, idx);
561 idx = remainder.indexOf(ACL_KEY_DELIMITER);
562 if (idx > 0 && idx < remainder.length()-1) {
563 permFamily = Bytes.toBytes(remainder.substring(0, idx));
564 permQualifier = Bytes.toBytes(remainder.substring(idx+1));
565 } else {
566 permFamily = Bytes.toBytes(remainder);
567 }
568 }
569
570 return new Pair<String,TablePermission>(username,
571 new TablePermission(TableName.valueOf(entryName), permFamily, permQualifier, value));
572 }
573
574
575
576
577
578
579
580 public static byte[] writePermissionsAsBytes(ListMultimap<String, TablePermission> perms,
581 Configuration conf) {
582 return ProtobufUtil.prependPBMagic(ProtobufUtil.toUserTablePermissions(perms).toByteArray());
583 }
584
585
586
587
588
589 public static ListMultimap<String, TablePermission> readPermissions(byte[] data,
590 Configuration conf)
591 throws DeserializationException {
592 if (ProtobufUtil.isPBMagicPrefix(data)) {
593 int pblen = ProtobufUtil.lengthOfPBMagic();
594 try {
595 AccessControlProtos.UsersAndPermissions.Builder builder =
596 AccessControlProtos.UsersAndPermissions.newBuilder();
597 ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen);
598 return ProtobufUtil.toUserTablePermissions(builder.build());
599 } catch (IOException e) {
600 throw new DeserializationException(e);
601 }
602 } else {
603 ListMultimap<String,TablePermission> perms = ArrayListMultimap.create();
604 try {
605 DataInput in = new DataInputStream(new ByteArrayInputStream(data));
606 int length = in.readInt();
607 for (int i=0; i<length; i++) {
608 String user = Text.readString(in);
609 List<TablePermission> userPerms =
610 (List)HbaseObjectWritableFor96Migration.readObject(in, conf);
611 perms.putAll(user, userPerms);
612 }
613 } catch (IOException e) {
614 throw new DeserializationException(e);
615 }
616 return perms;
617 }
618 }
619
620 public static boolean isNamespaceEntry(String entryName) {
621 return entryName.charAt(0) == NAMESPACE_PREFIX;
622 }
623
624 public static boolean isNamespaceEntry(byte[] entryName) {
625 return entryName[0] == NAMESPACE_PREFIX;
626 }
627
628 public static String toNamespaceEntry(String namespace) {
629 return NAMESPACE_PREFIX + namespace;
630 }
631
632 public static String fromNamespaceEntry(String namespace) {
633 if(namespace.charAt(0) != NAMESPACE_PREFIX)
634 throw new IllegalArgumentException("Argument is not a valid namespace entry");
635 return namespace.substring(1);
636 }
637
638 public static byte[] toNamespaceEntry(byte[] namespace) {
639 byte[] ret = new byte[namespace.length+1];
640 ret[0] = NAMESPACE_PREFIX;
641 System.arraycopy(namespace, 0, ret, 1, namespace.length);
642 return ret;
643 }
644
645 public static byte[] fromNamespaceEntry(byte[] namespace) {
646 if(namespace[0] != NAMESPACE_PREFIX) {
647 throw new IllegalArgumentException("Argument is not a valid namespace entry: " +
648 Bytes.toString(namespace));
649 }
650 return Arrays.copyOfRange(namespace, 1, namespace.length);
651 }
652
653 public static List<Permission> getCellPermissionsForUser(User user, Cell cell)
654 throws IOException {
655
656 if (cell.getTagsLength() == 0) {
657 return null;
658 }
659 List<Permission> results = Lists.newArrayList();
660 Iterator<Tag> tagsIterator = CellUtil.tagsIterator(cell.getTagsArray(), cell.getTagsOffset(),
661 cell.getTagsLength());
662 while (tagsIterator.hasNext()) {
663 Tag tag = tagsIterator.next();
664 if (tag.getType() == ACL_TAG_TYPE) {
665
666
667
668 AccessControlProtos.UsersAndPermissions.Builder builder =
669 AccessControlProtos.UsersAndPermissions.newBuilder();
670 ProtobufUtil.mergeFrom(builder, tag.getBuffer(), tag.getTagOffset(), tag.getTagLength());
671 ListMultimap<String,Permission> kvPerms =
672 ProtobufUtil.toUsersAndPermissions(builder.build());
673
674 List<Permission> userPerms = kvPerms.get(user.getShortName());
675 if (userPerms != null) {
676 results.addAll(userPerms);
677 }
678
679 String groupNames[] = user.getGroupNames();
680 if (groupNames != null) {
681 for (String group : groupNames) {
682 List<Permission> groupPerms = kvPerms.get(AuthUtil.toGroupEntry(group));
683 if (results != null) {
684 results.addAll(groupPerms);
685 }
686 }
687 }
688 }
689 }
690 return results;
691 }
692 }