View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.io.hfile.HFile;
27  import org.apache.hadoop.hbase.security.User;
28  import org.apache.hadoop.hbase.security.visibility.LoadTestDataGeneratorWithVisibilityLabels;
29  import org.apache.hadoop.hbase.security.visibility.VisibilityClient;
30  import org.apache.hadoop.hbase.security.visibility.VisibilityController;
31  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
32  import org.apache.hadoop.hbase.util.LoadTestTool;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(IntegrationTests.class)
36  public class IntegrationTestIngestWithVisibilityLabels extends IntegrationTestIngest {
37  
38    private static final char COMMA = ',';
39    private static final char COLON = ':';
40    private static final String[] LABELS = { "secret", "topsecret", "confidential", "public",
41        "private" };
42    private static final String[] VISIBILITY_EXPS = { "secret & confidential & !private",
43        "topsecret | confidential", "confidential & private", "public", "topsecret & private",
44        "!public | private", "(secret | topsecret) & private" };
45    private static final List<List<String>> AUTHS = new ArrayList<List<String>>();
46  
47    static {
48      ArrayList<String> tmp = new ArrayList<String>();
49      tmp.add("secret");
50      tmp.add("confidential");
51      AUTHS.add(tmp);
52      tmp = new ArrayList<String>();
53      tmp.add("topsecret");
54      AUTHS.add(tmp);
55      tmp = new ArrayList<String>();
56      tmp.add("confidential");
57      tmp.add("private");
58      AUTHS.add(tmp);
59      tmp = new ArrayList<String>();
60      tmp.add("public");
61      AUTHS.add(tmp);
62      tmp = new ArrayList<String>();
63      tmp.add("topsecret");
64      tmp.add("private");
65      AUTHS.add(tmp);
66      tmp = new ArrayList<String>();
67      tmp.add("confidential");
68      AUTHS.add(tmp);
69      tmp = new ArrayList<String>();
70      tmp.add("topsecret");
71      tmp.add("private");
72      AUTHS.add(tmp);
73    }
74  
75    @Override
76    public void setUpCluster() throws Exception {
77      util = getTestingUtil(null);
78      Configuration conf = util.getConfiguration();
79      conf.setInt(HFile.FORMAT_VERSION_KEY, 3);
80      conf.set("hbase.coprocessor.master.classes", VisibilityController.class.getName());
81      conf.set("hbase.coprocessor.region.classes", VisibilityController.class.getName());
82      conf.set("hbase.superuser", "admin," + User.getCurrent().getName());
83      super.setUpCluster();
84      addLabels();
85    }
86  
87    @Override
88    protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
89        long numKeys) {
90      String[] args = super.getArgsForLoadTestTool(mode, modeSpecificArg, startKey, numKeys);
91      List<String> tmp = new ArrayList<String>(Arrays.asList(args));
92      tmp.add(HIPHEN + LoadTestTool.OPT_GENERATOR);
93      StringBuilder sb = new StringBuilder(LoadTestDataGeneratorWithVisibilityLabels.class.getName());
94      sb.append(COLON);
95      sb.append(asCommaSeperatedString(VISIBILITY_EXPS));
96      sb.append(COLON);
97      String authorizationsStr = AUTHS.toString();
98      sb.append(authorizationsStr.substring(1, authorizationsStr.length() - 1));
99      tmp.add(sb.toString());
100     return tmp.toArray(new String[tmp.size()]);
101   }
102 
103   private static String asCommaSeperatedString(String[] list) {
104     StringBuilder sb = new StringBuilder();
105     for (String item : list) {
106       sb.append(item);
107       sb.append(COMMA);
108     }
109     if (sb.length() > 0) {
110       // Remove the trailing ,
111       sb.deleteCharAt(sb.length() - 1);
112     }
113     return sb.toString();
114   }
115   
116   private void addLabels() throws Exception {
117     try {
118       VisibilityClient.addLabels(util.getConfiguration(), LABELS);
119       VisibilityClient.setAuths(util.getConfiguration(), LABELS, User.getCurrent().getName());
120     } catch (Throwable t) {
121       throw new IOException(t);
122     }
123   }
124 }