001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with this
004 * work for additional information regarding copyright ownership. The ASF
005 * licenses this file to you under the Apache License, Version 2.0 (the
006 * "License"); you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017package org.apache.hadoop.hbase.util.test;
018
019import java.io.IOException;
020
021import org.apache.yetus.audience.InterfaceAudience;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.apache.hadoop.hbase.client.Delete;
025import org.apache.hadoop.hbase.client.Mutation;
026import org.apache.hadoop.hbase.security.access.Permission;
027import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
028
029@InterfaceAudience.Private
030public class LoadTestDataGeneratorWithACL extends DefaultDataGenerator {
031  private static final Logger LOG = LoggerFactory.getLogger(LoadTestDataGeneratorWithACL.class);
032  private String[] userNames = null;
033  private static final String COMMA = ",";
034  private int specialPermCellInsertionFactor = 100;
035
036  public LoadTestDataGeneratorWithACL(int minValueSize, int maxValueSize, int minColumnsPerKey,
037      int maxColumnsPerKey, byte[]... columnFamilies) {
038    super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
039  }
040
041  @Override
042  public void initialize(String[] args) {
043    super.initialize(args);
044    if (args.length != 3) {
045      throw new IllegalArgumentException(
046          "LoadTestDataGeneratorWithACL can have "
047              + "1st arguement which would be super user, the 2nd argument "
048              + "would be the user list and the 3rd argument should be the factor representing "
049              + "the row keys for which only write ACLs will be added.");
050    }
051    String temp = args[1];
052    // This will be comma separated list of expressions.
053    this.userNames = temp.split(COMMA);
054    this.specialPermCellInsertionFactor = Integer.parseInt(args[2]);
055  }
056
057  @Override
058  public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
059    if (!(m instanceof Delete)) {
060      if (userNames != null && userNames.length > 0) {
061        int mod = ((int) rowkeyBase % this.userNames.length);
062        if (((int) rowkeyBase % specialPermCellInsertionFactor) == 0) {
063          // These cells cannot be read back when running as user userName[mod]
064          if (LOG.isTraceEnabled()) {
065            LOG.trace("Adding special perm " + rowkeyBase);
066          }
067          m.setACL(userNames[mod], new Permission(Permission.Action.WRITE));
068        } else {
069          m.setACL(userNames[mod], new Permission(Permission.Action.READ));
070        }
071      }
072    }
073    return m;
074  }
075}