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.security.visibility; 018 019import java.io.IOException; 020 021import org.apache.yetus.audience.InterfaceAudience; 022import org.apache.hadoop.hbase.client.Get; 023import org.apache.hadoop.hbase.client.Mutation; 024import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator; 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("LoadTestDataGeneratorWithVisibilityLabels can have " 043 + "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(new CellVisibility(this.visibilityExps[(int) rowkeyBase 081 % this.visibilityExps.length])); 082 return m; 083 } 084 085 @Override 086 public Get beforeGet(long rowkeyBase, Get get) { 087 get.setAuthorizations(new Authorizations( 088 authorizations[(int) (rowkeyBase % authorizations.length)])); 089 return get; 090 } 091}