1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.security.visibility;
19
20 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23
24 import java.io.IOException;
25 import java.security.PrivilegedExceptionAction;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Result;
34 import org.apache.hadoop.hbase.client.ResultScanner;
35 import org.apache.hadoop.hbase.client.Scan;
36 import org.apache.hadoop.hbase.client.Table;
37 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
38 import org.apache.hadoop.hbase.security.User;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45 import org.junit.rules.TestName;
46
47 @Category(MediumTests.class)
48 public class TestVisibilityLabelsWithSLGStack {
49
50 public static final String CONFIDENTIAL = "confidential";
51 private static final String SECRET = "secret";
52 public static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
53 private static final byte[] ROW_1 = Bytes.toBytes("row1");
54 private final static byte[] CF = Bytes.toBytes("f");
55 private final static byte[] Q1 = Bytes.toBytes("q1");
56 private final static byte[] Q2 = Bytes.toBytes("q2");
57 private final static byte[] value = Bytes.toBytes("value");
58 public static Configuration conf;
59
60 @Rule
61 public final TestName TEST_NAME = new TestName();
62 public static User SUPERUSER;
63
64 @BeforeClass
65 public static void setupBeforeClass() throws Exception {
66
67 conf = TEST_UTIL.getConfiguration();
68 VisibilityTestUtil.enableVisiblityLabels(conf);
69 String classes = SimpleScanLabelGenerator.class.getCanonicalName() + " , "
70 + LabelFilteringScanLabelGenerator.class.getCanonicalName();
71 conf.setStrings(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, classes);
72 conf.set("hbase.superuser", "admin");
73 TEST_UTIL.startMiniCluster(1);
74 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
75
76
77 TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
78 addLabels();
79 }
80
81 @Test
82 public void testWithSAGStack() throws Exception {
83 TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
84 try (Table table = TEST_UTIL.createTable(tableName, CF)) {
85 Put put = new Put(ROW_1);
86 put.add(CF, Q1, HConstants.LATEST_TIMESTAMP, value);
87 put.setCellVisibility(new CellVisibility(SECRET));
88 table.put(put);
89 put = new Put(ROW_1);
90 put.add(CF, Q2, HConstants.LATEST_TIMESTAMP, value);
91 put.setCellVisibility(new CellVisibility(CONFIDENTIAL));
92 table.put(put);
93
94 LabelFilteringScanLabelGenerator.labelToFilter = CONFIDENTIAL;
95 Scan s = new Scan();
96 s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
97 ResultScanner scanner = table.getScanner(s);
98 Result next = scanner.next();
99 assertNotNull(next.getColumnLatestCell(CF, Q1));
100 assertNull(next.getColumnLatestCell(CF, Q2));
101 }
102 }
103
104 private static void addLabels() throws Exception {
105 PrivilegedExceptionAction<VisibilityLabelsResponse> action =
106 new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
107 public VisibilityLabelsResponse run() throws Exception {
108 String[] labels = { SECRET, CONFIDENTIAL };
109 try {
110 VisibilityClient.addLabels(conf, labels);
111 } catch (Throwable t) {
112 throw new IOException(t);
113 }
114 return null;
115 }
116 };
117 SUPERUSER.runAs(action);
118 }
119
120 @AfterClass
121 public static void tearDownAfterClass() throws Exception {
122 TEST_UTIL.shutdownMiniCluster();
123 }
124 }