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.util.LoadTestTool;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(IntegrationTests.class)
35  public class IntegrationTestIngestWithVisibilityLabels extends IntegrationTestIngest {
36  
37    private static final char COMMA = ',';
38    private static final char COLON = ':';
39    private static final String[] LABELS = { "secret", "topsecret", "confidential", "public",
40        "private" };
41    private static final String[] VISIBILITY_EXPS = { "secret & confidential & !private",
42        "topsecret | confidential", "confidential & private", "public", "topsecret & private",
43        "!public | private", "(secret | topsecret) & private" };
44    private static final List<List<String>> AUTHS = new ArrayList<List<String>>();
45  
46    static {
47      ArrayList<String> tmp = new ArrayList<String>();
48      tmp.add("secret");
49      tmp.add("confidential");
50      AUTHS.add(tmp);
51      tmp = new ArrayList<String>();
52      tmp.add("topsecret");
53      AUTHS.add(tmp);
54      tmp = new ArrayList<String>();
55      tmp.add("confidential");
56      tmp.add("private");
57      AUTHS.add(tmp);
58      tmp = new ArrayList<String>();
59      tmp.add("public");
60      AUTHS.add(tmp);
61      tmp = new ArrayList<String>();
62      tmp.add("topsecret");
63      tmp.add("private");
64      AUTHS.add(tmp);
65      tmp = new ArrayList<String>();
66      tmp.add("confidential");
67      AUTHS.add(tmp);
68      tmp = new ArrayList<String>();
69      tmp.add("topsecret");
70      tmp.add("private");
71      AUTHS.add(tmp);
72    }
73  
74    @Override
75    public void setUpCluster() throws Exception {
76      util = getTestingUtil(null);
77      Configuration conf = util.getConfiguration();
78      conf.setInt(HFile.FORMAT_VERSION_KEY, 3);
79      conf.set("hbase.coprocessor.master.classes", VisibilityController.class.getName());
80      conf.set("hbase.coprocessor.region.classes", VisibilityController.class.getName());
81      conf.set("hbase.superuser", "admin," + User.getCurrent().getName());
82      super.setUpCluster();
83      addLabels();
84    }
85  
86    @Override
87    protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
88        long numKeys) {
89      String[] args = super.getArgsForLoadTestTool(mode, modeSpecificArg, startKey, numKeys);
90      List<String> tmp = new ArrayList<String>(Arrays.asList(args));
91      tmp.add(HIPHEN + LoadTestTool.OPT_GENERATOR);
92      StringBuilder sb = new StringBuilder(LoadTestDataGeneratorWithVisibilityLabels.class.getName());
93      sb.append(COLON);
94      sb.append(asCommaSeperatedString(VISIBILITY_EXPS));
95      sb.append(COLON);
96      String authorizationsStr = AUTHS.toString();
97      sb.append(authorizationsStr.substring(1, authorizationsStr.length() - 1));
98      tmp.add(sb.toString());
99      return tmp.toArray(new String[tmp.size()]);
100   }
101 
102   private static String asCommaSeperatedString(String[] list) {
103     StringBuilder sb = new StringBuilder();
104     for (String item : list) {
105       sb.append(item);
106       sb.append(COMMA);
107     }
108     if (sb.length() > 0) {
109       // Remove the trailing ,
110       sb.deleteCharAt(sb.length() - 1);
111     }
112     return sb.toString();
113   }
114   
115   private void addLabels() throws Exception {
116     try {
117       VisibilityClient.addLabels(util.getConfiguration(), LABELS);
118       VisibilityClient.setAuths(util.getConfiguration(), LABELS, User.getCurrent().getName());
119     } catch (Throwable t) {
120       throw new IOException(t);
121     }
122   }
123 }