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.security.visibility;
19  
20  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_FAMILY;
21  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.NavigableMap;
26  
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.Result;
31  import org.apache.hadoop.hbase.security.User;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(MediumTests.class)
38  public class TestVisibilityLabelsWithCustomVisLabService extends TestVisibilityLabels {
39  
40    @BeforeClass
41    public static void setupBeforeClass() throws Exception {
42      // setup configuration
43      conf = TEST_UTIL.getConfiguration();
44      conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
45      VisibilityTestUtil.enableVisiblityLabels(conf);
46      conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class,
47          ScanLabelGenerator.class);
48      conf.setClass(VisibilityLabelServiceManager.VISIBILITY_LABEL_SERVICE_CLASS,
49          ExpAsStringVisibilityLabelServiceImpl.class, VisibilityLabelService.class);
50      conf.set("hbase.superuser", "admin");
51      TEST_UTIL.startMiniCluster(2);
52      SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
53  
54      // Wait for the labels table to become available
55      TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
56      addLabels();
57    }
58  
59    // Extending this test from super as we don't verify predefined labels in ExpAsStringVisibilityLabelServiceImpl
60    @Test
61    public void testVisibilityLabelsInPutsThatDoesNotMatchAnyDefinedLabels() throws Exception {
62      TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
63      // This put with label "SAMPLE_LABEL" should not get failed.
64      createTableAndWriteDataWithLabels(tableName, "SAMPLE_LABEL", "TEST");
65    }
66  
67    protected List<String> extractAuths(String user, List<Result> results) {
68      List<String> auths = new ArrayList<String>();
69      for (Result result : results) {
70        if (Bytes.equals(result.getRow(), Bytes.toBytes(user))) {
71          NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(LABELS_TABLE_FAMILY);
72          for (byte[] q : familyMap.keySet()) {
73            auths.add(Bytes.toString(q, 0, q.length));
74          }
75        }
76      }
77      return auths;
78    }
79  }