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