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