View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security.access;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.NamespaceDescriptor;
35  import org.apache.hadoop.hbase.TableName;
36  import org.apache.hadoop.hbase.TableNotFoundException;
37  import org.apache.hadoop.hbase.client.Admin;
38  import org.apache.hadoop.hbase.client.Connection;
39  import org.apache.hadoop.hbase.client.ConnectionFactory;
40  import org.apache.hadoop.hbase.client.Put;
41  import org.apache.hadoop.hbase.client.Result;
42  import org.apache.hadoop.hbase.client.ResultScanner;
43  import org.apache.hadoop.hbase.client.Scan;
44  import org.apache.hadoop.hbase.client.Table;
45  import org.apache.hadoop.hbase.security.User;
46  import org.apache.hadoop.hbase.security.access.Permission.Action;
47  import org.apache.hadoop.hbase.testclassification.LargeTests;
48  import org.apache.hadoop.hbase.util.Bytes;
49  import org.apache.hadoop.hbase.util.TestTableName;
50  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
51  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
52  import org.junit.After;
53  import org.junit.AfterClass;
54  import org.junit.Before;
55  import org.junit.BeforeClass;
56  import org.junit.Rule;
57  import org.junit.Test;
58  import org.junit.experimental.categories.Category;
59  
60  @Category(LargeTests.class)
61  public class TestAccessController2 extends SecureTestUtil {
62    private static final Log LOG = LogFactory.getLog(TestAccessController2.class);
63  
64    private static final byte[] TEST_ROW = Bytes.toBytes("test");
65    private static final byte[] TEST_FAMILY = Bytes.toBytes("f");
66    private static final byte[] TEST_QUALIFIER = Bytes.toBytes("q");
67    private static final byte[] TEST_VALUE = Bytes.toBytes("value");
68  
69    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70    private static Configuration conf;
71  
72    /** The systemUserConnection created here is tied to the system user. In case, you are planning
73     * to create AccessTestAction, DON'T use this systemUserConnection as the 'doAs' user
74     * gets  eclipsed by the system user. */
75    private static Connection systemUserConnection;
76  
77    private final static byte[] Q1 = Bytes.toBytes("q1");
78    private final static byte[] value1 = Bytes.toBytes("value1");
79  
80    private static byte[] TEST_FAMILY_2 = Bytes.toBytes("f2");
81    private static byte[] TEST_ROW_2 = Bytes.toBytes("r2");
82    private final static byte[] Q2 = Bytes.toBytes("q2");
83    private final static byte[] value2 = Bytes.toBytes("value2");
84  
85    private static byte[] TEST_ROW_3 = Bytes.toBytes("r3");
86  
87    private static final String TESTGROUP_1 = "testgroup_1";
88    private static final String TESTGROUP_2 = "testgroup_2";
89  
90    private static User TESTGROUP1_USER1;
91    private static User TESTGROUP2_USER1;
92  
93    @Rule
94    public TestTableName TEST_TABLE = new TestTableName();
95    private String namespace = "testNamespace";
96    private String tname = namespace + ":testtable1";
97    private TableName tableName = TableName.valueOf(tname);
98  
99    @BeforeClass
100   public static void setupBeforeClass() throws Exception {
101     conf = TEST_UTIL.getConfiguration();
102     // Enable security
103     enableSecurity(conf);
104     // Verify enableSecurity sets up what we require
105     verifyConfiguration(conf);
106     TEST_UTIL.startMiniCluster();
107     // Wait for the ACL table to become available
108     TEST_UTIL.waitUntilAllRegionsAssigned(AccessControlLists.ACL_TABLE_NAME);
109 
110     TESTGROUP1_USER1 =
111         User.createUserForTesting(conf, "testgroup1_user1", new String[] { TESTGROUP_1 });
112     TESTGROUP2_USER1 =
113         User.createUserForTesting(conf, "testgroup2_user2", new String[] { TESTGROUP_2 });
114 
115     systemUserConnection = ConnectionFactory.createConnection(conf);
116   }
117 
118   @Before
119   public void setUp() throws Exception {
120     createNamespace(TEST_UTIL, NamespaceDescriptor.create(namespace).build());
121     try (Table table = createTable(TEST_UTIL, tableName,
122           new byte[][] { TEST_FAMILY, TEST_FAMILY_2 })) {
123       TEST_UTIL.waitTableEnabled(tableName);
124 
125       // Ingesting test data.
126       table.put(Arrays.asList(new Put(TEST_ROW).addColumn(TEST_FAMILY, Q1, value1),
127           new Put(TEST_ROW_2).addColumn(TEST_FAMILY, Q2, value2),
128           new Put(TEST_ROW_3).addColumn(TEST_FAMILY_2, Q1, value1)));
129     }
130 
131     assertEquals(1, AccessControlLists.getTablePermissions(conf, tableName).size());
132     try {
133       assertEquals(1, AccessControlClient.getUserPermissions(systemUserConnection, tableName.toString())
134           .size());
135     } catch (Throwable e) {
136       LOG.error("Error during call of AccessControlClient.getUserPermissions. ", e);
137     }
138   }
139 
140   @AfterClass
141   public static void tearDownAfterClass() throws Exception {
142     systemUserConnection.close();
143     TEST_UTIL.shutdownMiniCluster();
144   }
145 
146   @After
147   public void tearDown() throws Exception {
148     // Clean the _acl_ table
149     try {
150       deleteTable(TEST_UTIL, tableName);
151     } catch (TableNotFoundException ex) {
152       // Test deleted the table, no problem
153       LOG.info("Test deleted table " + tableName);
154     }
155     deleteNamespace(TEST_UTIL, namespace);
156     // Verify all table/namespace permissions are erased
157     assertEquals(0, AccessControlLists.getTablePermissions(conf, tableName).size());
158     assertEquals(0, AccessControlLists.getNamespacePermissions(conf, namespace).size());
159   }
160 
161   @Test
162   public void testCreateWithCorrectOwner() throws Exception {
163     // Create a test user
164     final User testUser = User.createUserForTesting(TEST_UTIL.getConfiguration(), "TestUser",
165       new String[0]);
166     // Grant the test user the ability to create tables
167     SecureTestUtil.grantGlobal(TEST_UTIL, testUser.getShortName(), Action.CREATE);
168     verifyAllowed(new AccessTestAction() {
169       @Override
170       public Object run() throws Exception {
171         HTableDescriptor desc = new HTableDescriptor(TEST_TABLE.getTableName());
172         desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
173         try (Connection connection =
174             ConnectionFactory.createConnection(TEST_UTIL.getConfiguration(), testUser)) {
175           try (Admin admin = connection.getAdmin()) {
176             createTable(TEST_UTIL, admin, desc);
177           }
178         }
179         return null;
180       }
181     }, testUser);
182     TEST_UTIL.waitTableAvailable(TEST_TABLE.getTableName());
183     // Verify that owner permissions have been granted to the test user on the
184     // table just created
185     List<TablePermission> perms =
186       AccessControlLists.getTablePermissions(conf, TEST_TABLE.getTableName())
187        .get(testUser.getShortName());
188     assertNotNull(perms);
189     assertFalse(perms.isEmpty());
190     // Should be RWXCA
191     assertTrue(perms.get(0).implies(Permission.Action.READ));
192     assertTrue(perms.get(0).implies(Permission.Action.WRITE));
193     assertTrue(perms.get(0).implies(Permission.Action.EXEC));
194     assertTrue(perms.get(0).implies(Permission.Action.CREATE));
195     assertTrue(perms.get(0).implies(Permission.Action.ADMIN));
196   }
197 
198   @Test
199   public void testACLTableAccess() throws Exception {
200     final Configuration conf = TEST_UTIL.getConfiguration();
201 
202     // Superuser
203     User superUser = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
204 
205     // Global users
206     User globalRead = User.createUserForTesting(conf, "globalRead", new String[0]);
207     User globalWrite = User.createUserForTesting(conf, "globalWrite", new String[0]);
208     User globalCreate = User.createUserForTesting(conf, "globalCreate", new String[0]);
209     User globalAdmin = User.createUserForTesting(conf, "globalAdmin", new String[0]);
210     SecureTestUtil.grantGlobal(TEST_UTIL, globalRead.getShortName(), Action.READ);
211     SecureTestUtil.grantGlobal(TEST_UTIL, globalWrite.getShortName(), Action.WRITE);
212     SecureTestUtil.grantGlobal(TEST_UTIL, globalCreate.getShortName(), Action.CREATE);
213     SecureTestUtil.grantGlobal(TEST_UTIL, globalAdmin.getShortName(), Action.ADMIN);
214 
215     // Namespace users
216     User nsRead = User.createUserForTesting(conf, "nsRead", new String[0]);
217     User nsWrite = User.createUserForTesting(conf, "nsWrite", new String[0]);
218     User nsCreate = User.createUserForTesting(conf, "nsCreate", new String[0]);
219     User nsAdmin = User.createUserForTesting(conf, "nsAdmin", new String[0]);
220     SecureTestUtil.grantOnNamespace(TEST_UTIL, nsRead.getShortName(),
221       TEST_TABLE.getTableName().getNamespaceAsString(), Action.READ);
222     SecureTestUtil.grantOnNamespace(TEST_UTIL, nsWrite.getShortName(),
223       TEST_TABLE.getTableName().getNamespaceAsString(), Action.WRITE);
224     SecureTestUtil.grantOnNamespace(TEST_UTIL, nsCreate.getShortName(),
225       TEST_TABLE.getTableName().getNamespaceAsString(), Action.CREATE);
226     SecureTestUtil.grantOnNamespace(TEST_UTIL, nsAdmin.getShortName(),
227       TEST_TABLE.getTableName().getNamespaceAsString(), Action.ADMIN);
228 
229     // Table users
230     User tableRead = User.createUserForTesting(conf, "tableRead", new String[0]);
231     User tableWrite = User.createUserForTesting(conf, "tableWrite", new String[0]);
232     User tableCreate = User.createUserForTesting(conf, "tableCreate", new String[0]);
233     User tableAdmin = User.createUserForTesting(conf, "tableAdmin", new String[0]);
234     SecureTestUtil.grantOnTable(TEST_UTIL, tableRead.getShortName(),
235       TEST_TABLE.getTableName(), null, null, Action.READ);
236     SecureTestUtil.grantOnTable(TEST_UTIL, tableWrite.getShortName(),
237       TEST_TABLE.getTableName(), null, null, Action.WRITE);
238     SecureTestUtil.grantOnTable(TEST_UTIL, tableCreate.getShortName(),
239       TEST_TABLE.getTableName(), null, null, Action.CREATE);
240     SecureTestUtil.grantOnTable(TEST_UTIL, tableAdmin.getShortName(),
241       TEST_TABLE.getTableName(), null, null, Action.ADMIN);
242 
243     // Write tests
244 
245     AccessTestAction writeAction = new AccessTestAction() {
246       @Override
247       public Object run() throws Exception {
248         try(Connection conn = ConnectionFactory.createConnection(conf);
249             Table t = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) {
250           t.put(new Put(TEST_ROW).add(AccessControlLists.ACL_LIST_FAMILY, TEST_QUALIFIER,
251             TEST_VALUE));
252           return null;
253         }
254       }
255     };
256 
257     // All writes to ACL table denied except for GLOBAL WRITE permission and superuser
258 
259     verifyDenied(writeAction, globalAdmin, globalCreate, globalRead);
260     verifyDenied(writeAction, nsAdmin, nsCreate, nsRead, nsWrite);
261     verifyDenied(writeAction, tableAdmin, tableCreate, tableRead, tableWrite);
262     verifyAllowed(writeAction, superUser, globalWrite);
263 
264     // Read tests
265 
266     AccessTestAction scanAction = new AccessTestAction() {
267       @Override
268       public Object run() throws Exception {
269         try(Connection conn = ConnectionFactory.createConnection(conf);
270             Table t = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) {
271           ResultScanner s = t.getScanner(new Scan());
272           try {
273             for (Result r = s.next(); r != null; r = s.next()) {
274               // do nothing
275             }
276           } finally {
277             s.close();
278           }
279           return null;
280         }
281       }
282     };
283 
284     // All reads from ACL table denied except for GLOBAL READ and superuser
285 
286     verifyDenied(scanAction, globalAdmin, globalCreate, globalWrite);
287     verifyDenied(scanAction, nsCreate, nsAdmin, nsRead, nsWrite);
288     verifyDenied(scanAction, tableCreate, tableAdmin, tableRead, tableWrite);
289     verifyAllowed(scanAction, superUser, globalRead);
290   }
291 
292   /*
293    * Test table scan operation at table, column family and column qualifier level.
294    */
295   @Test(timeout = 300000)
296   public void testPostGrantAndRevokeScanAction() throws Exception {
297     AccessTestAction scanTableActionForGroupWithTableLevelAccess = new AccessTestAction() {
298       @Override
299       public Void run() throws Exception {
300         try (Connection connection = ConnectionFactory.createConnection(conf);
301             Table table = connection.getTable(tableName);) {
302           Scan s1 = new Scan();
303           try (ResultScanner scanner1 = table.getScanner(s1);) {
304             Result[] next1 = scanner1.next(5);
305             assertTrue("User having table level access should be able to scan all "
306                 + "the data in the table.", next1.length == 3);
307           }
308         }
309         return null;
310       }
311     };
312 
313     AccessTestAction scanTableActionForGroupWithFamilyLevelAccess = new AccessTestAction() {
314       @Override
315       public Void run() throws Exception {
316         try (Connection connection = ConnectionFactory.createConnection(conf);
317             Table table = connection.getTable(tableName);) {
318           Scan s1 = new Scan();
319           try (ResultScanner scanner1 = table.getScanner(s1);) {
320             Result[] next1 = scanner1.next(5);
321             assertTrue("User having column family level access should be able to scan all "
322                 + "the data belonging to that family.", next1.length == 2);
323           }
324         }
325         return null;
326       }
327     };
328 
329     AccessTestAction scanFamilyActionForGroupWithFamilyLevelAccess = new AccessTestAction() {
330       @Override
331       public Void run() throws Exception {
332         try (Connection connection = ConnectionFactory.createConnection(conf);
333             Table table = connection.getTable(tableName);) {
334           Scan s1 = new Scan();
335           s1.addFamily(TEST_FAMILY_2);
336           try (ResultScanner scanner1 = table.getScanner(s1);) {
337           }
338         }
339         return null;
340       }
341     };
342 
343     AccessTestAction scanTableActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
344       @Override
345       public Void run() throws Exception {
346         try (Connection connection = ConnectionFactory.createConnection(conf);
347             Table table = connection.getTable(tableName);) {
348           Scan s1 = new Scan();
349           try (ResultScanner scanner1 = table.getScanner(s1);) {
350             Result[] next1 = scanner1.next(5);
351             assertTrue("User having column qualifier level access should be able to scan "
352                 + "that column family qualifier data.", next1.length == 1);
353           }
354         }
355         return null;
356       }
357     };
358 
359     AccessTestAction scanFamilyActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
360       @Override
361       public Void run() throws Exception {
362         try (Connection connection = ConnectionFactory.createConnection(conf);
363             Table table = connection.getTable(tableName);) {
364           Scan s1 = new Scan();
365           s1.addFamily(TEST_FAMILY_2);
366           try (ResultScanner scanner1 = table.getScanner(s1);) {
367           }
368         }
369         return null;
370       }
371     };
372 
373     AccessTestAction scanQualifierActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
374       @Override
375       public Void run() throws Exception {
376         try (Connection connection = ConnectionFactory.createConnection(conf);
377             Table table = connection.getTable(tableName);) {
378           Scan s1 = new Scan();
379           s1.addColumn(TEST_FAMILY, Q2);
380           try (ResultScanner scanner1 = table.getScanner(s1);) {
381           }
382         }
383         return null;
384       }
385     };
386 
387     // Verify user from a group which has table level access can read all the data and group which
388     // has no access can't read any data.
389     grantOnTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, null, null, Action.READ);
390     verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithTableLevelAccess);
391     verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithTableLevelAccess);
392 
393     // Verify user from a group whose table level access has been revoked can't read any data.
394     revokeFromTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, null, null);
395     verifyDenied(TESTGROUP1_USER1, scanTableActionForGroupWithTableLevelAccess);
396 
397     // Verify user from a group which has column family level access can read all the data
398     // belonging to that family and group which has no access can't read any data.
399     grantOnTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, TEST_FAMILY, null,
400       Permission.Action.READ);
401     verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithFamilyLevelAccess);
402     verifyDenied(TESTGROUP1_USER1, scanFamilyActionForGroupWithFamilyLevelAccess);
403     verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithFamilyLevelAccess);
404     verifyDenied(TESTGROUP2_USER1, scanFamilyActionForGroupWithFamilyLevelAccess);
405 
406     // Verify user from a group whose column family level access has been revoked can't read any
407     // data from that family.
408     revokeFromTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, TEST_FAMILY, null);
409     verifyDenied(TESTGROUP1_USER1, scanTableActionForGroupWithFamilyLevelAccess);
410 
411     // Verify user from a group which has column qualifier level access can read data that has this
412     // family and qualifier, and group which has no access can't read any data.
413     grantOnTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, TEST_FAMILY, Q1, Action.READ);
414     verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithQualifierLevelAccess);
415     verifyDenied(TESTGROUP1_USER1, scanFamilyActionForGroupWithQualifierLevelAccess);
416     verifyDenied(TESTGROUP1_USER1, scanQualifierActionForGroupWithQualifierLevelAccess);
417     verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithQualifierLevelAccess);
418     verifyDenied(TESTGROUP2_USER1, scanFamilyActionForGroupWithQualifierLevelAccess);
419     verifyDenied(TESTGROUP2_USER1, scanQualifierActionForGroupWithQualifierLevelAccess);
420 
421     // Verify user from a group whose column qualifier level access has been revoked can't read the
422     // data having this column family and qualifier.
423     revokeFromTable(TEST_UTIL, convertToGroup(TESTGROUP_1), tableName, TEST_FAMILY, Q1);
424     verifyDenied(TESTGROUP1_USER1, scanTableActionForGroupWithQualifierLevelAccess);
425   }
426 
427   @Test
428   public void testACLZNodeDeletion() throws Exception {
429     String baseAclZNode = "/hbase/acl/";
430     String ns = "testACLZNodeDeletionNamespace";
431     NamespaceDescriptor desc = NamespaceDescriptor.create(ns).build();
432     createNamespace(TEST_UTIL, desc);
433 
434     final TableName table = TableName.valueOf(ns, "testACLZNodeDeletionTable");
435     final byte[] family = Bytes.toBytes("f1");
436     HTableDescriptor htd = new HTableDescriptor(table);
437     htd.addFamily(new HColumnDescriptor(family));
438     createTable(TEST_UTIL, htd);
439 
440     // Namespace needs this, as they follow the lazy creation of ACL znode.
441     grantOnNamespace(TEST_UTIL, TESTGROUP1_USER1.getShortName(), ns, Action.ADMIN);
442     ZooKeeperWatcher zkw = TEST_UTIL.getMiniHBaseCluster().getMaster().getZooKeeper();
443     assertTrue("The acl znode for table should exist",  ZKUtil.checkExists(zkw, baseAclZNode +
444         table.getNameAsString()) != -1);
445     assertTrue("The acl znode for namespace should exist", ZKUtil.checkExists(zkw, baseAclZNode +
446         convertToNamespace(ns)) != -1);
447 
448     revokeFromNamespace(TEST_UTIL, TESTGROUP1_USER1.getShortName(), ns, Action.ADMIN);
449     deleteTable(TEST_UTIL, table);
450     deleteNamespace(TEST_UTIL, ns);
451 
452     assertTrue("The acl znode for table should have been deleted",
453         ZKUtil.checkExists(zkw, baseAclZNode + table.getNameAsString()) == -1);
454     assertTrue( "The acl znode for namespace should have been deleted",
455         ZKUtil.checkExists(zkw, baseAclZNode + convertToNamespace(ns)) == -1);
456   }
457 }