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;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.security.User;
027import org.apache.hadoop.hbase.security.visibility.LoadTestDataGeneratorWithVisibilityLabels;
028import org.apache.hadoop.hbase.security.visibility.VisibilityClient;
029import org.apache.hadoop.hbase.security.visibility.VisibilityTestUtil;
030import org.apache.hadoop.hbase.testclassification.IntegrationTests;
031import org.apache.hadoop.hbase.util.LoadTestTool;
032import org.junit.experimental.categories.Category;
033
034@Category(IntegrationTests.class)
035public class IntegrationTestIngestWithVisibilityLabels extends IntegrationTestIngest {
036
037  private static final char COMMA = ',';
038  private static final char COLON = ':';
039  private static final String[] LABELS = { "secret", "topsecret", "confidential", "public",
040      "private" };
041  private static final String[] VISIBILITY_EXPS = { "secret & confidential & !private",
042      "topsecret | confidential", "confidential & private", "public", "topsecret & private",
043      "!public | private", "(secret | topsecret) & private" };
044  private static final List<List<String>> AUTHS = new ArrayList<>();
045
046  static {
047    ArrayList<String> tmp = new ArrayList<>(2);
048    tmp.add("secret");
049    tmp.add("confidential");
050    AUTHS.add(tmp);
051    tmp = new ArrayList<>(1);
052    tmp.add("topsecret");
053    AUTHS.add(tmp);
054    tmp = new ArrayList<>(2);
055    tmp.add("confidential");
056    tmp.add("private");
057    AUTHS.add(tmp);
058    tmp = new ArrayList<>(1);
059    tmp.add("public");
060    AUTHS.add(tmp);
061    tmp = new ArrayList<>(2);
062    tmp.add("topsecret");
063    tmp.add("private");
064    AUTHS.add(tmp);
065    tmp = new ArrayList<>(1);
066    tmp.add("confidential");
067    AUTHS.add(tmp);
068    tmp = new ArrayList<>(2);
069    tmp.add("topsecret");
070    tmp.add("private");
071    AUTHS.add(tmp);
072  }
073
074  @Override
075  public void setUpCluster() throws Exception {
076    util = getTestingUtil(null);
077    Configuration conf = util.getConfiguration();
078    VisibilityTestUtil.enableVisiblityLabels(conf);
079    conf.set("hbase.superuser", "admin," + User.getCurrent().getName());
080    super.setUpCluster();
081    addLabels();
082  }
083
084  @Override
085  protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
086      long numKeys) {
087    String[] args = super.getArgsForLoadTestTool(mode, modeSpecificArg, startKey, numKeys);
088    List<String> tmp = new ArrayList<>(Arrays.asList(args));
089    tmp.add(HIPHEN + LoadTestTool.OPT_GENERATOR);
090    StringBuilder sb = new StringBuilder(LoadTestDataGeneratorWithVisibilityLabels.class.getName());
091    sb.append(COLON);
092    sb.append(asCommaSeperatedString(VISIBILITY_EXPS));
093    sb.append(COLON);
094    String authorizationsStr = AUTHS.toString();
095    sb.append(authorizationsStr.substring(1, authorizationsStr.length() - 1));
096    tmp.add(sb.toString());
097    return tmp.toArray(new String[tmp.size()]);
098  }
099
100  private static String asCommaSeperatedString(String[] list) {
101    StringBuilder sb = new StringBuilder();
102    for (String item : list) {
103      sb.append(item);
104      sb.append(COMMA);
105    }
106    if (sb.length() > 0) {
107      // Remove the trailing ,
108      sb.deleteCharAt(sb.length() - 1);
109    }
110    return sb.toString();
111  }
112  
113  private void addLabels() throws Exception {
114    try {
115      VisibilityClient.addLabels(util.getConnection(), LABELS);
116      VisibilityClient.setAuths(util.getConnection(), LABELS, User.getCurrent().getName());
117    } catch (Throwable t) {
118      throw new IOException(t);
119    }
120  }
121}