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