1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.rest;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.security.PrivilegedExceptionAction;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33 import javax.xml.bind.Unmarshaller;
34
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.hbase.HBaseTestingUtility;
37 import org.apache.hadoop.hbase.HColumnDescriptor;
38 import org.apache.hadoop.hbase.HTableDescriptor;
39 import org.apache.hadoop.hbase.KeyValue;
40 import org.apache.hadoop.hbase.security.visibility.VisibilityTestUtil;
41 import org.apache.hadoop.hbase.testclassification.MediumTests;
42 import org.apache.hadoop.hbase.TableName;
43 import org.apache.hadoop.hbase.client.Admin;
44 import org.apache.hadoop.hbase.client.Connection;
45 import org.apache.hadoop.hbase.client.ConnectionFactory;
46 import org.apache.hadoop.hbase.client.Durability;
47 import org.apache.hadoop.hbase.client.HTable;
48 import org.apache.hadoop.hbase.client.Put;
49 import org.apache.hadoop.hbase.client.Table;
50 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
51 import org.apache.hadoop.hbase.rest.client.Client;
52 import org.apache.hadoop.hbase.rest.client.Cluster;
53 import org.apache.hadoop.hbase.rest.client.Response;
54 import org.apache.hadoop.hbase.rest.model.CellModel;
55 import org.apache.hadoop.hbase.rest.model.CellSetModel;
56 import org.apache.hadoop.hbase.rest.model.RowModel;
57 import org.apache.hadoop.hbase.rest.model.ScannerModel;
58 import org.apache.hadoop.hbase.security.User;
59 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
60 import org.apache.hadoop.hbase.security.visibility.ScanLabelGenerator;
61 import org.apache.hadoop.hbase.security.visibility.SimpleScanLabelGenerator;
62 import org.apache.hadoop.hbase.security.visibility.VisibilityClient;
63 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
64 import org.apache.hadoop.hbase.security.visibility.VisibilityController;
65 import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
66 import org.apache.hadoop.hbase.util.Bytes;
67 import org.junit.AfterClass;
68 import org.junit.BeforeClass;
69 import org.junit.Test;
70 import org.junit.experimental.categories.Category;
71
72 @Category(MediumTests.class)
73 public class TestScannersWithLabels {
74 private static final TableName TABLE = TableName.valueOf("TestScannersWithLabels");
75 private static final String CFA = "a";
76 private static final String CFB = "b";
77 private static final String COLUMN_1 = CFA + ":1";
78 private static final String COLUMN_2 = CFB + ":2";
79 private final static String TOPSECRET = "topsecret";
80 private final static String PUBLIC = "public";
81 private final static String PRIVATE = "private";
82 private final static String CONFIDENTIAL = "confidential";
83 private final static String SECRET = "secret";
84 private static User SUPERUSER;
85
86 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
87 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
88 private static Client client;
89 private static JAXBContext context;
90 private static Marshaller marshaller;
91 private static Unmarshaller unmarshaller;
92 private static Configuration conf;
93
94 private static int insertData(TableName tableName, String column, double prob) throws IOException {
95 byte[] k = new byte[3];
96 byte[][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
97
98 List<Put> puts = new ArrayList<>();
99 for (int i = 0; i < 9; i++) {
100 Put put = new Put(Bytes.toBytes("row" + i));
101 put.setDurability(Durability.SKIP_WAL);
102 put.add(famAndQf[0], famAndQf[1], k);
103 put.setCellVisibility(new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!"
104 + TOPSECRET));
105 puts.add(put);
106 }
107 try (Table table = new HTable(TEST_UTIL.getConfiguration(), tableName)) {
108 table.put(puts);
109 }
110 return puts.size();
111 }
112
113 private static int countCellSet(CellSetModel model) {
114 int count = 0;
115 Iterator<RowModel> rows = model.getRows().iterator();
116 while (rows.hasNext()) {
117 RowModel row = rows.next();
118 Iterator<CellModel> cells = row.getCells().iterator();
119 while (cells.hasNext()) {
120 cells.next();
121 count++;
122 }
123 }
124 return count;
125 }
126
127 @BeforeClass
128 public static void setUpBeforeClass() throws Exception {
129 SUPERUSER = User.createUserForTesting(conf, "admin",
130 new String[] { "supergroup" });
131 conf = TEST_UTIL.getConfiguration();
132 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS,
133 SimpleScanLabelGenerator.class, ScanLabelGenerator.class);
134 conf.set("hbase.superuser", SUPERUSER.getShortName());
135 VisibilityTestUtil.enableVisiblityLabels(conf);
136 TEST_UTIL.startMiniCluster(1);
137
138 TEST_UTIL.waitTableEnabled(VisibilityConstants.LABELS_TABLE_NAME.getName(), 50000);
139 createLabels();
140 setAuths();
141 REST_TEST_UTIL.startServletContainer(conf);
142 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
143 context = JAXBContext.newInstance(CellModel.class, CellSetModel.class, RowModel.class,
144 ScannerModel.class);
145 marshaller = context.createMarshaller();
146 unmarshaller = context.createUnmarshaller();
147 Admin admin = TEST_UTIL.getHBaseAdmin();
148 if (admin.tableExists(TABLE)) {
149 return;
150 }
151 HTableDescriptor htd = new HTableDescriptor(TABLE);
152 htd.addFamily(new HColumnDescriptor(CFA));
153 htd.addFamily(new HColumnDescriptor(CFB));
154 admin.createTable(htd);
155 insertData(TABLE, COLUMN_1, 1.0);
156 insertData(TABLE, COLUMN_2, 0.5);
157 }
158
159 @AfterClass
160 public static void tearDownAfterClass() throws Exception {
161 REST_TEST_UTIL.shutdownServletContainer();
162 TEST_UTIL.shutdownMiniCluster();
163 }
164
165 private static void createLabels() throws IOException, InterruptedException {
166 PrivilegedExceptionAction<VisibilityLabelsResponse> action = new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
167 @Override
168 public VisibilityLabelsResponse run() throws Exception {
169 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET };
170 try (Connection conn = ConnectionFactory.createConnection(conf)) {
171 VisibilityClient.addLabels(conn, labels);
172 } catch (Throwable t) {
173 throw new IOException(t);
174 }
175 return null;
176 }
177 };
178 SUPERUSER.runAs(action);
179 }
180 private static void setAuths() throws Exception {
181 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET };
182 try (Connection conn = ConnectionFactory.createConnection(conf)) {
183 VisibilityClient.setAuths(conn, labels, User.getCurrent().getShortName());
184 } catch (Throwable t) {
185 throw new IOException(t);
186 }
187 }
188 @Test
189 public void testSimpleScannerXMLWithLabelsThatReceivesNoData() throws IOException, JAXBException {
190 final int BATCH_SIZE = 5;
191
192 ScannerModel model = new ScannerModel();
193 model.setBatch(BATCH_SIZE);
194 model.addColumn(Bytes.toBytes(COLUMN_1));
195 model.addLabel(PUBLIC);
196 StringWriter writer = new StringWriter();
197 marshaller.marshal(model, writer);
198 byte[] body = Bytes.toBytes(writer.toString());
199
200 conf.set("hbase.rest.readonly", "false");
201 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
202 assertEquals(201, response.getCode());
203 String scannerURI = response.getLocation();
204 assertNotNull(scannerURI);
205
206
207 response = client.get(scannerURI, Constants.MIMETYPE_XML);
208
209 assertEquals(204, response.getCode());
210 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
211 }
212
213 @Test
214 public void testSimpleScannerXMLWithLabelsThatReceivesData() throws IOException, JAXBException {
215
216 ScannerModel model = new ScannerModel();
217 model.setBatch(5);
218 model.addColumn(Bytes.toBytes(COLUMN_1));
219 model.addLabel(SECRET);
220 StringWriter writer = new StringWriter();
221 marshaller.marshal(model, writer);
222 byte[] body = Bytes.toBytes(writer.toString());
223
224
225 conf.set("hbase.rest.readonly", "false");
226 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
227 assertEquals(201, response.getCode());
228 String scannerURI = response.getLocation();
229 assertNotNull(scannerURI);
230
231
232 response = client.get(scannerURI, Constants.MIMETYPE_XML);
233
234 assertEquals(200, response.getCode());
235 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
236 CellSetModel cellSet = (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response
237 .getBody()));
238 assertEquals(5, countCellSet(cellSet));
239 }
240
241 }