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.hamcrest.MatcherAssert.assertThat; 021import static org.hamcrest.Matchers.containsString; 022import static org.junit.jupiter.api.Assertions.assertArrayEquals; 023import static org.junit.jupiter.api.Assertions.assertEquals; 024import static org.junit.jupiter.api.Assertions.assertFalse; 025import static org.junit.jupiter.api.Assertions.assertNotNull; 026import static org.junit.jupiter.api.Assertions.assertNull; 027import static org.junit.jupiter.api.Assertions.assertThrows; 028import static org.junit.jupiter.api.Assertions.assertTrue; 029 030import java.io.IOException; 031import java.util.Arrays; 032import java.util.List; 033import java.util.Map; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.HBaseTestingUtil; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.client.Admin; 038import org.apache.hadoop.hbase.client.Connection; 039import org.apache.hadoop.hbase.client.ConnectionFactory; 040import org.apache.hadoop.hbase.client.Put; 041import org.apache.hadoop.hbase.client.Table; 042import org.apache.hadoop.hbase.security.User; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.testclassification.SecurityTests; 045import org.apache.hadoop.hbase.util.Bytes; 046import org.junit.jupiter.api.AfterAll; 047import org.junit.jupiter.api.AfterEach; 048import org.junit.jupiter.api.BeforeAll; 049import org.junit.jupiter.api.Tag; 050import org.junit.jupiter.api.Test; 051import org.slf4j.Logger; 052import org.slf4j.LoggerFactory; 053 054import org.apache.hbase.thirdparty.com.google.common.collect.ArrayListMultimap; 055import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap; 056 057/** 058 * Test the reading and writing of access permissions on {@code _acl_} table. 059 */ 060@Tag(SecurityTests.TAG) 061@Tag(MediumTests.TAG) 062public class TestTablePermissions { 063 064 private static final Logger LOG = LoggerFactory.getLogger(TestTablePermissions.class); 065 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 066 067 private static String TEST_NAMESPACE = "perms_test_ns"; 068 private static String TEST_NAMESPACE2 = "perms_test_ns2"; 069 private static TableName TEST_TABLE = TableName.valueOf("perms_test"); 070 private static TableName TEST_TABLE2 = TableName.valueOf("perms_test2"); 071 private static byte[] TEST_FAMILY = Bytes.toBytes("f1"); 072 private static byte[] TEST_QUALIFIER = Bytes.toBytes("col1"); 073 074 @BeforeAll 075 public static void beforeClass() throws Exception { 076 // setup configuration 077 Configuration conf = UTIL.getConfiguration(); 078 SecureTestUtil.enableSecurity(conf); 079 080 UTIL.startMiniCluster(); 081 082 // Wait for the ACL table to become available 083 UTIL.waitTableEnabled(PermissionStorage.ACL_TABLE_NAME); 084 UTIL.waitTableAvailable(TableName.valueOf("hbase:acl")); 085 086 UTIL.createTable(TEST_TABLE, TEST_FAMILY); 087 UTIL.createTable(TEST_TABLE2, TEST_FAMILY); 088 } 089 090 @AfterAll 091 public static void afterClass() throws Exception { 092 UTIL.shutdownMiniCluster(); 093 } 094 095 @AfterEach 096 public void tearDown() throws Exception { 097 Configuration conf = UTIL.getConfiguration(); 098 try (Connection conn = ConnectionFactory.createConnection(conf)) { 099 PermissionStorage.removeTablePermissions(conn, TEST_TABLE); 100 PermissionStorage.removeTablePermissions(conn, TEST_TABLE2); 101 PermissionStorage.removeTablePermissions(conn, PermissionStorage.ACL_TABLE_NAME); 102 } 103 } 104 105 private ListMultimap<String, UserPermission> getTablePerms(Connection conn, TableName tableName) 106 throws IOException { 107 ListMultimap<String, UserPermission> perms = ArrayListMultimap.create(); 108 PermissionStorage.getTablePermissions(conn, tableName) 109 .forEach(up -> perms.put(up.getUser(), up)); 110 return perms; 111 } 112 113 private ListMultimap<String, UserPermission> getGlobalPerms(Connection conn) throws IOException { 114 ListMultimap<String, UserPermission> perms = ArrayListMultimap.create(); 115 PermissionStorage.getGlobalPermissions(conn).forEach(up -> perms.put(up.getUser(), up)); 116 return perms; 117 } 118 119 @Test 120 public void testBasicWrite() throws Exception { 121 Configuration conf = UTIL.getConfiguration(); 122 try (Connection connection = ConnectionFactory.createConnection(conf)) { 123 // add some permissions 124 PermissionStorage.addUserPermission(connection, 125 new UserPermission("george", Permission.newBuilder(TEST_TABLE) 126 .withActions(Permission.Action.READ, Permission.Action.WRITE).build()), 127 false); 128 PermissionStorage.addUserPermission(connection, new UserPermission("hubert", 129 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build()), false); 130 PermissionStorage.addUserPermission(connection, 131 new UserPermission("humphrey", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 132 .withQualifier(TEST_QUALIFIER).withActions(Permission.Action.READ).build()), 133 false); 134 // retrieve the same 135 ListMultimap<String, UserPermission> perms = getTablePerms(connection, TEST_TABLE); 136 List<UserPermission> userPerms = perms.get("george"); 137 assertNotNull(userPerms, "Should have permissions for george"); 138 assertEquals(1, userPerms.size(), "Should have 1 permission for george"); 139 assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope()); 140 TablePermission permission = (TablePermission) userPerms.get(0).getPermission(); 141 assertEquals(TEST_TABLE, permission.getTableName(), "Permission should be for " + TEST_TABLE); 142 assertNull(permission.getFamily(), "Column family should be empty"); 143 144 // check actions 145 assertNotNull(permission.getActions()); 146 assertEquals(2, permission.getActions().length); 147 List<Permission.Action> actions = Arrays.asList(permission.getActions()); 148 assertTrue(actions.contains(TablePermission.Action.READ)); 149 assertTrue(actions.contains(TablePermission.Action.WRITE)); 150 151 userPerms = perms.get("hubert"); 152 assertNotNull(userPerms, "Should have permissions for hubert"); 153 assertEquals(1, userPerms.size(), "Should have 1 permission for hubert"); 154 assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope()); 155 permission = (TablePermission) userPerms.get(0).getPermission(); 156 assertEquals(TEST_TABLE, permission.getTableName(), "Permission should be for " + TEST_TABLE); 157 assertNull(permission.getFamily(), "Column family should be empty"); 158 159 // check actions 160 assertNotNull(permission.getActions()); 161 assertEquals(1, permission.getActions().length); 162 actions = Arrays.asList(permission.getActions()); 163 assertTrue(actions.contains(TablePermission.Action.READ)); 164 assertFalse(actions.contains(TablePermission.Action.WRITE)); 165 166 userPerms = perms.get("humphrey"); 167 assertNotNull(userPerms, "Should have permissions for humphrey"); 168 assertEquals(1, userPerms.size(), "Should have 1 permission for humphrey"); 169 assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope()); 170 permission = (TablePermission) userPerms.get(0).getPermission(); 171 assertEquals(TEST_TABLE, permission.getTableName(), "Permission should be for " + TEST_TABLE); 172 assertTrue(Bytes.equals(TEST_FAMILY, permission.getFamily()), 173 "Permission should be for family " + Bytes.toString(TEST_FAMILY)); 174 assertTrue(Bytes.equals(TEST_QUALIFIER, permission.getQualifier()), 175 "Permission should be for qualifier " + Bytes.toString(TEST_QUALIFIER)); 176 177 // check actions 178 assertNotNull(permission.getActions()); 179 assertEquals(1, permission.getActions().length); 180 actions = Arrays.asList(permission.getActions()); 181 assertTrue(actions.contains(TablePermission.Action.READ)); 182 assertFalse(actions.contains(TablePermission.Action.WRITE)); 183 184 // table 2 permissions 185 PermissionStorage.addUserPermission(connection, 186 new UserPermission("hubert", Permission.newBuilder(TEST_TABLE2) 187 .withActions(Permission.Action.READ, Permission.Action.WRITE).build()), 188 false); 189 190 // check full load 191 Map<byte[], ListMultimap<String, UserPermission>> allPerms = 192 PermissionStorage.loadAll(connection); 193 assertEquals(2, allPerms.size(), 194 "Full permission map should have entries for both test tables"); 195 196 userPerms = allPerms.get(TEST_TABLE.getName()).get("hubert"); 197 assertNotNull(userPerms); 198 assertEquals(1, userPerms.size()); 199 assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope()); 200 permission = (TablePermission) userPerms.get(0).getPermission(); 201 assertEquals(TEST_TABLE, permission.getTableName()); 202 assertEquals(1, permission.getActions().length); 203 assertEquals(Permission.Action.READ, permission.getActions()[0]); 204 205 userPerms = allPerms.get(TEST_TABLE2.getName()).get("hubert"); 206 assertNotNull(userPerms); 207 assertEquals(1, userPerms.size()); 208 assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope()); 209 permission = (TablePermission) userPerms.get(0).getPermission(); 210 assertEquals(TEST_TABLE2, permission.getTableName()); 211 assertEquals(2, permission.getActions().length); 212 actions = Arrays.asList(permission.getActions()); 213 assertTrue(actions.contains(Permission.Action.READ)); 214 assertTrue(actions.contains(Permission.Action.WRITE)); 215 } 216 } 217 218 @Test 219 public void testRevokeFamilyPermissionKeepsTableLevelPermission() throws Exception { 220 Configuration conf = UTIL.getConfiguration(); 221 try (Connection connection = ConnectionFactory.createConnection(conf)) { 222 // table level READ for userA 223 PermissionStorage.addUserPermission(connection, new UserPermission("userA", 224 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build()), false); 225 // family level WRITE for userA on the same table 226 PermissionStorage.addUserPermission(connection, 227 new UserPermission("userA", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 228 .withActions(Permission.Action.WRITE).build()), 229 false); 230 231 // revoke only the family level WRITE 232 PermissionStorage.removeUserPermission(connection, 233 new UserPermission("userA", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 234 .withActions(Permission.Action.WRITE).build())); 235 236 List<UserPermission> userPerms = getTablePerms(connection, TEST_TABLE).get("userA"); 237 assertEquals(1, userPerms.size(), 238 "family level WRITE should be revoked, only the table level READ should remain"); 239 TablePermission permission = (TablePermission) userPerms.get(0).getPermission(); 240 assertNull(permission.getFamily(), "table level permission should be kept untouched"); 241 assertArrayEquals(new Permission.Action[] { Permission.Action.READ }, permission.getActions(), 242 "table level READ should be kept untouched"); 243 } 244 } 245 246 @Test 247 public void testRevokeFamilyPermissionKeepsTableLevelActions() throws Exception { 248 Configuration conf = UTIL.getConfiguration(); 249 try (Connection connection = ConnectionFactory.createConnection(conf)) { 250 // table level READ,WRITE for userB 251 PermissionStorage.addUserPermission(connection, 252 new UserPermission("userB", Permission.newBuilder(TEST_TABLE) 253 .withActions(Permission.Action.READ, Permission.Action.WRITE).build()), 254 false); 255 // family level WRITE for userB on the same table 256 PermissionStorage.addUserPermission(connection, 257 new UserPermission("userB", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 258 .withActions(Permission.Action.WRITE).build()), 259 false); 260 261 // revoke only the family level WRITE 262 PermissionStorage.removeUserPermission(connection, 263 new UserPermission("userB", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 264 .withActions(Permission.Action.WRITE).build())); 265 266 List<UserPermission> userPerms = getTablePerms(connection, TEST_TABLE).get("userB"); 267 assertEquals(1, userPerms.size(), 268 "family level WRITE should be revoked, only the table level permission should remain"); 269 TablePermission permission = (TablePermission) userPerms.get(0).getPermission(); 270 assertNull(permission.getFamily(), "table level permission should be kept untouched"); 271 assertArrayEquals(new Permission.Action[] { Permission.Action.READ, Permission.Action.WRITE }, 272 permission.getActions(), "table level actions should be kept untouched"); 273 } 274 } 275 276 @Test 277 public void testPersistence() throws Exception { 278 Configuration conf = UTIL.getConfiguration(); 279 try (Connection connection = ConnectionFactory.createConnection(conf)) { 280 PermissionStorage.addUserPermission(connection, new UserPermission("albert", 281 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build()), false); 282 PermissionStorage.addUserPermission(connection, 283 new UserPermission("betty", Permission.newBuilder(TEST_TABLE) 284 .withActions(Permission.Action.READ, Permission.Action.WRITE).build()), 285 false); 286 PermissionStorage.addUserPermission(connection, 287 new UserPermission("clark", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 288 .withActions(Permission.Action.READ).build()), 289 false); 290 PermissionStorage.addUserPermission(connection, 291 new UserPermission("dwight", Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 292 .withQualifier(TEST_QUALIFIER).withActions(Permission.Action.WRITE).build()), 293 false); 294 // verify permissions survive changes in table metadata 295 ListMultimap<String, UserPermission> preperms = getTablePerms(connection, TEST_TABLE); 296 297 Table table = UTIL.getConnection().getTable(TEST_TABLE); 298 table.put( 299 new Put(Bytes.toBytes("row1")).addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes("v1"))); 300 table.put( 301 new Put(Bytes.toBytes("row2")).addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes("v2"))); 302 Admin admin = UTIL.getAdmin(); 303 try { 304 admin.split(TEST_TABLE); 305 } catch (IOException e) { 306 // although split fail, this may not affect following check 307 // In old Split API without AM2, if region's best split key is not found, 308 // there are not exception thrown. But in current API, exception 309 // will be thrown. 310 LOG.debug("region is not splittable, because " + e); 311 } 312 313 // wait for split 314 Thread.sleep(10000); 315 316 ListMultimap<String, UserPermission> postperms = getTablePerms(connection, TEST_TABLE); 317 318 checkMultimapEqual(preperms, postperms); 319 } 320 } 321 322 @Test 323 public void testSerialization() throws Exception { 324 Configuration conf = UTIL.getConfiguration(); 325 ListMultimap<String, UserPermission> permissions = createPermissions(); 326 byte[] permsData = PermissionStorage.writePermissionsAsBytes(permissions, conf); 327 328 ListMultimap<String, UserPermission> copy = 329 PermissionStorage.readUserPermission(permsData, conf); 330 331 checkMultimapEqual(permissions, copy); 332 } 333 334 private ListMultimap<String, UserPermission> createPermissions() { 335 ListMultimap<String, UserPermission> permissions = ArrayListMultimap.create(); 336 permissions.put("george", new UserPermission("george", 337 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build())); 338 permissions.put("george", new UserPermission("george", Permission.newBuilder(TEST_TABLE) 339 .withFamily(TEST_FAMILY).withActions(Permission.Action.WRITE).build())); 340 permissions.put("george", new UserPermission("george", 341 Permission.newBuilder(TEST_TABLE2).withActions(Permission.Action.READ).build())); 342 permissions.put("hubert", new UserPermission("hubert", Permission.newBuilder(TEST_TABLE2) 343 .withActions(Permission.Action.READ, Permission.Action.WRITE).build())); 344 permissions.put("bruce", new UserPermission("bruce", 345 Permission.newBuilder(TEST_NAMESPACE).withActions(Permission.Action.READ).build())); 346 return permissions; 347 } 348 349 public void checkMultimapEqual(ListMultimap<String, UserPermission> first, 350 ListMultimap<String, UserPermission> second) { 351 assertEquals(first.size(), second.size()); 352 for (String key : first.keySet()) { 353 List<UserPermission> firstPerms = first.get(key); 354 List<UserPermission> secondPerms = second.get(key); 355 assertNotNull(secondPerms); 356 assertEquals(firstPerms.size(), secondPerms.size()); 357 LOG.info("First permissions: " + firstPerms.toString()); 358 LOG.info("Second permissions: " + secondPerms.toString()); 359 for (UserPermission p : firstPerms) { 360 assertTrue(secondPerms.contains(p), "Permission " + p.toString() + " not found"); 361 } 362 } 363 } 364 365 @Test 366 public void testEquals() throws Exception { 367 Permission p1 = Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build(); 368 Permission p2 = Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build(); 369 assertTrue(p1.equals(p2)); 370 assertTrue(p2.equals(p1)); 371 372 p1 = Permission.newBuilder(TEST_TABLE) 373 .withActions(TablePermission.Action.READ, TablePermission.Action.WRITE).build(); 374 p2 = Permission.newBuilder(TEST_TABLE) 375 .withActions(TablePermission.Action.WRITE, TablePermission.Action.READ).build(); 376 assertTrue(p1.equals(p2)); 377 assertTrue(p2.equals(p1)); 378 379 p1 = Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 380 .withActions(TablePermission.Action.READ, TablePermission.Action.WRITE).build(); 381 p2 = Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 382 .withActions(TablePermission.Action.WRITE, TablePermission.Action.READ).build(); 383 assertTrue(p1.equals(p2)); 384 assertTrue(p2.equals(p1)); 385 386 p1 = Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withQualifier(TEST_QUALIFIER) 387 .withActions(TablePermission.Action.READ, TablePermission.Action.WRITE).build(); 388 p2 = Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withQualifier(TEST_QUALIFIER) 389 .withActions(TablePermission.Action.WRITE, TablePermission.Action.READ).build(); 390 assertTrue(p1.equals(p2)); 391 assertTrue(p2.equals(p1)); 392 393 p1 = Permission.newBuilder(TEST_TABLE).withActions(TablePermission.Action.READ).build(); 394 p2 = Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY) 395 .withActions(TablePermission.Action.READ).build(); 396 assertFalse(p1.equals(p2)); 397 assertFalse(p2.equals(p1)); 398 399 p1 = Permission.newBuilder(TEST_TABLE).withActions(TablePermission.Action.READ).build(); 400 p2 = Permission.newBuilder(TEST_TABLE).withActions(TablePermission.Action.WRITE).build(); 401 assertFalse(p1.equals(p2)); 402 assertFalse(p2.equals(p1)); 403 p2 = Permission.newBuilder(TEST_TABLE) 404 .withActions(TablePermission.Action.READ, TablePermission.Action.WRITE).build(); 405 assertFalse(p1.equals(p2)); 406 assertFalse(p2.equals(p1)); 407 408 p1 = Permission.newBuilder(TEST_TABLE).withActions(TablePermission.Action.READ).build(); 409 p2 = Permission.newBuilder(TEST_TABLE2).withActions(TablePermission.Action.READ).build(); 410 assertFalse(p1.equals(p2)); 411 assertFalse(p2.equals(p1)); 412 413 p1 = Permission.newBuilder(TEST_NAMESPACE).withActions(TablePermission.Action.READ).build(); 414 p2 = Permission.newBuilder(TEST_NAMESPACE).withActions(TablePermission.Action.READ).build(); 415 assertEquals(p1, p2); 416 417 p1 = Permission.newBuilder(TEST_NAMESPACE).withActions(TablePermission.Action.READ).build(); 418 p2 = Permission.newBuilder(TEST_NAMESPACE2).withActions(TablePermission.Action.READ).build(); 419 assertFalse(p1.equals(p2)); 420 assertFalse(p2.equals(p1)); 421 } 422 423 @Test 424 public void testGlobalPermission() throws Exception { 425 Configuration conf = UTIL.getConfiguration(); 426 427 // add some permissions 428 try (Connection connection = ConnectionFactory.createConnection(conf)) { 429 PermissionStorage.addUserPermission(connection, new UserPermission("user1", Permission 430 .newBuilder().withActions(Permission.Action.READ, Permission.Action.WRITE).build()), false); 431 PermissionStorage.addUserPermission(connection, new UserPermission("user2", 432 Permission.newBuilder().withActions(Permission.Action.CREATE).build()), false); 433 PermissionStorage.addUserPermission(connection, 434 new UserPermission("user3", 435 Permission.newBuilder() 436 .withActions(Permission.Action.ADMIN, Permission.Action.READ, Permission.Action.CREATE) 437 .build()), 438 false); 439 440 ListMultimap<String, UserPermission> perms = getGlobalPerms(connection); 441 List<UserPermission> user1Perms = perms.get("user1"); 442 assertEquals(1, user1Perms.size(), "Should have 1 permission for user1"); 443 assertArrayEquals(new Permission.Action[] { Permission.Action.READ, Permission.Action.WRITE }, 444 user1Perms.get(0).getPermission().getActions(), "user1 should have WRITE permission"); 445 446 List<UserPermission> user2Perms = perms.get("user2"); 447 assertEquals(1, user2Perms.size(), "Should have 1 permission for user2"); 448 assertArrayEquals(new Permission.Action[] { Permission.Action.CREATE }, 449 user2Perms.get(0).getPermission().getActions(), "user2 should have CREATE permission"); 450 451 List<UserPermission> user3Perms = perms.get("user3"); 452 assertEquals(1, user3Perms.size(), "Should have 1 permission for user3"); 453 assertArrayEquals( 454 new Permission.Action[] { Permission.Action.READ, Permission.Action.CREATE, 455 Permission.Action.ADMIN }, 456 user3Perms.get(0).getPermission().getActions(), 457 "user3 should have ADMIN, READ, CREATE permission"); 458 } 459 } 460 461 private void testAddUserPermissionWithInvalidUser0(Connection conn, String invalidUser, 462 String errorMsg) throws Exception { 463 Permission tablePerm = 464 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build(); 465 IOException e = assertThrows( 466 IOException.class, () -> PermissionStorage.addUserPermission(conn, 467 new UserPermission(invalidUser, tablePerm), false), 468 "Should reject user name [" + invalidUser + "]"); 469 assertThat(e.getMessage(), containsString(errorMsg)); 470 } 471 472 @Test 473 public void testAddUserPermissionWithInvalidUser() throws Exception { 474 Configuration conf = UTIL.getConfiguration(); 475 try (Connection conn = ConnectionFactory.createConnection(conf)) { 476 testAddUserPermissionWithInvalidUser0(conn, null, "null user name is not allowed"); 477 testAddUserPermissionWithInvalidUser0(conn, "", "Blank user name '' is not allowed"); 478 testAddUserPermissionWithInvalidUser0(conn, " ", "Blank user name ' ' is not allowed"); 479 testAddUserPermissionWithInvalidUser0(conn, "\t", "Blank user name '\t' is not allowed"); 480 testAddUserPermissionWithInvalidUser0(conn, " user", 481 "Leading or trailing whitespace in user name ' user' is not allowed"); 482 testAddUserPermissionWithInvalidUser0(conn, "user ", 483 "Leading or trailing whitespace in user name 'user ' is not allowed"); 484 testAddUserPermissionWithInvalidUser0(conn, " user ", 485 "Leading or trailing whitespace in user name ' user ' is not allowed"); 486 testAddUserPermissionWithInvalidUser0(conn, "user,user", 487 "',' in user name 'user,user' is not allowed"); 488 489 testAddUserPermissionWithInvalidUser0(conn, "@", "Blank group name '@' is not allowed"); 490 testAddUserPermissionWithInvalidUser0(conn, "@ ", "Blank group name '@ ' is not allowed"); 491 testAddUserPermissionWithInvalidUser0(conn, "@\t", "Blank group name '@\t' is not allowed"); 492 testAddUserPermissionWithInvalidUser0(conn, "@ group", 493 "Leading or trailing whitespace in group name '@ group' is not allowed"); 494 testAddUserPermissionWithInvalidUser0(conn, "@group ", 495 "Leading or trailing whitespace in group name '@group ' is not allowed"); 496 testAddUserPermissionWithInvalidUser0(conn, "@ group ", 497 "Leading or trailing whitespace in group name '@ group ' is not allowed"); 498 testAddUserPermissionWithInvalidUser0(conn, "@group,group", 499 "',' in group name '@group,group' is not allowed"); 500 501 // none of the rejected grants should have been persisted 502 assertTrue(getTablePerms(conn, TEST_TABLE).isEmpty(), 503 "No table permissions should have been written"); 504 } 505 } 506 507 @Test 508 public void testAddUserPermissionWithInvalidAction() throws Exception { 509 Configuration conf = UTIL.getConfiguration(); 510 try (Connection connection = ConnectionFactory.createConnection(conf)) { 511 // no actions associated with the permission 512 IOException e = 513 assertThrows(IOException.class, () -> PermissionStorage.addUserPermission(connection, 514 new UserPermission("george", Permission.newBuilder(TEST_TABLE).build()), false)); 515 assertThat(e.getMessage(), containsString("No actions associated with user")); 516 517 // none of the rejected grants should have been persisted 518 assertTrue(getTablePerms(connection, TEST_TABLE).isEmpty(), 519 "No table permissions should have been written"); 520 } 521 } 522 523 private void testRemoveUserPermissionWithInvalidUser0(Connection conn, String invalidUser, 524 String errorMsg) throws Exception { 525 Permission tablePerm = 526 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build(); 527 IOException e = assertThrows(IOException.class, 528 () -> PermissionStorage.removeUserPermission(conn, 529 new UserPermission(invalidUser, tablePerm)), 530 "Should reject user name [" + invalidUser + "]"); 531 assertThat(e.getMessage(), containsString(errorMsg)); 532 } 533 534 @Test 535 public void testRemoveUserPermissionWithInvalidUser() throws Exception { 536 Configuration conf = UTIL.getConfiguration(); 537 try (Connection conn = ConnectionFactory.createConnection(conf)) { 538 testRemoveUserPermissionWithInvalidUser0(conn, null, "null user name is not allowed"); 539 testRemoveUserPermissionWithInvalidUser0(conn, "", "Blank user name '' is not allowed"); 540 testRemoveUserPermissionWithInvalidUser0(conn, " ", "Blank user name ' ' is not allowed"); 541 testRemoveUserPermissionWithInvalidUser0(conn, "\t", "Blank user name '\t' is not allowed"); 542 testRemoveUserPermissionWithInvalidUser0(conn, " user", 543 "Leading or trailing whitespace in user name ' user' is not allowed"); 544 testRemoveUserPermissionWithInvalidUser0(conn, "user ", 545 "Leading or trailing whitespace in user name 'user ' is not allowed"); 546 testRemoveUserPermissionWithInvalidUser0(conn, " user ", 547 "Leading or trailing whitespace in user name ' user ' is not allowed"); 548 testRemoveUserPermissionWithInvalidUser0(conn, "user,user", 549 "',' in user name 'user,user' is not allowed"); 550 551 testRemoveUserPermissionWithInvalidUser0(conn, "@", "Blank group name '@' is not allowed"); 552 testRemoveUserPermissionWithInvalidUser0(conn, "@ ", "Blank group name '@ ' is not allowed"); 553 testRemoveUserPermissionWithInvalidUser0(conn, "@\t", 554 "Blank group name '@\t' is not allowed"); 555 testRemoveUserPermissionWithInvalidUser0(conn, "@ group", 556 "Leading or trailing whitespace in group name '@ group' is not allowed"); 557 testRemoveUserPermissionWithInvalidUser0(conn, "@group ", 558 "Leading or trailing whitespace in group name '@group ' is not allowed"); 559 testRemoveUserPermissionWithInvalidUser0(conn, "@ group ", 560 "Leading or trailing whitespace in group name '@ group ' is not allowed"); 561 testRemoveUserPermissionWithInvalidUser0(conn, "@group,group", 562 "',' in group name '@group,group' is not allowed"); 563 } 564 } 565 566 @Test 567 public void testRemoveWithNoActionsRemovesAllActions() throws Exception { 568 Configuration conf = UTIL.getConfiguration(); 569 try (Connection connection = ConnectionFactory.createConnection(conf)) { 570 PermissionStorage.addUserPermission(connection, 571 new UserPermission("userC", Permission.newBuilder(TEST_TABLE) 572 .withActions(Permission.Action.READ, Permission.Action.WRITE).build()), 573 false); 574 575 // revoking without specifying actions is valid and removes the whole permission 576 PermissionStorage.removeUserPermission(connection, 577 new UserPermission("userC", Permission.newBuilder(TEST_TABLE).build())); 578 579 assertTrue(getTablePerms(connection, TEST_TABLE).get("userC").isEmpty(), 580 "table level permission should be fully revoked"); 581 } 582 } 583 584 @Test 585 public void testAddGroupPermission() throws Exception { 586 Configuration conf = UTIL.getConfiguration(); 587 try (Connection connection = ConnectionFactory.createConnection(conf)) { 588 // a group name is stored as "@group", only a bare "@" is invalid 589 PermissionStorage.addUserPermission(connection, new UserPermission("@devs", 590 Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build()), false); 591 592 ListMultimap<String, UserPermission> perms = getTablePerms(connection, TEST_TABLE); 593 List<UserPermission> groupPerms = perms.get("@devs"); 594 assertEquals(1, groupPerms.size(), "Should have 1 permission for group devs"); 595 assertEquals(Permission.Scope.TABLE, groupPerms.get(0).getAccessScope()); 596 TablePermission permission = (TablePermission) groupPerms.get(0).getPermission(); 597 assertEquals(TEST_TABLE, permission.getTableName()); 598 assertArrayEquals(new Permission.Action[] { Permission.Action.READ }, 599 permission.getActions()); 600 } 601 } 602 603 @Test 604 public void testAuthManager() throws Exception { 605 Configuration conf = UTIL.getConfiguration(); 606 /** 607 * test a race condition causing AuthManager to sometimes fail global permissions checks when 608 * the global cache is being updated 609 */ 610 AuthManager authManager = new AuthManager(conf); 611 // currently running user is the system user and should have global admin perms 612 User currentUser = User.getCurrent(); 613 assertTrue(authManager.authorizeUserGlobal(currentUser, Permission.Action.ADMIN)); 614 try (Connection connection = ConnectionFactory.createConnection(conf)) { 615 for (int i = 1; i <= 50; i++) { 616 PermissionStorage.addUserPermission(connection, 617 new UserPermission("testauth" + i, 618 Permission.newBuilder() 619 .withActions(Permission.Action.ADMIN, Permission.Action.READ, Permission.Action.WRITE) 620 .build()), 621 false); 622 // make sure the system user still shows as authorized 623 assertTrue(authManager.authorizeUserGlobal(currentUser, Permission.Action.ADMIN), 624 "Failed current user auth check on iter " + i); 625 } 626 } 627 } 628}