Java Code Examples for org.apache.hadoop.hbase.Tag#getType()

The following examples show how to use org.apache.hadoop.hbase.Tag#getType() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   File: VisibilityUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Extracts and partitions the visibility tags and nonVisibility Tags
 *
 * @param cell - the cell for which we would extract and partition the
 * visibility and non visibility tags
 * @param visTags
 *          - all the visibilty tags of type TagType.VISIBILITY_TAG_TYPE would
 *          be added to this list
 * @param nonVisTags - all the non visibility tags would be added to this list
 * @return - the serailization format of the tag. Can be null if no tags are found or
 * if there is no visibility tag found
 */
public static Byte extractAndPartitionTags(Cell cell, List<Tag> visTags,
    List<Tag> nonVisTags) {
  Byte serializationFormat = null;
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      serializationFormat = Tag.getValueAsByte(tag);
    } else if (tag.getType() == VISIBILITY_TAG_TYPE) {
      visTags.add(tag);
    } else {
      // ignore string encoded visibility expressions, will be added in replication handling
      nonVisTags.add(tag);
    }
  }
  return serializationFormat;
}
 
Example 2
protected static void doAssert(byte[] row, String visTag) throws Exception {
  if (VisibilityReplicationEndPointForTest.lastEntries == null) {
    return; // first call
  }
  Assert.assertEquals(1, VisibilityReplicationEndPointForTest.lastEntries.size());
  List<Cell> cells = VisibilityReplicationEndPointForTest.lastEntries.get(0).getEdit().getCells();
  Assert.assertEquals(4, cells.size());
  boolean tagFound = false;
  for (Cell cell : cells) {
    if ((Bytes.equals(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(), row, 0,
        row.length))) {
      List<Tag> tags = PrivateCellUtil.getTags(cell);
      for (Tag tag : tags) {
        if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
          assertEquals(visTag, Tag.getValueAsString(tag));
          tagFound = true;
          break;
        }
      }
    }
  }
  assertTrue(tagFound);
}
 
Example 3
Source Project: hbase   File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether cell contains any tag with type as VISIBILITY_TAG_TYPE. This
 * tag type is reserved and should not be explicitly set by user.
 *
 * @param cell The cell under consideration
 * @param pair An optional pair of type {@code <Boolean, Tag>} which would be reused if already
 *     set and new one will be created if NULL is passed
 * @return If the boolean is false then it indicates that the cell has a RESERVERD_VIS_TAG and
 *     with boolean as true, not null tag indicates that a string modified tag was found.
 */
private Pair<Boolean, Tag> checkForReservedVisibilityTagPresence(Cell cell,
    Pair<Boolean, Tag> pair) throws IOException {
  if (pair == null) {
    pair = new Pair<>(false, null);
  } else {
    pair.setFirst(false);
    pair.setSecond(null);
  }
  // Bypass this check when the operation is done by a system/super user.
  // This is done because, while Replication, the Cells coming to the peer cluster with reserved
  // typed tags and this is fine and should get added to the peer cluster table
  if (isSystemOrSuperUser()) {
    // Does the cell contain special tag which indicates that the replicated
    // cell visiblilty tags
    // have been modified
    Tag modifiedTag = null;
    Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
    while (tagsIterator.hasNext()) {
      Tag tag = tagsIterator.next();
      if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
        modifiedTag = tag;
        break;
      }
    }
    pair.setFirst(true);
    pair.setSecond(modifiedTag);
    return pair;
  }
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(cell);
  while (tagsItr.hasNext()) {
    if (RESERVED_VIS_TAG_TYPES.contains(tagsItr.next().getType())) {
      return pair;
    }
  }
  pair.setFirst(true);
  return pair;
}
 
Example 4
Source Project: hbase   File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
private void removeReplicationVisibilityTag(List<Tag> tags) throws IOException {
  Iterator<Tag> iterator = tags.iterator();
  while (iterator.hasNext()) {
    Tag tag = iterator.next();
    if (tag.getType() == TagType.STRING_VIS_TAG_TYPE) {
      iterator.remove();
      break;
    }
  }
}
 
