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.apache.hadoop.hbase.AuthUtil.toGroupEntry;
021import static org.junit.jupiter.api.Assertions.assertArrayEquals;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertFalse;
024import static org.junit.jupiter.api.Assertions.assertNull;
025import static org.junit.jupiter.api.Assertions.assertTrue;
026
027import java.util.Arrays;
028import java.util.List;
029import java.util.stream.Collectors;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.Coprocessor;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.NamespaceDescriptor;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.TableNameTestExtension;
037import org.apache.hadoop.hbase.TableNotFoundException;
038import org.apache.hadoop.hbase.client.Admin;
039import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
040import org.apache.hadoop.hbase.client.Connection;
041import org.apache.hadoop.hbase.client.ConnectionFactory;
042import org.apache.hadoop.hbase.client.Get;
043import org.apache.hadoop.hbase.client.Put;
044import org.apache.hadoop.hbase.client.Result;
045import org.apache.hadoop.hbase.client.ResultScanner;
046import org.apache.hadoop.hbase.client.Scan;
047import org.apache.hadoop.hbase.client.Table;
048import org.apache.hadoop.hbase.client.TableDescriptor;
049import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
050import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
051import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
052import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
053import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost;
054import org.apache.hadoop.hbase.security.User;
055import org.apache.hadoop.hbase.security.access.Permission.Action;
056import org.apache.hadoop.hbase.testclassification.MediumTests;
057import org.apache.hadoop.hbase.testclassification.SecurityTests;
058import org.apache.hadoop.hbase.util.Bytes;
059import org.apache.hadoop.hbase.zookeeper.ZKUtil;
060import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
061import org.junit.jupiter.api.AfterAll;
062import org.junit.jupiter.api.AfterEach;
063import org.junit.jupiter.api.BeforeAll;
064import org.junit.jupiter.api.BeforeEach;
065import org.junit.jupiter.api.Tag;
066import org.junit.jupiter.api.Test;
067import org.junit.jupiter.api.extension.RegisterExtension;
068import org.slf4j.Logger;
069import org.slf4j.LoggerFactory;
070
071@Tag(SecurityTests.TAG)
072@Tag(MediumTests.TAG)
073public class TestAccessController2 extends SecureTestUtil {
074
075  private static final Logger LOG = LoggerFactory.getLogger(TestAccessController2.class);
076
077  private static final byte[] TEST_ROW = Bytes.toBytes("test");
078  private static final byte[] TEST_FAMILY = Bytes.toBytes("f");
079  private static final byte[] TEST_QUALIFIER = Bytes.toBytes("q");
080  private static final byte[] TEST_VALUE = Bytes.toBytes("value");
081
082  private static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
083  private static Configuration conf;
084
085  /**
086   * The systemUserConnection created here is tied to the system user. In case, you are planning to
087   * create AccessTestAction, DON'T use this systemUserConnection as the 'doAs' user gets eclipsed
088   * by the system user.
089   */
090  private static Connection systemUserConnection;
091
092  private final static byte[] Q1 = Bytes.toBytes("q1");
093  private final static byte[] value1 = Bytes.toBytes("value1");
094
095  private static byte[] TEST_FAMILY_2 = Bytes.toBytes("f2");
096  private static byte[] TEST_ROW_2 = Bytes.toBytes("r2");
097  private final static byte[] Q2 = Bytes.toBytes("q2");
098  private final static byte[] value2 = Bytes.toBytes("value2");
099
100  private static byte[] TEST_ROW_3 = Bytes.toBytes("r3");
101
102  private static final String TESTGROUP_1 = "testgroup_1";
103  private static final String TESTGROUP_2 = "testgroup_2";
104
105  private static User TESTGROUP1_USER1;
106  private static User TESTGROUP2_USER1;
107
108  @RegisterExtension
109  public TableNameTestExtension testTable = new TableNameTestExtension();
110  private String namespace = "testNamespace";
111  private String tname = namespace + ":testtable1";
112  private TableName tableName = TableName.valueOf(tname);
113  private static String TESTGROUP_1_NAME;
114
115  @BeforeAll
116  public static void setupBeforeClass() throws Exception {
117    conf = TEST_UTIL.getConfiguration();
118    // Up the handlers; this test needs more than usual.
119    conf.setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 10);
120    // Enable security
121    enableSecurity(conf);
122    // Verify enableSecurity sets up what we require
123    verifyConfiguration(conf);
124    TEST_UTIL.startMiniCluster();
125    // Wait for the ACL table to become available
126    TEST_UTIL.waitUntilAllRegionsAssigned(PermissionStorage.ACL_TABLE_NAME);
127
128    TESTGROUP_1_NAME = toGroupEntry(TESTGROUP_1);
129    TESTGROUP1_USER1 =
130      User.createUserForTesting(conf, "testgroup1_user1", new String[] { TESTGROUP_1 });
131    TESTGROUP2_USER1 =
132      User.createUserForTesting(conf, "testgroup2_user2", new String[] { TESTGROUP_2 });
133
134    systemUserConnection = ConnectionFactory.createConnection(conf);
135  }
136
137  @BeforeEach
138  public void setUp() throws Exception {
139    createNamespace(TEST_UTIL, NamespaceDescriptor.create(namespace).build());
140    try (Table table =
141      createTable(TEST_UTIL, tableName, new byte[][] { TEST_FAMILY, TEST_FAMILY_2 })) {
142      TEST_UTIL.waitTableEnabled(tableName);
143
144      // Ingesting test data.
145      table.put(Arrays.asList(new Put(TEST_ROW).addColumn(TEST_FAMILY, Q1, value1),
146        new Put(TEST_ROW_2).addColumn(TEST_FAMILY, Q2, value2),
147        new Put(TEST_ROW_3).addColumn(TEST_FAMILY_2, Q1, value1)));
148    }
149
150    assertEquals(1,
151      PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), tableName).size());
152    try {
153      assertEquals(1,
154        AccessControlClient.getUserPermissions(systemUserConnection, tableName.toString()).size());
155    } catch (Throwable e) {
156      LOG.error("Error during call of AccessControlClient.getUserPermissions. ", e);
157    }
158
159  }
160
161  @AfterAll
162  public static void tearDownAfterClass() throws Exception {
163    systemUserConnection.close();
164    TEST_UTIL.shutdownMiniCluster();
165  }
166
167  @AfterEach
168  public void tearDown() throws Exception {
169    // Clean the _acl_ table
170    try {
171      deleteTable(TEST_UTIL, tableName);
172    } catch (TableNotFoundException ex) {
173      // Test deleted the table, no problem
174      LOG.info("Test deleted table " + tableName);
175    }
176    deleteNamespace(TEST_UTIL, namespace);
177    // Verify all table/namespace permissions are erased
178    assertEquals(0,
179      PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), tableName).size());
180    assertEquals(0,
181      PermissionStorage.getNamespacePermissions(TEST_UTIL.getConnection(), namespace).size());
182  }
183
184  @Test
185  public void testCreateWithCorrectOwner() throws Exception {
186    // Create a test user
187    final User testUser =
188      User.createUserForTesting(TEST_UTIL.getConfiguration(), "TestUser", new String[0]);
189    // Grant the test user the ability to create tables
190    SecureTestUtil.grantGlobal(TEST_UTIL, testUser.getShortName(), Action.CREATE);
191    verifyAllowed(new AccessTestAction() {
192      @Override
193      public Object run() throws Exception {
194        TableDescriptor tableDescriptor =
195          TableDescriptorBuilder.newBuilder(testTable.getTableName())
196            .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build();
197        try (Connection connection =
198          ConnectionFactory.createConnection(TEST_UTIL.getConfiguration(), testUser)) {
199          try (Admin admin = connection.getAdmin()) {
200            createTable(TEST_UTIL, admin, tableDescriptor);
201          }
202        }
203        return null;
204      }
205    }, testUser);
206    TEST_UTIL.waitTableAvailable(testTable.getTableName());
207    // Verify that owner permissions have been granted to the test user on the
208    // table just created
209    List<UserPermission> perms = PermissionStorage
210      .getTablePermissions(TEST_UTIL.getConnection(), testTable.getTableName()).stream()
211      .filter(up -> up.getUser().equals(testUser.getShortName())).collect(Collectors.toList());
212    assertFalse(perms.isEmpty());
213    // Should be RWXCA
214    assertTrue(perms.get(0).getPermission().implies(Permission.Action.READ));
215    assertTrue(perms.get(0).getPermission().implies(Permission.Action.WRITE));
216    assertTrue(perms.get(0).getPermission().implies(Permission.Action.EXEC));
217    assertTrue(perms.get(0).getPermission().implies(Permission.Action.CREATE));
218    assertTrue(perms.get(0).getPermission().implies(Permission.Action.ADMIN));
219  }
220
221  @Test
222  public void testCreateTableWithGroupPermissions() throws Exception {
223    grantGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.CREATE);
224    try {
225      AccessTestAction createAction = new AccessTestAction() {
226        @Override
227        public Object run() throws Exception {
228          TableDescriptor tableDescriptor =
229            TableDescriptorBuilder.newBuilder(testTable.getTableName())
230              .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build();
231          try (Connection connection =
232            ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
233            try (Admin admin = connection.getAdmin()) {
234              admin.createTable(tableDescriptor);
235            }
236          }
237          return null;
238        }
239      };
240      verifyAllowed(createAction, TESTGROUP1_USER1);
241      verifyDenied(createAction, TESTGROUP2_USER1);
242    } finally {
243      revokeGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.CREATE);
244    }
245  }
246
247  @Test
248  public void testACLTableAccess() throws Exception {
249    final Configuration conf = TEST_UTIL.getConfiguration();
250
251    // Superuser
252    User superUser = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
253
254    // Global users
255    User globalRead = User.createUserForTesting(conf, "globalRead", new String[0]);
256    User globalWrite = User.createUserForTesting(conf, "globalWrite", new String[0]);
257    User globalCreate = User.createUserForTesting(conf, "globalCreate", new String[0]);
258    User globalAdmin = User.createUserForTesting(conf, "globalAdmin", new String[0]);
259    SecureTestUtil.grantGlobal(TEST_UTIL, globalRead.getShortName(), Action.READ);
260    SecureTestUtil.grantGlobal(TEST_UTIL, globalWrite.getShortName(), Action.WRITE);
261    SecureTestUtil.grantGlobal(TEST_UTIL, globalCreate.getShortName(), Action.CREATE);
262    SecureTestUtil.grantGlobal(TEST_UTIL, globalAdmin.getShortName(), Action.ADMIN);
263
264    // Namespace users
265    User nsRead = User.createUserForTesting(conf, "nsRead", new String[0]);
266    User nsWrite = User.createUserForTesting(conf, "nsWrite", new String[0]);
267    User nsCreate = User.createUserForTesting(conf, "nsCreate", new String[0]);
268    User nsAdmin = User.createUserForTesting(conf, "nsAdmin", new String[0]);
269    SecureTestUtil.grantOnNamespace(TEST_UTIL, nsRead.getShortName(),
270      testTable.getTableName().getNamespaceAsString(), Action.READ);
271    SecureTestUtil.grantOnNamespace(TEST_UTIL, nsWrite.getShortName(),
272      testTable.getTableName().getNamespaceAsString(), Action.WRITE);
273    SecureTestUtil.grantOnNamespace(TEST_UTIL, nsCreate.getShortName(),
274      testTable.getTableName().getNamespaceAsString(), Action.CREATE);
275    SecureTestUtil.grantOnNamespace(TEST_UTIL, nsAdmin.getShortName(),
276      testTable.getTableName().getNamespaceAsString(), Action.ADMIN);
277
278    // Table users
279    User tableRead = User.createUserForTesting(conf, "tableRead", new String[0]);
280    User tableWrite = User.createUserForTesting(conf, "tableWrite", new String[0]);
281    User tableCreate = User.createUserForTesting(conf, "tableCreate", new String[0]);
282    User tableAdmin = User.createUserForTesting(conf, "tableAdmin", new String[0]);
283    SecureTestUtil.grantOnTable(TEST_UTIL, tableRead.getShortName(), testTable.getTableName(), null,
284      null, Action.READ);
285    SecureTestUtil.grantOnTable(TEST_UTIL, tableWrite.getShortName(), testTable.getTableName(),
286      null, null, Action.WRITE);
287    SecureTestUtil.grantOnTable(TEST_UTIL, tableCreate.getShortName(), testTable.getTableName(),
288      null, null, Action.CREATE);
289    SecureTestUtil.grantOnTable(TEST_UTIL, tableAdmin.getShortName(), testTable.getTableName(),
290      null, null, Action.ADMIN);
291
292    grantGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.WRITE);
293    try {
294      // Write tests
295
296      AccessTestAction writeAction = new AccessTestAction() {
297        @Override
298        public Object run() throws Exception {
299
300          try (Connection conn = ConnectionFactory.createConnection(conf);
301            Table t = conn.getTable(PermissionStorage.ACL_TABLE_NAME)) {
302            t.put(new Put(TEST_ROW).addColumn(PermissionStorage.ACL_LIST_FAMILY, TEST_QUALIFIER,
303              TEST_VALUE));
304            return null;
305          } finally {
306          }
307        }
308      };
309
310      // All writes to ACL table denied except for GLOBAL WRITE permission and superuser
311
312      verifyDenied(writeAction, globalAdmin, globalCreate, globalRead, TESTGROUP2_USER1);
313      verifyDenied(writeAction, nsAdmin, nsCreate, nsRead, nsWrite);
314      verifyDenied(writeAction, tableAdmin, tableCreate, tableRead, tableWrite);
315      verifyAllowed(writeAction, superUser, globalWrite, TESTGROUP1_USER1);
316    } finally {
317      revokeGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.WRITE);
318    }
319
320    grantGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.READ);
321    try {
322      // Read tests
323
324      AccessTestAction scanAction = new AccessTestAction() {
325        @Override
326        public Object run() throws Exception {
327          try (Connection conn = ConnectionFactory.createConnection(conf);
328            Table t = conn.getTable(PermissionStorage.ACL_TABLE_NAME)) {
329            ResultScanner s = t.getScanner(new Scan());
330            try {
331              for (Result r = s.next(); r != null; r = s.next()) {
332                // do nothing
333              }
334            } finally {
335              s.close();
336            }
337            return null;
338          }
339        }
340      };
341
342      // All reads from ACL table denied except for GLOBAL READ and superuser
343
344      verifyDenied(scanAction, globalAdmin, globalCreate, globalWrite, TESTGROUP2_USER1);
345      verifyDenied(scanAction, nsCreate, nsAdmin, nsRead, nsWrite);
346      verifyDenied(scanAction, tableCreate, tableAdmin, tableRead, tableWrite);
347      verifyAllowed(scanAction, superUser, globalRead, TESTGROUP1_USER1);
348    } finally {
349      revokeGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.READ);
350    }
351  }
352
353  /*
354   * Test table scan operation at table, column family and column qualifier level.
355   */
356  @Test
357  public void testPostGrantAndRevokeScanAction() throws Exception {
358    AccessTestAction scanTableActionForGroupWithTableLevelAccess = new AccessTestAction() {
359      @Override
360      public Void run() throws Exception {
361        try (Connection connection = ConnectionFactory.createConnection(conf);
362          Table table = connection.getTable(tableName)) {
363          Scan s1 = new Scan();
364          try (ResultScanner scanner1 = table.getScanner(s1)) {
365            Result[] next1 = scanner1.next(5);
366            assertTrue(next1.length == 3,
367              "User having table level access should be able to scan all "
368                + "the data in the table.");
369          }
370        }
371        return null;
372      }
373    };
374
375    AccessTestAction scanTableActionForGroupWithFamilyLevelAccess = new AccessTestAction() {
376      @Override
377      public Void run() throws Exception {
378        try (Connection connection = ConnectionFactory.createConnection(conf);
379          Table table = connection.getTable(tableName)) {
380          Scan s1 = new Scan();
381          try (ResultScanner scanner1 = table.getScanner(s1)) {
382            Result[] next1 = scanner1.next(5);
383            assertTrue(next1.length == 2,
384              "User having column family level access should be able to scan all "
385                + "the data belonging to that family.");
386          }
387        }
388        return null;
389      }
390    };
391
392    AccessTestAction scanFamilyActionForGroupWithFamilyLevelAccess = new AccessTestAction() {
393      @Override
394      public Void run() throws Exception {
395        try (Connection connection = ConnectionFactory.createConnection(conf);
396          Table table = connection.getTable(tableName)) {
397          Scan s1 = new Scan();
398          s1.addFamily(TEST_FAMILY_2);
399          try (ResultScanner scanner1 = table.getScanner(s1)) {
400            scanner1.next();
401          }
402        }
403        return null;
404      }
405    };
406
407    AccessTestAction scanTableActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
408      @Override
409      public Void run() throws Exception {
410        try (Connection connection = ConnectionFactory.createConnection(conf);
411          Table table = connection.getTable(tableName)) {
412          Scan s1 = new Scan();
413          try (ResultScanner scanner1 = table.getScanner(s1)) {
414            Result[] next1 = scanner1.next(5);
415            assertTrue(next1.length == 1,
416              "User having column qualifier level access should be able to scan "
417                + "that column family qualifier data.");
418          }
419        }
420        return null;
421      }
422    };
423
424    AccessTestAction scanFamilyActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
425      @Override
426      public Void run() throws Exception {
427        try (Connection connection = ConnectionFactory.createConnection(conf);
428          Table table = connection.getTable(tableName)) {
429          Scan s1 = new Scan();
430          s1.addFamily(TEST_FAMILY_2);
431          try (ResultScanner scanner1 = table.getScanner(s1)) {
432            scanner1.next();
433          }
434        }
435        return null;
436      }
437    };
438
439    AccessTestAction scanQualifierActionForGroupWithQualifierLevelAccess = new AccessTestAction() {
440      @Override
441      public Void run() throws Exception {
442        try (Connection connection = ConnectionFactory.createConnection(conf);
443          Table table = connection.getTable(tableName)) {
444          Scan s1 = new Scan();
445          s1.addColumn(TEST_FAMILY, Q2);
446          try (ResultScanner scanner1 = table.getScanner(s1)) {
447            scanner1.next();
448          }
449        }
450        return null;
451      }
452    };
453
454    // Verify user from a group which has table level access can read all the data and group which
455    // has no access can't read any data.
456    grantOnTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, null, null, Action.READ);
457    verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithTableLevelAccess);
458    verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithTableLevelAccess);
459
460    // Verify user from a group whose table level access has been revoked can't read any data.
461    revokeFromTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, null, null);
462    verifyDenied(TESTGROUP1_USER1, scanTableActionForGroupWithTableLevelAccess);
463
464    // Verify user from a group which has column family level access can read all the data
465    // belonging to that family and group which has no access can't read any data.
466    grantOnTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, TEST_FAMILY, null, Permission.Action.READ);
467    verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithFamilyLevelAccess);
468    verifyDenied(TESTGROUP1_USER1, scanFamilyActionForGroupWithFamilyLevelAccess);
469    verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithFamilyLevelAccess);
470    verifyDenied(TESTGROUP2_USER1, scanFamilyActionForGroupWithFamilyLevelAccess);
471
472    // Verify user from a group whose column family level access has been revoked can't read any
473    // data from that family.
474    revokeFromTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, TEST_FAMILY, null);
475    verifyDenied(TESTGROUP1_USER1, scanTableActionForGroupWithFamilyLevelAccess);
476
477    // Verify user from a group which has column qualifier level access can read data that has this
478    // family and qualifier, and group which has no access can't read any data.
479    grantOnTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, TEST_FAMILY, Q1, Action.READ);
480    verifyAllowed(TESTGROUP1_USER1, scanTableActionForGroupWithQualifierLevelAccess);
481    verifyDenied(TESTGROUP1_USER1, scanFamilyActionForGroupWithQualifierLevelAccess);
482    verifyDenied(TESTGROUP1_USER1, scanQualifierActionForGroupWithQualifierLevelAccess);
483    verifyDenied(TESTGROUP2_USER1, scanTableActionForGroupWithQualifierLevelAccess);
484    verifyDenied(TESTGROUP2_USER1, scanFamilyActionForGroupWithQualifierLevelAccess);
485    verifyDenied(TESTGROUP2_USER1, scanQualifierActionForGroupWithQualifierLevelAccess);
486
487    // Verify user from a group whose column qualifier level access has been revoked can't read the
488    // data having this column family and qualifier.
489    revokeFromTable(TEST_UTIL, TESTGROUP_1_NAME, tableName, TEST_FAMILY, Q1);
490    verifyDenied(TESTGROUP1_USER1, scanQualifierActionForGroupWithQualifierLevelAccess);
491  }
492
493  @Test
494  public void testRevokeFamilyActionKeepsTableLevelPermission() throws Throwable {
495    User user = User.createUserForTesting(conf, "revoke_family_user", new String[0]);
496
497    AccessTestAction putFamilyAction = new AccessTestAction() {
498      @Override
499      public Object run() throws Exception {
500        try (Connection connection = ConnectionFactory.createConnection(conf);
501          Table table = connection.getTable(tableName)) {
502          table.put(new Put(TEST_ROW).addColumn(TEST_FAMILY, Q1, value1));
503        }
504        return null;
505      }
506    };
507
508    AccessTestAction getFamilyAction = new AccessTestAction() {
509      @Override
510      public Object run() throws Exception {
511        try (Connection connection = ConnectionFactory.createConnection(conf);
512          Table table = connection.getTable(tableName)) {
513          table.get(new Get(TEST_ROW).addColumn(TEST_FAMILY, Q1));
514        }
515        return null;
516      }
517    };
518
519    // Client side grants: table level READ plus family level WRITE on TEST_FAMILY
520    grantOnTable(TEST_UTIL, user.getShortName(), tableName, null, null, Action.READ);
521    grantOnTable(TEST_UTIL, user.getShortName(), tableName, TEST_FAMILY, null, Action.WRITE);
522    verifyAllowed(user, getFamilyAction);
523    verifyAllowed(user, putFamilyAction);
524
525    // Client side revoke of the family level WRITE only
526    revokeFromTable(TEST_UTIL, user.getShortName(), tableName, TEST_FAMILY, null, Action.WRITE);
527
528    // Only the table level READ should remain
529    List<UserPermission> userPerms = AccessControlClient.getUserPermissions(systemUserConnection,
530      tableName.getNameAsString(), user.getShortName());
531    assertEquals(1, userPerms.size(),
532      "family level WRITE should be revoked, only the table level READ should remain");
533    TablePermission permission = (TablePermission) userPerms.get(0).getPermission();
534    assertNull(permission.getFamily(), "table level permission should be kept untouched");
535    assertArrayEquals(new Permission.Action[] { Action.READ }, permission.getActions(),
536      "table level READ should be kept untouched");
537
538    // Enforcement: reading still works, writing to the family must now be denied
539    verifyAllowed(user, getFamilyAction);
540    verifyDenied(user, putFamilyAction);
541  }
542
543  public static class MyAccessController extends AccessController {
544  }
545
546  @Test
547  public void testCoprocessorLoading() throws Exception {
548    MasterCoprocessorHost cpHost =
549      TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterCoprocessorHost();
550    cpHost.load(MyAccessController.class, Coprocessor.PRIORITY_HIGHEST, conf);
551    AccessController ACCESS_CONTROLLER = cpHost.findCoprocessor(MyAccessController.class);
552    MasterCoprocessorEnvironment CP_ENV =
553      cpHost.createEnvironment(ACCESS_CONTROLLER, Coprocessor.PRIORITY_HIGHEST, 1, conf);
554    RegionServerCoprocessorHost rsHost =
555      TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).getRegionServerCoprocessorHost();
556    RegionServerCoprocessorEnvironment RSCP_ENV =
557      rsHost.createEnvironment(ACCESS_CONTROLLER, Coprocessor.PRIORITY_HIGHEST, 1, conf);
558  }
559
560  @Test
561  public void testACLZNodeDeletion() throws Exception {
562    String baseAclZNode = "/hbase/acl/";
563    String ns = "testACLZNodeDeletionNamespace";
564    NamespaceDescriptor desc = NamespaceDescriptor.create(ns).build();
565    createNamespace(TEST_UTIL, desc);
566
567    final TableName table = TableName.valueOf(ns, "testACLZNodeDeletionTable");
568    final byte[] family = Bytes.toBytes("f1");
569    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(table)
570      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
571    createTable(TEST_UTIL, tableDescriptor);
572
573    // Namespace needs this, as they follow the lazy creation of ACL znode.
574    grantOnNamespace(TEST_UTIL, TESTGROUP1_USER1.getShortName(), ns, Action.ADMIN);
575    ZKWatcher zkw = TEST_UTIL.getMiniHBaseCluster().getMaster().getZooKeeper();
576    assertTrue(ZKUtil.checkExists(zkw, baseAclZNode + table.getNameAsString()) != -1,
577      "The acl znode for table should exist");
578    assertTrue(ZKUtil.checkExists(zkw, baseAclZNode + convertToNamespace(ns)) != -1,
579      "The acl znode for namespace should exist");
580
581    revokeFromNamespace(TEST_UTIL, TESTGROUP1_USER1.getShortName(), ns, Action.ADMIN);
582    deleteTable(TEST_UTIL, table);
583    deleteNamespace(TEST_UTIL, ns);
584
585    assertTrue(ZKUtil.checkExists(zkw, baseAclZNode + table.getNameAsString()) == -1,
586      "The acl znode for table should have been deleted");
587    assertTrue(ZKUtil.checkExists(zkw, baseAclZNode + convertToNamespace(ns)) == -1,
588      "The acl znode for namespace should have been deleted");
589  }
590}