View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.util.test;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.client.Delete;
25  import org.apache.hadoop.hbase.client.Mutation;
26  import org.apache.hadoop.hbase.security.access.Permission;
27  import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
28  
29  @InterfaceAudience.Private
30  public class LoadTestDataGeneratorWithACL extends DefaultDataGenerator {
31    private static final Log LOG = LogFactory.getLog(LoadTestDataGeneratorWithACL.class);
32    private String[] userNames = null;
33    private static final String COMMA = ",";
34    private int specialPermCellInsertionFactor = 100;
35  
36    public LoadTestDataGeneratorWithACL(int minValueSize, int maxValueSize, int minColumnsPerKey,
37        int maxColumnsPerKey, byte[]... columnFamilies) {
38      super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
39    }
40  
41    @Override
42    public void initialize(String[] args) {
43      super.initialize(args);
44      if (args.length != 3) {
45        throw new IllegalArgumentException(
46            "LoadTestDataGeneratorWithACL can have "
47                + "1st arguement which would be super user, the 2nd argument "
48                + "would be the user list and the 3rd argument should be the factor representing "
49                + "the row keys for which only write ACLs will be added.");
50      }
51      String temp = args[1];
52      // This will be comma separated list of expressions.
53      this.userNames = temp.split(COMMA);
54      this.specialPermCellInsertionFactor = Integer.parseInt(args[2]);
55    }
56  
57    @Override
58    public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
59      if (!(m instanceof Delete)) {
60        if (userNames != null && userNames.length > 0) {
61          int mod = ((int) rowkeyBase % this.userNames.length);
62          if (((int) rowkeyBase % specialPermCellInsertionFactor) == 0) {
63            // These cells cannot be read back when running as user userName[mod]
64            if (LOG.isTraceEnabled()) {
65              LOG.trace("Adding special perm " + rowkeyBase);
66            }
67            m.setACL(userNames[mod], new Permission(Permission.Action.WRITE));
68          } else {
69            m.setACL(userNames[mod], new Permission(Permission.Action.READ));
70          }
71        }
72      }
73      return m;
74    }
75  }