Example 5
Source Project: hbase   File: VisibilityController.java    License: Apache License 2.0 5 votes vote down vote up
private Cell createNewCellWithTags(Mutation mutation, Cell newCell) throws IOException {
  List<Tag> tags = Lists.newArrayList();
  CellVisibility cellVisibility = null;
  try {
    cellVisibility = mutation.getCellVisibility();
  } catch (DeserializationException e) {
    throw new IOException(e);
  }
  if (cellVisibility == null) {
    return newCell;
  }
  // Prepend new visibility tags to a new list of tags for the cell
  // Don't check user auths for labels with Mutations when the user is super user
  boolean authCheck = authorizationEnabled && checkAuths && !(isSystemOrSuperUser());
  tags.addAll(this.visibilityLabelService.createVisibilityExpTags(cellVisibility.getExpression(),
      true, authCheck));
  // Carry forward all other tags
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(newCell);
  while (tagsItr.hasNext()) {
    Tag tag = tagsItr.next();
    if (tag.getType() != TagType.VISIBILITY_TAG_TYPE
        && tag.getType() != TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      tags.add(tag);
    }
  }

  return PrivateCellUtil.createCell(newCell, tags);
}
 
Example 6
Source Project: hbase   File: VisibilityUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extract the visibility tags of the given Cell into the given List
 * @param cell - the cell
 * @param tags - the array that will be populated if visibility tags are present
 * @return The visibility tags serialization format
 */
public static Byte extractVisibilityTags(Cell cell, List<Tag> tags) {
  Byte serializationFormat = null;
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      serializationFormat = Tag.getValueAsByte(tag);
    } else if (tag.getType() == VISIBILITY_TAG_TYPE) {
      tags.add(tag);
    }
  }
  return serializationFormat;
}
 
Example 7
Source Project: hbase   File: VisibilityUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isVisibilityTagsPresent(Cell cell) {
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == VISIBILITY_TAG_TYPE) {
      return true;
    }
  }
  return false;
}
 
Example 8
private static List<List<Integer>> sortTagsBasedOnOrdinal(List<Tag> tags) throws IOException {
  List<List<Integer>> fullTagsList = new ArrayList<>();
  for (Tag tag : tags) {
    if (tag.getType() == VISIBILITY_TAG_TYPE) {
      getSortedTagOrdinals(fullTagsList, tag);
    }
  }
  return fullTagsList;
}
 
Example 9
Source Project: hbase   File: MobUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Whether the tag list has a mob reference tag.
 * @param tags The tag list.
 * @return True if the list has a mob reference tag, false if it doesn't.
 */
public static boolean hasMobReferenceTag(List<Tag> tags) {
  if (!tags.isEmpty()) {
    for (Tag tag : tags) {
      if (tag.getType() == TagType.MOB_REFERENCE_TAG_TYPE) {
        return true;
      }
    }
  }
  return false;
}
 
Example 10
@Override
protected void verifyGet(final byte[] row, final String visString, final int expected,
    final boolean nullExpected, final String... auths) throws IOException,
    InterruptedException {
  PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {

    @Override
    public Void run() throws Exception {
      try (Connection connection = ConnectionFactory.createConnection(conf1);
           Table table2 = connection.getTable(TABLE_NAME)) {
        CellScanner cellScanner;
        Cell current;
        Get get = new Get(row);
        get.setAuthorizations(new Authorizations(auths));
        Result result = table2.get(get);
        cellScanner = result.cellScanner();
        boolean advance = cellScanner.advance();
        if (nullExpected) {
          assertTrue(!advance);
          return null;
        }
        current = cellScanner.current();
        assertArrayEquals(CellUtil.cloneRow(current), row);
        assertEquals(expected, TestCoprocessorForTagsAtSink.tags.size());
        boolean foundNonVisTag = false;
        for(Tag t : TestCoprocessorForTagsAtSink.tags) {
          if(t.getType() == NON_VIS_TAG_TYPE) {
            assertEquals(TEMP, Bytes.toString(Tag.cloneValue(t)));
            foundNonVisTag = true;
            break;
          }
        }
        doAssert(row, visString);
        assertTrue(foundNonVisTag);
        return null;
      }
    }
  };
  USER1.runAs(scanAction);
}
 
