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.assertNotNull; 025import static org.junit.jupiter.api.Assertions.assertTrue; 026import static org.junit.jupiter.api.Assertions.fail; 027 028import java.io.IOException; 029import java.security.PrivilegedAction; 030import java.util.ArrayList; 031import java.util.Arrays; 032import java.util.Collection; 033import java.util.Collections; 034import java.util.HashMap; 035import java.util.List; 036import java.util.Map; 037import org.apache.hadoop.conf.Configuration; 038import org.apache.hadoop.fs.CommonConfigurationKeys; 039import org.apache.hadoop.fs.FileStatus; 040import org.apache.hadoop.fs.FileSystem; 041import org.apache.hadoop.fs.Path; 042import org.apache.hadoop.fs.permission.FsPermission; 043import org.apache.hadoop.hbase.Coprocessor; 044import org.apache.hadoop.hbase.CoprocessorEnvironment; 045import org.apache.hadoop.hbase.HBaseIOException; 046import org.apache.hadoop.hbase.HBaseTestingUtil; 047import org.apache.hadoop.hbase.HConstants; 048import org.apache.hadoop.hbase.HRegionLocation; 049import org.apache.hadoop.hbase.KeyValue; 050import org.apache.hadoop.hbase.NamespaceDescriptor; 051import org.apache.hadoop.hbase.ServerName; 052import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 053import org.apache.hadoop.hbase.TableName; 054import org.apache.hadoop.hbase.TableNotFoundException; 055import org.apache.hadoop.hbase.Waiter.Predicate; 056import org.apache.hadoop.hbase.client.Admin; 057import org.apache.hadoop.hbase.client.Append; 058import org.apache.hadoop.hbase.client.BalanceRequest; 059import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 060import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 061import org.apache.hadoop.hbase.client.Connection; 062import org.apache.hadoop.hbase.client.ConnectionFactory; 063import org.apache.hadoop.hbase.client.Delete; 064import org.apache.hadoop.hbase.client.Get; 065import org.apache.hadoop.hbase.client.Hbck; 066import org.apache.hadoop.hbase.client.Increment; 067import org.apache.hadoop.hbase.client.MasterSwitchType; 068import org.apache.hadoop.hbase.client.Put; 069import org.apache.hadoop.hbase.client.RegionInfo; 070import org.apache.hadoop.hbase.client.RegionInfoBuilder; 071import org.apache.hadoop.hbase.client.RegionLocator; 072import org.apache.hadoop.hbase.client.Result; 073import org.apache.hadoop.hbase.client.ResultScanner; 074import org.apache.hadoop.hbase.client.Scan; 075import org.apache.hadoop.hbase.client.SnapshotDescription; 076import org.apache.hadoop.hbase.client.Table; 077import org.apache.hadoop.hbase.client.TableDescriptor; 078import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 079import org.apache.hadoop.hbase.client.TableState; 080import org.apache.hadoop.hbase.client.security.SecurityCapability; 081import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 082import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment; 083import org.apache.hadoop.hbase.coprocessor.ObserverContextImpl; 084import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 085import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 086import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment; 087import org.apache.hadoop.hbase.exceptions.HBaseException; 088import org.apache.hadoop.hbase.io.hfile.CacheConfig; 089import org.apache.hadoop.hbase.io.hfile.HFile; 090import org.apache.hadoop.hbase.io.hfile.HFileContext; 091import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 092import org.apache.hadoop.hbase.master.HMaster; 093import org.apache.hadoop.hbase.master.MasterCoprocessorHost; 094import org.apache.hadoop.hbase.master.RegionState; 095import org.apache.hadoop.hbase.master.locking.LockProcedure; 096import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 097import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface; 098import org.apache.hadoop.hbase.procedure2.LockType; 099import org.apache.hadoop.hbase.procedure2.Procedure; 100import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 101import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer; 102import org.apache.hadoop.hbase.procedure2.ProcedureYieldException; 103import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker; 104import org.apache.hadoop.hbase.regionserver.HRegion; 105import org.apache.hadoop.hbase.regionserver.HRegionServer; 106import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost; 107import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost; 108import org.apache.hadoop.hbase.regionserver.ScanType; 109import org.apache.hadoop.hbase.replication.ReplicationPeerConfig; 110import org.apache.hadoop.hbase.replication.SyncReplicationState; 111import org.apache.hadoop.hbase.security.AccessDeniedException; 112import org.apache.hadoop.hbase.security.Superusers; 113import org.apache.hadoop.hbase.security.User; 114import org.apache.hadoop.hbase.security.access.Permission.Action; 115import org.apache.hadoop.hbase.testclassification.LargeTests; 116import org.apache.hadoop.hbase.testclassification.SecurityTests; 117import org.apache.hadoop.hbase.tool.BulkLoadHFiles; 118import org.apache.hadoop.hbase.util.Bytes; 119import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 120import org.apache.hadoop.hbase.util.JVMClusterUtil; 121import org.apache.hadoop.hbase.util.Threads; 122import org.apache.hadoop.security.GroupMappingServiceProvider; 123import org.apache.hadoop.security.ShellBasedUnixGroupsMapping; 124import org.apache.hadoop.security.UserGroupInformation; 125import org.junit.jupiter.api.AfterAll; 126import org.junit.jupiter.api.BeforeAll; 127import org.junit.jupiter.api.Tag; 128import org.junit.jupiter.api.Test; 129import org.junit.jupiter.api.TestInfo; 130import org.slf4j.Logger; 131import org.slf4j.LoggerFactory; 132 133import org.apache.hbase.thirdparty.com.google.common.collect.ArrayListMultimap; 134import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap; 135import org.apache.hbase.thirdparty.com.google.protobuf.BlockingRpcChannel; 136import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 137import org.apache.hbase.thirdparty.com.google.protobuf.RpcController; 138import org.apache.hbase.thirdparty.com.google.protobuf.Service; 139import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 140 141import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.CountRequest; 142import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.CountResponse; 143import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.HelloRequest; 144import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.HelloResponse; 145import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.IncrementCountRequest; 146import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.IncrementCountResponse; 147import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.NoopRequest; 148import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.NoopResponse; 149import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.PingRequest; 150import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.PingResponse; 151import org.apache.hadoop.hbase.shaded.coprocessor.protobuf.generated.PingProtos.PingService; 152import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProcedureProtos; 153import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 154import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos; 155import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.AccessControlService; 156import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.CheckPermissionsRequest; 157import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState; 158 159/** 160 * Performs authorization checks for common operations, according to different levels of authorized 161 * users. 162 */ 163@Tag(SecurityTests.TAG) 164@Tag(LargeTests.TAG) 165public class TestAccessController extends SecureTestUtil { 166 167 private static final FsPermission FS_PERMISSION_ALL = FsPermission.valueOf("-rwxrwxrwx"); 168 private static final Logger LOG = LoggerFactory.getLogger(TestAccessController.class); 169 private static TableName TEST_TABLE = TableName.valueOf("testtable1"); 170 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 171 private static Configuration conf; 172 173 /** 174 * The systemUserConnection created here is tied to the system user. In case, you are planning to 175 * create AccessTestAction, DON'T use this systemUserConnection as the 'doAs' user gets eclipsed 176 * by the system user. 177 */ 178 private static Connection systemUserConnection; 179 180 // user with all permissions 181 private static User SUPERUSER; 182 // user granted with all global permission 183 private static User USER_ADMIN; 184 // user with rw permissions on column family. 185 private static User USER_RW; 186 // user with read-only permissions 187 private static User USER_RO; 188 // user is table owner. will have all permissions on table 189 private static User USER_OWNER; 190 // user with create table permissions alone 191 private static User USER_CREATE; 192 // user with no permissions 193 private static User USER_NONE; 194 // user with admin rights on the column family 195 private static User USER_ADMIN_CF; 196 197 private static final String GROUP_ADMIN = "group_admin"; 198 private static final String GROUP_CREATE = "group_create"; 199 private static final String GROUP_READ = "group_read"; 200 private static final String GROUP_WRITE = "group_write"; 201 202 private static User USER_GROUP_ADMIN; 203 private static User USER_GROUP_CREATE; 204 private static User USER_GROUP_READ; 205 private static User USER_GROUP_WRITE; 206 207 // TODO: convert this test to cover the full matrix in 208 // https://hbase.apache.org/docs/acl-matrix 209 // creating all Scope x Permission combinations 210 211 private static TableName TEST_TABLE2 = TableName.valueOf("testtable2"); 212 private static byte[] TEST_FAMILY = Bytes.toBytes("f1"); 213 private static byte[] TEST_QUALIFIER = Bytes.toBytes("q1"); 214 private static byte[] TEST_ROW = Bytes.toBytes("r1"); 215 216 private static MasterCoprocessorEnvironment CP_ENV; 217 private static AccessController ACCESS_CONTROLLER; 218 private static RegionServerCoprocessorEnvironment RSCP_ENV; 219 private static RegionCoprocessorEnvironment RCP_ENV; 220 221 @BeforeAll 222 public static void setupBeforeClass() throws Exception { 223 // setup configuration 224 conf = TEST_UTIL.getConfiguration(); 225 // Up the handlers; this test needs more than usual. 226 conf.setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 10); 227 228 conf.set(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 229 MyShellBasedUnixGroupsMapping.class.getName()); 230 UserGroupInformation.setConfiguration(conf); 231 232 // Enable security 233 enableSecurity(conf); 234 // In this particular test case, we can't use SecureBulkLoadEndpoint because its doAs will fail 235 // to move a file for a random user 236 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, AccessController.class.getName()); 237 // Verify enableSecurity sets up what we require 238 verifyConfiguration(conf); 239 240 // Enable EXEC permission checking 241 conf.setBoolean(AccessControlConstants.EXEC_PERMISSION_CHECKS_KEY, true); 242 243 TEST_UTIL.startMiniCluster(); 244 MasterCoprocessorHost masterCpHost = 245 TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterCoprocessorHost(); 246 masterCpHost.load(AccessController.class, Coprocessor.PRIORITY_HIGHEST, conf); 247 ACCESS_CONTROLLER = masterCpHost.findCoprocessor(AccessController.class); 248 CP_ENV = 249 masterCpHost.createEnvironment(ACCESS_CONTROLLER, Coprocessor.PRIORITY_HIGHEST, 1, conf); 250 RegionServerCoprocessorHost rsCpHost = 251 TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).getRegionServerCoprocessorHost(); 252 RSCP_ENV = rsCpHost.createEnvironment(ACCESS_CONTROLLER, Coprocessor.PRIORITY_HIGHEST, 1, conf); 253 254 // Wait for the ACL table to become available 255 TEST_UTIL.waitUntilAllRegionsAssigned(PermissionStorage.ACL_TABLE_NAME); 256 257 // create a set of test users 258 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" }); 259 USER_ADMIN = User.createUserForTesting(conf, "admin2", new String[0]); 260 USER_RW = User.createUserForTesting(conf, "rwuser", new String[0]); 261 USER_RO = User.createUserForTesting(conf, "rouser", new String[0]); 262 USER_OWNER = User.createUserForTesting(conf, "owner", new String[0]); 263 USER_CREATE = User.createUserForTesting(conf, "tbl_create", new String[0]); 264 USER_NONE = User.createUserForTesting(conf, "nouser", new String[0]); 265 USER_ADMIN_CF = User.createUserForTesting(conf, "col_family_admin", new String[0]); 266 267 USER_GROUP_ADMIN = 268 User.createUserForTesting(conf, "user_group_admin", new String[] { GROUP_ADMIN }); 269 USER_GROUP_CREATE = 270 User.createUserForTesting(conf, "user_group_create", new String[] { GROUP_CREATE }); 271 USER_GROUP_READ = 272 User.createUserForTesting(conf, "user_group_read", new String[] { GROUP_READ }); 273 USER_GROUP_WRITE = 274 User.createUserForTesting(conf, "user_group_write", new String[] { GROUP_WRITE }); 275 276 // Grant table creation permission to USER_OWNER 277 grantGlobal(TEST_UTIL, USER_OWNER.getShortName(), Action.CREATE); 278 279 systemUserConnection = TEST_UTIL.getConnection(); 280 setUpTableAndUserPermissions(); 281 } 282 283 @AfterAll 284 public static void tearDownAfterClass() throws Exception { 285 cleanUp(); 286 TEST_UTIL.shutdownMiniCluster(); 287 } 288 289 private static void setUpTableAndUserPermissions() throws Exception { 290 TableDescriptor tableDescriptor = 291 TableDescriptorBuilder.newBuilder(TEST_TABLE) 292 .setColumnFamily( 293 ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY).setMaxVersions(100).build()) 294 .build(); 295 createTable(TEST_UTIL, USER_OWNER, tableDescriptor, new byte[][] { Bytes.toBytes("s") }); 296 297 HRegion region = TEST_UTIL.getHBaseCluster().getRegions(TEST_TABLE).get(0); 298 RegionCoprocessorHost rcpHost = region.getCoprocessorHost(); 299 RCP_ENV = rcpHost.createEnvironment(ACCESS_CONTROLLER, Coprocessor.PRIORITY_HIGHEST, 1, conf); 300 301 // Set up initial grants 302 303 grantGlobal(TEST_UTIL, USER_ADMIN.getShortName(), Permission.Action.ADMIN, 304 Permission.Action.CREATE, Permission.Action.READ, Permission.Action.WRITE); 305 306 grantOnTable(TEST_UTIL, USER_RW.getShortName(), TEST_TABLE, TEST_FAMILY, null, 307 Permission.Action.READ, Permission.Action.WRITE); 308 309 // USER_CREATE is USER_RW plus CREATE permissions 310 grantOnTable(TEST_UTIL, USER_CREATE.getShortName(), TEST_TABLE, null, null, 311 Permission.Action.CREATE, Permission.Action.READ, Permission.Action.WRITE); 312 313 grantOnTable(TEST_UTIL, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null, 314 Permission.Action.READ); 315 316 grantOnTable(TEST_UTIL, USER_ADMIN_CF.getShortName(), TEST_TABLE, TEST_FAMILY, null, 317 Permission.Action.ADMIN, Permission.Action.CREATE); 318 319 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_ADMIN), Permission.Action.ADMIN); 320 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_CREATE), Permission.Action.CREATE); 321 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_READ), Permission.Action.READ); 322 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_WRITE), Permission.Action.WRITE); 323 324 assertEquals(5, 325 PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), TEST_TABLE).size()); 326 int size = 0; 327 try { 328 size = 329 AccessControlClient.getUserPermissions(systemUserConnection, TEST_TABLE.toString()).size(); 330 } catch (Throwable e) { 331 LOG.error("error during call of AccessControlClient.getUserPermissions. ", e); 332 fail("error during call of AccessControlClient.getUserPermissions."); 333 } 334 assertEquals(5, size); 335 } 336 337 private static void cleanUp() throws Exception { 338 // Clean the _acl_ table 339 try { 340 deleteTable(TEST_UTIL, TEST_TABLE); 341 } catch (TableNotFoundException ex) { 342 // Test deleted the table, no problem 343 LOG.info("Test deleted table " + TEST_TABLE); 344 } 345 // Verify all table/namespace permissions are erased 346 assertEquals(0, 347 PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), TEST_TABLE).size()); 348 assertEquals(0, 349 PermissionStorage 350 .getNamespacePermissions(TEST_UTIL.getConnection(), TEST_TABLE.getNamespaceAsString()) 351 .size()); 352 } 353 354 @Test 355 public void testUnauthorizedShutdown() throws Exception { 356 AccessTestAction action = new AccessTestAction() { 357 @Override 358 public Object run() throws Exception { 359 HMaster master = TEST_UTIL.getHBaseCluster().getMaster(); 360 master.shutdown(); 361 return null; 362 } 363 }; 364 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 365 USER_GROUP_WRITE, USER_GROUP_CREATE); 366 } 367 368 @Test 369 public void testUnauthorizedStopMaster() throws Exception { 370 AccessTestAction action = new AccessTestAction() { 371 @Override 372 public Object run() throws Exception { 373 HMaster master = TEST_UTIL.getHBaseCluster().getMaster(); 374 master.stopMaster(); 375 return null; 376 } 377 }; 378 379 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 380 USER_GROUP_WRITE, USER_GROUP_CREATE); 381 } 382 383 @Test 384 public void testUnauthorizedSetTableStateInMeta() throws Exception { 385 AccessTestAction action = () -> { 386 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 387 Hbck hbck = conn.getHbck()) { 388 hbck.setTableStateInMeta(new TableState(TEST_TABLE, TableState.State.DISABLED)); 389 } 390 return null; 391 }; 392 393 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 394 USER_GROUP_WRITE, USER_GROUP_CREATE); 395 } 396 397 @Test 398 public void testUnauthorizedSetRegionStateInMeta() throws Exception { 399 Admin admin = TEST_UTIL.getAdmin(); 400 final List<RegionInfo> regions = admin.getRegions(TEST_TABLE); 401 RegionInfo closeRegion = regions.get(0); 402 Map<String, RegionState.State> newStates = new HashMap<>(); 403 newStates.put(closeRegion.getEncodedName(), RegionState.State.CLOSED); 404 AccessTestAction action = () -> { 405 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 406 Hbck hbck = conn.getHbck()) { 407 hbck.setRegionStateInMeta(newStates); 408 } 409 return null; 410 }; 411 412 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 413 USER_GROUP_WRITE, USER_GROUP_CREATE); 414 } 415 416 @Test 417 public void testUnauthorizedFixMeta() throws Exception { 418 AccessTestAction action = () -> { 419 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 420 Hbck hbck = conn.getHbck()) { 421 hbck.fixMeta(); 422 } 423 return null; 424 }; 425 426 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 427 USER_GROUP_WRITE, USER_GROUP_CREATE); 428 } 429 430 @Test 431 public void testSecurityCapabilities() throws Exception { 432 List<SecurityCapability> capabilities = 433 TEST_UTIL.getConnection().getAdmin().getSecurityCapabilities(); 434 assertTrue(capabilities.contains(SecurityCapability.AUTHORIZATION), 435 "AUTHORIZATION capability is missing"); 436 assertTrue(capabilities.contains(SecurityCapability.CELL_AUTHORIZATION), 437 "CELL_AUTHORIZATION capability is missing"); 438 } 439 440 @Test 441 public void testTableCreate(TestInfo testInfo) throws Exception { 442 AccessTestAction createTable = new AccessTestAction() { 443 @Override 444 public Object run() throws Exception { 445 TableDescriptor tableDescriptor = TableDescriptorBuilder 446 .newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName())) 447 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build(); 448 ACCESS_CONTROLLER.preCreateTable(ObserverContextImpl.createAndPrepare(CP_ENV), 449 tableDescriptor, null); 450 return null; 451 } 452 }; 453 454 // verify that superuser can create tables 455 verifyAllowed(createTable, SUPERUSER, USER_ADMIN, USER_GROUP_CREATE, USER_GROUP_ADMIN); 456 457 // all others should be denied 458 verifyDenied(createTable, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 459 USER_GROUP_WRITE); 460 } 461 462 @Test 463 public void testTableModify() throws Exception { 464 AccessTestAction modifyTable = new AccessTestAction() { 465 @Override 466 public Object run() throws Exception { 467 TableDescriptorBuilder tableDescriptorBuilder = 468 TableDescriptorBuilder.newBuilder(TEST_TABLE); 469 ColumnFamilyDescriptor columnFamilyDescriptor = 470 ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY).build(); 471 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); 472 columnFamilyDescriptor = ColumnFamilyDescriptorBuilder 473 .newBuilder(Bytes.toBytes("fam_" + User.getCurrent().getShortName())).build(); 474 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); 475 ACCESS_CONTROLLER.preModifyTable(ObserverContextImpl.createAndPrepare(CP_ENV), TEST_TABLE, 476 null, // not needed by AccessController 477 tableDescriptorBuilder.build()); 478 return null; 479 } 480 }; 481 482 verifyAllowed(modifyTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE, 483 USER_GROUP_ADMIN); 484 verifyDenied(modifyTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 485 } 486 487 private ListMultimap<String, UserPermission> getTablePerms(TableName tableName) 488 throws IOException { 489 ListMultimap<String, UserPermission> perms = ArrayListMultimap.create(); 490 PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), tableName) 491 .forEach(up -> perms.put(up.getUser(), up)); 492 return perms; 493 } 494 495 @Test 496 public void testTableModifyRemoveFamiliesClearsPermissions(TestInfo testInfo) throws Exception { 497 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 498 final byte[] family1 = Bytes.toBytes("cf1"); 499 final byte[] family2 = Bytes.toBytes("cf2"); 500 final byte[] family3 = Bytes.toBytes("cf3"); 501 // a qualifier on a surviving family, sharing its name with a dropped column family 502 final byte[] qualifier = Bytes.toBytes("cf1"); 503 504 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 505 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family1)) 506 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family2)) 507 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family3)).build(); 508 createTable(TEST_UTIL, tableDescriptor); 509 510 try { 511 grantOnTable(TEST_UTIL, USER_NONE.getShortName(), tableName, null, null, Action.READ); 512 grantOnTable(TEST_UTIL, USER_RO.getShortName(), tableName, family1, null, Action.READ); 513 grantOnTable(TEST_UTIL, USER_RW.getShortName(), tableName, family1, TEST_QUALIFIER, 514 Action.READ); 515 grantOnTable(TEST_UTIL, USER_CREATE.getShortName(), tableName, family2, null, Action.READ); 516 grantOnTable(TEST_UTIL, USER_ADMIN_CF.getShortName(), tableName, family3, qualifier, 517 Action.READ); 518 519 ListMultimap<String, UserPermission> perms = getTablePerms(tableName); 520 assertTablePermission(perms, USER_NONE, null, null, true); 521 assertTablePermission(perms, USER_RO, family1, null, true); 522 assertTablePermission(perms, USER_RW, family1, TEST_QUALIFIER, true); 523 assertTablePermission(perms, USER_CREATE, family2, null, true); 524 assertTablePermission(perms, USER_ADMIN_CF, family3, qualifier, true); 525 526 // Drop cf1 and cf2 in a single modify. The permissions scoped to them must be removed 527 // along with them, while the table level permission and the permissions on cf3 survive. 528 TableDescriptor droppedFamilyDesc = TableDescriptorBuilder.newBuilder(tableName) 529 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family3)).build(); 530 try (Admin admin = TEST_UTIL.getAdmin()) { 531 admin.modifyTable(droppedFamilyDesc); 532 } 533 534 perms = getTablePerms(tableName); 535 assertTablePermission(perms, USER_RO, family1, null, false); 536 assertTablePermission(perms, USER_RW, family1, TEST_QUALIFIER, false); 537 assertTablePermission(perms, USER_CREATE, family2, null, false); 538 assertTablePermission(perms, USER_NONE, null, null, true); 539 assertTablePermission(perms, USER_ADMIN_CF, family3, qualifier, true); 540 541 // Add cf1 and cf2 back. The removed permissions must not be resurrected. 542 TableDescriptor addedFamilyDesc = TableDescriptorBuilder.newBuilder(tableName) 543 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family1)) 544 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family2)) 545 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family3)).build(); 546 try (Admin admin = TEST_UTIL.getAdmin()) { 547 admin.modifyTable(addedFamilyDesc); 548 } 549 550 perms = getTablePerms(tableName); 551 assertTablePermission(perms, USER_RO, family1, null, false); 552 assertTablePermission(perms, USER_RW, family1, TEST_QUALIFIER, false); 553 assertTablePermission(perms, USER_CREATE, family2, null, false); 554 555 final AccessTestAction getFamily1Action = new AccessTestAction() { 556 @Override 557 public Object run() throws Exception { 558 Get get = new Get(TEST_ROW); 559 get.addColumn(family1, TEST_QUALIFIER); 560 try (Connection conn = ConnectionFactory.createConnection(conf); 561 Table table = conn.getTable(tableName)) { 562 table.get(get); 563 } 564 return null; 565 } 566 }; 567 // wait until the cache update is propagated to all region servers 568 TEST_UTIL.waitFor(30000, 100, new Predicate<IOException>() { 569 @Override 570 public boolean evaluate() throws IOException { 571 try { 572 USER_RW.runAs(getFamily1Action); 573 return false; 574 } catch (AccessDeniedException e) { 575 return true; 576 } catch (InterruptedException e) { 577 Thread.currentThread().interrupt(); 578 return false; 579 } 580 } 581 }); 582 verifyDenied(getFamily1Action, USER_RO, USER_RW); 583 584 final AccessTestAction getFamily2Action = new AccessTestAction() { 585 @Override 586 public Object run() throws Exception { 587 Get get = new Get(TEST_ROW); 588 get.addColumn(family2, TEST_QUALIFIER); 589 try (Connection conn = ConnectionFactory.createConnection(conf); 590 Table table = conn.getTable(tableName)) { 591 table.get(get); 592 } 593 return null; 594 } 595 }; 596 verifyDenied(getFamily2Action, USER_CREATE); 597 598 final AccessTestAction getFamily3Action = new AccessTestAction() { 599 @Override 600 public Object run() throws Exception { 601 Get get = new Get(TEST_ROW); 602 get.addColumn(family3, qualifier); 603 try (Connection conn = ConnectionFactory.createConnection(conf); 604 Table table = conn.getTable(tableName)) { 605 table.get(get); 606 } 607 return null; 608 } 609 }; 610 verifyAllowed(getFamily3Action, USER_ADMIN_CF); 611 } finally { 612 deleteTable(TEST_UTIL, tableName); 613 } 614 } 615 616 private static void assertTablePermission(ListMultimap<String, UserPermission> perms, User user, 617 byte[] family, byte[] qualifier, boolean expected) { 618 boolean found = false; 619 for (UserPermission perm : perms.get(user.getShortName())) { 620 if (perm.getPermission() instanceof TablePermission) { 621 TablePermission tablePermission = (TablePermission) perm.getPermission(); 622 if ( 623 Arrays.equals(family, tablePermission.getFamily()) 624 && Arrays.equals(qualifier, tablePermission.getQualifier()) 625 ) { 626 found = true; 627 break; 628 } 629 } 630 } 631 assertEquals(expected, found, 632 "Unexpected permission state for " + user.getShortName() + " on family=" 633 + (family == null ? "<table level>" : Bytes.toStringBinary(family)) + ", qualifier=" 634 + (qualifier == null ? "<family level>" : Bytes.toStringBinary(qualifier))); 635 } 636 637 @Test 638 public void testTableDelete() throws Exception { 639 AccessTestAction deleteTable = new AccessTestAction() { 640 @Override 641 public Object run() throws Exception { 642 ACCESS_CONTROLLER.preDeleteTable(ObserverContextImpl.createAndPrepare(CP_ENV), TEST_TABLE); 643 return null; 644 } 645 }; 646 647 verifyAllowed(deleteTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE, 648 USER_GROUP_ADMIN); 649 verifyDenied(deleteTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 650 } 651 652 @Test 653 public void testTableTruncate() throws Exception { 654 AccessTestAction truncateTable = new AccessTestAction() { 655 @Override 656 public Object run() throws Exception { 657 ACCESS_CONTROLLER.preTruncateTable(ObserverContextImpl.createAndPrepare(CP_ENV), 658 TEST_TABLE); 659 return null; 660 } 661 }; 662 663 verifyAllowed(truncateTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE, 664 USER_GROUP_ADMIN); 665 verifyDenied(truncateTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 666 } 667 668 @Test 669 public void testTableDisable() throws Exception { 670 AccessTestAction disableTable = new AccessTestAction() { 671 @Override 672 public Object run() throws Exception { 673 ACCESS_CONTROLLER.preDisableTable(ObserverContextImpl.createAndPrepare(CP_ENV), TEST_TABLE); 674 return null; 675 } 676 }; 677 678 AccessTestAction disableAclTable = new AccessTestAction() { 679 @Override 680 public Object run() throws Exception { 681 ACCESS_CONTROLLER.preDisableTable(ObserverContextImpl.createAndPrepare(CP_ENV), 682 PermissionStorage.ACL_TABLE_NAME); 683 return null; 684 } 685 }; 686 687 verifyAllowed(disableTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE, 688 USER_GROUP_ADMIN); 689 verifyDenied(disableTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 690 691 // No user should be allowed to disable _acl_ table 692 verifyDenied(disableAclTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_RW, USER_RO, 693 USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_WRITE); 694 } 695 696 @Test 697 public void testTableEnable() throws Exception { 698 AccessTestAction enableTable = new AccessTestAction() { 699 @Override 700 public Object run() throws Exception { 701 ACCESS_CONTROLLER.preEnableTable(ObserverContextImpl.createAndPrepare(CP_ENV), TEST_TABLE); 702 return null; 703 } 704 }; 705 706 verifyAllowed(enableTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE, 707 USER_GROUP_ADMIN); 708 verifyDenied(enableTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 709 } 710 711 public static class TestTableDDLProcedure extends Procedure<MasterProcedureEnv> 712 implements TableProcedureInterface { 713 private TableName tableName; 714 715 public TestTableDDLProcedure() { 716 } 717 718 public TestTableDDLProcedure(final MasterProcedureEnv env, final TableName tableName) 719 throws IOException { 720 this.tableName = tableName; 721 this.setTimeout(180000); // Timeout in 3 minutes 722 this.setOwner(env.getRequestUser()); 723 } 724 725 @Override 726 public TableName getTableName() { 727 return tableName; 728 } 729 730 @Override 731 public TableOperationType getTableOperationType() { 732 return TableOperationType.EDIT; 733 } 734 735 @Override 736 protected boolean abort(MasterProcedureEnv env) { 737 return true; 738 } 739 740 @Override 741 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 742 TestProcedureProtos.TestTableDDLStateData.Builder testTableDDLMsg = 743 TestProcedureProtos.TestTableDDLStateData.newBuilder() 744 .setTableName(tableName.getNameAsString()); 745 serializer.serialize(testTableDDLMsg.build()); 746 } 747 748 @Override 749 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 750 TestProcedureProtos.TestTableDDLStateData testTableDDLMsg = 751 serializer.deserialize(TestProcedureProtos.TestTableDDLStateData.class); 752 tableName = TableName.valueOf(testTableDDLMsg.getTableName()); 753 } 754 755 @Override 756 protected Procedure[] execute(MasterProcedureEnv env) 757 throws ProcedureYieldException, InterruptedException { 758 // Not letting the procedure to complete until timed out 759 setState(ProcedureState.WAITING_TIMEOUT); 760 return null; 761 } 762 763 @Override 764 protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException { 765 } 766 } 767 768 @Test 769 public void testAbortProcedure() throws Exception { 770 long procId = 1; 771 AccessTestAction abortProcedureAction = new AccessTestAction() { 772 @Override 773 public Object run() throws Exception { 774 ACCESS_CONTROLLER.preAbortProcedure(ObserverContextImpl.createAndPrepare(CP_ENV), procId); 775 return null; 776 } 777 }; 778 779 verifyAllowed(abortProcedureAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 780 } 781 782 @Test 783 public void testGetProcedures(TestInfo testInfo) throws Exception { 784 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 785 final ProcedureExecutor<MasterProcedureEnv> procExec = 786 TEST_UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 787 Procedure proc = new TestTableDDLProcedure(procExec.getEnvironment(), tableName); 788 proc.setOwner(USER_OWNER); 789 procExec.submitProcedure(proc); 790 final List<Procedure<MasterProcedureEnv>> procList = procExec.getProcedures(); 791 792 AccessTestAction getProceduresAction = new AccessTestAction() { 793 @Override 794 public Object run() throws Exception { 795 ACCESS_CONTROLLER.postGetProcedures(ObserverContextImpl.createAndPrepare(CP_ENV)); 796 return null; 797 } 798 }; 799 800 verifyAllowed(getProceduresAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 801 verifyAllowed(getProceduresAction, USER_OWNER); 802 verifyIfNull(getProceduresAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 803 USER_GROUP_WRITE); 804 } 805 806 @Test 807 public void testGetLocks() throws Exception { 808 AccessTestAction action = new AccessTestAction() { 809 @Override 810 public Object run() throws Exception { 811 ACCESS_CONTROLLER.preGetLocks(ObserverContextImpl.createAndPrepare(CP_ENV)); 812 return null; 813 } 814 }; 815 816 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 817 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 818 USER_GROUP_WRITE, USER_GROUP_CREATE); 819 } 820 821 @Test 822 public void testMove() throws Exception { 823 List<HRegionLocation> regions; 824 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { 825 regions = locator.getAllRegionLocations(); 826 } 827 HRegionLocation location = regions.get(0); 828 final RegionInfo hri = location.getRegion(); 829 final ServerName server = location.getServerName(); 830 AccessTestAction action = new AccessTestAction() { 831 @Override 832 public Object run() throws Exception { 833 ACCESS_CONTROLLER.preMove(ObserverContextImpl.createAndPrepare(CP_ENV), hri, server, 834 server); 835 return null; 836 } 837 }; 838 839 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 840 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 841 USER_GROUP_WRITE, USER_GROUP_CREATE); 842 } 843 844 @Test 845 public void testAssign() throws Exception { 846 List<HRegionLocation> regions; 847 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { 848 regions = locator.getAllRegionLocations(); 849 } 850 HRegionLocation location = regions.get(0); 851 final RegionInfo hri = location.getRegion(); 852 AccessTestAction action = new AccessTestAction() { 853 @Override 854 public Object run() throws Exception { 855 ACCESS_CONTROLLER.preAssign(ObserverContextImpl.createAndPrepare(CP_ENV), hri); 856 return null; 857 } 858 }; 859 860 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 861 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 862 USER_GROUP_WRITE, USER_GROUP_CREATE); 863 } 864 865 @Test 866 public void testUnassign() throws Exception { 867 List<HRegionLocation> regions; 868 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { 869 regions = locator.getAllRegionLocations(); 870 } 871 HRegionLocation location = regions.get(0); 872 final RegionInfo hri = location.getRegion(); 873 AccessTestAction action = new AccessTestAction() { 874 @Override 875 public Object run() throws Exception { 876 ACCESS_CONTROLLER.preUnassign(ObserverContextImpl.createAndPrepare(CP_ENV), hri); 877 return null; 878 } 879 }; 880 881 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 882 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 883 USER_GROUP_WRITE, USER_GROUP_CREATE); 884 } 885 886 @Test 887 public void testRegionOffline() throws Exception { 888 List<HRegionLocation> regions; 889 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { 890 regions = locator.getAllRegionLocations(); 891 } 892 HRegionLocation location = regions.get(0); 893 final RegionInfo hri = location.getRegion(); 894 AccessTestAction action = new AccessTestAction() { 895 @Override 896 public Object run() throws Exception { 897 ACCESS_CONTROLLER.preRegionOffline(ObserverContextImpl.createAndPrepare(CP_ENV), hri); 898 return null; 899 } 900 }; 901 902 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 903 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 904 USER_GROUP_WRITE, USER_GROUP_CREATE); 905 } 906 907 @Test 908 public void testSetSplitOrMergeEnabled() throws Exception { 909 AccessTestAction action = new AccessTestAction() { 910 @Override 911 public Object run() throws Exception { 912 ACCESS_CONTROLLER.preSetSplitOrMergeEnabled(ObserverContextImpl.createAndPrepare(CP_ENV), 913 true, MasterSwitchType.MERGE); 914 return null; 915 } 916 }; 917 918 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 919 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 920 USER_GROUP_WRITE, USER_GROUP_CREATE); 921 } 922 923 @Test 924 public void testBalance() throws Exception { 925 AccessTestAction action = new AccessTestAction() { 926 @Override 927 public Object run() throws Exception { 928 ACCESS_CONTROLLER.preBalance(ObserverContextImpl.createAndPrepare(CP_ENV), 929 BalanceRequest.defaultInstance()); 930 return null; 931 } 932 }; 933 934 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 935 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 936 USER_GROUP_WRITE, USER_GROUP_CREATE); 937 } 938 939 @Test 940 public void testBalanceSwitch() throws Exception { 941 AccessTestAction action = new AccessTestAction() { 942 @Override 943 public Object run() throws Exception { 944 ACCESS_CONTROLLER.preBalanceSwitch(ObserverContextImpl.createAndPrepare(CP_ENV), true); 945 return null; 946 } 947 }; 948 949 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 950 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 951 USER_GROUP_WRITE, USER_GROUP_CREATE); 952 } 953 954 @Test 955 public void testShutdown() throws Exception { 956 AccessTestAction action = new AccessTestAction() { 957 @Override 958 public Object run() throws Exception { 959 ACCESS_CONTROLLER.preShutdown(ObserverContextImpl.createAndPrepare(CP_ENV)); 960 return null; 961 } 962 }; 963 964 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 965 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 966 USER_GROUP_WRITE, USER_GROUP_CREATE); 967 } 968 969 @Test 970 public void testStopMaster() throws Exception { 971 AccessTestAction action = new AccessTestAction() { 972 @Override 973 public Object run() throws Exception { 974 ACCESS_CONTROLLER.preStopMaster(ObserverContextImpl.createAndPrepare(CP_ENV)); 975 return null; 976 } 977 }; 978 979 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 980 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 981 USER_GROUP_WRITE, USER_GROUP_CREATE); 982 } 983 984 private void verifyWrite(AccessTestAction action) throws Exception { 985 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW, 986 USER_GROUP_WRITE); 987 verifyDenied(action, USER_NONE, USER_RO, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_CREATE); 988 } 989 990 @Test 991 public void testSplitWithSplitRow(TestInfo testInfo) throws Exception { 992 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 993 createTestTable(tableName); 994 AccessTestAction action = new AccessTestAction() { 995 @Override 996 public Object run() throws Exception { 997 ACCESS_CONTROLLER.preSplitRegion(ObserverContextImpl.createAndPrepare(CP_ENV), tableName, 998 TEST_ROW); 999 return null; 1000 } 1001 }; 1002 1003 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1004 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1005 USER_GROUP_WRITE, USER_GROUP_CREATE); 1006 } 1007 1008 @Test 1009 public void testFlush() throws Exception { 1010 AccessTestAction action = new AccessTestAction() { 1011 @Override 1012 public Object run() throws Exception { 1013 ACCESS_CONTROLLER.preFlush(ObserverContextImpl.createAndPrepare(RCP_ENV), 1014 FlushLifeCycleTracker.DUMMY); 1015 return null; 1016 } 1017 }; 1018 1019 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_GROUP_CREATE, 1020 USER_GROUP_ADMIN); 1021 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 1022 } 1023 1024 @Test 1025 public void testCompact() throws Exception { 1026 AccessTestAction action = new AccessTestAction() { 1027 @Override 1028 public Object run() throws Exception { 1029 ACCESS_CONTROLLER.preCompact(ObserverContextImpl.createAndPrepare(RCP_ENV), null, null, 1030 ScanType.COMPACT_RETAIN_DELETES, null, null); 1031 return null; 1032 } 1033 }; 1034 1035 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_GROUP_CREATE, 1036 USER_GROUP_ADMIN); 1037 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 1038 } 1039 1040 private void verifyRead(AccessTestAction action) throws Exception { 1041 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW, USER_RO, 1042 USER_GROUP_READ); 1043 verifyDenied(action, USER_NONE, USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_WRITE); 1044 } 1045 1046 private void verifyReadWrite(AccessTestAction action) throws Exception { 1047 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW); 1048 verifyDenied(action, USER_NONE, USER_RO, USER_GROUP_ADMIN, USER_GROUP_CREATE, USER_GROUP_READ, 1049 USER_GROUP_WRITE); 1050 } 1051 1052 @Test 1053 public void testRead() throws Exception { 1054 // get action 1055 AccessTestAction getAction = new AccessTestAction() { 1056 @Override 1057 public Object run() throws Exception { 1058 Get g = new Get(TEST_ROW); 1059 g.addFamily(TEST_FAMILY); 1060 try (Connection conn = ConnectionFactory.createConnection(conf); 1061 Table t = conn.getTable(TEST_TABLE)) { 1062 t.get(g); 1063 } 1064 return null; 1065 } 1066 }; 1067 verifyRead(getAction); 1068 1069 // action for scanning 1070 AccessTestAction scanAction = new AccessTestAction() { 1071 @Override 1072 public Object run() throws Exception { 1073 Scan s = new Scan(); 1074 s.addFamily(TEST_FAMILY); 1075 try (Connection conn = ConnectionFactory.createConnection(conf); 1076 Table table = conn.getTable(TEST_TABLE)) { 1077 ResultScanner scanner = table.getScanner(s); 1078 try { 1079 for (Result r = scanner.next(); r != null; r = scanner.next()) { 1080 // do nothing 1081 } 1082 } finally { 1083 scanner.close(); 1084 } 1085 } 1086 return null; 1087 } 1088 }; 1089 verifyRead(scanAction); 1090 } 1091 1092 @Test 1093 // test put, delete, increment 1094 public void testWrite() throws Exception { 1095 // put action 1096 AccessTestAction putAction = new AccessTestAction() { 1097 @Override 1098 public Object run() throws Exception { 1099 Put p = new Put(TEST_ROW); 1100 p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1)); 1101 try (Connection conn = ConnectionFactory.createConnection(conf); 1102 Table t = conn.getTable(TEST_TABLE)) { 1103 t.put(p); 1104 } 1105 return null; 1106 } 1107 }; 1108 verifyWrite(putAction); 1109 1110 // delete action 1111 AccessTestAction deleteAction = new AccessTestAction() { 1112 @Override 1113 public Object run() throws Exception { 1114 Delete d = new Delete(TEST_ROW); 1115 d.addFamily(TEST_FAMILY); 1116 try (Connection conn = ConnectionFactory.createConnection(conf); 1117 Table t = conn.getTable(TEST_TABLE)) { 1118 t.delete(d); 1119 } 1120 return null; 1121 } 1122 }; 1123 verifyWrite(deleteAction); 1124 1125 // increment action 1126 AccessTestAction incrementAction = new AccessTestAction() { 1127 @Override 1128 public Object run() throws Exception { 1129 Increment inc = new Increment(TEST_ROW); 1130 inc.addColumn(TEST_FAMILY, TEST_QUALIFIER, 1); 1131 try (Connection conn = ConnectionFactory.createConnection(conf); 1132 Table t = conn.getTable(TEST_TABLE)) { 1133 t.increment(inc); 1134 } 1135 return null; 1136 } 1137 }; 1138 verifyWrite(incrementAction); 1139 } 1140 1141 @Test 1142 public void testReadWrite() throws Exception { 1143 // action for checkAndDelete 1144 AccessTestAction checkAndDeleteAction = new AccessTestAction() { 1145 @Override 1146 public Object run() throws Exception { 1147 Delete d = new Delete(TEST_ROW); 1148 d.addFamily(TEST_FAMILY); 1149 try (Connection conn = ConnectionFactory.createConnection(conf); 1150 Table t = conn.getTable(TEST_TABLE)) { 1151 t.checkAndMutate(TEST_ROW, TEST_FAMILY).qualifier(TEST_QUALIFIER) 1152 .ifEquals(Bytes.toBytes("test_value")).thenDelete(d); 1153 } 1154 return null; 1155 } 1156 }; 1157 verifyReadWrite(checkAndDeleteAction); 1158 1159 // action for checkAndPut() 1160 AccessTestAction checkAndPut = new AccessTestAction() { 1161 @Override 1162 public Object run() throws Exception { 1163 Put p = new Put(TEST_ROW); 1164 p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1)); 1165 try (Connection conn = ConnectionFactory.createConnection(conf); 1166 Table t = conn.getTable(TEST_TABLE)) { 1167 t.checkAndMutate(TEST_ROW, TEST_FAMILY).qualifier(TEST_QUALIFIER) 1168 .ifEquals(Bytes.toBytes("test_value")).thenPut(p); 1169 } 1170 return null; 1171 } 1172 }; 1173 verifyReadWrite(checkAndPut); 1174 } 1175 1176 @Test 1177 public void testBulkLoad() throws Exception { 1178 try { 1179 FileSystem fs = TEST_UTIL.getTestFileSystem(); 1180 final Path dir = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoad"); 1181 fs.mkdirs(dir); 1182 // need to make it globally writable 1183 // so users creating HFiles have write permissions 1184 fs.setPermission(dir, FS_PERMISSION_ALL); 1185 1186 AccessTestAction bulkLoadAction = new AccessTestAction() { 1187 @Override 1188 public Object run() throws Exception { 1189 int numRows = 3; 1190 1191 // Making the assumption that the test table won't split between the range 1192 byte[][][] hfileRanges = { { { (byte) 0 }, { (byte) 9 } } }; 1193 1194 Path bulkLoadBasePath = new Path(dir, new Path(User.getCurrent().getName())); 1195 new BulkLoadHelper(bulkLoadBasePath) 1196 .initHFileData(TEST_FAMILY, TEST_QUALIFIER, hfileRanges, numRows, FS_PERMISSION_ALL) 1197 .bulkLoadHFile(TEST_TABLE); 1198 return null; 1199 } 1200 }; 1201 1202 // User performing bulk loads must have privilege to read table metadata 1203 // (ADMIN or CREATE) 1204 verifyAllowed(bulkLoadAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, 1205 USER_GROUP_CREATE, USER_GROUP_ADMIN); 1206 verifyDenied(bulkLoadAction, USER_RW, USER_NONE, USER_RO, USER_GROUP_READ, USER_GROUP_WRITE); 1207 } finally { 1208 // Reinit after the bulk upload 1209 TEST_UTIL.getAdmin().disableTable(TEST_TABLE); 1210 TEST_UTIL.getAdmin().enableTable(TEST_TABLE); 1211 } 1212 } 1213 1214 private class BulkLoadAccessTestAction implements AccessTestAction { 1215 private FsPermission filePermission; 1216 private Path testDataDir; 1217 1218 public BulkLoadAccessTestAction(FsPermission perm, Path testDataDir) { 1219 this.filePermission = perm; 1220 this.testDataDir = testDataDir; 1221 } 1222 1223 @Override 1224 public Object run() throws Exception { 1225 FileSystem fs = TEST_UTIL.getTestFileSystem(); 1226 fs.mkdirs(testDataDir); 1227 fs.setPermission(testDataDir, FS_PERMISSION_ALL); 1228 // Making the assumption that the test table won't split between the range 1229 byte[][][] hfileRanges = { { { (byte) 0 }, { (byte) 9 } } }; 1230 Path bulkLoadBasePath = new Path(testDataDir, new Path(User.getCurrent().getName())); 1231 new BulkLoadHelper(bulkLoadBasePath) 1232 .initHFileData(TEST_FAMILY, TEST_QUALIFIER, hfileRanges, 3, filePermission) 1233 .bulkLoadHFile(TEST_TABLE); 1234 return null; 1235 } 1236 } 1237 1238 @Test 1239 public void testBulkLoadWithoutWritePermission() throws Exception { 1240 // Use the USER_CREATE to initialize the source directory. 1241 Path testDataDir0 = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoadWithoutWritePermission0"); 1242 Path testDataDir1 = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoadWithoutWritePermission1"); 1243 AccessTestAction bulkLoadAction1 = 1244 new BulkLoadAccessTestAction(FsPermission.valueOf("-r-xr-xr-x"), testDataDir0); 1245 AccessTestAction bulkLoadAction2 = 1246 new BulkLoadAccessTestAction(FS_PERMISSION_ALL, testDataDir1); 1247 // Test the incorrect case. 1248 BulkLoadHelper.setPermission(TEST_UTIL.getTestFileSystem(), 1249 TEST_UTIL.getTestFileSystem().getWorkingDirectory(), FS_PERMISSION_ALL); 1250 try { 1251 USER_CREATE.runAs(bulkLoadAction1); 1252 fail("Should fail because the hbase user has no write permission on hfiles."); 1253 } catch (IOException e) { 1254 } 1255 // Ensure the correct case. 1256 USER_CREATE.runAs(bulkLoadAction2); 1257 } 1258 1259 public static class BulkLoadHelper { 1260 private final FileSystem fs; 1261 private final Path loadPath; 1262 private final Configuration conf; 1263 1264 public BulkLoadHelper(Path loadPath) throws IOException { 1265 fs = TEST_UTIL.getTestFileSystem(); 1266 conf = TEST_UTIL.getConfiguration(); 1267 loadPath = loadPath.makeQualified(fs); 1268 this.loadPath = loadPath; 1269 } 1270 1271 private void createHFile(Path path, byte[] family, byte[] qualifier, byte[] startKey, 1272 byte[] endKey, int numRows) throws IOException { 1273 HFile.Writer writer = null; 1274 long now = EnvironmentEdgeManager.currentTime(); 1275 try { 1276 HFileContext context = new HFileContextBuilder().build(); 1277 writer = HFile.getWriterFactory(conf, new CacheConfig(conf)).withPath(fs, path) 1278 .withFileContext(context).create(); 1279 // subtract 2 since numRows doesn't include boundary keys 1280 for (byte[] key : Bytes.iterateOnSplits(startKey, endKey, true, numRows - 2)) { 1281 KeyValue kv = new KeyValue(key, family, qualifier, now, key); 1282 writer.append(kv); 1283 } 1284 } finally { 1285 if (writer != null) { 1286 writer.close(); 1287 } 1288 } 1289 } 1290 1291 private BulkLoadHelper initHFileData(byte[] family, byte[] qualifier, byte[][][] hfileRanges, 1292 int numRowsPerRange, FsPermission filePermission) throws Exception { 1293 Path familyDir = new Path(loadPath, Bytes.toString(family)); 1294 fs.mkdirs(familyDir); 1295 int hfileIdx = 0; 1296 List<Path> hfiles = new ArrayList<>(); 1297 for (byte[][] range : hfileRanges) { 1298 byte[] from = range[0]; 1299 byte[] to = range[1]; 1300 Path hfile = new Path(familyDir, "hfile_" + (hfileIdx++)); 1301 hfiles.add(hfile); 1302 createHFile(hfile, family, qualifier, from, to, numRowsPerRange); 1303 } 1304 // set global read so RegionServer can move it 1305 setPermission(fs, loadPath, FS_PERMISSION_ALL); 1306 // Ensure the file permission as requested. 1307 for (Path hfile : hfiles) { 1308 setPermission(fs, hfile, filePermission); 1309 } 1310 return this; 1311 } 1312 1313 private void bulkLoadHFile(TableName tableName) throws Exception { 1314 TEST_UTIL.waitUntilAllRegionsAssigned(tableName); 1315 BulkLoadHFiles.create(conf).bulkLoad(tableName, loadPath); 1316 } 1317 1318 private static void setPermission(FileSystem fs, Path dir, FsPermission perm) 1319 throws IOException { 1320 if (!fs.getFileStatus(dir).isDirectory()) { 1321 fs.setPermission(dir, perm); 1322 } else { 1323 for (FileStatus el : fs.listStatus(dir)) { 1324 fs.setPermission(el.getPath(), perm); 1325 setPermission(fs, el.getPath(), perm); 1326 } 1327 } 1328 } 1329 } 1330 1331 @Test 1332 public void testAppend() throws Exception { 1333 1334 AccessTestAction appendAction = new AccessTestAction() { 1335 @Override 1336 public Object run() throws Exception { 1337 byte[] row = TEST_ROW; 1338 byte[] qualifier = TEST_QUALIFIER; 1339 Put put = new Put(row); 1340 put.addColumn(TEST_FAMILY, qualifier, Bytes.toBytes(1)); 1341 Append append = new Append(row); 1342 append.addColumn(TEST_FAMILY, qualifier, Bytes.toBytes(2)); 1343 try (Connection conn = ConnectionFactory.createConnection(conf); 1344 Table t = conn.getTable(TEST_TABLE)) { 1345 t.put(put); 1346 t.append(append); 1347 } 1348 return null; 1349 } 1350 }; 1351 1352 verifyAllowed(appendAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW, 1353 USER_GROUP_WRITE); 1354 verifyDenied(appendAction, USER_RO, USER_NONE, USER_GROUP_CREATE, USER_GROUP_READ, 1355 USER_GROUP_ADMIN); 1356 } 1357 1358 @Test 1359 public void testGrantRevoke() throws Exception { 1360 AccessTestAction grantAction = new AccessTestAction() { 1361 @Override 1362 public Object run() throws Exception { 1363 try (Connection conn = ConnectionFactory.createConnection(conf)) { 1364 conn.getAdmin().grant(new UserPermission(USER_RO.getShortName(), Permission 1365 .newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withActions(Action.READ).build()), 1366 false); 1367 } 1368 return null; 1369 } 1370 }; 1371 1372 AccessTestAction revokeAction = new AccessTestAction() { 1373 @Override 1374 public Object run() throws Exception { 1375 try (Connection conn = ConnectionFactory.createConnection(conf)) { 1376 conn.getAdmin().revoke(new UserPermission(USER_RO.getShortName(), Permission 1377 .newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withActions(Action.READ).build())); 1378 } 1379 return null; 1380 } 1381 }; 1382 1383 AccessTestAction getTablePermissionsAction = new AccessTestAction() { 1384 @Override 1385 public Object run() throws Exception { 1386 try (Connection conn = ConnectionFactory.createConnection(conf)) { 1387 conn.getAdmin() 1388 .getUserPermissions(GetUserPermissionsRequest.newBuilder(TEST_TABLE).build()); 1389 } 1390 return null; 1391 } 1392 }; 1393 1394 AccessTestAction getGlobalPermissionsAction = new AccessTestAction() { 1395 @Override 1396 public Object run() throws Exception { 1397 try (Connection conn = ConnectionFactory.createConnection(conf)) { 1398 conn.getAdmin().getUserPermissions(GetUserPermissionsRequest.newBuilder().build()); 1399 } 1400 return null; 1401 } 1402 }; 1403 1404 AccessTestAction preGrantAction = new AccessTestAction() { 1405 @Override 1406 public Object run() throws Exception { 1407 ACCESS_CONTROLLER.preGrant(ObserverContextImpl.createAndPrepare(CP_ENV), 1408 new UserPermission(USER_RO.getShortName(), Permission.newBuilder(TEST_TABLE) 1409 .withFamily(TEST_FAMILY).withActions(Action.READ).build()), 1410 false); 1411 return null; 1412 } 1413 }; 1414 1415 AccessTestAction preRevokeAction = new AccessTestAction() { 1416 @Override 1417 public Object run() throws Exception { 1418 ACCESS_CONTROLLER.preRevoke(ObserverContextImpl.createAndPrepare(CP_ENV), 1419 new UserPermission(USER_RO.getShortName(), Permission.newBuilder(TEST_TABLE) 1420 .withFamily(TEST_FAMILY).withActions(Action.READ).build())); 1421 return null; 1422 } 1423 }; 1424 1425 AccessTestAction grantCPAction = new AccessTestAction() { 1426 @Override 1427 public Object run() throws Exception { 1428 try (Connection conn = ConnectionFactory.createConnection(conf); 1429 Table acl = conn.getTable(PermissionStorage.ACL_TABLE_NAME)) { 1430 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName()); 1431 AccessControlService.BlockingInterface protocol = 1432 AccessControlService.newBlockingStub(service); 1433 AccessControlUtil.grant(null, protocol, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, 1434 null, false, Action.READ); 1435 } 1436 return null; 1437 } 1438 }; 1439 1440 AccessTestAction revokeCPAction = new AccessTestAction() { 1441 @Override 1442 public Object run() throws Exception { 1443 try (Connection conn = ConnectionFactory.createConnection(conf); 1444 Table acl = conn.getTable(PermissionStorage.ACL_TABLE_NAME)) { 1445 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName()); 1446 AccessControlService.BlockingInterface protocol = 1447 AccessControlService.newBlockingStub(service); 1448 AccessControlUtil.revoke(null, protocol, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, 1449 null, Action.READ); 1450 } 1451 return null; 1452 } 1453 }; 1454 1455 verifyAllowed(grantAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1456 verifyDenied(grantAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1457 USER_GROUP_WRITE, USER_GROUP_CREATE); 1458 try { 1459 verifyAllowed(revokeAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1460 verifyDenied(revokeAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1461 USER_GROUP_WRITE, USER_GROUP_CREATE); 1462 1463 verifyAllowed(getTablePermissionsAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1464 verifyDenied(getTablePermissionsAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, 1465 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 1466 1467 verifyAllowed(getGlobalPermissionsAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 1468 verifyDenied(getGlobalPermissionsAction, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, 1469 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 1470 1471 verifyAllowed(preGrantAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1472 verifyDenied(preGrantAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1473 USER_GROUP_WRITE, USER_GROUP_CREATE); 1474 1475 verifyAllowed(preRevokeAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1476 verifyDenied(preRevokeAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1477 USER_GROUP_WRITE, USER_GROUP_CREATE); 1478 1479 verifyAllowed(grantCPAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1480 verifyDenied(grantCPAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1481 USER_GROUP_WRITE, USER_GROUP_CREATE); 1482 1483 verifyAllowed(revokeCPAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 1484 verifyDenied(revokeCPAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 1485 USER_GROUP_WRITE, USER_GROUP_CREATE); 1486 } finally { 1487 // Cleanup, Grant the revoked permission back to the user 1488 grantOnTable(TEST_UTIL, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null, 1489 Permission.Action.READ); 1490 } 1491 } 1492 1493 @Test 1494 public void testPostGrantRevoke() throws Exception { 1495 final TableName tableName = TableName.valueOf("TempTable"); 1496 final byte[] family1 = Bytes.toBytes("f1"); 1497 final byte[] family2 = Bytes.toBytes("f2"); 1498 final byte[] qualifier = Bytes.toBytes("q"); 1499 1500 // create table 1501 Admin admin = TEST_UTIL.getAdmin(); 1502 if (admin.tableExists(tableName)) { 1503 deleteTable(TEST_UTIL, tableName); 1504 } 1505 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 1506 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family1)) 1507 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family2)).build(); 1508 createTable(TEST_UTIL, tableDescriptor); 1509 try { 1510 // create temp users 1511 User tblUser = 1512 User.createUserForTesting(TEST_UTIL.getConfiguration(), "tbluser", new String[0]); 1513 User gblUser = 1514 User.createUserForTesting(TEST_UTIL.getConfiguration(), "gbluser", new String[0]); 1515 1516 // prepare actions: 1517 AccessTestAction putActionAll = new AccessTestAction() { 1518 @Override 1519 public Object run() throws Exception { 1520 Put p = new Put(Bytes.toBytes("a")); 1521 p.addColumn(family1, qualifier, Bytes.toBytes("v1")); 1522 p.addColumn(family2, qualifier, Bytes.toBytes("v2")); 1523 1524 try (Connection conn = ConnectionFactory.createConnection(conf); 1525 Table t = conn.getTable(tableName)) { 1526 t.put(p); 1527 } 1528 return null; 1529 } 1530 }; 1531 1532 AccessTestAction putAction1 = new AccessTestAction() { 1533 @Override 1534 public Object run() throws Exception { 1535 Put p = new Put(Bytes.toBytes("a")); 1536 p.addColumn(family1, qualifier, Bytes.toBytes("v1")); 1537 1538 try (Connection conn = ConnectionFactory.createConnection(conf); 1539 Table t = conn.getTable(tableName)) { 1540 t.put(p); 1541 } 1542 return null; 1543 } 1544 }; 1545 1546 AccessTestAction putAction2 = new AccessTestAction() { 1547 @Override 1548 public Object run() throws Exception { 1549 Put p = new Put(Bytes.toBytes("a")); 1550 p.addColumn(family2, qualifier, Bytes.toBytes("v2")); 1551 try (Connection conn = ConnectionFactory.createConnection(conf); 1552 Table t = conn.getTable(tableName)) { 1553 t.put(p); 1554 } 1555 return null; 1556 } 1557 }; 1558 1559 AccessTestAction getActionAll = new AccessTestAction() { 1560 @Override 1561 public Object run() throws Exception { 1562 Get g = new Get(TEST_ROW); 1563 g.addFamily(family1); 1564 g.addFamily(family2); 1565 try (Connection conn = ConnectionFactory.createConnection(conf); 1566 Table t = conn.getTable(tableName)) { 1567 t.get(g); 1568 } 1569 return null; 1570 } 1571 }; 1572 1573 AccessTestAction getAction1 = new AccessTestAction() { 1574 @Override 1575 public Object run() throws Exception { 1576 Get g = new Get(TEST_ROW); 1577 g.addFamily(family1); 1578 try (Connection conn = ConnectionFactory.createConnection(conf); 1579 Table t = conn.getTable(tableName)) { 1580 t.get(g); 1581 } 1582 return null; 1583 } 1584 }; 1585 1586 AccessTestAction getAction2 = new AccessTestAction() { 1587 @Override 1588 public Object run() throws Exception { 1589 Get g = new Get(TEST_ROW); 1590 g.addFamily(family2); 1591 try (Connection conn = ConnectionFactory.createConnection(conf); 1592 Table t = conn.getTable(tableName)) { 1593 t.get(g); 1594 } 1595 return null; 1596 } 1597 }; 1598 1599 AccessTestAction deleteActionAll = new AccessTestAction() { 1600 @Override 1601 public Object run() throws Exception { 1602 Delete d = new Delete(TEST_ROW); 1603 d.addFamily(family1); 1604 d.addFamily(family2); 1605 try (Connection conn = ConnectionFactory.createConnection(conf); 1606 Table t = conn.getTable(tableName)) { 1607 t.delete(d); 1608 } 1609 return null; 1610 } 1611 }; 1612 1613 AccessTestAction deleteAction1 = new AccessTestAction() { 1614 @Override 1615 public Object run() throws Exception { 1616 Delete d = new Delete(TEST_ROW); 1617 d.addFamily(family1); 1618 try (Connection conn = ConnectionFactory.createConnection(conf); 1619 Table t = conn.getTable(tableName)) { 1620 t.delete(d); 1621 } 1622 return null; 1623 } 1624 }; 1625 1626 AccessTestAction deleteAction2 = new AccessTestAction() { 1627 @Override 1628 public Object run() throws Exception { 1629 Delete d = new Delete(TEST_ROW); 1630 d.addFamily(family2); 1631 try (Connection conn = ConnectionFactory.createConnection(conf); 1632 Table t = conn.getTable(tableName)) { 1633 t.delete(d); 1634 } 1635 return null; 1636 } 1637 }; 1638 1639 // initial check: 1640 verifyDenied(tblUser, getActionAll, getAction1, getAction2); 1641 verifyDenied(tblUser, putActionAll, putAction1, putAction2); 1642 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1643 1644 verifyDenied(gblUser, getActionAll, getAction1, getAction2); 1645 verifyDenied(gblUser, putActionAll, putAction1, putAction2); 1646 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1647 1648 // grant table read permission 1649 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.READ); 1650 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null, 1651 Permission.Action.READ); 1652 1653 // check 1654 verifyAllowed(tblUser, getActionAll, getAction1, getAction2); 1655 verifyDenied(tblUser, putActionAll, putAction1, putAction2); 1656 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1657 1658 verifyAllowed(gblUser, getActionAll, getAction1, getAction2); 1659 verifyDenied(gblUser, putActionAll, putAction1, putAction2); 1660 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1661 1662 // grant table write permission while revoking read permissions 1663 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.WRITE); 1664 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null, 1665 Permission.Action.WRITE); 1666 1667 verifyDenied(tblUser, getActionAll, getAction1, getAction2); 1668 verifyAllowed(tblUser, putActionAll, putAction1, putAction2); 1669 verifyAllowed(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1670 1671 verifyDenied(gblUser, getActionAll, getAction1, getAction2); 1672 verifyAllowed(gblUser, putActionAll, putAction1, putAction2); 1673 verifyAllowed(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1674 1675 // revoke table permissions 1676 revokeGlobal(TEST_UTIL, gblUser.getShortName()); 1677 revokeFromTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null); 1678 1679 verifyDenied(tblUser, getActionAll, getAction1, getAction2); 1680 verifyDenied(tblUser, putActionAll, putAction1, putAction2); 1681 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1682 1683 verifyDenied(gblUser, getActionAll, getAction1, getAction2); 1684 verifyDenied(gblUser, putActionAll, putAction1, putAction2); 1685 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1686 1687 // grant column family read permission 1688 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.READ); 1689 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, family1, null, 1690 Permission.Action.READ); 1691 1692 // Access should be denied for family2 1693 verifyAllowed(tblUser, getActionAll, getAction1); 1694 verifyDenied(tblUser, getAction2); 1695 verifyDenied(tblUser, putActionAll, putAction1, putAction2); 1696 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1697 1698 verifyAllowed(gblUser, getActionAll, getAction1, getAction2); 1699 verifyDenied(gblUser, putActionAll, putAction1, putAction2); 1700 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1701 1702 // grant column family write permission 1703 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.WRITE); 1704 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, family2, null, 1705 Permission.Action.WRITE); 1706 1707 // READ from family1, WRITE to family2 are allowed 1708 verifyAllowed(tblUser, getActionAll, getAction1); 1709 verifyAllowed(tblUser, putAction2, deleteAction2); 1710 verifyDenied(tblUser, getAction2); 1711 verifyDenied(tblUser, putActionAll, putAction1); 1712 verifyDenied(tblUser, deleteActionAll, deleteAction1); 1713 1714 verifyDenied(gblUser, getActionAll, getAction1, getAction2); 1715 verifyAllowed(gblUser, putActionAll, putAction1, putAction2); 1716 verifyAllowed(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1717 1718 // revoke column family permission 1719 revokeGlobal(TEST_UTIL, gblUser.getShortName()); 1720 revokeFromTable(TEST_UTIL, tblUser.getShortName(), tableName, family2, null); 1721 1722 // Revoke on family2 should not have impact on family1 permissions 1723 verifyAllowed(tblUser, getActionAll, getAction1); 1724 verifyDenied(tblUser, getAction2); 1725 verifyDenied(tblUser, putActionAll, putAction1, putAction2); 1726 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2); 1727 1728 // Should not have access as global permissions are completely revoked 1729 verifyDenied(gblUser, getActionAll, getAction1, getAction2); 1730 verifyDenied(gblUser, putActionAll, putAction1, putAction2); 1731 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2); 1732 } finally { 1733 // delete table 1734 deleteTable(TEST_UTIL, tableName); 1735 } 1736 } 1737 1738 private boolean hasFoundUserPermission(List<UserPermission> userPermissions, 1739 List<UserPermission> perms) { 1740 return perms.containsAll(userPermissions); 1741 } 1742 1743 private boolean hasFoundUserPermission(UserPermission userPermission, 1744 List<UserPermission> perms) { 1745 return perms.contains(userPermission); 1746 } 1747 1748 @Test 1749 public void testPostGrantRevokeAtQualifierLevel(TestInfo testInfo) throws Exception { 1750 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 1751 final byte[] family1 = Bytes.toBytes("f1"); 1752 final byte[] family2 = Bytes.toBytes("f2"); 1753 final byte[] qualifier = Bytes.toBytes("q"); 1754 1755 // create table 1756 Admin admin = TEST_UTIL.getAdmin(); 1757 if (admin.tableExists(tableName)) { 1758 deleteTable(TEST_UTIL, tableName); 1759 } 1760 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 1761 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family1)) 1762 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family2)).build(); 1763 createTable(TEST_UTIL, tableDescriptor); 1764 1765 try { 1766 // create temp users 1767 User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "user", new String[0]); 1768 1769 AccessTestAction getQualifierAction = new AccessTestAction() { 1770 @Override 1771 public Object run() throws Exception { 1772 Get g = new Get(TEST_ROW); 1773 g.addColumn(family1, qualifier); 1774 try (Connection conn = ConnectionFactory.createConnection(conf); 1775 Table t = conn.getTable(tableName)) { 1776 t.get(g); 1777 } 1778 return null; 1779 } 1780 }; 1781 1782 AccessTestAction putQualifierAction = new AccessTestAction() { 1783 @Override 1784 public Object run() throws Exception { 1785 Put p = new Put(TEST_ROW); 1786 p.addColumn(family1, qualifier, Bytes.toBytes("v1")); 1787 try (Connection conn = ConnectionFactory.createConnection(conf); 1788 Table t = conn.getTable(tableName)) { 1789 t.put(p); 1790 } 1791 return null; 1792 } 1793 }; 1794 1795 AccessTestAction deleteQualifierAction = new AccessTestAction() { 1796 @Override 1797 public Object run() throws Exception { 1798 Delete d = new Delete(TEST_ROW); 1799 d.addColumn(family1, qualifier); 1800 // d.deleteFamily(family1); 1801 try (Connection conn = ConnectionFactory.createConnection(conf); 1802 Table t = conn.getTable(tableName)) { 1803 t.delete(d); 1804 } 1805 return null; 1806 } 1807 }; 1808 1809 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, null); 1810 1811 verifyDenied(user, getQualifierAction); 1812 verifyDenied(user, putQualifierAction); 1813 verifyDenied(user, deleteQualifierAction); 1814 1815 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1816 Permission.Action.READ); 1817 1818 verifyAllowed(user, getQualifierAction); 1819 verifyDenied(user, putQualifierAction); 1820 verifyDenied(user, deleteQualifierAction); 1821 1822 // only grant write permission 1823 // TODO: comment this portion after HBASE-3583 1824 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1825 Permission.Action.WRITE); 1826 1827 verifyDenied(user, getQualifierAction); 1828 verifyAllowed(user, putQualifierAction); 1829 verifyAllowed(user, deleteQualifierAction); 1830 1831 // grant both read and write permission 1832 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1833 Permission.Action.READ, Permission.Action.WRITE); 1834 1835 verifyAllowed(user, getQualifierAction); 1836 verifyAllowed(user, putQualifierAction); 1837 verifyAllowed(user, deleteQualifierAction); 1838 1839 // revoke family level permission won't impact column level 1840 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier); 1841 1842 verifyDenied(user, getQualifierAction); 1843 verifyDenied(user, putQualifierAction); 1844 verifyDenied(user, deleteQualifierAction); 1845 } finally { 1846 // delete table 1847 deleteTable(TEST_UTIL, tableName); 1848 } 1849 } 1850 1851 @Test 1852 public void testPermissionList(TestInfo testInfo) throws Exception { 1853 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 1854 final byte[] family1 = Bytes.toBytes("f1"); 1855 final byte[] family2 = Bytes.toBytes("f2"); 1856 final byte[] qualifier = Bytes.toBytes("q"); 1857 1858 // create table 1859 Admin admin = TEST_UTIL.getAdmin(); 1860 if (admin.tableExists(tableName)) { 1861 deleteTable(TEST_UTIL, tableName); 1862 } 1863 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 1864 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family1)) 1865 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family2)).build(); 1866 createTable(TEST_UTIL, USER_OWNER, tableDescriptor); 1867 try { 1868 List<UserPermission> perms = 1869 admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build()); 1870 UserPermission ownerperm = new UserPermission(USER_OWNER.getName(), 1871 Permission.newBuilder(tableName).withActions(Action.values()).build()); 1872 assertTrue(hasFoundUserPermission(ownerperm, perms), 1873 "Owner should have all permissions on table"); 1874 1875 User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "user", new String[0]); 1876 String userName = user.getShortName(); 1877 1878 UserPermission up = new UserPermission(userName, Permission.newBuilder(tableName) 1879 .withFamily(family1).withQualifier(qualifier).withActions(Permission.Action.READ).build()); 1880 assertFalse(hasFoundUserPermission(up, perms), 1881 "User should not be granted permission: " + up.toString()); 1882 1883 // grant read permission 1884 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1885 Permission.Action.READ); 1886 1887 perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build()); 1888 UserPermission upToVerify = new UserPermission(userName, Permission.newBuilder(tableName) 1889 .withFamily(family1).withQualifier(qualifier).withActions(Permission.Action.READ).build()); 1890 assertTrue(hasFoundUserPermission(upToVerify, perms), 1891 "User should be granted permission: " + upToVerify.toString()); 1892 1893 upToVerify = new UserPermission(userName, Permission.newBuilder(tableName).withFamily(family1) 1894 .withQualifier(qualifier).withActions(Permission.Action.WRITE).build()); 1895 assertFalse(hasFoundUserPermission(upToVerify, perms), 1896 "User should not be granted permission: " + upToVerify.toString()); 1897 1898 // grant read+write 1899 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1900 Permission.Action.WRITE, Permission.Action.READ); 1901 1902 perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build()); 1903 upToVerify = new UserPermission(userName, 1904 Permission.newBuilder(tableName).withFamily(family1).withQualifier(qualifier) 1905 .withActions(Permission.Action.WRITE, Permission.Action.READ).build()); 1906 assertTrue(hasFoundUserPermission(upToVerify, perms), 1907 "User should be granted permission: " + upToVerify.toString()); 1908 1909 // revoke 1910 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier, 1911 Permission.Action.WRITE, Permission.Action.READ); 1912 1913 perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build()); 1914 assertFalse(hasFoundUserPermission(upToVerify, perms), 1915 "User should not be granted permission: " + upToVerify.toString()); 1916 1917 User newOwner = User.createUserForTesting(conf, "new_owner", new String[] {}); 1918 grantOnTable(TEST_UTIL, newOwner.getShortName(), tableName, null, null, 1919 Permission.Action.values()); 1920 1921 perms = admin.getUserPermissions(GetUserPermissionsRequest.newBuilder(tableName).build()); 1922 UserPermission newOwnerperm = new UserPermission(newOwner.getName(), 1923 Permission.newBuilder(tableName).withActions(Action.values()).build()); 1924 assertTrue(hasFoundUserPermission(newOwnerperm, perms), 1925 "New owner should have all permissions on table"); 1926 } finally { 1927 // delete table 1928 deleteTable(TEST_UTIL, tableName); 1929 } 1930 } 1931 1932 @Test 1933 public void testGlobalPermissionList() throws Exception { 1934 List<UserPermission> perms = systemUserConnection.getAdmin() 1935 .getUserPermissions(GetUserPermissionsRequest.newBuilder().build()); 1936 1937 Collection<String> superUsers = Superusers.getSuperUsers(); 1938 List<UserPermission> adminPerms = new ArrayList<>(superUsers.size() + 1); 1939 adminPerms.add(new UserPermission(USER_ADMIN.getShortName(), Permission.newBuilder() 1940 .withActions(Action.ADMIN, Action.CREATE, Action.READ, Action.WRITE).build())); 1941 for (String user : superUsers) { 1942 // Global permission 1943 adminPerms.add( 1944 new UserPermission(user, Permission.newBuilder().withActions(Action.values()).build())); 1945 } 1946 assertTrue(perms.size() == 6 + superUsers.size() && hasFoundUserPermission(adminPerms, perms), 1947 "Only super users, global users and user admin has permission on table hbase:acl " 1948 + "per setup"); 1949 } 1950 1951 /** global operations */ 1952 private void verifyGlobal(AccessTestAction action) throws Exception { 1953 verifyAllowed(action, SUPERUSER); 1954 1955 verifyDenied(action, USER_CREATE, USER_RW, USER_NONE, USER_RO); 1956 } 1957 1958 @Test 1959 public void testCheckPermissions() throws Exception { 1960 // -------------------------------------- 1961 // test global permissions 1962 AccessTestAction globalAdmin = new AccessTestAction() { 1963 @Override 1964 public Void run() throws Exception { 1965 checkGlobalPerms(TEST_UTIL, Permission.Action.ADMIN); 1966 return null; 1967 } 1968 }; 1969 // verify that only superuser can admin 1970 verifyGlobal(globalAdmin); 1971 1972 // -------------------------------------- 1973 // test multiple permissions 1974 AccessTestAction globalReadWrite = new AccessTestAction() { 1975 @Override 1976 public Void run() throws Exception { 1977 checkGlobalPerms(TEST_UTIL, Permission.Action.READ, Permission.Action.WRITE); 1978 return null; 1979 } 1980 }; 1981 1982 verifyGlobal(globalReadWrite); 1983 1984 // -------------------------------------- 1985 // table/column/qualifier level permissions 1986 final byte[] TEST_Q1 = Bytes.toBytes("q1"); 1987 final byte[] TEST_Q2 = Bytes.toBytes("q2"); 1988 1989 User userTable = User.createUserForTesting(conf, "user_check_perms_table", new String[0]); 1990 User userColumn = User.createUserForTesting(conf, "user_check_perms_family", new String[0]); 1991 User userQualifier = User.createUserForTesting(conf, "user_check_perms_q", new String[0]); 1992 1993 grantOnTable(TEST_UTIL, userTable.getShortName(), TEST_TABLE, null, null, 1994 Permission.Action.READ); 1995 grantOnTable(TEST_UTIL, userColumn.getShortName(), TEST_TABLE, TEST_FAMILY, null, 1996 Permission.Action.READ); 1997 grantOnTable(TEST_UTIL, userQualifier.getShortName(), TEST_TABLE, TEST_FAMILY, TEST_Q1, 1998 Permission.Action.READ); 1999 2000 try { 2001 AccessTestAction tableRead = new AccessTestAction() { 2002 @Override 2003 public Void run() throws Exception { 2004 checkTablePerms(TEST_UTIL, TEST_TABLE, null, null, Permission.Action.READ); 2005 return null; 2006 } 2007 }; 2008 2009 AccessTestAction columnRead = new AccessTestAction() { 2010 @Override 2011 public Void run() throws Exception { 2012 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, null, Permission.Action.READ); 2013 return null; 2014 } 2015 }; 2016 2017 AccessTestAction qualifierRead = new AccessTestAction() { 2018 @Override 2019 public Void run() throws Exception { 2020 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, TEST_Q1, Permission.Action.READ); 2021 return null; 2022 } 2023 }; 2024 2025 AccessTestAction multiQualifierRead = new AccessTestAction() { 2026 @Override 2027 public Void run() throws Exception { 2028 checkTablePerms(TEST_UTIL, 2029 new Permission[] { 2030 Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withQualifier(TEST_Q1) 2031 .withActions(Permission.Action.READ).build(), 2032 Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withQualifier(TEST_Q2) 2033 .withActions(Permission.Action.READ).build(), }); 2034 return null; 2035 } 2036 }; 2037 2038 AccessTestAction globalAndTableRead = new AccessTestAction() { 2039 @Override 2040 public Void run() throws Exception { 2041 checkTablePerms(TEST_UTIL, new Permission[] { new Permission(Permission.Action.READ), 2042 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build() }); 2043 return null; 2044 } 2045 }; 2046 2047 AccessTestAction noCheck = new AccessTestAction() { 2048 @Override 2049 public Void run() throws Exception { 2050 checkTablePerms(TEST_UTIL, new Permission[0]); 2051 return null; 2052 } 2053 }; 2054 2055 verifyAllowed(tableRead, SUPERUSER, userTable); 2056 verifyDenied(tableRead, userColumn, userQualifier); 2057 2058 verifyAllowed(columnRead, SUPERUSER, userTable, userColumn); 2059 verifyDenied(columnRead, userQualifier); 2060 2061 verifyAllowed(qualifierRead, SUPERUSER, userTable, userColumn, userQualifier); 2062 2063 verifyAllowed(multiQualifierRead, SUPERUSER, userTable, userColumn); 2064 verifyDenied(multiQualifierRead, userQualifier); 2065 2066 verifyAllowed(globalAndTableRead, SUPERUSER); 2067 verifyDenied(globalAndTableRead, userTable, userColumn, userQualifier); 2068 2069 verifyAllowed(noCheck, SUPERUSER, userTable, userColumn, userQualifier); 2070 2071 // -------------------------------------- 2072 // test family level multiple permissions 2073 AccessTestAction familyReadWrite = new AccessTestAction() { 2074 @Override 2075 public Void run() throws Exception { 2076 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, null, Permission.Action.READ, 2077 Permission.Action.WRITE); 2078 return null; 2079 } 2080 }; 2081 2082 verifyAllowed(familyReadWrite, SUPERUSER, USER_OWNER, USER_CREATE, USER_RW); 2083 verifyDenied(familyReadWrite, USER_NONE, USER_RO); 2084 2085 // -------------------------------------- 2086 // check for wrong table region 2087 CheckPermissionsRequest checkRequest = CheckPermissionsRequest.newBuilder() 2088 .addPermission(AccessControlProtos.Permission.newBuilder() 2089 .setType(AccessControlProtos.Permission.Type.Table) 2090 .setTablePermission(AccessControlProtos.TablePermission.newBuilder() 2091 .setTableName(ProtobufUtil.toProtoTableName(TEST_TABLE)) 2092 .addAction(AccessControlProtos.Permission.Action.CREATE))) 2093 .build(); 2094 Table acl = systemUserConnection.getTable(PermissionStorage.ACL_TABLE_NAME); 2095 try { 2096 BlockingRpcChannel channel = acl.coprocessorService(new byte[0]); 2097 AccessControlService.BlockingInterface protocol = 2098 AccessControlService.newBlockingStub(channel); 2099 try { 2100 // but ask for TablePermissions for TEST_TABLE 2101 protocol.checkPermissions(null, checkRequest); 2102 fail("this should have thrown CoprocessorException"); 2103 } catch (ServiceException ex) { 2104 // expected 2105 } 2106 } finally { 2107 acl.close(); 2108 } 2109 2110 } finally { 2111 revokeFromTable(TEST_UTIL, userTable.getShortName(), TEST_TABLE, null, null, 2112 Permission.Action.READ); 2113 revokeFromTable(TEST_UTIL, userColumn.getShortName(), TEST_TABLE, TEST_FAMILY, null, 2114 Permission.Action.READ); 2115 revokeFromTable(TEST_UTIL, userQualifier.getShortName(), TEST_TABLE, TEST_FAMILY, TEST_Q1, 2116 Permission.Action.READ); 2117 } 2118 } 2119 2120 @Test 2121 public void testStopRegionServer() throws Exception { 2122 AccessTestAction action = new AccessTestAction() { 2123 @Override 2124 public Object run() throws Exception { 2125 ACCESS_CONTROLLER.preStopRegionServer(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 2126 return null; 2127 } 2128 }; 2129 2130 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2131 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2132 USER_GROUP_WRITE, USER_GROUP_CREATE); 2133 } 2134 2135 @Test 2136 public void testRollWALWriterRequest() throws Exception { 2137 AccessTestAction action = new AccessTestAction() { 2138 @Override 2139 public Object run() throws Exception { 2140 ACCESS_CONTROLLER.preRollWALWriterRequest(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 2141 return null; 2142 } 2143 }; 2144 2145 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2146 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2147 USER_GROUP_WRITE, USER_GROUP_CREATE); 2148 } 2149 2150 @Test 2151 public void testOpenRegion() throws Exception { 2152 AccessTestAction action = new AccessTestAction() { 2153 @Override 2154 public Object run() throws Exception { 2155 ACCESS_CONTROLLER.preOpen(ObserverContextImpl.createAndPrepare(RCP_ENV)); 2156 return null; 2157 } 2158 }; 2159 2160 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2161 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_GROUP_CREATE, 2162 USER_GROUP_READ, USER_GROUP_WRITE); 2163 } 2164 2165 @Test 2166 public void testCloseRegion() throws Exception { 2167 AccessTestAction action = new AccessTestAction() { 2168 @Override 2169 public Object run() throws Exception { 2170 ACCESS_CONTROLLER.preClose(ObserverContextImpl.createAndPrepare(RCP_ENV), false); 2171 return null; 2172 } 2173 }; 2174 2175 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2176 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_GROUP_CREATE, 2177 USER_GROUP_READ, USER_GROUP_WRITE); 2178 } 2179 2180 @Test 2181 public void testSnapshot() throws Exception { 2182 Admin admin = TEST_UTIL.getAdmin(); 2183 final TableDescriptor htd = admin.getDescriptor(TEST_TABLE); 2184 final SnapshotDescription snapshot = 2185 new SnapshotDescription(TEST_TABLE.getNameAsString() + "-snapshot", TEST_TABLE); 2186 AccessTestAction snapshotAction = new AccessTestAction() { 2187 @Override 2188 public Object run() throws Exception { 2189 ACCESS_CONTROLLER.preSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, htd); 2190 return null; 2191 } 2192 }; 2193 2194 AccessTestAction deleteAction = new AccessTestAction() { 2195 @Override 2196 public Object run() throws Exception { 2197 ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot); 2198 return null; 2199 } 2200 }; 2201 2202 AccessTestAction restoreAction = new AccessTestAction() { 2203 @Override 2204 public Object run() throws Exception { 2205 ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2206 htd); 2207 return null; 2208 } 2209 }; 2210 2211 AccessTestAction cloneAction = new AccessTestAction() { 2212 @Override 2213 public Object run() throws Exception { 2214 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2215 null); 2216 return null; 2217 } 2218 }; 2219 2220 verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2221 verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2222 USER_GROUP_WRITE, USER_GROUP_CREATE); 2223 2224 verifyAllowed(cloneAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2225 verifyDenied(cloneAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_GROUP_READ, 2226 USER_GROUP_WRITE, USER_GROUP_CREATE); 2227 2228 verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2229 verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2230 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2231 2232 verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2233 verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2234 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2235 } 2236 2237 @Test 2238 public void testSnapshotWithOwner() throws Exception { 2239 Admin admin = TEST_UTIL.getAdmin(); 2240 final TableDescriptor htd = admin.getDescriptor(TEST_TABLE); 2241 final SnapshotDescription snapshot = new SnapshotDescription( 2242 TEST_TABLE.getNameAsString() + "-snapshot", TEST_TABLE, null, USER_OWNER.getName()); 2243 2244 AccessTestAction snapshotAction = new AccessTestAction() { 2245 @Override 2246 public Object run() throws Exception { 2247 ACCESS_CONTROLLER.preSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, htd); 2248 return null; 2249 } 2250 }; 2251 verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2252 verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2253 USER_GROUP_WRITE, USER_GROUP_CREATE); 2254 2255 AccessTestAction deleteAction = new AccessTestAction() { 2256 @Override 2257 public Object run() throws Exception { 2258 ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot); 2259 return null; 2260 } 2261 }; 2262 verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2263 verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2264 USER_GROUP_WRITE, USER_GROUP_CREATE); 2265 2266 AccessTestAction restoreAction = new AccessTestAction() { 2267 @Override 2268 public Object run() throws Exception { 2269 ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2270 htd); 2271 return null; 2272 } 2273 }; 2274 verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2275 verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2276 USER_GROUP_WRITE, USER_GROUP_CREATE); 2277 } 2278 2279 @Test 2280 public void testCloneSnapshotWithOwner() throws Exception { 2281 Admin admin = TEST_UTIL.getAdmin(); 2282 final TableDescriptor originalTd = admin.getDescriptor(TEST_TABLE); 2283 final SnapshotDescription snapshot = new SnapshotDescription( 2284 TEST_TABLE.getNameAsString() + "-snapshot", TEST_TABLE, null, USER_OWNER.getName()); 2285 String namespace = "testCloneSnapshot"; 2286 NamespaceDescriptor desc = NamespaceDescriptor.create(namespace).build(); 2287 createNamespace(TEST_UTIL, desc); 2288 2289 String differentTableString = "testtable2"; 2290 TableName differentTable = TableName.valueOf(namespace, differentTableString); 2291 TableDescriptor diffrentTd = TableDescriptorBuilder.newBuilder(differentTable) 2292 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build(); 2293 2294 // recreating the original table 2295 AccessTestAction cloneOriginalAction = new AccessTestAction() { 2296 @Override 2297 public Object run() throws Exception { 2298 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2299 originalTd); 2300 return null; 2301 } 2302 }; 2303 verifyAllowed(cloneOriginalAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN, USER_OWNER); 2304 verifyDenied(cloneOriginalAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2305 USER_GROUP_WRITE, USER_GROUP_CREATE); 2306 2307 // cloning to a different table 2308 AccessTestAction cloneDifferentAction = new AccessTestAction() { 2309 @Override 2310 public Object run() throws Exception { 2311 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2312 diffrentTd); 2313 return null; 2314 } 2315 }; 2316 verifyAllowed(cloneDifferentAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2317 verifyDenied(cloneDifferentAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2318 USER_GROUP_WRITE, USER_GROUP_CREATE, USER_OWNER); 2319 2320 // cloning to a different table where user is namespace admin 2321 grantOnNamespace(TEST_UTIL, USER_OWNER.getShortName(), namespace, Action.ADMIN); 2322 2323 AccessTestAction cloneNamespaceAdminAction = new AccessTestAction() { 2324 @Override 2325 public Object run() throws Exception { 2326 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV), snapshot, 2327 diffrentTd); 2328 return null; 2329 } 2330 }; 2331 verifyAllowed(cloneNamespaceAdminAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN, USER_OWNER); 2332 verifyDenied(cloneNamespaceAdminAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, 2333 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2334 2335 deleteNamespace(TEST_UTIL, namespace); 2336 } 2337 2338 @Test 2339 public void testGlobalAuthorizationForNewRegisteredRS() throws Exception { 2340 LOG.debug("Test for global authorization for a new registered RegionServer."); 2341 SingleProcessHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster(); 2342 2343 final Admin admin = TEST_UTIL.getAdmin(); 2344 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TEST_TABLE2) 2345 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAMILY)).build(); 2346 createTable(TEST_UTIL, tableDescriptor); 2347 2348 // Starting a new RegionServer. 2349 JVMClusterUtil.RegionServerThread newRsThread = hbaseCluster.startRegionServer(); 2350 final HRegionServer newRs = newRsThread.getRegionServer(); 2351 2352 // Move region to the new RegionServer. 2353 List<HRegionLocation> regions; 2354 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE2)) { 2355 regions = locator.getAllRegionLocations(); 2356 } 2357 HRegionLocation location = regions.get(0); 2358 final RegionInfo hri = location.getRegion(); 2359 final ServerName server = location.getServerName(); 2360 try (Table table = systemUserConnection.getTable(TEST_TABLE2)) { 2361 AccessTestAction moveAction = new AccessTestAction() { 2362 @Override 2363 public Object run() throws Exception { 2364 admin.move(hri.getEncodedNameAsBytes(), newRs.getServerName()); 2365 return null; 2366 } 2367 }; 2368 SUPERUSER.runAs(moveAction); 2369 2370 final int RETRIES_LIMIT = 10; 2371 int retries = 0; 2372 while (newRs.getRegions(TEST_TABLE2).size() < 1 && retries < RETRIES_LIMIT) { 2373 LOG.debug("Waiting for region to be opened. Already retried " + retries + " times."); 2374 try { 2375 Thread.sleep(1000); 2376 } catch (InterruptedException e) { 2377 } 2378 retries++; 2379 if (retries == RETRIES_LIMIT - 1) { 2380 fail("Retry exhaust for waiting region to be opened."); 2381 } 2382 } 2383 // Verify write permission for user "admin2" who has the global 2384 // permissions. 2385 AccessTestAction putAction = new AccessTestAction() { 2386 @Override 2387 public Object run() throws Exception { 2388 Put put = new Put(Bytes.toBytes("test")); 2389 put.addColumn(TEST_FAMILY, Bytes.toBytes("qual"), Bytes.toBytes("value")); 2390 table.put(put); 2391 return null; 2392 } 2393 }; 2394 USER_ADMIN.runAs(putAction); 2395 } 2396 } 2397 2398 @Test 2399 public void testTableDescriptorsEnumeration() throws Exception { 2400 User TABLE_ADMIN = User.createUserForTesting(conf, "UserA", new String[0]); 2401 2402 // Grant TABLE ADMIN privs 2403 grantOnTable(TEST_UTIL, TABLE_ADMIN.getShortName(), TEST_TABLE, null, null, 2404 Permission.Action.ADMIN); 2405 try { 2406 AccessTestAction listTablesAction = new AccessTestAction() { 2407 @Override 2408 public Object run() throws Exception { 2409 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 2410 Admin admin = conn.getAdmin()) { 2411 return admin.listTableDescriptors(); 2412 } 2413 } 2414 }; 2415 2416 AccessTestAction getTableDescAction = new AccessTestAction() { 2417 @Override 2418 public Object run() throws Exception { 2419 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 2420 Admin admin = conn.getAdmin()) { 2421 return admin.getDescriptor(TEST_TABLE); 2422 } 2423 } 2424 }; 2425 2426 verifyAllowed(listTablesAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, TABLE_ADMIN, 2427 USER_GROUP_CREATE, USER_GROUP_ADMIN); 2428 verifyIfEmptyList(listTablesAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2429 USER_GROUP_WRITE); 2430 2431 verifyAllowed(getTableDescAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, TABLE_ADMIN, 2432 USER_GROUP_CREATE, USER_GROUP_ADMIN); 2433 verifyDenied(getTableDescAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2434 USER_GROUP_WRITE); 2435 } finally { 2436 // Cleanup, revoke TABLE ADMIN privs 2437 revokeFromTable(TEST_UTIL, TABLE_ADMIN.getShortName(), TEST_TABLE, null, null, 2438 Permission.Action.ADMIN); 2439 } 2440 } 2441 2442 @Test 2443 public void testTableNameEnumeration() throws Exception { 2444 AccessTestAction listTablesAction = new AccessTestAction() { 2445 @Override 2446 public Object run() throws Exception { 2447 Connection unmanagedConnection = 2448 ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 2449 Admin admin = unmanagedConnection.getAdmin(); 2450 try { 2451 return Arrays.asList(admin.listTableNames()); 2452 } finally { 2453 admin.close(); 2454 unmanagedConnection.close(); 2455 } 2456 } 2457 }; 2458 2459 verifyAllowed(listTablesAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_RW, 2460 USER_RO, USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_WRITE); 2461 verifyIfEmptyList(listTablesAction, USER_NONE); 2462 } 2463 2464 @Test 2465 public void testTableDeletion(TestInfo testInfo) throws Exception { 2466 User TABLE_ADMIN = User.createUserForTesting(conf, "TestUser", new String[0]); 2467 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 2468 createTestTable(tableName); 2469 2470 // Grant TABLE ADMIN privs 2471 grantOnTable(TEST_UTIL, TABLE_ADMIN.getShortName(), tableName, null, null, 2472 Permission.Action.ADMIN); 2473 2474 AccessTestAction deleteTableAction = new AccessTestAction() { 2475 @Override 2476 public Object run() throws Exception { 2477 Connection unmanagedConnection = 2478 ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 2479 Admin admin = unmanagedConnection.getAdmin(); 2480 try { 2481 deleteTable(TEST_UTIL, admin, tableName); 2482 } finally { 2483 admin.close(); 2484 unmanagedConnection.close(); 2485 } 2486 return null; 2487 } 2488 }; 2489 2490 verifyDenied(deleteTableAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE); 2491 verifyAllowed(deleteTableAction, TABLE_ADMIN); 2492 } 2493 2494 private void createTestTable(TableName tname) throws Exception { 2495 createTestTable(tname, TEST_FAMILY); 2496 } 2497 2498 private void createTestTable(TableName tname, byte[] cf) throws Exception { 2499 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tname) 2500 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(cf).setMaxVersions(100).build()) 2501 .build(); 2502 createTable(TEST_UTIL, USER_OWNER, tableDescriptor, new byte[][] { Bytes.toBytes("s") }); 2503 } 2504 2505 @Test 2506 public void testNamespaceUserGrant() throws Exception { 2507 AccessTestAction getAction = new AccessTestAction() { 2508 @Override 2509 public Object run() throws Exception { 2510 try (Connection conn = ConnectionFactory.createConnection(conf); 2511 Table t = conn.getTable(TEST_TABLE)) { 2512 return t.get(new Get(TEST_ROW)); 2513 } 2514 } 2515 }; 2516 2517 String namespace = TEST_TABLE.getNamespaceAsString(); 2518 2519 // Grant namespace READ to USER_NONE, this should supersede any table permissions 2520 grantOnNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ); 2521 // Now USER_NONE should be able to read 2522 verifyAllowed(getAction, USER_NONE); 2523 2524 // Revoke namespace READ to USER_NONE 2525 revokeFromNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ); 2526 verifyDenied(getAction, USER_NONE); 2527 } 2528 2529 @Test 2530 public void testAccessControlClientGrantRevoke() throws Exception { 2531 // Create user for testing, who has no READ privileges by default. 2532 User testGrantRevoke = User.createUserForTesting(conf, "testGrantRevoke", new String[0]); 2533 AccessTestAction getAction = new AccessTestAction() { 2534 @Override 2535 public Object run() throws Exception { 2536 try (Connection conn = ConnectionFactory.createConnection(conf); 2537 Table t = conn.getTable(TEST_TABLE)) { 2538 return t.get(new Get(TEST_ROW)); 2539 } 2540 } 2541 }; 2542 2543 verifyDenied(getAction, testGrantRevoke); 2544 2545 // Grant table READ permissions to testGrantRevoke. 2546 try { 2547 grantOnTableUsingAccessControlClient(TEST_UTIL, systemUserConnection, 2548 testGrantRevoke.getShortName(), TEST_TABLE, null, null, Permission.Action.READ); 2549 } catch (Throwable e) { 2550 LOG.error("error during call of AccessControlClient.grant. ", e); 2551 } 2552 2553 // Now testGrantRevoke should be able to read also 2554 verifyAllowed(getAction, testGrantRevoke); 2555 2556 // Revoke table READ permission to testGrantRevoke. 2557 try { 2558 revokeFromTableUsingAccessControlClient(TEST_UTIL, systemUserConnection, 2559 testGrantRevoke.getShortName(), TEST_TABLE, null, null, Permission.Action.READ); 2560 } catch (Throwable e) { 2561 LOG.error("error during call of AccessControlClient.revoke ", e); 2562 } 2563 2564 // Now testGrantRevoke shouldn't be able read 2565 verifyDenied(getAction, testGrantRevoke); 2566 } 2567 2568 @Test 2569 public void testAccessControlClientGlobalGrantRevoke() throws Exception { 2570 // Create user for testing, who has no READ privileges by default. 2571 User testGlobalGrantRevoke = 2572 User.createUserForTesting(conf, "testGlobalGrantRevoke", new String[0]); 2573 AccessTestAction getAction = new AccessTestAction() { 2574 @Override 2575 public Object run() throws Exception { 2576 try (Connection conn = ConnectionFactory.createConnection(conf); 2577 Table t = conn.getTable(TEST_TABLE)) { 2578 return t.get(new Get(TEST_ROW)); 2579 } 2580 } 2581 }; 2582 2583 verifyDenied(getAction, testGlobalGrantRevoke); 2584 2585 // Grant table READ permissions to testGlobalGrantRevoke. 2586 String userName = testGlobalGrantRevoke.getShortName(); 2587 try { 2588 grantGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2589 Permission.Action.READ); 2590 } catch (Throwable e) { 2591 LOG.error("error during call of AccessControlClient.grant. ", e); 2592 } 2593 try { 2594 // Now testGlobalGrantRevoke should be able to read also 2595 verifyAllowed(getAction, testGlobalGrantRevoke); 2596 } catch (Exception e) { 2597 revokeGlobal(TEST_UTIL, userName, Permission.Action.READ); 2598 throw e; 2599 } 2600 2601 // Revoke table READ permission to testGlobalGrantRevoke. 2602 try { 2603 revokeGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2604 Permission.Action.READ); 2605 } catch (Throwable e) { 2606 LOG.error("error during call of AccessControlClient.revoke ", e); 2607 } 2608 2609 // Now testGlobalGrantRevoke shouldn't be able read 2610 verifyDenied(getAction, testGlobalGrantRevoke); 2611 2612 } 2613 2614 @Test 2615 public void testAccessControlClientMultiGrantRevoke() throws Exception { 2616 User testGrantRevoke = User.createUserForTesting(conf, "testGrantRevoke", new String[0]); 2617 AccessTestAction getAction = new AccessTestAction() { 2618 @Override 2619 public Object run() throws Exception { 2620 try (Connection conn = ConnectionFactory.createConnection(conf); 2621 Table t = conn.getTable(TEST_TABLE)) { 2622 return t.get(new Get(TEST_ROW)); 2623 } 2624 } 2625 }; 2626 2627 AccessTestAction putAction = new AccessTestAction() { 2628 @Override 2629 public Object run() throws Exception { 2630 Put p = new Put(TEST_ROW); 2631 p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1)); 2632 try (Connection conn = ConnectionFactory.createConnection(conf); 2633 Table t = conn.getTable(TEST_TABLE)) { 2634 t.put(p); 2635 return null; 2636 } 2637 } 2638 }; 2639 2640 verifyDenied(getAction, testGrantRevoke); 2641 verifyDenied(putAction, testGrantRevoke); 2642 2643 // Grant global READ permissions to testGrantRevoke. 2644 String userName = testGrantRevoke.getShortName(); 2645 try { 2646 grantGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2647 Permission.Action.READ); 2648 } catch (Throwable e) { 2649 LOG.error("error during call of AccessControlClient.grant. ", e); 2650 } 2651 verifyAllowed(getAction, testGrantRevoke); 2652 verifyDenied(putAction, testGrantRevoke); 2653 2654 // Grant global WRITE permissions to testGrantRevoke. 2655 try { 2656 grantGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2657 Permission.Action.WRITE); 2658 } catch (Throwable e) { 2659 LOG.error("error during call of AccessControlClient.grant. ", e); 2660 } 2661 verifyAllowed(getAction, testGrantRevoke); 2662 verifyAllowed(putAction, testGrantRevoke); 2663 2664 // Revoke global READ permission to testGrantRevoke. 2665 try { 2666 revokeGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2667 Permission.Action.READ, Permission.Action.WRITE); 2668 } catch (Throwable e) { 2669 LOG.error("error during call of AccessControlClient.revoke ", e); 2670 } 2671 verifyDenied(getAction, testGrantRevoke); 2672 verifyDenied(putAction, testGrantRevoke); 2673 2674 // Grant table READ & WRITE permissions to testGrantRevoke 2675 try { 2676 grantOnTableUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, TEST_TABLE, 2677 null, null, Permission.Action.READ); 2678 } catch (Throwable e) { 2679 LOG.error("error during call of AccessControlClient.grant. ", e); 2680 } 2681 verifyAllowed(getAction, testGrantRevoke); 2682 verifyDenied(putAction, testGrantRevoke); 2683 2684 // Grant table WRITE permissions to testGrantRevoke 2685 try { 2686 grantOnTableUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, TEST_TABLE, 2687 null, null, Action.WRITE); 2688 } catch (Throwable e) { 2689 LOG.error("error during call of AccessControlClient.grant. ", e); 2690 } 2691 verifyAllowed(getAction, testGrantRevoke); 2692 verifyAllowed(putAction, testGrantRevoke); 2693 2694 // Revoke table READ & WRITE permission to testGrantRevoke. 2695 try { 2696 revokeFromTableUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, TEST_TABLE, 2697 null, null, Permission.Action.READ, Permission.Action.WRITE); 2698 } catch (Throwable e) { 2699 LOG.error("error during call of AccessControlClient.revoke ", e); 2700 } 2701 verifyDenied(getAction, testGrantRevoke); 2702 verifyDenied(putAction, testGrantRevoke); 2703 2704 // Grant Namespace READ permissions to testGrantRevoke 2705 String namespace = TEST_TABLE.getNamespaceAsString(); 2706 try { 2707 grantOnNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, namespace, 2708 Permission.Action.READ); 2709 } catch (Throwable e) { 2710 LOG.error("error during call of AccessControlClient.grant. ", e); 2711 } 2712 verifyAllowed(getAction, testGrantRevoke); 2713 verifyDenied(putAction, testGrantRevoke); 2714 2715 // Grant Namespace WRITE permissions to testGrantRevoke 2716 try { 2717 grantOnNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, namespace, 2718 Permission.Action.WRITE); 2719 } catch (Throwable e) { 2720 LOG.error("error during call of AccessControlClient.grant. ", e); 2721 } 2722 verifyAllowed(getAction, testGrantRevoke); 2723 verifyAllowed(putAction, testGrantRevoke); 2724 2725 // Revoke table READ & WRITE permission to testGrantRevoke. 2726 try { 2727 revokeFromNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2728 TEST_TABLE.getNamespaceAsString(), Permission.Action.READ, Permission.Action.WRITE); 2729 } catch (Throwable e) { 2730 LOG.error("error during call of AccessControlClient.revoke ", e); 2731 } 2732 verifyDenied(getAction, testGrantRevoke); 2733 verifyDenied(putAction, testGrantRevoke); 2734 } 2735 2736 @Test 2737 public void testAccessControlClientGrantRevokeOnNamespace() throws Exception { 2738 // Create user for testing, who has no READ privileges by default. 2739 User testNS = User.createUserForTesting(conf, "testNS", new String[0]); 2740 AccessTestAction getAction = new AccessTestAction() { 2741 @Override 2742 public Object run() throws Exception { 2743 try (Connection conn = ConnectionFactory.createConnection(conf); 2744 Table t = conn.getTable(TEST_TABLE)) { 2745 return t.get(new Get(TEST_ROW)); 2746 } 2747 } 2748 }; 2749 2750 verifyDenied(getAction, testNS); 2751 2752 String userName = testNS.getShortName(); 2753 String namespace = TEST_TABLE.getNamespaceAsString(); 2754 // Grant namespace READ to testNS, this should supersede any table permissions 2755 try { 2756 grantOnNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, namespace, 2757 Permission.Action.READ); 2758 } catch (Throwable e) { 2759 LOG.error("error during call of AccessControlClient.grant. ", e); 2760 } 2761 try { 2762 // Now testNS should be able to read also 2763 verifyAllowed(getAction, testNS); 2764 } catch (Exception e) { 2765 revokeFromNamespace(TEST_UTIL, userName, namespace, Permission.Action.READ); 2766 throw e; 2767 } 2768 2769 // Revoke namespace READ to testNS, this should supersede any table permissions 2770 try { 2771 revokeFromNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName, 2772 namespace, Permission.Action.READ); 2773 } catch (Throwable e) { 2774 LOG.error("error during call of AccessControlClient.revoke ", e); 2775 } 2776 2777 // Now testNS shouldn't be able read 2778 verifyDenied(getAction, testNS); 2779 } 2780 2781 public static class PingCoprocessor extends PingService implements RegionCoprocessor { 2782 2783 @Override 2784 public void start(CoprocessorEnvironment env) throws IOException { 2785 } 2786 2787 @Override 2788 public void stop(CoprocessorEnvironment env) throws IOException { 2789 } 2790 2791 @Override 2792 public Iterable<Service> getServices() { 2793 return Collections.singleton(this); 2794 } 2795 2796 @Override 2797 public void ping(RpcController controller, PingRequest request, 2798 RpcCallback<PingResponse> callback) { 2799 callback.run(PingResponse.newBuilder().setPong("Pong!").build()); 2800 } 2801 2802 @Override 2803 public void count(RpcController controller, CountRequest request, 2804 RpcCallback<CountResponse> callback) { 2805 callback.run(CountResponse.newBuilder().build()); 2806 } 2807 2808 @Override 2809 public void increment(RpcController controller, IncrementCountRequest requet, 2810 RpcCallback<IncrementCountResponse> callback) { 2811 callback.run(IncrementCountResponse.newBuilder().build()); 2812 } 2813 2814 @Override 2815 public void hello(RpcController controller, HelloRequest request, 2816 RpcCallback<HelloResponse> callback) { 2817 callback.run(HelloResponse.newBuilder().setResponse("Hello!").build()); 2818 } 2819 2820 @Override 2821 public void noop(RpcController controller, NoopRequest request, 2822 RpcCallback<NoopResponse> callback) { 2823 callback.run(NoopResponse.newBuilder().build()); 2824 } 2825 } 2826 2827 @Test 2828 public void testCoprocessorExec() throws Exception { 2829 // Set up our ping endpoint service on all regions of our test table 2830 for (JVMClusterUtil.RegionServerThread thread : TEST_UTIL.getMiniHBaseCluster() 2831 .getRegionServerThreads()) { 2832 HRegionServer rs = thread.getRegionServer(); 2833 for (HRegion region : rs.getRegions(TEST_TABLE)) { 2834 region.getCoprocessorHost().load(PingCoprocessor.class, Coprocessor.PRIORITY_USER, conf); 2835 } 2836 } 2837 2838 // Create users for testing, and grant EXEC privileges on our test table 2839 // only to user A 2840 User userA = User.createUserForTesting(conf, "UserA", new String[0]); 2841 User userB = User.createUserForTesting(conf, "UserB", new String[0]); 2842 2843 grantOnTable(TEST_UTIL, userA.getShortName(), TEST_TABLE, null, null, Permission.Action.EXEC); 2844 try { 2845 // Create an action for invoking our test endpoint 2846 AccessTestAction execEndpointAction = new AccessTestAction() { 2847 @Override 2848 public Object run() throws Exception { 2849 try (Connection conn = ConnectionFactory.createConnection(conf); 2850 Table t = conn.getTable(TEST_TABLE)) { 2851 BlockingRpcChannel service = t.coprocessorService(HConstants.EMPTY_BYTE_ARRAY); 2852 PingCoprocessor.newBlockingStub(service).noop(null, NoopRequest.newBuilder().build()); 2853 } 2854 return null; 2855 } 2856 }; 2857 2858 String namespace = TEST_TABLE.getNamespaceAsString(); 2859 // Now grant EXEC to the entire namespace to user B 2860 grantOnNamespace(TEST_UTIL, userB.getShortName(), namespace, Permission.Action.EXEC); 2861 // User B should now be allowed also 2862 verifyAllowed(execEndpointAction, userA, userB); 2863 2864 revokeFromNamespace(TEST_UTIL, userB.getShortName(), namespace, Permission.Action.EXEC); 2865 // Verify that EXEC permission is checked correctly 2866 verifyDenied(execEndpointAction, userB); 2867 verifyAllowed(execEndpointAction, userA); 2868 } finally { 2869 // Cleanup, revoke the userA privileges 2870 revokeFromTable(TEST_UTIL, userA.getShortName(), TEST_TABLE, null, null, 2871 Permission.Action.EXEC); 2872 } 2873 } 2874 2875 @Test 2876 public void testSetQuota() throws Exception { 2877 AccessTestAction setUserQuotaAction = new AccessTestAction() { 2878 @Override 2879 public Object run() throws Exception { 2880 ACCESS_CONTROLLER.preSetUserQuota(ObserverContextImpl.createAndPrepare(CP_ENV), null, null); 2881 return null; 2882 } 2883 }; 2884 2885 AccessTestAction setUserTableQuotaAction = new AccessTestAction() { 2886 @Override 2887 public Object run() throws Exception { 2888 ACCESS_CONTROLLER.preSetUserQuota(ObserverContextImpl.createAndPrepare(CP_ENV), null, 2889 TEST_TABLE, null); 2890 return null; 2891 } 2892 }; 2893 2894 AccessTestAction setUserNamespaceQuotaAction = new AccessTestAction() { 2895 @Override 2896 public Object run() throws Exception { 2897 ACCESS_CONTROLLER.preSetUserQuota(ObserverContextImpl.createAndPrepare(CP_ENV), null, 2898 (String) null, null); 2899 return null; 2900 } 2901 }; 2902 2903 AccessTestAction setTableQuotaAction = new AccessTestAction() { 2904 @Override 2905 public Object run() throws Exception { 2906 ACCESS_CONTROLLER.preSetTableQuota(ObserverContextImpl.createAndPrepare(CP_ENV), TEST_TABLE, 2907 null); 2908 return null; 2909 } 2910 }; 2911 2912 AccessTestAction setNamespaceQuotaAction = new AccessTestAction() { 2913 @Override 2914 public Object run() throws Exception { 2915 ACCESS_CONTROLLER.preSetNamespaceQuota(ObserverContextImpl.createAndPrepare(CP_ENV), null, 2916 null); 2917 return null; 2918 } 2919 }; 2920 2921 AccessTestAction setRegionServerQuotaAction = new AccessTestAction() { 2922 @Override 2923 public Object run() throws Exception { 2924 ACCESS_CONTROLLER.preSetRegionServerQuota(ObserverContextImpl.createAndPrepare(CP_ENV), 2925 null, null); 2926 return null; 2927 } 2928 }; 2929 2930 verifyAllowed(setUserQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2931 verifyDenied(setUserQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2932 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2933 2934 verifyAllowed(setUserTableQuotaAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2935 verifyDenied(setUserTableQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 2936 USER_GROUP_WRITE, USER_GROUP_CREATE); 2937 2938 verifyAllowed(setUserNamespaceQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2939 verifyDenied(setUserNamespaceQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2940 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2941 2942 verifyAllowed(setTableQuotaAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); 2943 verifyDenied(setTableQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE); 2944 2945 verifyAllowed(setNamespaceQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2946 verifyDenied(setNamespaceQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2947 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2948 2949 verifyAllowed(setRegionServerQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 2950 verifyDenied(setRegionServerQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 2951 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); 2952 } 2953 2954 @Test 2955 public void testGetNamespacePermission() throws Exception { 2956 String namespace = "testGetNamespacePermission"; 2957 NamespaceDescriptor desc = NamespaceDescriptor.create(namespace).build(); 2958 createNamespace(TEST_UTIL, desc); 2959 grantOnNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ); 2960 2961 // Test 1: A specific namespace 2962 getNamespacePermissionsAndVerify(namespace, 1, namespace); 2963 2964 // Test 2: '@.*' 2965 getNamespacePermissionsAndVerify(".*", 1, namespace); 2966 2967 // Test 3: A more complex regex 2968 getNamespacePermissionsAndVerify("^test[a-zA-Z]*", 1, namespace); 2969 2970 deleteNamespace(TEST_UTIL, namespace); 2971 } 2972 2973 /** 2974 * List all user permissions match the given regular expression for namespace and verify each of 2975 * them. 2976 * @param namespaceRegexWithoutPrefix the regualar expression for namespace, without 2977 * NAMESPACE_PREFIX 2978 * @param expectedAmount the expected amount of user permissions returned 2979 * @param expectedNamespace the expected namespace of each user permission returned 2980 * @throws HBaseException in the case of any HBase exception when accessing hbase:acl table 2981 */ 2982 private void getNamespacePermissionsAndVerify(String namespaceRegexWithoutPrefix, 2983 int expectedAmount, String expectedNamespace) throws HBaseException { 2984 try { 2985 List<UserPermission> namespacePermissions = 2986 AccessControlClient.getUserPermissions(systemUserConnection, 2987 Bytes.toString(PermissionStorage.toNamespaceEntry(namespaceRegexWithoutPrefix))); 2988 assertTrue(namespacePermissions != null); 2989 assertEquals(expectedAmount, namespacePermissions.size()); 2990 for (UserPermission namespacePermission : namespacePermissions) { 2991 // Verify it is not a global user permission 2992 assertFalse(namespacePermission.getAccessScope() == Permission.Scope.GLOBAL); 2993 // Verify namespace is set 2994 NamespacePermission nsPerm = (NamespacePermission) namespacePermission.getPermission(); 2995 assertEquals(expectedNamespace, nsPerm.getNamespace()); 2996 } 2997 } catch (Throwable thw) { 2998 throw new HBaseException(thw); 2999 } 3000 } 3001 3002 @Test 3003 public void testTruncatePerms() throws Exception { 3004 try { 3005 List<UserPermission> existingPerms = 3006 AccessControlClient.getUserPermissions(systemUserConnection, TEST_TABLE.getNameAsString()); 3007 assertTrue(existingPerms != null); 3008 assertTrue(existingPerms.size() > 1); 3009 TEST_UTIL.getAdmin().disableTable(TEST_TABLE); 3010 TEST_UTIL.truncateTable(TEST_TABLE); 3011 TEST_UTIL.waitTableAvailable(TEST_TABLE); 3012 List<UserPermission> perms = 3013 AccessControlClient.getUserPermissions(systemUserConnection, TEST_TABLE.getNameAsString()); 3014 assertTrue(perms != null); 3015 assertEquals(existingPerms.size(), perms.size()); 3016 } catch (Throwable e) { 3017 throw new HBaseIOException(e); 3018 } 3019 } 3020 3021 private PrivilegedAction<List<UserPermission>> getPrivilegedAction(final String regex) { 3022 return new PrivilegedAction<List<UserPermission>>() { 3023 @Override 3024 public List<UserPermission> run() { 3025 try (Connection conn = ConnectionFactory.createConnection(conf)) { 3026 return AccessControlClient.getUserPermissions(conn, regex); 3027 } catch (Throwable e) { 3028 LOG.error("error during call of AccessControlClient.getUserPermissions.", e); 3029 return null; 3030 } 3031 } 3032 }; 3033 } 3034 3035 @Test 3036 public void testAccessControlClientUserPerms(TestInfo testInfo) throws Exception { 3037 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 3038 createTestTable(tableName); 3039 try { 3040 final String regex = tableName.getNameWithNamespaceInclAsString(); 3041 User testUserPerms = User.createUserForTesting(conf, "testUserPerms", new String[0]); 3042 assertEquals(0, testUserPerms.runAs(getPrivilegedAction(regex)).size()); 3043 // Grant TABLE ADMIN privs to testUserPerms 3044 grantOnTable(TEST_UTIL, testUserPerms.getShortName(), tableName, null, null, Action.ADMIN); 3045 List<UserPermission> perms = testUserPerms.runAs(getPrivilegedAction(regex)); 3046 assertNotNull(perms); 3047 // Superuser, testUserPerms 3048 assertEquals(2, perms.size()); 3049 } finally { 3050 deleteTable(TEST_UTIL, tableName); 3051 } 3052 } 3053 3054 @Test 3055 public void testAccessControllerUserPermsRegexHandling(TestInfo testInfo) throws Exception { 3056 User testRegexHandler = User.createUserForTesting(conf, "testRegexHandling", new String[0]); 3057 3058 final String REGEX_ALL_TABLES = ".*"; 3059 final String tableName = testInfo.getTestMethod().get().getName(); 3060 final TableName table1 = TableName.valueOf(tableName); 3061 final byte[] family = Bytes.toBytes("f1"); 3062 3063 // create table in default ns 3064 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(table1) 3065 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 3066 createTable(TEST_UTIL, tableDescriptor); 3067 3068 // creating the ns and table in it 3069 String ns = "testNamespace"; 3070 NamespaceDescriptor desc = NamespaceDescriptor.create(ns).build(); 3071 final TableName table2 = TableName.valueOf(ns, tableName); 3072 createNamespace(TEST_UTIL, desc); 3073 tableDescriptor = TableDescriptorBuilder.newBuilder(table2) 3074 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 3075 createTable(TEST_UTIL, tableDescriptor); 3076 3077 // Verify that we can read sys-tables 3078 String aclTableName = PermissionStorage.ACL_TABLE_NAME.getNameAsString(); 3079 assertEquals(6, SUPERUSER.runAs(getPrivilegedAction(aclTableName)).size()); 3080 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction(aclTableName)).size()); 3081 3082 // Grant TABLE ADMIN privs to testUserPerms 3083 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size()); 3084 grantOnTable(TEST_UTIL, testRegexHandler.getShortName(), table1, null, null, Action.ADMIN); 3085 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size()); 3086 grantOnTable(TEST_UTIL, testRegexHandler.getShortName(), table2, null, null, Action.ADMIN); 3087 assertEquals(4, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size()); 3088 3089 // USER_ADMIN, testUserPerms must have a row each. 3090 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(tableName)).size()); 3091 assertEquals(2, 3092 testRegexHandler 3093 .runAs(getPrivilegedAction( 3094 NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR + TableName.NAMESPACE_DELIM + tableName)) 3095 .size()); 3096 assertEquals(2, testRegexHandler 3097 .runAs(getPrivilegedAction(ns + TableName.NAMESPACE_DELIM + tableName)).size()); 3098 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction("notMatchingAny")).size()); 3099 3100 deleteTable(TEST_UTIL, table1); 3101 deleteTable(TEST_UTIL, table2); 3102 deleteNamespace(TEST_UTIL, ns); 3103 } 3104 3105 private void verifyAnyCreate(AccessTestAction action) throws Exception { 3106 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_ADMIN_CF, 3107 USER_GROUP_CREATE, USER_GROUP_ADMIN); 3108 verifyDenied(action, USER_NONE, USER_RO, USER_RW, USER_GROUP_READ, USER_GROUP_WRITE); 3109 } 3110 3111 @Test 3112 public void testPrepareAndCleanBulkLoad() throws Exception { 3113 AccessTestAction prepareBulkLoadAction = new AccessTestAction() { 3114 @Override 3115 public Object run() throws Exception { 3116 ACCESS_CONTROLLER.prePrepareBulkLoad(ObserverContextImpl.createAndPrepare(RCP_ENV)); 3117 return null; 3118 } 3119 }; 3120 AccessTestAction cleanupBulkLoadAction = new AccessTestAction() { 3121 @Override 3122 public Object run() throws Exception { 3123 ACCESS_CONTROLLER.preCleanupBulkLoad(ObserverContextImpl.createAndPrepare(RCP_ENV)); 3124 return null; 3125 } 3126 }; 3127 verifyAnyCreate(prepareBulkLoadAction); 3128 verifyAnyCreate(cleanupBulkLoadAction); 3129 } 3130 3131 @Test 3132 public void testReplicateLogEntries() throws Exception { 3133 AccessTestAction replicateLogEntriesAction = new AccessTestAction() { 3134 @Override 3135 public Object run() throws Exception { 3136 ACCESS_CONTROLLER.preReplicateLogEntries(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 3137 ACCESS_CONTROLLER.postReplicateLogEntries(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 3138 return null; 3139 } 3140 }; 3141 3142 verifyAllowed(replicateLogEntriesAction, SUPERUSER, USER_ADMIN, USER_GROUP_WRITE); 3143 verifyDenied(replicateLogEntriesAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, 3144 USER_GROUP_READ, USER_GROUP_ADMIN, USER_GROUP_CREATE); 3145 } 3146 3147 @Test 3148 public void testAddReplicationPeer() throws Exception { 3149 AccessTestAction action = new AccessTestAction() { 3150 @Override 3151 public Object run() throws Exception { 3152 ACCESS_CONTROLLER.preAddReplicationPeer(ObserverContextImpl.createAndPrepare(CP_ENV), 3153 "test", null); 3154 return null; 3155 } 3156 }; 3157 3158 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3159 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3160 } 3161 3162 @Test 3163 public void testRemoveReplicationPeer() throws Exception { 3164 AccessTestAction action = new AccessTestAction() { 3165 @Override 3166 public Object run() throws Exception { 3167 ACCESS_CONTROLLER.preRemoveReplicationPeer(ObserverContextImpl.createAndPrepare(CP_ENV), 3168 "test"); 3169 return null; 3170 } 3171 }; 3172 3173 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3174 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3175 } 3176 3177 @Test 3178 public void testEnableReplicationPeer() throws Exception { 3179 AccessTestAction action = new AccessTestAction() { 3180 @Override 3181 public Object run() throws Exception { 3182 ACCESS_CONTROLLER.preEnableReplicationPeer(ObserverContextImpl.createAndPrepare(CP_ENV), 3183 "test"); 3184 return null; 3185 } 3186 }; 3187 3188 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3189 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3190 } 3191 3192 @Test 3193 public void testDisableReplicationPeer() throws Exception { 3194 AccessTestAction action = new AccessTestAction() { 3195 @Override 3196 public Object run() throws Exception { 3197 ACCESS_CONTROLLER.preDisableReplicationPeer(ObserverContextImpl.createAndPrepare(CP_ENV), 3198 "test"); 3199 return null; 3200 } 3201 }; 3202 3203 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3204 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3205 } 3206 3207 @Test 3208 public void testGetReplicationPeerConfig() throws Exception { 3209 AccessTestAction action = new AccessTestAction() { 3210 @Override 3211 public Object run() throws Exception { 3212 ACCESS_CONTROLLER.preGetReplicationPeerConfig(ObserverContextImpl.createAndPrepare(CP_ENV), 3213 "test"); 3214 return null; 3215 } 3216 }; 3217 3218 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3219 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3220 } 3221 3222 @Test 3223 public void testUpdateReplicationPeerConfig() throws Exception { 3224 AccessTestAction action = new AccessTestAction() { 3225 @Override 3226 public Object run() throws Exception { 3227 ACCESS_CONTROLLER.preUpdateReplicationPeerConfig( 3228 ObserverContextImpl.createAndPrepare(CP_ENV), "test", 3229 ReplicationPeerConfig.newBuilder().build()); 3230 return null; 3231 } 3232 }; 3233 3234 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3235 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3236 } 3237 3238 @Test 3239 public void testUpdateMasterConfiguration() throws Exception { 3240 AccessTestAction action = () -> { 3241 ACCESS_CONTROLLER.preUpdateMasterConfiguration(ObserverContextImpl.createAndPrepare(CP_ENV), 3242 null); 3243 return null; 3244 }; 3245 3246 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3247 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3248 } 3249 3250 @Test 3251 public void testUpdateRegionServerConfiguration() throws Exception { 3252 AccessTestAction action = () -> { 3253 ACCESS_CONTROLLER 3254 .preUpdateRegionServerConfiguration(ObserverContextImpl.createAndPrepare(RSCP_ENV), null); 3255 return null; 3256 }; 3257 3258 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3259 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3260 } 3261 3262 @Test 3263 public void testClearRegionBlockCache() throws Exception { 3264 AccessTestAction action = () -> { 3265 ACCESS_CONTROLLER.preClearRegionBlockCache(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 3266 return null; 3267 }; 3268 3269 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3270 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3271 } 3272 3273 @Test 3274 public void testTransitSyncReplicationPeerState() throws Exception { 3275 AccessTestAction action = new AccessTestAction() { 3276 @Override 3277 public Object run() throws Exception { 3278 ACCESS_CONTROLLER.preTransitReplicationPeerSyncReplicationState( 3279 ObserverContextImpl.createAndPrepare(CP_ENV), "test", SyncReplicationState.NONE); 3280 return null; 3281 } 3282 }; 3283 3284 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3285 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3286 } 3287 3288 @Test 3289 public void testListReplicationPeers() throws Exception { 3290 AccessTestAction action = new AccessTestAction() { 3291 @Override 3292 public Object run() throws Exception { 3293 ACCESS_CONTROLLER.preListReplicationPeers(ObserverContextImpl.createAndPrepare(CP_ENV), 3294 "test"); 3295 return null; 3296 } 3297 }; 3298 3299 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3300 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3301 } 3302 3303 @Test 3304 public void testRemoteLocks(TestInfo testInfo) throws Exception { 3305 String namespace = "preQueueNs"; 3306 final TableName tableName = 3307 TableName.valueOf(namespace, testInfo.getTestMethod().get().getName()); 3308 RegionInfo[] regionInfos = new RegionInfo[] { RegionInfoBuilder.newBuilder(tableName).build() }; 3309 3310 // Setup Users 3311 // User will be granted ADMIN and CREATE on namespace. Should be denied before grant. 3312 User namespaceUser = User.createUserForTesting(conf, "qLNSUser", new String[0]); 3313 // User will be granted ADMIN and CREATE on table. Should be denied before grant. 3314 User tableACUser = User.createUserForTesting(conf, "qLTableACUser", new String[0]); 3315 // User will be granted READ, WRITE, EXECUTE on table. Should be denied. 3316 User tableRWXUser = User.createUserForTesting(conf, "qLTableRWXUser", new String[0]); 3317 grantOnTable(TEST_UTIL, tableRWXUser.getShortName(), tableName, null, null, Action.READ, 3318 Action.WRITE, Action.EXEC); 3319 // User with global READ, WRITE, EXECUTE should be denied lock access. 3320 User globalRWXUser = User.createUserForTesting(conf, "qLGlobalRWXUser", new String[0]); 3321 grantGlobal(TEST_UTIL, globalRWXUser.getShortName(), Action.READ, Action.WRITE, Action.EXEC); 3322 3323 AccessTestAction namespaceLockAction = new AccessTestAction() { 3324 @Override 3325 public Object run() throws Exception { 3326 ACCESS_CONTROLLER.preRequestLock(ObserverContextImpl.createAndPrepare(CP_ENV), namespace, 3327 null, null, null); 3328 return null; 3329 } 3330 }; 3331 verifyAllowed(namespaceLockAction, SUPERUSER, USER_ADMIN); 3332 verifyDenied(namespaceLockAction, globalRWXUser, tableACUser, namespaceUser, tableRWXUser); 3333 grantOnNamespace(TEST_UTIL, namespaceUser.getShortName(), namespace, Action.ADMIN); 3334 // Why I need this pause? I don't need it elsewhere. 3335 Threads.sleep(1000); 3336 verifyAllowed(namespaceLockAction, namespaceUser); 3337 3338 AccessTestAction tableLockAction = new AccessTestAction() { 3339 @Override 3340 public Object run() throws Exception { 3341 ACCESS_CONTROLLER.preRequestLock(ObserverContextImpl.createAndPrepare(CP_ENV), null, 3342 tableName, null, null); 3343 return null; 3344 } 3345 }; 3346 verifyAllowed(tableLockAction, SUPERUSER, USER_ADMIN, namespaceUser); 3347 verifyDenied(tableLockAction, globalRWXUser, tableACUser, tableRWXUser); 3348 grantOnTable(TEST_UTIL, tableACUser.getShortName(), tableName, null, null, Action.ADMIN, 3349 Action.CREATE); 3350 // See if this can fail (flakie) because grant hasn't propagated yet. 3351 for (int i = 0; i < 10; i++) { 3352 try { 3353 verifyAllowed(tableLockAction, tableACUser); 3354 } catch (AssertionError e) { 3355 LOG.warn("Retrying assertion error", e); 3356 Threads.sleep(1000); 3357 continue; 3358 } 3359 } 3360 3361 AccessTestAction regionsLockAction = new AccessTestAction() { 3362 @Override 3363 public Object run() throws Exception { 3364 ACCESS_CONTROLLER.preRequestLock(ObserverContextImpl.createAndPrepare(CP_ENV), null, null, 3365 regionInfos, null); 3366 return null; 3367 } 3368 }; 3369 verifyAllowed(regionsLockAction, SUPERUSER, USER_ADMIN, namespaceUser, tableACUser); 3370 verifyDenied(regionsLockAction, globalRWXUser, tableRWXUser); 3371 3372 // Test heartbeats 3373 // Create a lock procedure and try sending heartbeat to it. It doesn't matter how the lock 3374 // was created, we just need namespace from the lock's tablename. 3375 LockProcedure proc = new LockProcedure(conf, tableName, LockType.EXCLUSIVE, "test", null); 3376 AccessTestAction regionLockHeartbeatAction = new AccessTestAction() { 3377 @Override 3378 public Object run() throws Exception { 3379 ACCESS_CONTROLLER.preLockHeartbeat(ObserverContextImpl.createAndPrepare(CP_ENV), 3380 proc.getTableName(), proc.getDescription()); 3381 return null; 3382 } 3383 }; 3384 verifyAllowed(regionLockHeartbeatAction, SUPERUSER, USER_ADMIN, namespaceUser, tableACUser); 3385 verifyDenied(regionLockHeartbeatAction, globalRWXUser, tableRWXUser); 3386 } 3387 3388 @Test 3389 public void testAccessControlRevokeOnlyFewPermission() throws Throwable { 3390 TableName tname = TableName.valueOf("revoke"); 3391 try { 3392 TEST_UTIL.createTable(tname, TEST_FAMILY); 3393 User testUserPerms = User.createUserForTesting(conf, "revokePerms", new String[0]); 3394 Permission.Action[] actions = { Action.READ, Action.WRITE }; 3395 AccessControlClient.grant(TEST_UTIL.getConnection(), tname, testUserPerms.getShortName(), 3396 null, null, actions); 3397 3398 List<UserPermission> userPermissions = 3399 AccessControlClient.getUserPermissions(TEST_UTIL.getConnection(), tname.getNameAsString()); 3400 assertEquals(2, userPermissions.size()); 3401 3402 AccessControlClient.revoke(TEST_UTIL.getConnection(), tname, testUserPerms.getShortName(), 3403 null, null, Action.WRITE); 3404 3405 userPermissions = 3406 AccessControlClient.getUserPermissions(TEST_UTIL.getConnection(), tname.getNameAsString()); 3407 assertEquals(2, userPermissions.size()); 3408 3409 Permission.Action[] expectedAction = { Action.READ }; 3410 boolean userFound = false; 3411 for (UserPermission p : userPermissions) { 3412 if (testUserPerms.getShortName().equals(p.getUser())) { 3413 assertArrayEquals(expectedAction, p.getPermission().getActions()); 3414 userFound = true; 3415 break; 3416 } 3417 } 3418 assertTrue(userFound); 3419 } finally { 3420 TEST_UTIL.deleteTable(tname); 3421 } 3422 } 3423 3424 @Test 3425 public void testGetClusterStatus() throws Exception { 3426 AccessTestAction action = new AccessTestAction() { 3427 @Override 3428 public Object run() throws Exception { 3429 ACCESS_CONTROLLER.preGetClusterMetrics(ObserverContextImpl.createAndPrepare(CP_ENV)); 3430 return null; 3431 } 3432 }; 3433 3434 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_CREATE, USER_RW, USER_RO, USER_NONE, 3435 USER_OWNER); 3436 } 3437 3438 @Test 3439 public void testExecuteProcedures() throws Exception { 3440 AccessTestAction action = new AccessTestAction() { 3441 @Override 3442 public Object run() throws Exception { 3443 ACCESS_CONTROLLER.preExecuteProcedures(ObserverContextImpl.createAndPrepare(RSCP_ENV)); 3444 return null; 3445 } 3446 }; 3447 3448 verifyAllowed(action, SUPERUSER); 3449 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_ADMIN); 3450 } 3451 3452 @Test 3453 public void testGetUserPermissions() throws Throwable { 3454 Connection conn = null; 3455 try { 3456 conn = ConnectionFactory.createConnection(conf); 3457 User nSUser1 = User.createUserForTesting(conf, "nsuser1", new String[0]); 3458 User nSUser2 = User.createUserForTesting(conf, "nsuser2", new String[0]); 3459 User nSUser3 = User.createUserForTesting(conf, "nsuser3", new String[0]); 3460 3461 // Global access groups 3462 User globalGroupUser1 = 3463 User.createUserForTesting(conf, "globalGroupUser1", new String[] { "group_admin" }); 3464 User globalGroupUser2 = User.createUserForTesting(conf, "globalGroupUser2", 3465 new String[] { "group_admin", "group_create" }); 3466 // Namespace access groups 3467 User nsGroupUser1 = 3468 User.createUserForTesting(conf, "nsGroupUser1", new String[] { "ns_group1" }); 3469 User nsGroupUser2 = 3470 User.createUserForTesting(conf, "nsGroupUser2", new String[] { "ns_group2" }); 3471 // table Access groups 3472 User tableGroupUser1 = 3473 User.createUserForTesting(conf, "tableGroupUser1", new String[] { "table_group1" }); 3474 User tableGroupUser2 = 3475 User.createUserForTesting(conf, "tableGroupUser2", new String[] { "table_group2" }); 3476 3477 // Create namespaces 3478 String nsPrefix = "testNS"; 3479 final String namespace1 = nsPrefix + "1"; 3480 NamespaceDescriptor desc1 = NamespaceDescriptor.create(namespace1).build(); 3481 createNamespace(TEST_UTIL, desc1); 3482 String namespace2 = nsPrefix + "2"; 3483 NamespaceDescriptor desc2 = NamespaceDescriptor.create(namespace2).build(); 3484 createNamespace(TEST_UTIL, desc2); 3485 3486 // Grant namespace permission 3487 grantOnNamespace(TEST_UTIL, nSUser1.getShortName(), namespace1, Permission.Action.ADMIN); 3488 grantOnNamespace(TEST_UTIL, nSUser3.getShortName(), namespace1, Permission.Action.READ); 3489 grantOnNamespace(TEST_UTIL, toGroupEntry("ns_group1"), namespace1, Permission.Action.ADMIN); 3490 grantOnNamespace(TEST_UTIL, nSUser2.getShortName(), namespace2, Permission.Action.ADMIN); 3491 grantOnNamespace(TEST_UTIL, nSUser3.getShortName(), namespace2, Permission.Action.ADMIN); 3492 grantOnNamespace(TEST_UTIL, toGroupEntry("ns_group2"), namespace2, Permission.Action.READ, 3493 Permission.Action.WRITE); 3494 3495 // Create tables 3496 TableName table1 = TableName.valueOf(namespace1 + TableName.NAMESPACE_DELIM + "t1"); 3497 TableName table2 = TableName.valueOf(namespace2 + TableName.NAMESPACE_DELIM + "t2"); 3498 byte[] TEST_FAMILY2 = Bytes.toBytes("f2"); 3499 byte[] TEST_QUALIFIER2 = Bytes.toBytes("q2"); 3500 createTestTable(table1, TEST_FAMILY); 3501 createTestTable(table2, TEST_FAMILY2); 3502 3503 // Grant table permissions 3504 grantOnTable(TEST_UTIL, toGroupEntry("table_group1"), table1, null, null, 3505 Permission.Action.ADMIN); 3506 grantOnTable(TEST_UTIL, USER_ADMIN.getShortName(), table1, null, null, 3507 Permission.Action.ADMIN); 3508 grantOnTable(TEST_UTIL, USER_ADMIN_CF.getShortName(), table1, TEST_FAMILY, null, 3509 Permission.Action.ADMIN); 3510 grantOnTable(TEST_UTIL, USER_RW.getShortName(), table1, TEST_FAMILY, TEST_QUALIFIER, 3511 Permission.Action.READ); 3512 grantOnTable(TEST_UTIL, USER_RW.getShortName(), table1, TEST_FAMILY, TEST_QUALIFIER2, 3513 Permission.Action.WRITE); 3514 3515 grantOnTable(TEST_UTIL, toGroupEntry("table_group2"), table2, null, null, 3516 Permission.Action.ADMIN); 3517 grantOnTable(TEST_UTIL, USER_ADMIN.getShortName(), table2, null, null, 3518 Permission.Action.ADMIN); 3519 grantOnTable(TEST_UTIL, USER_ADMIN_CF.getShortName(), table2, TEST_FAMILY2, null, 3520 Permission.Action.ADMIN); 3521 grantOnTable(TEST_UTIL, USER_RW.getShortName(), table2, TEST_FAMILY2, TEST_QUALIFIER, 3522 Permission.Action.READ); 3523 grantOnTable(TEST_UTIL, USER_RW.getShortName(), table2, TEST_FAMILY2, TEST_QUALIFIER2, 3524 Permission.Action.WRITE); 3525 3526 List<UserPermission> userPermissions = null; 3527 Collection<String> superUsers = Superusers.getSuperUsers(); 3528 int superUserCount = superUsers.size(); 3529 3530 // Global User ACL 3531 validateGlobalUserACLForGetUserPermissions(conn, nSUser1, globalGroupUser1, globalGroupUser2, 3532 superUsers, superUserCount); 3533 3534 // Namespace ACL 3535 validateNamespaceUserACLForGetUserPermissions(conn, nSUser1, nSUser3, nsGroupUser1, 3536 nsGroupUser2, nsPrefix, namespace1, namespace2); 3537 3538 // Table + Users 3539 validateTableACLForGetUserPermissions(conn, nSUser1, tableGroupUser1, tableGroupUser2, 3540 nsPrefix, table1, table2, TEST_QUALIFIER2, superUsers); 3541 3542 // exception scenarios 3543 3544 try { 3545 // test case with table name as null 3546 assertEquals(3, AccessControlClient.getUserPermissions(conn, null, TEST_FAMILY).size()); 3547 fail("this should have thrown IllegalArgumentException"); 3548 } catch (IllegalArgumentException ex) { 3549 // expected 3550 } 3551 try { 3552 // test case with table name as emplty 3553 assertEquals(3, AccessControlClient 3554 .getUserPermissions(conn, HConstants.EMPTY_STRING, TEST_FAMILY).size()); 3555 fail("this should have thrown IllegalArgumentException"); 3556 } catch (IllegalArgumentException ex) { 3557 // expected 3558 } 3559 try { 3560 // test case with table name as namespace name 3561 assertEquals(3, 3562 AccessControlClient.getUserPermissions(conn, "@" + namespace2, TEST_FAMILY).size()); 3563 fail("this should have thrown IllegalArgumentException"); 3564 } catch (IllegalArgumentException ex) { 3565 // expected 3566 } 3567 3568 // Clean the table and namespace 3569 deleteTable(TEST_UTIL, table1); 3570 deleteTable(TEST_UTIL, table2); 3571 deleteNamespace(TEST_UTIL, namespace1); 3572 deleteNamespace(TEST_UTIL, namespace2); 3573 } finally { 3574 if (conn != null) { 3575 conn.close(); 3576 } 3577 } 3578 } 3579 3580 @Test 3581 public void testGetUserPermissionsScopeBasedAuthorization() throws Throwable { 3582 // Regression test: authorization must be based on permissionScope (derived from 3583 // the type field), not on the tableName field. A user with only table-level ADMIN 3584 // should not be able to request global permissions by passing tableName with 3585 // permissionScope=GLOBAL. 3586 AccessTestAction tablePermWithGlobalScope = new AccessTestAction() { 3587 @Override 3588 public Object run() throws Exception { 3589 ACCESS_CONTROLLER.preGetUserPermissions(ObserverContextImpl.createAndPrepare(CP_ENV), null, 3590 null, TEST_TABLE, null, null, Permission.Scope.GLOBAL); 3591 return null; 3592 } 3593 }; 3594 3595 // USER_ADMIN has global ADMIN, should be allowed 3596 verifyAllowed(tablePermWithGlobalScope, SUPERUSER, USER_ADMIN); 3597 // Users with only table-level ADMIN should be denied 3598 verifyDenied(tablePermWithGlobalScope, USER_OWNER, USER_ADMIN_CF, USER_CREATE, USER_RW, USER_RO, 3599 USER_NONE); 3600 3601 AccessTestAction tablePermWithNamespaceScope = new AccessTestAction() { 3602 @Override 3603 public Object run() throws Exception { 3604 ACCESS_CONTROLLER.preGetUserPermissions(ObserverContextImpl.createAndPrepare(CP_ENV), null, 3605 TEST_TABLE.getNamespaceAsString(), TEST_TABLE, null, null, Permission.Scope.NAMESPACE); 3606 return null; 3607 } 3608 }; 3609 3610 // Users without namespace ADMIN should be denied 3611 verifyDenied(tablePermWithNamespaceScope, USER_OWNER, USER_ADMIN_CF, USER_CREATE, USER_RW, 3612 USER_RO, USER_NONE); 3613 3614 AccessTestAction tablePermWithTableScope = new AccessTestAction() { 3615 @Override 3616 public Object run() throws Exception { 3617 ACCESS_CONTROLLER.preGetUserPermissions(ObserverContextImpl.createAndPrepare(CP_ENV), null, 3618 null, TEST_TABLE, null, null, Permission.Scope.TABLE); 3619 return null; 3620 } 3621 }; 3622 3623 // Users with table ADMIN should be allowed 3624 verifyAllowed(tablePermWithTableScope, SUPERUSER, USER_ADMIN, USER_OWNER); 3625 verifyDenied(tablePermWithTableScope, USER_ADMIN_CF, USER_CREATE, USER_RW, USER_RO, USER_NONE); 3626 3627 AccessTestAction globalPermWithNullScope = new AccessTestAction() { 3628 @Override 3629 public Object run() throws Exception { 3630 ACCESS_CONTROLLER.preGetUserPermissions(ObserverContextImpl.createAndPrepare(CP_ENV), null, 3631 null, null, null, null, null); 3632 return null; 3633 } 3634 }; 3635 3636 // null scope falls back to old behavior (no tableName -> require global ADMIN) 3637 verifyAllowed(globalPermWithNullScope, SUPERUSER, USER_ADMIN); 3638 verifyDenied(globalPermWithNullScope, USER_OWNER, USER_ADMIN_CF, USER_CREATE, USER_RW, USER_RO, 3639 USER_NONE); 3640 } 3641 3642 @Test 3643 public void testHasPermission() throws Throwable { 3644 Connection conn = null; 3645 try { 3646 conn = ConnectionFactory.createConnection(conf); 3647 // Create user and set namespace ACL 3648 User user1 = User.createUserForTesting(conf, "testHasPermissionUser1", new String[0]); 3649 // Grant namespace permission 3650 grantOnNamespaceUsingAccessControlClient(TEST_UTIL, conn, user1.getShortName(), 3651 NamespaceDescriptor.DEFAULT_NAMESPACE.getName(), Permission.Action.ADMIN, 3652 Permission.Action.CREATE, Permission.Action.READ); 3653 3654 // Create user and set table ACL 3655 User user2 = User.createUserForTesting(conf, "testHasPermissionUser2", new String[0]); 3656 // Grant namespace permission 3657 grantOnTableUsingAccessControlClient(TEST_UTIL, conn, user2.getShortName(), TEST_TABLE, 3658 TEST_FAMILY, TEST_QUALIFIER, Permission.Action.READ, Permission.Action.WRITE); 3659 3660 // Verify action privilege 3661 AccessTestAction hasPermissionActionCP = new AccessTestAction() { 3662 @Override 3663 public Object run() throws Exception { 3664 try (Connection conn = ConnectionFactory.createConnection(conf); 3665 Table acl = conn.getTable(PermissionStorage.ACL_TABLE_NAME)) { 3666 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName()); 3667 AccessControlService.BlockingInterface protocol = 3668 AccessControlService.newBlockingStub(service); 3669 Permission.Action[] actions = { Permission.Action.READ, Permission.Action.WRITE }; 3670 AccessControlUtil.hasPermission(null, protocol, TEST_TABLE, TEST_FAMILY, 3671 HConstants.EMPTY_BYTE_ARRAY, "dummy", actions); 3672 } 3673 return null; 3674 } 3675 }; 3676 AccessTestAction hasPermissionAction = new AccessTestAction() { 3677 @Override 3678 public Object run() throws Exception { 3679 try (Connection conn = ConnectionFactory.createConnection(conf)) { 3680 Permission.Action[] actions = { Permission.Action.READ, Permission.Action.WRITE }; 3681 conn.getAdmin().hasUserPermissions("dummy", 3682 Arrays.asList(Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 3683 .withQualifier(HConstants.EMPTY_BYTE_ARRAY).withActions(actions).build())); 3684 } 3685 return null; 3686 } 3687 }; 3688 verifyAllowed(hasPermissionActionCP, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN, USER_OWNER, 3689 USER_ADMIN_CF, user1); 3690 verifyDenied(hasPermissionActionCP, USER_CREATE, USER_RW, USER_RO, USER_NONE, user2); 3691 verifyAllowed(hasPermissionAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN, USER_OWNER, 3692 USER_ADMIN_CF, user1); 3693 verifyDenied(hasPermissionAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, user2); 3694 3695 // Check for global user 3696 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3697 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_ADMIN.getShortName(), 3698 Permission.Action.READ, Permission.Action.WRITE, Permission.Action.CREATE, 3699 Permission.Action.ADMIN)); 3700 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3701 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_ADMIN.getShortName(), 3702 Permission.Action.READ, Permission.Action.WRITE, Permission.Action.CREATE, 3703 Permission.Action.ADMIN, Permission.Action.EXEC)); 3704 3705 // Check for namespace access user 3706 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3707 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, user1.getShortName(), 3708 Permission.Action.ADMIN, Permission.Action.CREATE)); 3709 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3710 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, user1.getShortName(), 3711 Permission.Action.ADMIN, Permission.Action.READ, Permission.Action.EXEC)); 3712 3713 // Check for table owner 3714 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3715 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_OWNER.getShortName(), 3716 Permission.Action.READ, Permission.Action.WRITE, Permission.Action.EXEC, 3717 Permission.Action.CREATE, Permission.Action.ADMIN)); 3718 3719 // Check for table user 3720 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3721 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_CREATE.getShortName(), 3722 Permission.Action.READ, Permission.Action.WRITE)); 3723 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3724 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_RO.getShortName(), 3725 Permission.Action.READ, Permission.Action.WRITE)); 3726 3727 // Check for family access user 3728 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3729 HConstants.EMPTY_BYTE_ARRAY, USER_RO.getShortName(), Permission.Action.READ)); 3730 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3731 HConstants.EMPTY_BYTE_ARRAY, USER_RW.getShortName(), Permission.Action.READ, 3732 Permission.Action.WRITE)); 3733 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3734 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_ADMIN_CF.getShortName(), 3735 Permission.Action.ADMIN, Permission.Action.CREATE)); 3736 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3737 HConstants.EMPTY_BYTE_ARRAY, USER_ADMIN_CF.getShortName(), Permission.Action.ADMIN, 3738 Permission.Action.CREATE)); 3739 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3740 HConstants.EMPTY_BYTE_ARRAY, USER_ADMIN_CF.getShortName(), Permission.Action.READ)); 3741 3742 // Check for qualifier access user 3743 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3744 TEST_QUALIFIER, user2.getShortName(), Permission.Action.READ, Permission.Action.WRITE)); 3745 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), TEST_FAMILY, 3746 TEST_QUALIFIER, user2.getShortName(), Permission.Action.EXEC, Permission.Action.READ)); 3747 assertFalse(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3748 HConstants.EMPTY_BYTE_ARRAY, TEST_QUALIFIER, USER_RW.getShortName(), 3749 Permission.Action.WRITE, Permission.Action.READ)); 3750 3751 // exception scenarios 3752 try { 3753 // test case with table name as null 3754 assertTrue(AccessControlClient.hasPermission(conn, null, HConstants.EMPTY_BYTE_ARRAY, 3755 HConstants.EMPTY_BYTE_ARRAY, null, Permission.Action.READ)); 3756 fail("this should have thrown IllegalArgumentException"); 3757 } catch (IllegalArgumentException ex) { 3758 // expected 3759 } 3760 try { 3761 // test case with username as null 3762 assertTrue(AccessControlClient.hasPermission(conn, TEST_TABLE.getNameAsString(), 3763 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, null, Permission.Action.READ)); 3764 fail("this should have thrown IllegalArgumentException"); 3765 } catch (IllegalArgumentException ex) { 3766 // expected 3767 } 3768 3769 revokeFromNamespaceUsingAccessControlClient(TEST_UTIL, conn, user1.getShortName(), 3770 NamespaceDescriptor.DEFAULT_NAMESPACE.getName(), Permission.Action.ADMIN, 3771 Permission.Action.CREATE, Permission.Action.READ); 3772 revokeFromTableUsingAccessControlClient(TEST_UTIL, conn, user2.getShortName(), TEST_TABLE, 3773 TEST_FAMILY, TEST_QUALIFIER, Permission.Action.READ, Permission.Action.WRITE); 3774 } finally { 3775 if (conn != null) { 3776 conn.close(); 3777 } 3778 } 3779 } 3780 3781 @Test 3782 public void testSwitchRpcThrottle() throws Exception { 3783 AccessTestAction action = new AccessTestAction() { 3784 @Override 3785 public Object run() throws Exception { 3786 ACCESS_CONTROLLER.preSwitchRpcThrottle(ObserverContextImpl.createAndPrepare(CP_ENV), true); 3787 return null; 3788 } 3789 }; 3790 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3791 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3792 } 3793 3794 @Test 3795 public void testIsRpcThrottleEnabled() throws Exception { 3796 AccessTestAction action = new AccessTestAction() { 3797 @Override 3798 public Object run() throws Exception { 3799 ACCESS_CONTROLLER.preIsRpcThrottleEnabled(ObserverContextImpl.createAndPrepare(CP_ENV)); 3800 return null; 3801 } 3802 }; 3803 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3804 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3805 } 3806 3807 @Test 3808 public void testSwitchExceedThrottleQuota() throws Exception { 3809 AccessTestAction action = new AccessTestAction() { 3810 @Override 3811 public Object run() throws Exception { 3812 ACCESS_CONTROLLER.preSwitchExceedThrottleQuota(ObserverContextImpl.createAndPrepare(CP_ENV), 3813 true); 3814 return null; 3815 } 3816 }; 3817 verifyAllowed(action, SUPERUSER, USER_ADMIN); 3818 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER); 3819 } 3820 3821 /* 3822 * Validate Global User ACL 3823 */ 3824 private void validateGlobalUserACLForGetUserPermissions(final Connection conn, User nSUser1, 3825 User globalGroupUser1, User globalGroupUser2, Collection<String> superUsers, int superUserCount) 3826 throws Throwable { 3827 // Verify action privilege 3828 AccessTestAction globalUserPermissionAction = new AccessTestAction() { 3829 @Override 3830 public Object run() throws Exception { 3831 try (Connection conn = ConnectionFactory.createConnection(conf)) { 3832 conn.getAdmin().getUserPermissions( 3833 GetUserPermissionsRequest.newBuilder().withUserName("dummy").build()); 3834 } 3835 return null; 3836 } 3837 }; 3838 verifyAllowed(globalUserPermissionAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 3839 verifyDenied(globalUserPermissionAction, USER_GROUP_CREATE, USER_GROUP_READ, USER_GROUP_WRITE); 3840 3841 // Validate global user permission 3842 List<UserPermission> userPermissions; 3843 assertEquals(6 + superUserCount, AccessControlClient.getUserPermissions(conn, null).size()); 3844 assertEquals(6 + superUserCount, 3845 AccessControlClient.getUserPermissions(conn, HConstants.EMPTY_STRING).size()); 3846 assertEquals(6 + superUserCount, 3847 AccessControlClient.getUserPermissions(conn, null, HConstants.EMPTY_STRING).size()); 3848 userPermissions = AccessControlClient.getUserPermissions(conn, null, USER_ADMIN.getName()); 3849 verifyGetUserPermissionResult(userPermissions, 1, null, null, USER_ADMIN.getName(), superUsers); 3850 assertEquals(0, AccessControlClient.getUserPermissions(conn, null, nSUser1.getName()).size()); 3851 // Global group user ACL 3852 assertEquals(1, 3853 AccessControlClient.getUserPermissions(conn, null, globalGroupUser1.getName()).size()); 3854 assertEquals(2, 3855 AccessControlClient.getUserPermissions(conn, null, globalGroupUser2.getName()).size()); 3856 } 3857 3858 /* 3859 * Validate Namespace User ACL 3860 */ 3861 private void validateNamespaceUserACLForGetUserPermissions(final Connection conn, User nSUser1, 3862 User nSUser3, User nsGroupUser1, User nsGroupUser2, String nsPrefix, final String namespace1, 3863 String namespace2) throws Throwable { 3864 AccessTestAction namespaceUserPermissionAction = new AccessTestAction() { 3865 @Override 3866 public Object run() throws Exception { 3867 try (Connection conn = ConnectionFactory.createConnection(conf)) { 3868 conn.getAdmin().getUserPermissions( 3869 GetUserPermissionsRequest.newBuilder(namespace1).withUserName("dummy").build()); 3870 } 3871 return null; 3872 } 3873 }; 3874 verifyAllowed(namespaceUserPermissionAction, SUPERUSER, USER_GROUP_ADMIN, USER_ADMIN, nSUser1, 3875 nsGroupUser1); 3876 verifyDenied(namespaceUserPermissionAction, USER_GROUP_CREATE, USER_GROUP_READ, 3877 USER_GROUP_WRITE, nSUser3, nsGroupUser2); 3878 3879 List<UserPermission> userPermissions; 3880 assertEquals(6, AccessControlClient.getUserPermissions(conn, "@" + nsPrefix + ".*").size()); 3881 assertEquals(3, AccessControlClient.getUserPermissions(conn, "@" + namespace1).size()); 3882 assertEquals(3, AccessControlClient 3883 .getUserPermissions(conn, "@" + namespace1, HConstants.EMPTY_STRING).size()); 3884 userPermissions = 3885 AccessControlClient.getUserPermissions(conn, "@" + namespace1, nSUser1.getName()); 3886 verifyGetUserPermissionResult(userPermissions, 1, null, null, nSUser1.getName(), null); 3887 userPermissions = 3888 AccessControlClient.getUserPermissions(conn, "@" + namespace1, nSUser3.getName()); 3889 verifyGetUserPermissionResult(userPermissions, 1, null, null, nSUser3.getName(), null); 3890 assertEquals(0, 3891 AccessControlClient.getUserPermissions(conn, "@" + namespace1, USER_ADMIN.getName()).size()); 3892 // Namespace group user ACL 3893 assertEquals(1, AccessControlClient 3894 .getUserPermissions(conn, "@" + namespace1, nsGroupUser1.getName()).size()); 3895 assertEquals(1, AccessControlClient 3896 .getUserPermissions(conn, "@" + namespace2, nsGroupUser2.getName()).size()); 3897 } 3898 3899 /* 3900 * Validate Table User ACL 3901 */ 3902 private void validateTableACLForGetUserPermissions(final Connection conn, User nSUser1, 3903 User tableGroupUser1, User tableGroupUser2, String nsPrefix, TableName table1, TableName table2, 3904 byte[] TEST_QUALIFIER2, Collection<String> superUsers) throws Throwable { 3905 AccessTestAction tableUserPermissionAction = new AccessTestAction() { 3906 @Override 3907 public Object run() throws Exception { 3908 try (Connection conn = ConnectionFactory.createConnection(conf)) { 3909 conn.getAdmin().getUserPermissions(GetUserPermissionsRequest.newBuilder(TEST_TABLE) 3910 .withFamily(TEST_FAMILY).withQualifier(TEST_QUALIFIER).withUserName("dummy").build()); 3911 } 3912 return null; 3913 } 3914 }; 3915 verifyAllowed(tableUserPermissionAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_ADMIN_CF); 3916 verifyDenied(tableUserPermissionAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_CREATE); 3917 3918 List<UserPermission> userPermissions; 3919 assertEquals(12, AccessControlClient.getUserPermissions(conn, nsPrefix + ".*").size()); 3920 assertEquals(6, AccessControlClient.getUserPermissions(conn, table1.getNameAsString()).size()); 3921 assertEquals(6, AccessControlClient 3922 .getUserPermissions(conn, table1.getNameAsString(), HConstants.EMPTY_STRING).size()); 3923 userPermissions = AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3924 USER_ADMIN_CF.getName()); 3925 verifyGetUserPermissionResult(userPermissions, 1, null, null, USER_ADMIN_CF.getName(), null); 3926 assertEquals(0, AccessControlClient 3927 .getUserPermissions(conn, table1.getNameAsString(), nSUser1.getName()).size()); 3928 // Table group user ACL 3929 assertEquals(1, AccessControlClient 3930 .getUserPermissions(conn, table1.getNameAsString(), tableGroupUser1.getName()).size()); 3931 assertEquals(1, AccessControlClient 3932 .getUserPermissions(conn, table2.getNameAsString(), tableGroupUser2.getName()).size()); 3933 3934 // Table Users + CF 3935 assertEquals(12, AccessControlClient 3936 .getUserPermissions(conn, nsPrefix + ".*", HConstants.EMPTY_BYTE_ARRAY).size()); 3937 userPermissions = AccessControlClient.getUserPermissions(conn, nsPrefix + ".*", TEST_FAMILY); 3938 verifyGetUserPermissionResult(userPermissions, 3, TEST_FAMILY, null, null, null); 3939 assertEquals(0, AccessControlClient 3940 .getUserPermissions(conn, table1.getNameAsString(), Bytes.toBytes("dummmyCF")).size()); 3941 3942 // Table Users + CF + User 3943 assertEquals(3, 3944 AccessControlClient 3945 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, HConstants.EMPTY_STRING) 3946 .size()); 3947 userPermissions = AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3948 TEST_FAMILY, USER_ADMIN_CF.getName()); 3949 verifyGetUserPermissionResult(userPermissions, 1, null, null, USER_ADMIN_CF.getName(), 3950 superUsers); 3951 assertEquals(0, AccessControlClient 3952 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, nSUser1.getName()).size()); 3953 3954 // Table Users + CF + CQ 3955 assertEquals(3, AccessControlClient 3956 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, HConstants.EMPTY_BYTE_ARRAY) 3957 .size()); 3958 assertEquals(1, AccessControlClient 3959 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, TEST_QUALIFIER).size()); 3960 assertEquals(1, AccessControlClient 3961 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, TEST_QUALIFIER2).size()); 3962 assertEquals(2, AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3963 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, USER_RW.getName()).size()); 3964 assertEquals(0, 3965 AccessControlClient 3966 .getUserPermissions(conn, table1.getNameAsString(), TEST_FAMILY, Bytes.toBytes("dummmyCQ")) 3967 .size()); 3968 3969 // Table Users + CF + CQ + User 3970 assertEquals(3, AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3971 TEST_FAMILY, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_STRING).size()); 3972 assertEquals(1, AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3973 TEST_FAMILY, TEST_QUALIFIER, USER_RW.getName()).size()); 3974 assertEquals(1, AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3975 TEST_FAMILY, TEST_QUALIFIER2, USER_RW.getName()).size()); 3976 assertEquals(0, AccessControlClient.getUserPermissions(conn, table1.getNameAsString(), 3977 TEST_FAMILY, TEST_QUALIFIER2, nSUser1.getName()).size()); 3978 } 3979 3980 /* 3981 * Validate the user permission against the specified column family, column qualifier and user 3982 * name. 3983 */ 3984 private void verifyGetUserPermissionResult(List<UserPermission> userPermissions, int resultCount, 3985 byte[] cf, byte[] cq, String userName, Collection<String> superUsers) { 3986 assertEquals(resultCount, userPermissions.size()); 3987 3988 for (UserPermission perm : userPermissions) { 3989 if (perm.getPermission() instanceof TablePermission) { 3990 TablePermission tablePerm = (TablePermission) perm.getPermission(); 3991 if (cf != null) { 3992 assertTrue(Bytes.equals(cf, tablePerm.getFamily())); 3993 } 3994 if (cq != null) { 3995 assertTrue(Bytes.equals(cq, tablePerm.getQualifier())); 3996 } 3997 if (userName != null && (superUsers == null || !superUsers.contains(perm.getUser()))) { 3998 assertTrue(userName.equals(perm.getUser())); 3999 } 4000 } else if ( 4001 perm.getPermission() instanceof NamespacePermission 4002 || perm.getPermission() instanceof GlobalPermission 4003 ) { 4004 if (userName != null && (superUsers == null || !superUsers.contains(perm.getUser()))) { 4005 assertTrue(userName.equals(perm.getUser())); 4006 } 4007 } 4008 } 4009 } 4010 4011 /* 4012 * Dummy ShellBasedUnixGroupsMapping class to retrieve the groups for the test users. 4013 */ 4014 public static class MyShellBasedUnixGroupsMapping extends ShellBasedUnixGroupsMapping 4015 implements GroupMappingServiceProvider { 4016 @Override 4017 public List<String> getGroups(String user) throws IOException { 4018 if (user.equals("globalGroupUser1")) { 4019 return Arrays.asList(new String[] { "group_admin" }); 4020 } else if (user.equals("globalGroupUser2")) { 4021 return Arrays.asList(new String[] { "group_admin", "group_create" }); 4022 } else if (user.equals("nsGroupUser1")) { 4023 return Arrays.asList(new String[] { "ns_group1" }); 4024 } else if (user.equals("nsGroupUser2")) { 4025 return Arrays.asList(new String[] { "ns_group2" }); 4026 } else if (user.equals("tableGroupUser1")) { 4027 return Arrays.asList(new String[] { "table_group1" }); 4028 } else if (user.equals("tableGroupUser2")) { 4029 return Arrays.asList(new String[] { "table_group2" }); 4030 } else { 4031 return super.getGroups(user); 4032 } 4033 } 4034 } 4035}