001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.rest; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022 023import java.io.ByteArrayInputStream; 024import java.io.IOException; 025import java.io.StringWriter; 026import java.security.PrivilegedExceptionAction; 027import java.util.ArrayList; 028import java.util.Iterator; 029import java.util.List; 030import javax.xml.bind.JAXBContext; 031import javax.xml.bind.JAXBException; 032import javax.xml.bind.Marshaller; 033import javax.xml.bind.Unmarshaller; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.CellUtil; 036import org.apache.hadoop.hbase.HBaseClassTestRule; 037import org.apache.hadoop.hbase.HBaseTestingUtility; 038import org.apache.hadoop.hbase.HColumnDescriptor; 039import org.apache.hadoop.hbase.HTableDescriptor; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.client.Admin; 042import org.apache.hadoop.hbase.client.Connection; 043import org.apache.hadoop.hbase.client.ConnectionFactory; 044import org.apache.hadoop.hbase.client.Durability; 045import org.apache.hadoop.hbase.client.Put; 046import org.apache.hadoop.hbase.client.Table; 047import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse; 048import org.apache.hadoop.hbase.rest.client.Client; 049import org.apache.hadoop.hbase.rest.client.Cluster; 050import org.apache.hadoop.hbase.rest.client.Response; 051import org.apache.hadoop.hbase.rest.model.CellModel; 052import org.apache.hadoop.hbase.rest.model.CellSetModel; 053import org.apache.hadoop.hbase.rest.model.RowModel; 054import org.apache.hadoop.hbase.rest.model.ScannerModel; 055import org.apache.hadoop.hbase.security.User; 056import org.apache.hadoop.hbase.security.visibility.CellVisibility; 057import org.apache.hadoop.hbase.security.visibility.ScanLabelGenerator; 058import org.apache.hadoop.hbase.security.visibility.SimpleScanLabelGenerator; 059import org.apache.hadoop.hbase.security.visibility.VisibilityClient; 060import org.apache.hadoop.hbase.security.visibility.VisibilityConstants; 061import org.apache.hadoop.hbase.security.visibility.VisibilityTestUtil; 062import org.apache.hadoop.hbase.security.visibility.VisibilityUtils; 063import org.apache.hadoop.hbase.testclassification.MediumTests; 064import org.apache.hadoop.hbase.testclassification.RestTests; 065import org.apache.hadoop.hbase.util.Bytes; 066import org.junit.AfterClass; 067import org.junit.BeforeClass; 068import org.junit.ClassRule; 069import org.junit.Test; 070import org.junit.experimental.categories.Category; 071 072@Category({ RestTests.class, MediumTests.class }) 073public class TestScannersWithLabels { 074 @ClassRule 075 public static final HBaseClassTestRule CLASS_RULE = 076 HBaseClassTestRule.forClass(TestScannersWithLabels.class); 077 078 private static final TableName TABLE = TableName.valueOf("TestScannersWithLabels"); 079 private static final String CFA = "a"; 080 private static final String CFB = "b"; 081 private static final String COLUMN_1 = CFA + ":1"; 082 private static final String COLUMN_2 = CFB + ":2"; 083 private final static String TOPSECRET = "topsecret"; 084 private final static String PUBLIC = "public"; 085 private final static String PRIVATE = "private"; 086 private final static String CONFIDENTIAL = "confidential"; 087 private final static String SECRET = "secret"; 088 private static User SUPERUSER; 089 090 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 091 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility(); 092 private static Client client; 093 private static JAXBContext context; 094 private static Marshaller marshaller; 095 private static Unmarshaller unmarshaller; 096 private static Configuration conf; 097 098 private static int insertData(TableName tableName, String column, double prob) 099 throws IOException { 100 byte[] k = new byte[3]; 101 byte[][] famAndQf = CellUtil.parseColumn(Bytes.toBytes(column)); 102 103 List<Put> puts = new ArrayList<>(9); 104 for (int i = 0; i < 9; i++) { 105 Put put = new Put(Bytes.toBytes("row" + i)); 106 put.setDurability(Durability.SKIP_WAL); 107 put.addColumn(famAndQf[0], famAndQf[1], k); 108 put.setCellVisibility( 109 new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET)); 110 puts.add(put); 111 } 112 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { 113 table.put(puts); 114 } 115 return puts.size(); 116 } 117 118 private static int countCellSet(CellSetModel model) { 119 int count = 0; 120 Iterator<RowModel> rows = model.getRows().iterator(); 121 while (rows.hasNext()) { 122 RowModel row = rows.next(); 123 Iterator<CellModel> cells = row.getCells().iterator(); 124 while (cells.hasNext()) { 125 cells.next(); 126 count++; 127 } 128 } 129 return count; 130 } 131 132 @BeforeClass 133 public static void setUpBeforeClass() throws Exception { 134 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" }); 135 conf = TEST_UTIL.getConfiguration(); 136 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class, 137 ScanLabelGenerator.class); 138 conf.set("hbase.superuser", SUPERUSER.getShortName()); 139 VisibilityTestUtil.enableVisiblityLabels(conf); 140 TEST_UTIL.startMiniCluster(1); 141 // Wait for the labels table to become available 142 TEST_UTIL.waitTableEnabled(VisibilityConstants.LABELS_TABLE_NAME.getName(), 50000); 143 createLabels(); 144 setAuths(); 145 REST_TEST_UTIL.startServletContainer(conf); 146 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())); 147 context = JAXBContext.newInstance(CellModel.class, CellSetModel.class, RowModel.class, 148 ScannerModel.class); 149 marshaller = context.createMarshaller(); 150 unmarshaller = context.createUnmarshaller(); 151 Admin admin = TEST_UTIL.getAdmin(); 152 if (admin.tableExists(TABLE)) { 153 return; 154 } 155 HTableDescriptor htd = new HTableDescriptor(TABLE); 156 htd.addFamily(new HColumnDescriptor(CFA)); 157 htd.addFamily(new HColumnDescriptor(CFB)); 158 admin.createTable(htd); 159 insertData(TABLE, COLUMN_1, 1.0); 160 insertData(TABLE, COLUMN_2, 0.5); 161 } 162 163 @AfterClass 164 public static void tearDownAfterClass() throws Exception { 165 REST_TEST_UTIL.shutdownServletContainer(); 166 TEST_UTIL.shutdownMiniCluster(); 167 } 168 169 private static void createLabels() throws IOException, InterruptedException { 170 PrivilegedExceptionAction<VisibilityLabelsResponse> action = () -> { 171 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET }; 172 try (Connection conn = ConnectionFactory.createConnection(conf)) { 173 VisibilityClient.addLabels(conn, labels); 174 } catch (Throwable t) { 175 throw new IOException(t); 176 } 177 return null; 178 }; 179 SUPERUSER.runAs(action); 180 } 181 182 private static void setAuths() throws Exception { 183 String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, PUBLIC, TOPSECRET }; 184 try (Connection conn = ConnectionFactory.createConnection(conf)) { 185 VisibilityClient.setAuths(conn, labels, User.getCurrent().getShortName()); 186 } catch (Throwable t) { 187 throw new IOException(t); 188 } 189 } 190 191 @Test 192 public void testSimpleScannerXMLWithLabelsThatReceivesNoData() throws IOException, JAXBException { 193 final int BATCH_SIZE = 5; 194 // new scanner 195 ScannerModel model = new ScannerModel(); 196 model.setBatch(BATCH_SIZE); 197 model.addColumn(Bytes.toBytes(COLUMN_1)); 198 model.addLabel(PUBLIC); 199 StringWriter writer = new StringWriter(); 200 marshaller.marshal(model, writer); 201 byte[] body = Bytes.toBytes(writer.toString()); 202 // recall previous put operation with read-only off 203 conf.set("hbase.rest.readonly", "false"); 204 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body); 205 assertEquals(201, response.getCode()); 206 String scannerURI = response.getLocation(); 207 assertNotNull(scannerURI); 208 209 // get a cell set 210 response = client.get(scannerURI, Constants.MIMETYPE_XML); 211 // Respond with 204 as there are no cells to be retrieved 212 assertEquals(204, response.getCode()); 213 // With no content in the payload, the 'Content-Type' header is not echo back 214 } 215 216 @Test 217 public void testSimpleScannerXMLWithLabelsThatReceivesData() throws IOException, JAXBException { 218 // new scanner 219 ScannerModel model = new ScannerModel(); 220 model.setBatch(5); 221 model.addColumn(Bytes.toBytes(COLUMN_1)); 222 model.addLabel(SECRET); 223 StringWriter writer = new StringWriter(); 224 marshaller.marshal(model, writer); 225 byte[] body = Bytes.toBytes(writer.toString()); 226 227 // recall previous put operation with read-only off 228 conf.set("hbase.rest.readonly", "false"); 229 Response response = client.put("/" + TABLE + "/scanner", Constants.MIMETYPE_XML, body); 230 assertEquals(201, response.getCode()); 231 String scannerURI = response.getLocation(); 232 assertNotNull(scannerURI); 233 234 // get a cell set 235 response = client.get(scannerURI, Constants.MIMETYPE_XML); 236 // Respond with 204 as there are no cells to be retrieved 237 assertEquals(200, response.getCode()); 238 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 239 CellSetModel cellSet = 240 (CellSetModel) unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); 241 assertEquals(5, countCellSet(cellSet)); 242 } 243}