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.apache.hadoop.hbase.security.visibility.VisibilityUtils.SYSTEM_LABEL;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.security.PrivilegedExceptionAction;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.ResultScanner;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult;
43 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameBytesPair;
44 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse;
45 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
46 import org.apache.hadoop.hbase.security.User;
47 import org.apache.hadoop.hbase.testclassification.MediumTests;
48 import org.apache.hadoop.hbase.testclassification.SecurityTests;
49 import org.apache.hadoop.hbase.util.Bytes;
50 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
51 import org.apache.hadoop.hbase.util.Threads;
52 import org.junit.Assert;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56
57 import com.google.protobuf.ByteString;
58
59 @Category({SecurityTests.class, MediumTests.class})
60 public class TestVisibilityLabelsWithDefaultVisLabelService extends TestVisibilityLabels {
61 private static final Log LOG = LogFactory.getLog(
62 TestVisibilityLabelsWithDefaultVisLabelService.class);
63
64 @BeforeClass
65 public static void setupBeforeClass() throws Exception {
66
67 conf = TEST_UTIL.getConfiguration();
68 conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
69 conf.setBoolean("hbase.online.schema.update.enable", true);
70 VisibilityTestUtil.enableVisiblityLabels(conf);
71 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class,
72 ScanLabelGenerator.class);
73 conf.set("hbase.superuser", "admin");
74 TEST_UTIL.startMiniCluster(2);
75 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
76 USER1 = User.createUserForTesting(conf, "user1", new String[] {});
77
78
79 TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
80 addLabels();
81 }
82
83 @Test
84 public void testAddLabels() throws Throwable {
85 PrivilegedExceptionAction<VisibilityLabelsResponse> action =
86 new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
87 public VisibilityLabelsResponse run() throws Exception {
88 String[] labels = { "L1", SECRET, "L2", "invalid~", "L3" };
89 VisibilityLabelsResponse response = null;
90 try (Connection conn = ConnectionFactory.createConnection(conf)) {
91 response = VisibilityClient.addLabels(conn, labels);
92 } catch (Throwable e) {
93 fail("Should not have thrown exception");
94 }
95 List<RegionActionResult> resultList = response.getResultList();
96 assertEquals(5, resultList.size());
97 assertTrue(resultList.get(0).getException().getValue().isEmpty());
98 assertEquals("org.apache.hadoop.hbase.DoNotRetryIOException", resultList.get(1)
99 .getException().getName());
100 assertTrue(Bytes.toString(resultList.get(1).getException().getValue().toByteArray())
101 .contains(
102 "org.apache.hadoop.hbase.security.visibility.LabelAlreadyExistsException: "
103 + "Label 'secret' already exists"));
104 assertTrue(resultList.get(2).getException().getValue().isEmpty());
105 assertTrue(resultList.get(3).getException().getValue().isEmpty());
106 assertTrue(resultList.get(4).getException().getValue().isEmpty());
107 return null;
108 }
109 };
110 SUPERUSER.runAs(action);
111 }
112
113 @Test(timeout = 60 * 1000)
114 public void testAddVisibilityLabelsOnRSRestart() throws Exception {
115 List<RegionServerThread> regionServerThreads = TEST_UTIL.getHBaseCluster()
116 .getRegionServerThreads();
117 for (RegionServerThread rsThread : regionServerThreads) {
118 rsThread.getRegionServer().abort("Aborting ");
119 }
120
121 RegionServerThread rs = TEST_UTIL.getHBaseCluster().startRegionServer();
122 waitForLabelsRegionAvailability(rs.getRegionServer());
123 final AtomicBoolean vcInitialized = new AtomicBoolean(true);
124 do {
125 PrivilegedExceptionAction<VisibilityLabelsResponse> action =
126 new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
127 public VisibilityLabelsResponse run() throws Exception {
128 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, "ABC", "XYZ" };
129 try (Connection conn = ConnectionFactory.createConnection(conf)) {
130 VisibilityLabelsResponse resp = VisibilityClient.addLabels(conn, labels);
131 List<RegionActionResult> results = resp.getResultList();
132 if (results.get(0).hasException()) {
133 NameBytesPair pair = results.get(0).getException();
134 Throwable t = ProtobufUtil.toException(pair);
135 LOG.debug("Got exception writing labels", t);
136 if (t instanceof VisibilityControllerNotReadyException) {
137 vcInitialized.set(false);
138 LOG.warn("VisibilityController was not yet initialized");
139 Threads.sleep(10);
140 } else {
141 vcInitialized.set(true);
142 }
143 } else LOG.debug("new labels added: " + resp);
144 } catch (Throwable t) {
145 throw new IOException(t);
146 }
147 return null;
148 }
149 };
150 SUPERUSER.runAs(action);
151 } while (!vcInitialized.get());
152
153 Scan s = new Scan();
154 s.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL));
155
156 int i = 0;
157 try (Table ht = TEST_UTIL.getConnection().getTable(LABELS_TABLE_NAME);
158 ResultScanner scanner = ht.getScanner(s)) {
159 while (true) {
160 Result next = scanner.next();
161 if (next == null) {
162 break;
163 }
164 i++;
165 }
166 }
167
168 Assert.assertEquals("The count should be 13", 13, i);
169 }
170
171 @Test
172 public void testListLabels() throws Throwable {
173 PrivilegedExceptionAction<ListLabelsResponse> action =
174 new PrivilegedExceptionAction<ListLabelsResponse>() {
175 public ListLabelsResponse run() throws Exception {
176 ListLabelsResponse response = null;
177 try (Connection conn = ConnectionFactory.createConnection(conf)) {
178 response = VisibilityClient.listLabels(conn, null);
179 } catch (Throwable e) {
180 fail("Should not have thrown exception");
181 }
182
183
184
185
186
187 List<ByteString> labels = response.getLabelList();
188 assertEquals(12, labels.size());
189 assertTrue(labels.contains(ByteString.copyFrom(SECRET.getBytes())));
190 assertTrue(labels.contains(ByteString.copyFrom(TOPSECRET.getBytes())));
191 assertTrue(labels.contains(ByteString.copyFrom(CONFIDENTIAL.getBytes())));
192 assertTrue(labels.contains(ByteString.copyFrom("ABC".getBytes())));
193 assertTrue(labels.contains(ByteString.copyFrom("XYZ".getBytes())));
194 assertFalse(labels.contains(ByteString.copyFrom(SYSTEM_LABEL.getBytes())));
195 return null;
196 }
197 };
198 SUPERUSER.runAs(action);
199 }
200
201 @Test
202 public void testListLabelsWithRegEx() throws Throwable {
203 PrivilegedExceptionAction<ListLabelsResponse> action =
204 new PrivilegedExceptionAction<ListLabelsResponse>() {
205 public ListLabelsResponse run() throws Exception {
206 ListLabelsResponse response = null;
207 try (Connection conn = ConnectionFactory.createConnection(conf)) {
208 response = VisibilityClient.listLabels(conn, ".*secret");
209 } catch (Throwable e) {
210 fail("Should not have thrown exception");
211 }
212
213 List<ByteString> labels = response.getLabelList();
214 assertEquals(2, labels.size());
215 assertTrue(labels.contains(ByteString.copyFrom(SECRET.getBytes())));
216 assertTrue(labels.contains(ByteString.copyFrom(TOPSECRET.getBytes())));
217 return null;
218 }
219 };
220 SUPERUSER.runAs(action);
221 }
222 }