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.rsgroup; 019 020import static org.apache.hadoop.hbase.AuthUtil.toGroupEntry; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.fail; 023 024import java.io.IOException; 025import java.util.Optional; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.TableNotFoundException; 030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 031import org.apache.hadoop.hbase.client.Connection; 032import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 033import org.apache.hadoop.hbase.ipc.RpcServer; 034import org.apache.hadoop.hbase.master.HMaster; 035import org.apache.hadoop.hbase.security.User; 036import org.apache.hadoop.hbase.security.UserProvider; 037import org.apache.hadoop.hbase.security.access.AccessChecker; 038import org.apache.hadoop.hbase.security.access.AccessControlClient; 039import org.apache.hadoop.hbase.security.access.Permission; 040import org.apache.hadoop.hbase.security.access.PermissionStorage; 041import org.apache.hadoop.hbase.security.access.SecureTestUtil; 042import org.apache.hadoop.hbase.testclassification.MediumTests; 043import org.apache.hadoop.hbase.testclassification.SecurityTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.BeforeAll; 047import org.junit.jupiter.api.Tag; 048import org.junit.jupiter.api.Test; 049import org.slf4j.Logger; 050import org.slf4j.LoggerFactory; 051 052/** 053 * Performs authorization checks for rsgroup operations, according to different levels of authorized 054 * users. 055 */ 056@Tag(SecurityTests.TAG) 057@Tag(MediumTests.TAG) 058public class TestRSGroupsWithACL extends SecureTestUtil { 059 060 private static final Logger LOG = LoggerFactory.getLogger(TestRSGroupsWithACL.class); 061 private static TableName TEST_TABLE = TableName.valueOf("testtable1"); 062 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 063 private static Configuration conf; 064 065 private static Connection systemUserConnection; 066 // user with all permissions 067 private static User SUPERUSER; 068 // user granted with all global permission 069 private static User USER_ADMIN; 070 // user with rw permissions on column family. 071 private static User USER_RW; 072 // user with read-only permissions 073 private static User USER_RO; 074 // user is table owner. will have all permissions on table 075 private static User USER_OWNER; 076 // user with create table permissions alone 077 private static User USER_CREATE; 078 // user with no permissions 079 private static User USER_NONE; 080 081 private static final String GROUP_ADMIN = "group_admin"; 082 private static final String GROUP_CREATE = "group_create"; 083 private static final String GROUP_READ = "group_read"; 084 private static final String GROUP_WRITE = "group_write"; 085 086 private static User USER_GROUP_ADMIN; 087 private static User USER_GROUP_CREATE; 088 private static User USER_GROUP_READ; 089 private static User USER_GROUP_WRITE; 090 091 private static byte[] TEST_FAMILY = Bytes.toBytes("f1"); 092 private static HMaster master; 093 private static AccessChecker accessChecker; 094 private static UserProvider userProvider; 095 096 @BeforeAll 097 public static void setupBeforeClass() throws Exception { 098 // setup configuration 099 conf = TEST_UTIL.getConfiguration(); 100 // Enable security 101 enableSecurity(conf); 102 // Verify enableSecurity sets up what we require 103 verifyConfiguration(conf); 104 // Enable rsgroup 105 RSGroupUtil.enableRSGroup(conf); 106 107 TEST_UTIL.startMiniCluster(); 108 // Wait for the ACL table to become available 109 TEST_UTIL.waitUntilAllRegionsAssigned(PermissionStorage.ACL_TABLE_NAME); 110 TEST_UTIL.waitUntilAllRegionsAssigned(RSGroupInfoManagerImpl.RSGROUP_TABLE_NAME); 111 TEST_UTIL.waitUntilNoRegionsInTransition(); 112 113 // create a set of test users 114 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" }); 115 USER_ADMIN = User.createUserForTesting(conf, "admin2", new String[0]); 116 USER_RW = User.createUserForTesting(conf, "rwuser", new String[0]); 117 USER_RO = User.createUserForTesting(conf, "rouser", new String[0]); 118 USER_OWNER = User.createUserForTesting(conf, "owner", new String[0]); 119 USER_CREATE = User.createUserForTesting(conf, "tbl_create", new String[0]); 120 USER_NONE = User.createUserForTesting(conf, "nouser", new String[0]); 121 122 USER_GROUP_ADMIN = 123 User.createUserForTesting(conf, "user_group_admin", new String[] { GROUP_ADMIN }); 124 USER_GROUP_CREATE = 125 User.createUserForTesting(conf, "user_group_create", new String[] { GROUP_CREATE }); 126 USER_GROUP_READ = 127 User.createUserForTesting(conf, "user_group_read", new String[] { GROUP_READ }); 128 USER_GROUP_WRITE = 129 User.createUserForTesting(conf, "user_group_write", new String[] { GROUP_WRITE }); 130 131 // Grant table creation permission to USER_OWNER 132 grantGlobal(TEST_UTIL, USER_OWNER.getShortName(), Permission.Action.CREATE); 133 134 systemUserConnection = TEST_UTIL.getConnection(); 135 setUpTableAndUserPermissions(); 136 master = TEST_UTIL.getHBaseCluster().getMaster(); 137 accessChecker = master.getAccessChecker(); 138 userProvider = UserProvider.instantiate(TEST_UTIL.getConfiguration()); 139 } 140 141 private void checkPermission(String request) throws IOException { 142 accessChecker.requirePermission(getActiveUser(), request, null, Permission.Action.ADMIN); 143 } 144 145 private User getActiveUser() throws IOException { 146 // for non-rpc handling, fallback to system user 147 Optional<User> optionalUser = RpcServer.getRequestUser(); 148 if (optionalUser.isPresent()) { 149 return optionalUser.get(); 150 } 151 return userProvider.getCurrent(); 152 } 153 154 private static void setUpTableAndUserPermissions() throws Exception { 155 TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(TEST_TABLE); 156 ColumnFamilyDescriptorBuilder cfd = ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY); 157 cfd.setMaxVersions(100); 158 tableBuilder.setColumnFamily(cfd.build()); 159 createTable(TEST_UTIL, USER_OWNER, tableBuilder.build(), new byte[][] { Bytes.toBytes("s") }); 160 161 // Set up initial grants 162 grantGlobal(TEST_UTIL, USER_ADMIN.getShortName(), Permission.Action.ADMIN, 163 Permission.Action.CREATE, Permission.Action.READ, Permission.Action.WRITE); 164 165 grantOnTable(TEST_UTIL, USER_RW.getShortName(), TEST_TABLE, TEST_FAMILY, null, 166 Permission.Action.READ, Permission.Action.WRITE); 167 168 // USER_CREATE is USER_RW plus CREATE permissions 169 grantOnTable(TEST_UTIL, USER_CREATE.getShortName(), TEST_TABLE, null, null, 170 Permission.Action.CREATE, Permission.Action.READ, Permission.Action.WRITE); 171 172 grantOnTable(TEST_UTIL, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null, 173 Permission.Action.READ); 174 175 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_ADMIN), Permission.Action.ADMIN); 176 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_CREATE), Permission.Action.CREATE); 177 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_READ), Permission.Action.READ); 178 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_WRITE), Permission.Action.WRITE); 179 180 assertEquals(4, 181 PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), TEST_TABLE).size()); 182 try { 183 assertEquals(4, 184 AccessControlClient.getUserPermissions(systemUserConnection, TEST_TABLE.toString()).size()); 185 } catch (AssertionError e) { 186 fail(e.getMessage()); 187 } catch (Throwable e) { 188 LOG.error("error during call of AccessControlClient.getUserPermissions. ", e); 189 } 190 } 191 192 private static void cleanUp() throws Exception { 193 // Clean the _acl_ table 194 try { 195 deleteTable(TEST_UTIL, TEST_TABLE); 196 } catch (TableNotFoundException ex) { 197 // Test deleted the table, no problem 198 LOG.info("Test deleted table " + TEST_TABLE); 199 } 200 // Verify all table/namespace permissions are erased 201 assertEquals(0, 202 PermissionStorage.getTablePermissions(TEST_UTIL.getConnection(), TEST_TABLE).size()); 203 assertEquals(0, 204 PermissionStorage 205 .getNamespacePermissions(TEST_UTIL.getConnection(), TEST_TABLE.getNamespaceAsString()) 206 .size()); 207 } 208 209 @AfterAll 210 public static void tearDownAfterClass() throws Exception { 211 cleanUp(); 212 TEST_UTIL.shutdownMiniCluster(); 213 } 214 215 @Test 216 public void testGetRSGroupInfo() throws Exception { 217 AccessTestAction action = () -> { 218 checkPermission("getRSGroupInfo"); 219 return null; 220 }; 221 222 validateAdminPermissions(action); 223 } 224 225 @Test 226 public void testGetRSGroupInfoOfTable() throws Exception { 227 AccessTestAction action = () -> { 228 checkPermission("getRSGroupInfoOfTable"); 229 return null; 230 }; 231 232 validateAdminPermissions(action); 233 } 234 235 @Test 236 public void testMoveServers() throws Exception { 237 AccessTestAction action = () -> { 238 checkPermission("moveServers"); 239 return null; 240 }; 241 242 validateAdminPermissions(action); 243 } 244 245 @Test 246 public void testMoveTables() throws Exception { 247 AccessTestAction action = () -> { 248 checkPermission("moveTables"); 249 return null; 250 }; 251 252 validateAdminPermissions(action); 253 } 254 255 @Test 256 public void testAddRSGroup() throws Exception { 257 AccessTestAction action = () -> { 258 checkPermission("addRSGroup"); 259 return null; 260 }; 261 262 validateAdminPermissions(action); 263 } 264 265 @Test 266 public void testRemoveRSGroup() throws Exception { 267 AccessTestAction action = () -> { 268 checkPermission("removeRSGroup"); 269 return null; 270 }; 271 272 validateAdminPermissions(action); 273 } 274 275 @Test 276 public void testBalanceRSGroup() throws Exception { 277 AccessTestAction action = () -> { 278 checkPermission("balanceRSGroup"); 279 return null; 280 }; 281 282 validateAdminPermissions(action); 283 } 284 285 @Test 286 public void testListRSGroup() throws Exception { 287 AccessTestAction action = () -> { 288 checkPermission("listRSGroup"); 289 return null; 290 }; 291 292 validateAdminPermissions(action); 293 } 294 295 @Test 296 public void testGetRSGroupInfoOfServer() throws Exception { 297 AccessTestAction action = () -> { 298 checkPermission("getRSGroupInfoOfServer"); 299 return null; 300 }; 301 302 validateAdminPermissions(action); 303 } 304 305 @Test 306 public void testMoveServersAndTables() throws Exception { 307 AccessTestAction action = () -> { 308 checkPermission("moveServersAndTables"); 309 return null; 310 }; 311 312 validateAdminPermissions(action); 313 } 314 315 @Test 316 public void testRemoveServers() throws Exception { 317 AccessTestAction action = () -> { 318 checkPermission("removeServers"); 319 return null; 320 }; 321 322 validateAdminPermissions(action); 323 } 324 325 @Test 326 public void testRenameRSGroup() throws Exception { 327 AccessTestAction action = () -> { 328 checkPermission("renameRSGroup"); 329 return null; 330 }; 331 332 validateAdminPermissions(action); 333 } 334 335 @Test 336 public void testUpdateRSGroupConfig() throws Exception { 337 AccessTestAction action = () -> { 338 checkPermission("updateRSGroupConfig"); 339 return null; 340 }; 341 342 validateAdminPermissions(action); 343 } 344 345 private void validateAdminPermissions(AccessTestAction action) throws Exception { 346 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN); 347 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, 348 USER_GROUP_WRITE, USER_GROUP_CREATE); 349 } 350}