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.HBaseTestingUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableNameTestRule;
030import org.apache.hadoop.hbase.TableNotFoundException;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.Connection;
033import org.apache.hadoop.hbase.client.ConnectionFactory;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.Result;
036import org.apache.hadoop.hbase.client.Scan;
037import org.apache.hadoop.hbase.client.Table;
038import org.apache.hadoop.hbase.client.TableDescriptor;
039import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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    // Grant table creation permission to USER_OWNER
111    grantGlobal(TEST_UTIL, USER_OWNER.getShortName(), Action.CREATE);
112  }
113
114  @AfterClass
115  public static void tearDownAfterClass() throws Exception {
116    TEST_UTIL.shutdownMiniCluster();
117  }
118
119  @Before
120  public void setUp() throws Exception {
121    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(testTable.getTableName())
122      .setColumnFamily(
123        ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY1).setMaxVersions(10).build())
124      .setColumnFamily(
125        ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY2).setMaxVersions(10).build())
126      // Enable backwards compatible early termination behavior in the HTD. We
127      // want to confirm that the per-table configuration is properly picked up.
128      .setValue(AccessControlConstants.CF_ATTRIBUTE_EARLY_OUT, "true").build();
129
130    createTable(TEST_UTIL, USER_OWNER, tableDescriptor);
131    TEST_UTIL.waitUntilAllRegionsAssigned(testTable.getTableName());
132  }
133
134  @After
135  public void tearDown() throws Exception {
136    // Clean the _acl_ table
137    try {
138      TEST_UTIL.deleteTable(testTable.getTableName());
139    } catch (TableNotFoundException ex) {
140      // Test deleted the table, no problem
141      LOG.info("Test deleted table " + testTable.getTableName());
142    }
143    assertEquals(0, PermissionStorage.getTablePermissions(conf, testTable.getTableName()).size());
144  }
145
146  @Test
147  public void testEarlyScanTermination() throws Exception {
148    // Grant USER_OTHER access to TEST_FAMILY1 only
149    grantOnTable(TEST_UTIL, USER_OTHER.getShortName(), testTable.getTableName(), TEST_FAMILY1, null,
150      Action.READ);
151
152    // Set up test data
153    verifyAllowed(new AccessTestAction() {
154      @Override
155      public Object run() throws Exception {
156        // force a new RS connection
157        conf.set("testkey", TEST_UTIL.getRandomUUID().toString());
158        Connection connection = ConnectionFactory.createConnection(conf);
159        Table t = connection.getTable(testTable.getTableName());
160        try {
161          Put put = new Put(TEST_ROW).addColumn(TEST_FAMILY1, TEST_Q1, ZERO);
162          t.put(put);
163          // Set a READ cell ACL for USER_OTHER on this value in FAMILY2
164          put = new Put(TEST_ROW).addColumn(TEST_FAMILY2, TEST_Q1, ZERO);
165          put.setACL(USER_OTHER.getShortName(), new Permission(Action.READ));
166          t.put(put);
167          // Set an empty cell ACL for USER_OTHER on this other value in FAMILY2
168          put = new Put(TEST_ROW).addColumn(TEST_FAMILY2, TEST_Q2, ZERO);
169          put.setACL(USER_OTHER.getShortName(), new Permission());
170          t.put(put);
171        } finally {
172          t.close();
173          connection.close();
174        }
175        return null;
176      }
177    }, USER_OWNER);
178
179    // A scan of FAMILY1 will be allowed
180    verifyAllowed(new AccessTestAction() {
181      @Override
182      public Object run() throws Exception {
183        // force a new RS connection
184        conf.set("testkey", TEST_UTIL.getRandomUUID().toString());
185        Connection connection = ConnectionFactory.createConnection(conf);
186        Table t = connection.getTable(testTable.getTableName());
187        try {
188          Scan scan = new Scan().addFamily(TEST_FAMILY1);
189          Result result = t.getScanner(scan).next();
190          if (result != null) {
191            assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1));
192            assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1));
193            return result.listCells();
194          }
195          return null;
196        } finally {
197          t.close();
198          connection.close();
199        }
200      }
201    }, USER_OTHER);
202
203    // A scan of FAMILY1 and FAMILY2 will produce results for FAMILY1 without
204    // throwing an exception, however no cells from FAMILY2 will be returned
205    // because we early out checks at the CF level.
206    verifyAllowed(new AccessTestAction() {
207      @Override
208      public Object run() throws Exception {
209        // force a new RS connection
210        conf.set("testkey", TEST_UTIL.getRandomUUID().toString());
211        Connection connection = ConnectionFactory.createConnection(conf);
212        Table t = connection.getTable(testTable.getTableName());
213        try {
214          Scan scan = new Scan();
215          Result result = t.getScanner(scan).next();
216          if (result != null) {
217            assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1));
218            assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1));
219            return result.listCells();
220          }
221          return null;
222        } finally {
223          t.close();
224          connection.close();
225        }
226      }
227    }, USER_OTHER);
228
229    // A scan of FAMILY2 will throw an AccessDeniedException
230    verifyDenied(new AccessTestAction() {
231      @Override
232      public Object run() throws Exception {
233        // force a new RS connection
234        conf.set("testkey", TEST_UTIL.getRandomUUID().toString());
235        Connection connection = ConnectionFactory.createConnection(conf);
236        Table t = connection.getTable(testTable.getTableName());
237        try {
238          Scan scan = new Scan().addFamily(TEST_FAMILY2);
239          Result result = t.getScanner(scan).next();
240          if (result != null) {
241            return result.listCells();
242          }
243          return null;
244        } finally {
245          t.close();
246          connection.close();
247        }
248      }
249    }, USER_OTHER);
250
251    // Now grant USER_OTHER access to TEST_FAMILY2:TEST_Q2
252    grantOnTable(TEST_UTIL, USER_OTHER.getShortName(), testTable.getTableName(), TEST_FAMILY2,
253      TEST_Q2, Action.READ);
254
255    // A scan of FAMILY1 and FAMILY2 will produce combined results. In FAMILY2
256    // we have access granted to Q2 at the CF level. Because we early out
257    // checks at the CF level the cell ACL on Q1 also granting access is ignored.
258    verifyAllowed(new AccessTestAction() {
259      @Override
260      public Object run() throws Exception {
261        // force a new RS connection
262        conf.set("testkey", TEST_UTIL.getRandomUUID().toString());
263        Connection connection = ConnectionFactory.createConnection(conf);
264        Table t = connection.getTable(testTable.getTableName());
265        try {
266          Scan scan = new Scan();
267          Result result = t.getScanner(scan).next();
268          if (result != null) {
269            assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY1, TEST_Q1));
270            assertFalse("Improper inclusion", result.containsColumn(TEST_FAMILY2, TEST_Q1));
271            assertTrue("Improper exclusion", result.containsColumn(TEST_FAMILY2, TEST_Q2));
272            return result.listCells();
273          }
274          return null;
275        } finally {
276          t.close();
277          connection.close();
278        }
279      }
280    }, USER_OTHER);
281  }
282}