2 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional information regarding
4 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9 * for the specific language governing permissions and limitations under the License.
12 package org
.apache
.hadoop
.hbase
.client
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertFalse
;
16 import static org
.junit
.Assert
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
19 import java
.util
.List
;
20 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
21 import org
.apache
.hadoop
.hbase
.TableName
;
22 import org
.apache
.hadoop
.hbase
.security
.User
;
23 import org
.apache
.hadoop
.hbase
.security
.access
.GetUserPermissionsRequest
;
24 import org
.apache
.hadoop
.hbase
.security
.access
.Permission
;
25 import org
.apache
.hadoop
.hbase
.security
.access
.PermissionStorage
;
26 import org
.apache
.hadoop
.hbase
.security
.access
.SecureTestUtil
;
27 import org
.apache
.hadoop
.hbase
.security
.access
.SecureTestUtil
.AccessTestAction
;
28 import org
.apache
.hadoop
.hbase
.security
.access
.UserPermission
;
29 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
31 import org
.junit
.BeforeClass
;
32 import org
.junit
.ClassRule
;
33 import org
.junit
.Test
;
34 import org
.junit
.experimental
.categories
.Category
;
35 import org
.junit
.runner
.RunWith
;
36 import org
.junit
.runners
.Parameterized
;
37 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Lists
;
39 @RunWith(Parameterized
.class)
40 @Category({ ClientTests
.class, SmallTests
.class })
41 public class TestAsyncAccessControlAdminApi
extends TestAsyncAdminBase
{
44 public static final HBaseClassTestRule CLASS_RULE
=
45 HBaseClassTestRule
.forClass(TestAsyncAccessControlAdminApi
.class);
48 public static void setUpBeforeClass() throws Exception
{
49 SecureTestUtil
.enableSecurity(TEST_UTIL
.getConfiguration());
50 TEST_UTIL
.startMiniCluster(1);
51 TEST_UTIL
.waitTableAvailable(PermissionStorage
.ACL_TABLE_NAME
);
52 ASYNC_CONN
= ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get();
56 public void test() throws Exception
{
57 TableName tableName
= TableName
.valueOf("test-table");
58 String userName1
= "user1";
59 String userName2
= "user2";
60 User user2
= User
.createUserForTesting(TEST_UTIL
.getConfiguration(), userName2
, new String
[0]);
61 Permission permission
=
62 Permission
.newBuilder(tableName
).withActions(Permission
.Action
.READ
).build();
63 UserPermission userPermission
= new UserPermission(userName1
, permission
);
65 // grant user1 table permission
66 admin
.grant(userPermission
, false).get();
68 // get table permissions
69 List
<UserPermission
> userPermissions
=
70 admin
.getUserPermissions(GetUserPermissionsRequest
.newBuilder(tableName
).build()).get();
71 assertEquals(1, userPermissions
.size());
72 assertEquals(userPermission
, userPermissions
.get(0));
74 // get table permissions
78 GetUserPermissionsRequest
.newBuilder(tableName
).withUserName(userName1
).build())
80 assertEquals(1, userPermissions
.size());
81 assertEquals(userPermission
, userPermissions
.get(0));
86 GetUserPermissionsRequest
.newBuilder(tableName
).withUserName(userName2
).build())
88 assertEquals(0, userPermissions
.size());
90 // has user permission
91 List
<Permission
> permissions
= Lists
.newArrayList(permission
);
92 boolean hasPermission
=
93 admin
.hasUserPermissions(userName1
, permissions
).get().get(0).booleanValue();
94 assertTrue(hasPermission
);
95 hasPermission
= admin
.hasUserPermissions(userName2
, permissions
).get().get(0).booleanValue();
96 assertFalse(hasPermission
);
98 AccessTestAction hasPermissionAction
= new AccessTestAction() {
100 public Object
run() throws Exception
{
101 try (AsyncConnection conn
=
102 ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get()) {
103 return conn
.getAdmin().hasUserPermissions(userName1
, permissions
).get().get(0);
108 user2
.runAs(hasPermissionAction
);
109 fail("Should not come here");
110 } catch (Exception e
) {
111 LOG
.error("Call has permission error", e
);
115 admin
.hasUserPermissions(permissions
);
116 AccessTestAction checkPermissionsAction
= new AccessTestAction() {
118 public Object
run() throws Exception
{
119 try (AsyncConnection conn
=
120 ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get()) {
121 return conn
.getAdmin().hasUserPermissions(permissions
).get().get(0);
125 assertFalse((Boolean
) user2
.runAs(checkPermissionsAction
));