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.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import java.util.Arrays;
026import java.util.List;
027import java.util.Map;
028import java.util.Objects;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.NamespaceDescriptor;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Admin;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
036import org.apache.hadoop.hbase.client.Connection;
037import org.apache.hadoop.hbase.client.ConnectionFactory;
038import org.apache.hadoop.hbase.client.Get;
039import org.apache.hadoop.hbase.client.Result;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.client.TableDescriptor;
042import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
043import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
044import org.apache.hadoop.hbase.coprocessor.ObserverContextImpl;
045import org.apache.hadoop.hbase.ipc.NettyRpcClientConfigHelper;
046import org.apache.hadoop.hbase.security.User;
047import org.apache.hadoop.hbase.security.access.Permission.Action;
048import org.apache.hadoop.hbase.testclassification.MediumTests;
049import org.apache.hadoop.hbase.testclassification.SecurityTests;
050import org.apache.hadoop.hbase.util.Bytes;
051import org.apache.hadoop.hbase.util.JVMClusterUtil;
052import org.junit.jupiter.api.AfterAll;
053import org.junit.jupiter.api.BeforeAll;
054import org.junit.jupiter.api.Tag;
055import org.junit.jupiter.api.Test;
056import org.slf4j.Logger;
057import org.slf4j.LoggerFactory;
058
059import org.apache.hbase.thirdparty.com.google.common.collect.ArrayListMultimap;
060import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap;
061import org.apache.hbase.thirdparty.com.google.protobuf.BlockingRpcChannel;
062
063import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.AccessControlService;
064
065@Tag(SecurityTests.TAG)
066@Tag(MediumTests.TAG)
067public class TestNamespaceCommands extends SecureTestUtil {
068
069  private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
070  private static final Logger LOG = LoggerFactory.getLogger(TestNamespaceCommands.class);
071  private static String TEST_NAMESPACE = "ns1";
072  private static String TEST_NAMESPACE2 = "ns2";
073  private static Configuration conf;
074  private static MasterCoprocessorEnvironment CP_ENV;
075  private static AccessController ACCESS_CONTROLLER;
076
077  // user with all permissions
078  private static User SUPERUSER;
079
080  // user with A permission on global
081  private static User USER_GLOBAL_ADMIN;
082  // user with C permission on global
083  private static User USER_GLOBAL_CREATE;
084  // user with W permission on global
085  private static User USER_GLOBAL_WRITE;
086  // user with R permission on global
087  private static User USER_GLOBAL_READ;
088  // user with X permission on global
089  private static User USER_GLOBAL_EXEC;
090
091  // user with A permission on namespace
092  private static User USER_NS_ADMIN;
093  // user with C permission on namespace
094  private static User USER_NS_CREATE;
095  // user with W permission on namespace
096  private static User USER_NS_WRITE;
097  // user with R permission on namespace.
098  private static User USER_NS_READ;
099  // user with X permission on namespace.
100  private static User USER_NS_EXEC;
101
102  // user with rw permissions
103  private static User USER_TABLE_WRITE; // TODO: WE DO NOT GIVE ANY PERMS TO THIS USER
104  // user with create table permissions alone
105  private static User USER_TABLE_CREATE; // TODO: WE DO NOT GIVE ANY PERMS TO THIS USER
106
107  private static final String GROUP_ADMIN = "group_admin";
108  private static final String GROUP_NS_ADMIN = "group_ns_admin";
109  private static final String GROUP_CREATE = "group_create";
110  private static final String GROUP_READ = "group_read";
111  private static final String GROUP_WRITE = "group_write";
112
113  private static User USER_GROUP_ADMIN;
114  private static User USER_GROUP_NS_ADMIN;
115  private static User USER_GROUP_CREATE;
116  private static User USER_GROUP_READ;
117  private static User USER_GROUP_WRITE;
118
119  private static String TEST_TABLE = TEST_NAMESPACE + ":testtable";
120  private static byte[] TEST_FAMILY = Bytes.toBytes("f1");
121
122  @BeforeAll
123  public static void beforeClass() throws Exception {
124    conf = UTIL.getConfiguration();
125    enableSecurity(conf);
126
127    SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
128    // Users with global permissions
129    USER_GLOBAL_ADMIN = User.createUserForTesting(conf, "global_admin", new String[0]);
130    USER_GLOBAL_CREATE = User.createUserForTesting(conf, "global_create", new String[0]);
131    USER_GLOBAL_WRITE = User.createUserForTesting(conf, "global_write", new String[0]);
132    USER_GLOBAL_READ = User.createUserForTesting(conf, "global_read", new String[0]);
133    USER_GLOBAL_EXEC = User.createUserForTesting(conf, "global_exec", new String[0]);
134
135    USER_NS_ADMIN = User.createUserForTesting(conf, "namespace_admin", new String[0]);
136    USER_NS_CREATE = User.createUserForTesting(conf, "namespace_create", new String[0]);
137    USER_NS_WRITE = User.createUserForTesting(conf, "namespace_write", new String[0]);
138    USER_NS_READ = User.createUserForTesting(conf, "namespace_read", new String[0]);
139    USER_NS_EXEC = User.createUserForTesting(conf, "namespace_exec", new String[0]);
140
141    USER_TABLE_CREATE = User.createUserForTesting(conf, "table_create", new String[0]);
142    USER_TABLE_WRITE = User.createUserForTesting(conf, "table_write", new String[0]);
143
144    USER_GROUP_ADMIN =
145      User.createUserForTesting(conf, "user_group_admin", new String[] { GROUP_ADMIN });
146    USER_GROUP_NS_ADMIN =
147      User.createUserForTesting(conf, "user_group_ns_admin", new String[] { GROUP_NS_ADMIN });
148    USER_GROUP_CREATE =
149      User.createUserForTesting(conf, "user_group_create", new String[] { GROUP_CREATE });
150    USER_GROUP_READ =
151      User.createUserForTesting(conf, "user_group_read", new String[] { GROUP_READ });
152    USER_GROUP_WRITE =
153      User.createUserForTesting(conf, "user_group_write", new String[] { GROUP_WRITE });
154    // TODO: other table perms
155
156    UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
157    UTIL.startMiniCluster();
158    // Wait for the ACL table to become available
159    UTIL.waitTableAvailable(PermissionStorage.ACL_TABLE_NAME.getName(), 30 * 1000);
160
161    // Find the Access Controller CP. Could be on master or if master is not serving regions, is
162    // on an arbitrary server.
163    for (JVMClusterUtil.RegionServerThread rst : UTIL.getMiniHBaseCluster()
164      .getLiveRegionServerThreads()) {
165      ACCESS_CONTROLLER = rst.getRegionServer().getRegionServerCoprocessorHost()
166        .findCoprocessor(AccessController.class);
167      if (ACCESS_CONTROLLER != null) {
168        break;
169      }
170    }
171    if (ACCESS_CONTROLLER == null) {
172      throw new NullPointerException();
173    }
174
175    UTIL.getAdmin().createNamespace(NamespaceDescriptor.create(TEST_NAMESPACE).build());
176    UTIL.getAdmin().createNamespace(NamespaceDescriptor.create(TEST_NAMESPACE2).build());
177
178    // grants on global
179    grantGlobal(UTIL, USER_GLOBAL_ADMIN.getShortName(), Permission.Action.ADMIN);
180    grantGlobal(UTIL, USER_GLOBAL_CREATE.getShortName(), Permission.Action.CREATE);
181    grantGlobal(UTIL, USER_GLOBAL_WRITE.getShortName(), Permission.Action.WRITE);
182    grantGlobal(UTIL, USER_GLOBAL_READ.getShortName(), Permission.Action.READ);
183    grantGlobal(UTIL, USER_GLOBAL_EXEC.getShortName(), Permission.Action.EXEC);
184
185    // grants on namespace
186    grantOnNamespace(UTIL, USER_NS_ADMIN.getShortName(), TEST_NAMESPACE, Permission.Action.ADMIN);
187    grantOnNamespace(UTIL, USER_NS_CREATE.getShortName(), TEST_NAMESPACE, Permission.Action.CREATE);
188    grantOnNamespace(UTIL, USER_NS_WRITE.getShortName(), TEST_NAMESPACE, Permission.Action.WRITE);
189    grantOnNamespace(UTIL, USER_NS_READ.getShortName(), TEST_NAMESPACE, Permission.Action.READ);
190    grantOnNamespace(UTIL, USER_NS_EXEC.getShortName(), TEST_NAMESPACE, Permission.Action.EXEC);
191    grantOnNamespace(UTIL, toGroupEntry(GROUP_NS_ADMIN), TEST_NAMESPACE, Permission.Action.ADMIN);
192
193    grantOnNamespace(UTIL, USER_NS_ADMIN.getShortName(), TEST_NAMESPACE2, Permission.Action.ADMIN);
194
195    grantGlobal(UTIL, toGroupEntry(GROUP_ADMIN), Permission.Action.ADMIN);
196    grantGlobal(UTIL, toGroupEntry(GROUP_CREATE), Permission.Action.CREATE);
197    grantGlobal(UTIL, toGroupEntry(GROUP_READ), Permission.Action.READ);
198    grantGlobal(UTIL, toGroupEntry(GROUP_WRITE), Permission.Action.WRITE);
199  }
200
201  @AfterAll
202  public static void afterClass() throws Exception {
203    UTIL.getAdmin().deleteNamespace(TEST_NAMESPACE);
204    UTIL.getAdmin().deleteNamespace(TEST_NAMESPACE2);
205    UTIL.shutdownMiniCluster();
206  }
207
208  private ListMultimap<String, UserPermission> getNsPerms(String namespace) throws IOException {
209    ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
210    PermissionStorage.getNamespacePermissions(UTIL.getConnection(), namespace)
211      .forEach(up -> perms.put(up.getUser(), up));
212    return perms;
213  }
214
215  @Test
216  public void testAclTableEntries() throws Exception {
217    String userTestNamespace = "userTestNsp";
218    Table acl = UTIL.getConnection().getTable(PermissionStorage.ACL_TABLE_NAME);
219    try {
220      ListMultimap<String, UserPermission> perms = getNsPerms(TEST_NAMESPACE);
221      for (Map.Entry<String, UserPermission> entry : perms.entries()) {
222        LOG.debug(Objects.toString(entry));
223      }
224      assertEquals(6, perms.size());
225
226      // Grant and check state in ACL table
227      grantOnNamespace(UTIL, userTestNamespace, TEST_NAMESPACE, Permission.Action.WRITE);
228
229      Result result = acl.get(new Get(Bytes.toBytes(userTestNamespace)));
230      assertTrue(result != null);
231      perms = getNsPerms(TEST_NAMESPACE);
232      assertEquals(7, perms.size());
233      List<UserPermission> namespacePerms = perms.get(userTestNamespace);
234      assertTrue(perms.containsKey(userTestNamespace));
235      assertEquals(1, namespacePerms.size());
236      assertEquals(TEST_NAMESPACE,
237        ((NamespacePermission) namespacePerms.get(0).getPermission()).getNamespace());
238      assertEquals(1, namespacePerms.get(0).getPermission().getActions().length);
239      assertEquals(Permission.Action.WRITE, namespacePerms.get(0).getPermission().getActions()[0]);
240
241      // Revoke and check state in ACL table
242      revokeFromNamespace(UTIL, userTestNamespace, TEST_NAMESPACE, Permission.Action.WRITE);
243
244      perms = getNsPerms(TEST_NAMESPACE);
245      assertEquals(6, perms.size());
246    } finally {
247      acl.close();
248    }
249  }
250
251  @Test
252  public void testModifyNamespace() throws Exception {
253    AccessTestAction modifyNamespace = new AccessTestAction() {
254      @Override
255      public Object run() throws Exception {
256        ACCESS_CONTROLLER.preModifyNamespace(ObserverContextImpl.createAndPrepare(CP_ENV), null, // not
257                                                                                                 // needed
258                                                                                                 // by
259                                                                                                 // AccessController
260          NamespaceDescriptor.create(TEST_NAMESPACE).addConfiguration("abc", "156").build());
261        return null;
262      }
263    };
264
265    // modifyNamespace: superuser | global(A) | NS(A)
266    verifyAllowed(modifyNamespace, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN);
267    verifyDenied(modifyNamespace, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
268      USER_GLOBAL_EXEC, USER_NS_ADMIN, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
269      USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
270  }
271
272  @Test
273  public void testCreateAndDeleteNamespace() throws Exception {
274    AccessTestAction createNamespace = new AccessTestAction() {
275      @Override
276      public Object run() throws Exception {
277        ACCESS_CONTROLLER.preCreateNamespace(ObserverContextImpl.createAndPrepare(CP_ENV),
278          NamespaceDescriptor.create(TEST_NAMESPACE2).build());
279        return null;
280      }
281    };
282
283    AccessTestAction deleteNamespace = new AccessTestAction() {
284      @Override
285      public Object run() throws Exception {
286        ACCESS_CONTROLLER.preDeleteNamespace(ObserverContextImpl.createAndPrepare(CP_ENV),
287          TEST_NAMESPACE2);
288        return null;
289      }
290    };
291
292    // createNamespace: superuser | global(A)
293    verifyAllowed(createNamespace, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN);
294    // all others should be denied
295    verifyDenied(createNamespace, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
296      USER_GLOBAL_EXEC, USER_NS_ADMIN, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
297      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
298
299    // deleteNamespace: superuser | global(A) | NS(A)
300    verifyAllowed(deleteNamespace, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN);
301    verifyDenied(deleteNamespace, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
302      USER_GLOBAL_EXEC, USER_NS_ADMIN, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
303      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
304  }
305
306  @Test
307  public void testGetNamespaceDescriptor() throws Exception {
308    AccessTestAction getNamespaceAction = new AccessTestAction() {
309      @Override
310      public Object run() throws Exception {
311        ACCESS_CONTROLLER.preGetNamespaceDescriptor(ObserverContextImpl.createAndPrepare(CP_ENV),
312          TEST_NAMESPACE);
313        return null;
314      }
315    };
316    // getNamespaceDescriptor : superuser | global(A) | NS(A)
317    verifyAllowed(getNamespaceAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_NS_ADMIN,
318      USER_GROUP_ADMIN);
319    verifyDenied(getNamespaceAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
320      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
321      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
322  }
323
324  @Test
325  public void testListNamespaces() throws Exception {
326    AccessTestAction listAction = new AccessTestAction() {
327      @Override
328      public Object run() throws Exception {
329        Connection unmanagedConnection =
330          ConnectionFactory.createConnection(UTIL.getConfiguration());
331        Admin admin = unmanagedConnection.getAdmin();
332        try {
333          return Arrays.asList(admin.listNamespaceDescriptors());
334        } finally {
335          admin.close();
336          unmanagedConnection.close();
337        }
338      }
339    };
340
341    // listNamespaces : All access*
342    // * Returned list will only show what you can call getNamespaceDescriptor()
343
344    verifyAllowed(listAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_NS_ADMIN, USER_GROUP_ADMIN);
345
346    // we have 3 namespaces: [default, hbase, TEST_NAMESPACE, TEST_NAMESPACE2]
347    assertEquals(4, ((List) SUPERUSER.runAs(listAction)).size());
348    assertEquals(4, ((List) USER_GLOBAL_ADMIN.runAs(listAction)).size());
349    assertEquals(4, ((List) USER_GROUP_ADMIN.runAs(listAction)).size());
350
351    assertEquals(2, ((List) USER_NS_ADMIN.runAs(listAction)).size());
352
353    assertEquals(0, ((List) USER_GLOBAL_CREATE.runAs(listAction)).size());
354    assertEquals(0, ((List) USER_GLOBAL_WRITE.runAs(listAction)).size());
355    assertEquals(0, ((List) USER_GLOBAL_READ.runAs(listAction)).size());
356    assertEquals(0, ((List) USER_GLOBAL_EXEC.runAs(listAction)).size());
357    assertEquals(0, ((List) USER_NS_CREATE.runAs(listAction)).size());
358    assertEquals(0, ((List) USER_NS_WRITE.runAs(listAction)).size());
359    assertEquals(0, ((List) USER_NS_READ.runAs(listAction)).size());
360    assertEquals(0, ((List) USER_NS_EXEC.runAs(listAction)).size());
361    assertEquals(0, ((List) USER_TABLE_CREATE.runAs(listAction)).size());
362    assertEquals(0, ((List) USER_TABLE_WRITE.runAs(listAction)).size());
363    assertEquals(0, ((List) USER_GROUP_CREATE.runAs(listAction)).size());
364    assertEquals(0, ((List) USER_GROUP_READ.runAs(listAction)).size());
365    assertEquals(0, ((List) USER_GROUP_WRITE.runAs(listAction)).size());
366  }
367
368  @SuppressWarnings("checkstyle:MethodLength")
369  @Test
370  public void testGrantRevoke() throws Exception {
371    final String testUser = "testUser";
372    // Set this else in test context, with limit on the number of threads for
373    // netty eventloopgroup, we can run out of threads if one group used throughout.
374    NettyRpcClientConfigHelper.createEventLoopPerClient(conf);
375    // Test if client API actions are authorized
376    AccessTestAction grantAction = new AccessTestAction() {
377      @Override
378      public Object run() throws Exception {
379        try (Connection connection = ConnectionFactory.createConnection(conf)) {
380          connection.getAdmin().grant(new UserPermission(testUser,
381            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.WRITE).build()), false);
382        }
383        return null;
384      }
385    };
386    AccessTestAction grantNamespaceAction = new AccessTestAction() {
387      @Override
388      public Object run() throws Exception {
389        try (Connection conn = ConnectionFactory.createConnection(conf)) {
390          conn.getAdmin().grant(new UserPermission(USER_GROUP_NS_ADMIN.getShortName(),
391            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.READ).build()), false);
392        }
393        return null;
394      }
395    };
396
397    AccessTestAction revokeAction = new AccessTestAction() {
398      @Override
399      public Object run() throws Exception {
400        try (Connection connection = ConnectionFactory.createConnection(conf)) {
401          connection.getAdmin().revoke(new UserPermission(testUser,
402            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.WRITE).build()));
403        }
404        return null;
405      }
406    };
407    AccessTestAction revokeNamespaceAction = new AccessTestAction() {
408      @Override
409      public Object run() throws Exception {
410        try (Connection connection = ConnectionFactory.createConnection(conf)) {
411          connection.getAdmin().revoke(new UserPermission(USER_GROUP_NS_ADMIN.getShortName(),
412            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.READ).build()));
413        }
414        return null;
415      }
416    };
417
418    AccessTestAction getPermissionsAction = new AccessTestAction() {
419      @Override
420      public Object run() throws Exception {
421        try (Connection connection = ConnectionFactory.createConnection(conf)) {
422          connection.getAdmin()
423            .getUserPermissions(GetUserPermissionsRequest.newBuilder(TEST_NAMESPACE).build());
424        }
425        return null;
426      }
427    };
428
429    AccessTestAction preGrantAction = new AccessTestAction() {
430      @Override
431      public Object run() throws Exception {
432        ACCESS_CONTROLLER.preGrant(ObserverContextImpl.createAndPrepare(CP_ENV),
433          new UserPermission(testUser,
434            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.WRITE).build()),
435          false);
436        return null;
437      }
438    };
439    AccessTestAction preRevokeAction = new AccessTestAction() {
440      @Override
441      public Object run() throws Exception {
442        ACCESS_CONTROLLER.preRevoke(ObserverContextImpl.createAndPrepare(CP_ENV),
443          new UserPermission(testUser,
444            Permission.newBuilder(TEST_NAMESPACE).withActions(Action.WRITE).build()));
445        return null;
446      }
447    };
448
449    AccessTestAction grantCPAction = new AccessTestAction() {
450      @Override
451      public Object run() throws Exception {
452        try (Connection connection = ConnectionFactory.createConnection(conf);
453          Table acl = connection.getTable(PermissionStorage.ACL_TABLE_NAME)) {
454          BlockingRpcChannel service = acl.coprocessorService(HConstants.EMPTY_START_ROW);
455          AccessControlService.BlockingInterface protocol =
456            AccessControlService.newBlockingStub(service);
457          AccessControlUtil.grant(null, protocol, testUser, TEST_NAMESPACE, false, Action.WRITE);
458        }
459        return null;
460      }
461    };
462    AccessTestAction revokeCPAction = new AccessTestAction() {
463      @Override
464      public Object run() throws Exception {
465        try (Connection connection = ConnectionFactory.createConnection(conf);
466          Table acl = connection.getTable(PermissionStorage.ACL_TABLE_NAME)) {
467          BlockingRpcChannel service = acl.coprocessorService(HConstants.EMPTY_START_ROW);
468          AccessControlService.BlockingInterface protocol =
469            AccessControlService.newBlockingStub(service);
470          AccessControlUtil.revoke(null, protocol, testUser, TEST_NAMESPACE, Action.WRITE);
471        }
472        return null;
473      }
474    };
475
476    verifyAllowed(grantAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
477    verifyDenied(grantAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
478      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
479      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
480    verifyAllowed(grantNamespaceAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN,
481      USER_NS_ADMIN, USER_GROUP_NS_ADMIN);
482    verifyDenied(grantNamespaceAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
483      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
484      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
485
486    verifyAllowed(revokeAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
487    verifyDenied(revokeAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
488      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
489      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
490    verifyAllowed(revokeNamespaceAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN,
491      USER_NS_ADMIN, USER_GROUP_NS_ADMIN);
492    verifyDenied(revokeNamespaceAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
493      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
494      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
495
496    verifyAllowed(getPermissionsAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_NS_ADMIN,
497      USER_GROUP_ADMIN);
498    verifyDenied(getPermissionsAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
499      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
500      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
501
502    verifyAllowed(preGrantAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
503    verifyDenied(preGrantAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
504      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
505      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
506    verifyAllowed(preRevokeAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
507    verifyDenied(preRevokeAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
508      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
509      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
510
511    verifyAllowed(grantCPAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
512    verifyDenied(grantCPAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
513      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
514      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
515    verifyAllowed(revokeCPAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_GROUP_ADMIN, USER_NS_ADMIN);
516    verifyDenied(revokeCPAction, USER_GLOBAL_CREATE, USER_GLOBAL_WRITE, USER_GLOBAL_READ,
517      USER_GLOBAL_EXEC, USER_NS_CREATE, USER_NS_WRITE, USER_NS_READ, USER_NS_EXEC,
518      USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
519  }
520
521  @Test
522  public void testCreateTableWithNamespace() throws Exception {
523    AccessTestAction createTable = new AccessTestAction() {
524      @Override
525      public Object run() throws Exception {
526        TableDescriptor tableDescriptor =
527          TableDescriptorBuilder.newBuilder(TableName.valueOf(TEST_TABLE))
528            .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build();
529        ACCESS_CONTROLLER.preCreateTable(ObserverContextImpl.createAndPrepare(CP_ENV),
530          tableDescriptor, null);
531        return null;
532      }
533    };
534
535    // createTable : superuser | global(AC) | NS(AC)
536    verifyAllowed(createTable, SUPERUSER, USER_GLOBAL_CREATE, USER_NS_CREATE, USER_GROUP_CREATE,
537      USER_GLOBAL_ADMIN, USER_NS_ADMIN, USER_GROUP_ADMIN);
538    verifyDenied(createTable, USER_GLOBAL_WRITE, USER_GLOBAL_READ, USER_GLOBAL_EXEC, USER_NS_WRITE,
539      USER_NS_READ, USER_NS_EXEC, USER_TABLE_CREATE, USER_TABLE_WRITE, USER_GROUP_READ,
540      USER_GROUP_WRITE);
541  }
542}