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.security.visibility;
18  
19  import java.io.IOException;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.client.Get;
23  import org.apache.hadoop.hbase.client.Mutation;
24  import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
25  
26  @InterfaceAudience.Private
27  public class LoadTestDataGeneratorWithVisibilityLabels extends DefaultDataGenerator {
28  
29    private static final String COMMA = ",";
30    private String[] visibilityExps = null;
31    private String[][] authorizations = null;
32  
33    public LoadTestDataGeneratorWithVisibilityLabels(int minValueSize, int maxValueSize,
34        int minColumnsPerKey, int maxColumnsPerKey, byte[]... columnFamilies) {
35      super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
36    }
37  
38    @Override
39    public void initialize(String[] args) {
40      super.initialize(args);
41      if (args.length < 1 || args.length > 2) {
42        throw new IllegalArgumentException("LoadTestDataGeneratorWithVisibilityLabels can have "
43            + "1 or 2 initialization arguments");
44      }
45      // 1st arg in args is supposed to be the visibilityExps to be used with Mutations.
46      String temp = args[0];
47      // This will be comma separated list of expressions.
48      this.visibilityExps = temp.split(COMMA);
49      // 2nd arg in args,if present, is supposed to be comma separated set of authorizations to be
50      // used with Gets. Each of the set will be comma separated within square brackets.
51      // Eg: [secret,private],[confidential,private],[public]
52      if (args.length == 2) {
53        this.authorizations = toAuthorizationsSet(args[1]);
54      }
55    }
56  
57    private static String[][] toAuthorizationsSet(String authorizationsStr) {
58      // Eg: [secret,private],[confidential,private],[public]
59      String[] split = authorizationsStr.split("],");
60      String[][] result = new String[split.length][];
61      for (int i = 0; i < split.length; i++) {
62        String s = split[i].trim();
63        assert s.charAt(0) == '[';
64        s = s.substring(1);
65        if (i == split.length - 1) {
66          assert s.charAt(s.length() - 1) == ']';
67          s = s.substring(0, s.length() - 1);
68        }
69        String[] tmp = s.split(COMMA);
70        for (int j = 0; j < tmp.length; j++) {
71          tmp[j] = tmp[j].trim();
72        }
73        result[i] = tmp;
74      }
75      return result;
76    }
77  
78    @Override
79    public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
80      m.setCellVisibility(new CellVisibility(this.visibilityExps[(int) rowkeyBase
81          % this.visibilityExps.length]));
82      return m;
83    }
84  
85    @Override
86    public Get beforeGet(long rowkeyBase, Get get) {
87      get.setAuthorizations(new Authorizations(
88          authorizations[(int) (rowkeyBase % authorizations.length)]));
89      return get;
90    }
91  }