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.security.visibility;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.client.Get;
022import org.apache.hadoop.hbase.client.Mutation;
023import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
024import org.apache.yetus.audience.InterfaceAudience;
025
026@InterfaceAudience.Private
027public class LoadTestDataGeneratorWithVisibilityLabels extends DefaultDataGenerator {
028
029  private static final String COMMA = ",";
030  private String[] visibilityExps = null;
031  private String[][] authorizations = null;
032
033  public LoadTestDataGeneratorWithVisibilityLabels(int minValueSize, int maxValueSize,
034    int minColumnsPerKey, int maxColumnsPerKey, byte[]... columnFamilies) {
035    super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
036  }
037
038  @Override
039  public void initialize(String[] args) {
040    super.initialize(args);
041    if (args.length < 1 || args.length > 2) {
042      throw new IllegalArgumentException(
043        "LoadTestDataGeneratorWithVisibilityLabels can have " + "1 or 2 initialization arguments");
044    }
045    // 1st arg in args is supposed to be the visibilityExps to be used with Mutations.
046    String temp = args[0];
047    // This will be comma separated list of expressions.
048    this.visibilityExps = temp.split(COMMA);
049    // 2nd arg in args,if present, is supposed to be comma separated set of authorizations to be
050    // used with Gets. Each of the set will be comma separated within square brackets.
051    // Eg: [secret,private],[confidential,private],[public]
052    if (args.length == 2) {
053      this.authorizations = toAuthorizationsSet(args[1]);
054    }
055  }
056
057  private static String[][] toAuthorizationsSet(String authorizationsStr) {
058    // Eg: [secret,private],[confidential,private],[public]
059    String[] split = authorizationsStr.split("],");
060    String[][] result = new String[split.length][];
061    for (int i = 0; i < split.length; i++) {
062      String s = split[i].trim();
063      assert s.charAt(0) == '[';
064      s = s.substring(1);
065      if (i == split.length - 1) {
066        assert s.charAt(s.length() - 1) == ']';
067        s = s.substring(0, s.length() - 1);
068      }
069      String[] tmp = s.split(COMMA);
070      for (int j = 0; j < tmp.length; j++) {
071        tmp[j] = tmp[j].trim();
072      }
073      result[i] = tmp;
074    }
075    return result;
076  }
077
078  @Override
079  public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
080    m.setCellVisibility(
081      new CellVisibility(this.visibilityExps[(int) rowkeyBase % this.visibilityExps.length]));
082    return m;
083  }
084
085  @Override
086  public Get beforeGet(long rowkeyBase, Get get) {
087    get.setAuthorizations(
088      new Authorizations(authorizations[(int) (rowkeyBase % authorizations.length)]));
089    return get;
090  }
091}