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