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.security.visibility.VisibilityTestUtil;
32  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
33  import org.apache.hadoop.hbase.util.LoadTestTool;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(IntegrationTests.class)
37  public class IntegrationTestIngestWithVisibilityLabels extends IntegrationTestIngest {
38  
39    private static final char COMMA = ',';
40    private static final char COLON = ':';
41    private static final String[] LABELS = { "secret", "topsecret", "confidential", "public",
42        "private" };
43    private static final String[] VISIBILITY_EXPS = { "secret & confidential & !private",
44        "topsecret | confidential", "confidential & private", "public", "topsecret & private",
45        "!public | private", "(secret | topsecret) & private" };
46    private static final List<List<String>> AUTHS = new ArrayList<List<String>>();
47  
48    static {
49      ArrayList<String> tmp = new ArrayList<String>();
50      tmp.add("secret");
51      tmp.add("confidential");
52      AUTHS.add(tmp);
53      tmp = new ArrayList<String>();
54      tmp.add("topsecret");
55      AUTHS.add(tmp);
56      tmp = new ArrayList<String>();
57      tmp.add("confidential");
58      tmp.add("private");
59      AUTHS.add(tmp);
60      tmp = new ArrayList<String>();
61      tmp.add("public");
62      AUTHS.add(tmp);
63      tmp = new ArrayList<String>();
64      tmp.add("topsecret");
65      tmp.add("private");
66      AUTHS.add(tmp);
67      tmp = new ArrayList<String>();
68      tmp.add("confidential");
69      AUTHS.add(tmp);
70      tmp = new ArrayList<String>();
71      tmp.add("topsecret");
72      tmp.add("private");
73      AUTHS.add(tmp);
74    }
75  
76    @Override
77    public void setUpCluster() throws Exception {
78      util = getTestingUtil(null);
79      Configuration conf = util.getConfiguration();
80      VisibilityTestUtil.enableVisiblityLabels(conf);
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.getConnection(), LABELS);
118       VisibilityClient.setAuths(util.getConnection(), LABELS, User.getCurrent().getName());
119     } catch (Throwable t) {
120       throw new IOException(t);
121     }
122   }
123 }