Example 11
Source Project: hbase   File: PermissionStorage.java    License: Apache License 2.0 4 votes vote down vote up
public static List<Permission> getCellPermissionsForUser(User user, Cell cell)
    throws IOException {
  // Save an object allocation where we can
  if (cell.getTagsLength() == 0) {
    return null;
  }
  List<Permission> results = Lists.newArrayList();
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == ACL_TAG_TYPE) {
      // Deserialize the table permissions from the KV
      // TODO: This can be improved. Don't build UsersAndPermissions just to unpack it again,
      // use the builder
      AccessControlProtos.UsersAndPermissions.Builder builder =
          AccessControlProtos.UsersAndPermissions.newBuilder();
      if (tag.hasArray()) {
        ProtobufUtil.mergeFrom(builder, tag.getValueArray(), tag.getValueOffset(),
          tag.getValueLength());
      } else {
        ProtobufUtil.mergeFrom(builder, Tag.cloneValue(tag));
      }
      ListMultimap<String,Permission> kvPerms =
          AccessControlUtil.toUsersAndPermissions(builder.build());
      // Are there permissions for this user?
      List<Permission> userPerms = kvPerms.get(user.getShortName());
      if (userPerms != null) {
        results.addAll(userPerms);
      }
      // Are there permissions for any of the groups this user belongs to?
      String[] groupNames = user.getGroupNames();
      if (groupNames != null) {
        for (String group : groupNames) {
          List<Permission> groupPerms = kvPerms.get(AuthUtil.toGroupEntry(group));
          if (results != null) {
            results.addAll(groupPerms);
          }
        }
      }
    }
  }
  return results;
}
 
Example 12
Source Project: hbase   File: AccessController.java    License: Apache License 2.0 4 votes vote down vote up
private Cell createNewCellWithTags(Mutation mutation, Cell oldCell, Cell newCell) {
  // Collect any ACLs from the old cell
  List<Tag> tags = Lists.newArrayList();
  List<Tag> aclTags = Lists.newArrayList();
  ListMultimap<String,Permission> perms = ArrayListMultimap.create();
  if (oldCell != null) {
    Iterator<Tag> tagIterator = PrivateCellUtil.tagsIterator(oldCell);
    while (tagIterator.hasNext()) {
      Tag tag = tagIterator.next();
      if (tag.getType() != PermissionStorage.ACL_TAG_TYPE) {
        // Not an ACL tag, just carry it through
        if (LOG.isTraceEnabled()) {
          LOG.trace("Carrying forward tag from " + oldCell + ": type " + tag.getType()
              + " length " + tag.getValueLength());
        }
        tags.add(tag);
      } else {
        aclTags.add(tag);
      }
    }
  }

  // Do we have an ACL on the operation?
  byte[] aclBytes = mutation.getACL();
  if (aclBytes != null) {
    // Yes, use it
    tags.add(new ArrayBackedTag(PermissionStorage.ACL_TAG_TYPE, aclBytes));
  } else {
    // No, use what we carried forward
    if (perms != null) {
      // TODO: If we collected ACLs from more than one tag we may have a
      // List<Permission> of size > 1, this can be collapsed into a single
      // Permission
      if (LOG.isTraceEnabled()) {
        LOG.trace("Carrying forward ACLs from " + oldCell + ": " + perms);
      }
      tags.addAll(aclTags);
    }
  }

  // If we have no tags to add, just return
  if (tags.isEmpty()) {
    return newCell;
  }

  return PrivateCellUtil.createCell(newCell, tags);
}