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.security.access; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.Coprocessor; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HColumnDescriptor; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.HTableDescriptor; 031import org.apache.hadoop.hbase.TableNameTestRule; 032import org.apache.hadoop.hbase.TableNotFoundException; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.Connection; 035import org.apache.hadoop.hbase.client.ConnectionFactory; 036import org.apache.hadoop.hbase.client.Put; 037import org.apache.hadoop.hbase.client.Result; 038import org.apache.hadoop.hbase.client.Scan; 039import org.apache.hadoop.hbase.client.Table; 040import org.apache.hadoop.hbase.master.MasterCoprocessorHost; 041import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost; 042import org.apache.hadoop.hbase.security.User; 043import org.apache.hadoop.hbase.security.access.Permission.Action; 044import org.apache.hadoop.hbase.testclassification.MediumTests; 045import org.apache.hadoop.hbase.testclassification.SecurityTests; 046import org.apache.hadoop.hbase.util.Bytes; 047import org.junit.After; 048import org.junit.AfterClass; 049import org.junit.Before; 050import org.junit.BeforeClass; 051import org.junit.ClassRule; 052import org.junit.Rule; 053import org.junit.Test; 054import org.junit.experimental.categories.Category; 055import org.slf4j.Logger; 056import org.slf4j.LoggerFactory; 057 058@Category({ SecurityTests.class, MediumTests.class }) 059public class TestScanEarlyTermination extends SecureTestUtil { 060 061 @ClassRule 062 public static final HBaseClassTestRule CLASS_RULE = 063 HBaseClassTestRule.forClass(TestScanEarlyTermination.class); 064 065 private static final Logger LOG = LoggerFactory.getLogger(TestScanEarlyTermination.class); 066 067 @Rule 068 public TableNameTestRule testTable = new TableNameTestRule(); 069 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 070 private static final byte[] TEST_FAMILY1 = Bytes.toBytes("f1"); 071 private static final byte[] TEST_FAMILY2 = Bytes.toBytes("f2"); 072 private static final byte[] TEST_ROW = Bytes.toBytes("testrow"); 073 private static final byte[] TEST_Q1 = Bytes.toBytes("q1"); 074 private static final byte[] TEST_Q2 = Bytes.toBytes("q2"); 075 private static final byte[] ZERO = Bytes.toBytes(0L); 076 077 private static Configuration conf; 078 079 private static User USER_OWNER; 080 private static User USER_OTHER; 081 082 @BeforeClass 083 public static void setupBeforeClass() throws Exception { 084 // setup configuration 085 conf = TEST_UTIL.getConfiguration(); 086 conf.setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 10); 087 // Enable security 088 enableSecurity(conf); 089 // Verify enableSecurity sets up what we require 090 verifyConfiguration(conf); 091 092 TEST_UTIL.startMiniCluster(); 093 MasterCoprocessorHost cpHost = 094 TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterCoprocessorHost(); 095 cpHost.load(AccessController.class, Coprocessor.PRIORITY_HIGHEST, conf); 096 AccessController ac = 097 (AccessController) cpHost.findCoprocessor(AccessController.class.getName()); 098 cpHost.createEnvironment(ac, Coprocessor.PRIORITY_HIGHEST, 1, conf); 099 RegionServerCoprocessorHost rsHost = 100 TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).getRegionServerCoprocessorHost(); 101 rsHost.createEnvironment(ac, Coprocessor.PRIORITY_HIGHEST, 1, conf); 102 103 // Wait for the ACL table to become available 104 TEST_UTIL.waitTableEnabled(PermissionStorage.ACL_TABLE_NAME); 105 106 // create a set of test users 107 USER_OWNER = User.createUserForTesting(conf, "owner", new String[0]); 108 USER_OTHER = User.createUserForTesting(conf, "other", new String[0]); 109 } 110 111 @AfterClass 112 public static void tearDownAfterClass() throws Exception { 113 TEST_UTIL.shutdownMiniCluster(); 114 } 115 116 @Before 117 public void setUp() throws Exception { 118 Admin admin = TEST_UTIL.getAdmin(); 119 HTableDescriptor htd = new HTableDescriptor(testTable.getTableName()); 120 htd.setOwner(USER_OWNER); 121 HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY1); 122 hcd.setMaxVersions(10); 123 htd.addFamily(hcd); 124 hcd = new HColumnDescriptor(TEST_FAMILY2); 125 hcd.setMaxVersions(10); 126 htd.addFamily(hcd); 127 128 // Enable backwards compatible early termination behavior in the HTD. We 129 // want to confirm that the per-table configuration is properly picked up. 130 htd.setConfiguration(AccessControlConstants.CF_ATTRIBUTE_EARLY_OUT, "true"); 131 132 admin.createTable(htd); 133 TEST_UTIL.waitUntilAllRegionsAssigned(testTable.getTableName()); 134 } 135 136 @After 137 public void tearDown() throws Exception { 138 // Clean the _acl_ table 139 try { 140 TEST_UTIL.deleteTable(testTable.getTableName()); 141 } catch (TableNotFoundException ex) { 142 // Test deleted the table, no problem 143 LOG.info("Test deleted table " + testTable.getTableName()); 144 } 145 assertEquals(0, PermissionStorage.getTablePermissions(conf, testTable.getTableName()).size()); 146 } 147 148 @Test 149 public void testEarlyScanTermination() throws Exception { 150 // Grant USER_OTHER access to TEST_FAMILY1 only 151 grantOnTable(TEST_UTIL, USER_OTHER.getShortName(), testTable.getTableName(), TEST_FAMILY1, null, 152 Action.READ); 153 154 // Set up test data 155 verifyAllowed(new AccessTestAction() { 156 @Override 157 public Object run() throws Exception { 158 // force a new RS connection 159 conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); 160 Connection connection = ConnectionFactory.createConnection(conf); 161 Table t = connection.getTable(testTable.getTableName()); 162 try { 163 Put put = new Put(TEST_ROW).addColumn(TEST_FAMILY1, TEST_Q1, ZERO); 164 t.put(put); 165 // Set a READ cell ACL for USER_OTHER on this value in FAMILY2 166 put = new Put(TEST_ROW).addColumn(TEST_FAMILY2, TEST_Q1, ZERO); 167 put.setACL(USER_OTHER.getShortName(), new Permission(Action.READ)); 168 t.put(put); 169 // Set an empty cell ACL for USER_OTHER on this other value in FAMILY2 170 put = new Put(TEST_ROW).addColumn(TEST_FAMILY2, TEST_Q2, ZERO); 171 put.setACL(USER_OTHER.getShortName(), new Permission()); 172 t.put(put); 173 } finally { 174 t.close(); 175 connection.close(); 176 } 177 return null; 178 } 179 }, USER_OWNER); 180 181 // A scan of FAMILY1 will be allowed 182 verifyAllowed(new AccessTestAction() { 183 @Override 184 public Object run() throws Exception { 185 // force a new RS connection 186 conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); 187 Connection connection = ConnectionFactory.createConnection(conf); 188 Table t = connection.getTable(testTable.getTableName()); 189 try { 190 Scan scan = new Scan().addFamily(TEST_FAMILY1); 191 Result result = t.getScanner(scan).next(); 192 if (result != null) { 193 assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1)); 194 assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1)); 195 return result.listCells(); 196 } 197 return null; 198 } finally { 199 t.close(); 200 connection.close(); 201 } 202 } 203 }, USER_OTHER); 204 205 // A scan of FAMILY1 and FAMILY2 will produce results for FAMILY1 without 206 // throwing an exception, however no cells from FAMILY2 will be returned 207 // because we early out checks at the CF level. 208 verifyAllowed(new AccessTestAction() { 209 @Override 210 public Object run() throws Exception { 211 // force a new RS connection 212 conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); 213 Connection connection = ConnectionFactory.createConnection(conf); 214 Table t = connection.getTable(testTable.getTableName()); 215 try { 216 Scan scan = new Scan(); 217 Result result = t.getScanner(scan).next(); 218 if (result != null) { 219 assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1)); 220 assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1)); 221 return result.listCells(); 222 } 223 return null; 224 } finally { 225 t.close(); 226 connection.close(); 227 } 228 } 229 }, USER_OTHER); 230 231 // A scan of FAMILY2 will throw an AccessDeniedException 232 verifyDenied(new AccessTestAction() { 233 @Override 234 public Object run() throws Exception { 235 // force a new RS connection 236 conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); 237 Connection connection = ConnectionFactory.createConnection(conf); 238 Table t = connection.getTable(testTable.getTableName()); 239 try { 240 Scan scan = new Scan().addFamily(TEST_FAMILY2); 241 Result result = t.getScanner(scan).next(); 242 if (result != null) { 243 return result.listCells(); 244 } 245 return null; 246 } finally { 247 t.close(); 248 connection.close(); 249 } 250 } 251 }, USER_OTHER); 252 253 // Now grant USER_OTHER access to TEST_FAMILY2:TEST_Q2 254 grantOnTable(TEST_UTIL, USER_OTHER.getShortName(), testTable.getTableName(), TEST_FAMILY2, 255 TEST_Q2, Action.READ); 256 257 // A scan of FAMILY1 and FAMILY2 will produce combined results. In FAMILY2 258 // we have access granted to Q2 at the CF level. Because we early out 259 // checks at the CF level the cell ACL on Q1 also granting access is ignored. 260 verifyAllowed(new AccessTestAction() { 261 @Override 262 public Object run() throws Exception { 263 // force a new RS connection 264 conf.set("testkey", TEST_UTIL.getRandomUUID().toString()); 265 Connection connection = ConnectionFactory.createConnection(conf); 266 Table t = connection.getTable(testTable.getTableName()); 267 try { 268 Scan scan = new Scan(); 269 Result result = t.getScanner(scan).next(); 270 if (result != null) { 271 assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1)); 272 assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1)); 273 assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY2, TEST_Q2)); 274 return result.listCells(); 275 } 276 return null; 277 } finally { 278 t.close(); 279 connection.close(); 280 } 281 } 282 }, USER_OTHER); 283 } 284}