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 import java.util.Random;
30
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34 import javax.xml.bind.Unmarshaller;
35
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.HColumnDescriptor;
39 import org.apache.hadoop.hbase.HTableDescriptor;
40 import org.apache.hadoop.hbase.KeyValue;
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.Durability;
45 import org.apache.hadoop.hbase.client.HTable;
46 import org.apache.hadoop.hbase.client.Put;
47 import org.apache.hadoop.hbase.client.Table;
48 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
49 import org.apache.hadoop.hbase.rest.client.Client;
50 import org.apache.hadoop.hbase.rest.client.Cluster;
51 import org.apache.hadoop.hbase.rest.client.Response;
52 import org.apache.hadoop.hbase.rest.model.CellModel;
53 import org.apache.hadoop.hbase.rest.model.CellSetModel;
54 import org.apache.hadoop.hbase.rest.model.RowModel;
55 import org.apache.hadoop.hbase.rest.model.ScannerModel;
56 import org.apache.hadoop.hbase.security.User;
57 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
58 import org.apache.hadoop.hbase.security.visibility.ScanLabelGenerator;
59 import org.apache.hadoop.hbase.security.visibility.SimpleScanLabelGenerator;
60 import org.apache.hadoop.hbase.security.visibility.VisibilityClient;
61 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
62 import org.apache.hadoop.hbase.security.visibility.VisibilityController;
63 import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
64 import org.apache.hadoop.hbase.util.Bytes;
65 import org.junit.AfterClass;
66 import org.junit.BeforeClass;
67 import org.junit.Test;
68 import org.junit.experimental.categories.Category;
69
70 @Category(MediumTests.class)
71 public class TestScannersWithLabels {
72 private static final TableName TABLE = TableName.valueOf("TestScannersWithLabels");
73 private static final String CFA = "a";
74 private static final String CFB = "b";
75 private static final String COLUMN_1 = CFA + ":1";
76 private static final String COLUMN_2 = CFB + ":2";
77 private final static String TOPSECRET = "topsecret";
78 private final static String PUBLIC = "public";
79 private final static String PRIVATE = "private";
80 private final static String CONFIDENTIAL = "confidential";
81 private final static String SECRET = "secret";
82 private static User SUPERUSER;
83
84 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
85 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
86 private static Client client;
87 private static JAXBContext context;
88 private static Marshaller marshaller;
89 private static Unmarshaller unmarshaller;
90 private static Configuration conf;
91
92 private static int insertData(TableName tableName, String column, double prob) throws IOException {
93 byte[] k = new byte[3];
94 byte[][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
95
96 List<Put> puts = new ArrayList<>();
97 for (int i = 0; i < 9; i++) {
98 Put put = new Put(Bytes.toBytes("row" + i));
99 put.setDurability(Durability.SKIP_WAL);
100 put.add(famAndQf[0], famAndQf[1], k);
101 put.setCellVisibility(new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!"
102 + TOPSECRET));
103 puts.add(put);
104 }
105 try (Table table = new HTable(TEST_UTIL.getConfiguration(), tableName)) {
106 table.put(puts);
107 }
108 return puts.size();
109 }
110
111 private static int countCellSet(CellSetModel model) {
112 int count = 0;
113 Iterator<RowModel> rows = model.getRows().iterator();
114 while (rows.hasNext()) {
115 RowModel row = rows.next();
116 Iterator<CellModel> cells = row.getCells().iterator();
117 while (cells.hasNext()) {
118 cells.next();
119 count++;
120 }
121 }
122 return count;
123 }
124
125 @BeforeClass
126 public static void setUpBeforeClass() throws Exception {
127 SUPERUSER = User.createUserForTesting(conf, "admin",
128 new String[] { "supergroup" });
129 conf = TEST_UTIL.getConfiguration();
130 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS,
131 SimpleScanLabelGenerator.class, ScanLabelGenerator.class);
132 conf.setInt("hfile.format.version", 3);
133 conf.set("hbase.superuser", SUPERUSER.getShortName());
134 conf.set("hbase.coprocessor.master.classes", VisibilityController.class.getName());
135 conf.set("hbase.coprocessor.region.classes", VisibilityController.class.getName());
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 public VisibilityLabelsResponse run() throws Exception {
168 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET };
169 try {
170 VisibilityClient.addLabels(conf, labels);
171 } catch (Throwable t) {
172 throw new IOException(t);
173 }
174 return null;
175 }
176 };
177 SUPERUSER.runAs(action);
178 }
179 private static void setAuths() throws Exception {
180 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET };
181 try {
182 VisibilityClient.setAuths(conf, labels, User.getCurrent().getShortName());
183 } catch (Throwable t) {
184 throw new IOException(t);
185 }
186 }
187 @Test
188 public void testSimpleScannerXMLWithLabelsThatReceivesNoData() throws IOException, JAXBException {
189 final int BATCH_SIZE = 5;
190
191 ScannerModel model = new ScannerModel();
192 model.setBatch(BATCH_SIZE);
193 model.addColumn(Bytes.toBytes(COLUMN_1));
194 model.addLabel(PUBLIC);
195 StringWriter writer = new StringWriter();
196 marshaller.marshal(model, writer);
197 byte[] body = Bytes.toBytes(writer.toString());
198
199 conf.set("hbase.rest.readonly", "false");
200 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
201 assertEquals(response.getCode(), 201);
202 String scannerURI = response.getLocation();
203 assertNotNull(scannerURI);
204
205
206 response = client.get(scannerURI, Constants.MIMETYPE_XML);
207
208 assertEquals(response.getCode(), 204);
209 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
210 }
211
212 @Test
213 public void testSimpleScannerXMLWithLabelsThatReceivesData() throws IOException, JAXBException {
214
215 ScannerModel model = new ScannerModel();
216 model.setBatch(5);
217 model.addColumn(Bytes.toBytes(COLUMN_1));
218 model.addLabel(SECRET);
219 StringWriter writer = new StringWriter();
220 marshaller.marshal(model, writer);
221 byte[] body = Bytes.toBytes(writer.toString());
222
223
224 conf.set("hbase.rest.readonly", "false");
225 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body);
226 assertEquals(response.getCode(), 201);
227 String scannerURI = response.getLocation();
228 assertNotNull(scannerURI);
229
230
231 response = client.get(scannerURI, Constants.MIMETYPE_XML);
232
233 assertEquals(response.getCode(), 200);
234 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
235 CellSetModel cellSet = (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response
236 .getBody()));
237 assertEquals(countCellSet(cellSet), 5);
238 }
239
240 }