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