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