Caideyipi commented on code in PR #13158:
URL: https://github.com/apache/iotdb/pull/13158#discussion_r1904987413
##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/IAuthorizer.java:
##########
@@ -56,36 +58,27 @@ public interface IAuthorizer extends SnapshotProcessor {
*
* @param username the username of the user.
* @throws AuthException When attempting to delete the default administrator
or the user does not
- * exists.
+ * exist.
*/
void deleteUser(String username) throws AuthException;
/**
* Grant a privilege on a seriesPath to a user.
*
- * @param username The username of the user to which the privilege should be
added.
- * @param path The seriesPath on which the privilege takes effect. If the
privilege is a
- * seriesPath-free privilege, this should be "root".
- * @param privilegeId An integer that represents a privilege.
- * @param grantOpt Whether the privilege is grant option.
+ * @param userName The username of the user to which the privilege should be
added.
Review Comment:
Better add a little description fro the "union" field
##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/parser/StatementGeneratorTest.java:
##########
@@ -756,11 +757,12 @@ public void testNormalGrantRevoke() {
// 3. check simple privilege revoke from user/role on simple path
for (PrivilegeType type : PrivilegeType.values()) {
+ if (type.isRelationalPrivilege()) continue;
Review Comment:
Better place the "continue" in a separate line?
##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/UserTest.java:
##########
@@ -35,17 +36,18 @@ public void testUser() throws IllegalPathException {
User user = new User("user", "password");
PathPrivilege pathPrivilege = new PathPrivilege(new
PartialPath("root.ln"));
user.setPrivilegeList(Collections.singletonList(pathPrivilege));
- user.setPathPrivileges(new PartialPath("root.ln"),
Collections.singleton(1));
+ user.setPathPrivileges(
+ new PartialPath("root.ln"),
Collections.singleton(PrivilegeType.WRITE_DATA));
Assert.assertEquals(
- "User{name='user', password='password', pathPrivilegeList=[root.ln :
WRITE_DATA], sysPrivilegeSet=[], roleList=[], "
- + "isOpenIdUser=false, useWaterMark=false}",
+ "User{name='user', password='password', pathPrivilegeList=[root.ln :
WRITE_DATA], "
+ + "sysPrivilegeSet=[], AnyScopePrivilegeMap=[],
objectPrivilegeMap={}, roleList=[], isOpenIdUser=false}",
user.toString());
User user1 = new User("user1", "password1");
user1.deserialize(user.serialize());
Assert.assertEquals(
- "User{name='user', password='password', pathPrivilegeList=[root.ln :
WRITE_DATA], sysPrivilegeSet=[], roleList=[], "
- + "isOpenIdUser=false, useWaterMark=false}",
+ "User{name='user', password='password', pathPrivilegeList=[root.ln :
WRITE_DATA], "
+ + "sysPrivilegeSet=[], AnyScopePrivilegeMap=[],
objectPrivilegeMap={}, roleList=[], isOpenIdUser=false}",
user1.toString());
- Assert.assertTrue(user1.equals(user));
+ Assert.assertEquals(user1, user);
Review Comment:
Better swap the sequence
##########
integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBClusterAuthorityRelationalIT.java:
##########
@@ -0,0 +1,480 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.it.auth;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.auth.entity.PrivilegeModelType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeUnion;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
+import org.apache.iotdb.commons.utils.AuthUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerRelationalReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerResp;
+import org.apache.iotdb.confignode.rpc.thrift.TCheckUserPrivilegesReq;
+import org.apache.iotdb.confignode.rpc.thrift.TLoginReq;
+import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp;
+import org.apache.iotdb.confignode.rpc.thrift.TRoleResp;
+import org.apache.iotdb.confignode.rpc.thrift.TUserResp;
+import org.apache.iotdb.db.queryengine.plan.relational.type.AuthorRType;
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({ClusterIT.class})
+public class IoTDBClusterAuthorityRelationalIT {
+ @Before
+ public void setUp() throws Exception {
+ // Init 1C0D environment
+ EnvFactory.getEnv().initClusterEnvironment(1, 1);
+ }
+
+ @After
+ public void tearDown() {
+ EnvFactory.getEnv().cleanClusterEnvironment();
+ }
+
+ private void runAndCheck(SyncConfigNodeIServiceClient client,
TAuthorizerRelationalReq req)
+ throws TException {
+ TSStatus status;
+ status = client.operateRPermission(req);
+ assertEquals(status.getCode(),
TSStatusCode.SUCCESS_STATUS.getStatusCode());
+ }
+
+ private void cleanUserAndRole(SyncConfigNodeIServiceClient client) throws
TException {
Review Comment:
Only "user" is cleared?
##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/TablePrivilegeTest.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.auth.entity;
+
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+import org.apache.iotdb.commons.auth.entity.TablePrivilege;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class TablePrivilegeTest {
+ @Test
+ public void testTablePrivilege_Init() {
Review Comment:
This may not reflect the real change of table privilege, because the
"privilege" and "grant option" seems indepentent here.
##########
integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBClusterAuthorityRelationalIT.java:
##########
@@ -0,0 +1,480 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.it.auth;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.auth.entity.PrivilegeModelType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeUnion;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
+import org.apache.iotdb.commons.utils.AuthUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerRelationalReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerResp;
+import org.apache.iotdb.confignode.rpc.thrift.TCheckUserPrivilegesReq;
+import org.apache.iotdb.confignode.rpc.thrift.TLoginReq;
+import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp;
+import org.apache.iotdb.confignode.rpc.thrift.TRoleResp;
+import org.apache.iotdb.confignode.rpc.thrift.TUserResp;
+import org.apache.iotdb.db.queryengine.plan.relational.type.AuthorRType;
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({ClusterIT.class})
+public class IoTDBClusterAuthorityRelationalIT {
+ @Before
+ public void setUp() throws Exception {
+ // Init 1C0D environment
Review Comment:
1C1D?
##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/role/LocalFileRoleAccessorTest.java:
##########
@@ -69,81 +67,56 @@ public void test() throws IOException, IllegalPathException
{
roles[i] = new Role("role" + i);
for (int j = 0; j <= i; j++) {
PathPrivilege pathPrivilege = new PathPrivilege(new
PartialPath("root.a.b.c" + j));
- pathPrivilege.getPrivileges().add(j);
+ pathPrivilege.grantPrivilege(PrivilegeType.values()[j], true);
roles[i].getPathPrivilegeList().add(pathPrivilege);
- roles[i].getSysPrivilege().add(i + 4);
+ roles[i].grantSysPrivilege(PrivilegeType.values()[i + 4], false);
+ roles[i].grantDBPrivilege("testdb", PrivilegeType.CREATE, false);
Review Comment:
Seemingly the "CREATE" is granted twice
##########
integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBRelationalAuthIT.java:
##########
@@ -0,0 +1,306 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.it.auth;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.itbase.category.LocalStandaloneIT;
+import org.apache.iotdb.itbase.env.BaseEnv;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({LocalStandaloneIT.class, ClusterIT.class})
Review Comment:
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
##########
integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBClusterAuthorityRelationalIT.java:
##########
@@ -0,0 +1,480 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.it.auth;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.auth.entity.PrivilegeModelType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeUnion;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
+import org.apache.iotdb.commons.utils.AuthUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerRelationalReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerResp;
+import org.apache.iotdb.confignode.rpc.thrift.TCheckUserPrivilegesReq;
+import org.apache.iotdb.confignode.rpc.thrift.TLoginReq;
+import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp;
+import org.apache.iotdb.confignode.rpc.thrift.TRoleResp;
+import org.apache.iotdb.confignode.rpc.thrift.TUserResp;
+import org.apache.iotdb.db.queryengine.plan.relational.type.AuthorRType;
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({ClusterIT.class})
Review Comment:
@category({TableClusterIT.class})
##########
integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBClusterAuthorityRelationalIT.java:
##########
@@ -0,0 +1,480 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.it.auth;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.auth.entity.PrivilegeModelType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+import org.apache.iotdb.commons.auth.entity.PrivilegeUnion;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
+import org.apache.iotdb.commons.utils.AuthUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerRelationalReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerReq;
+import org.apache.iotdb.confignode.rpc.thrift.TAuthorizerResp;
+import org.apache.iotdb.confignode.rpc.thrift.TCheckUserPrivilegesReq;
+import org.apache.iotdb.confignode.rpc.thrift.TLoginReq;
+import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp;
+import org.apache.iotdb.confignode.rpc.thrift.TRoleResp;
+import org.apache.iotdb.confignode.rpc.thrift.TUserResp;
+import org.apache.iotdb.db.queryengine.plan.relational.type.AuthorRType;
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.ClusterIT;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({ClusterIT.class})
+public class IoTDBClusterAuthorityRelationalIT {
+ @Before
+ public void setUp() throws Exception {
+ // Init 1C0D environment
+ EnvFactory.getEnv().initClusterEnvironment(1, 1);
+ }
+
+ @After
+ public void tearDown() {
+ EnvFactory.getEnv().cleanClusterEnvironment();
+ }
+
+ private void runAndCheck(SyncConfigNodeIServiceClient client,
TAuthorizerRelationalReq req)
+ throws TException {
+ TSStatus status;
+ status = client.operateRPermission(req);
+ assertEquals(status.getCode(),
TSStatusCode.SUCCESS_STATUS.getStatusCode());
+ }
+
+ private void cleanUserAndRole(SyncConfigNodeIServiceClient client) throws
TException {
+ TSStatus status;
+
+ // clean user
+ TAuthorizerRelationalReq authorizerReq =
+ new TAuthorizerRelationalReq(
+ AuthorRType.LIST_USER.ordinal(), "", "", "", "", "", -1, false);
+
+ TAuthorizerResp authorizerResp = client.queryRPermission(authorizerReq);
+ status = authorizerResp.getStatus();
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
status.getCode());
+
+ List<String> allUsers = authorizerResp.getMemberInfo();
+ for (String user : allUsers) {
+ if (!user.equals("root")) {
+ authorizerReq =
+ new TAuthorizerRelationalReq(
+ AuthorRType.DROP_USER.ordinal(), user, "", "", "", "", -1,
false);
+ status = client.operateRPermission(authorizerReq);
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
status.getCode());
+ }
+ }
+ }
+
+ private void createUserORRoleAndCheck(
+ SyncConfigNodeIServiceClient client, String name, boolean isUser, String
password)
+ throws TException {
+ TSStatus status;
+ TAuthorizerRelationalReq authorizerReq =
+ new TAuthorizerRelationalReq(
+ isUser ? AuthorRType.CREATE_USER.ordinal() :
AuthorRType.CREATE_ROLE.ordinal(),
+ isUser ? name : "",
+ isUser ? "" : name,
+ password,
+ "",
+ "",
+ -1,
+ false);
+ status = client.operateRPermission(authorizerReq);
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
status.getCode());
+ authorizerReq =
+ new TAuthorizerRelationalReq(
+ isUser ? AuthorRType.LIST_USER.ordinal() :
AuthorRType.LIST_ROLE.ordinal(),
+ "",
+ "",
+ "",
+ "",
+ "",
+ -1,
+ false);
+ TAuthorizerResp resp = client.queryRPermission(authorizerReq);
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
resp.getStatus().getCode());
+ assertTrue(resp.getMemberInfo().contains(name));
+ }
+
+ private void grantSysPrivilegeAndCheck(
+ SyncConfigNodeIServiceClient client,
+ String userName,
+ String roleName,
+ boolean toUser,
+ PrivilegeType sysPriv,
+ boolean grantOpt)
+ throws TException {
+ TSStatus status;
+ TAuthorizerRelationalReq authorizerRelationalReq =
+ new TAuthorizerRelationalReq(
+ toUser ? AuthorRType.GRANT_USER_SYS.ordinal() :
AuthorRType.GRANT_ROLE_SYS.ordinal(),
+ userName,
+ roleName,
+ "",
+ "",
+ "",
+ sysPriv.ordinal(),
+ grantOpt);
+
+ status = client.operateRPermission(authorizerRelationalReq);
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
status.getCode());
+ TCheckUserPrivilegesReq checkUserPrivilegesReq =
+ new TCheckUserPrivilegesReq(
+ userName, PrivilegeModelType.SYSTEM.ordinal(), sysPriv.ordinal(),
grantOpt);
+ TPermissionInfoResp resp =
client.checkUserPrivileges(checkUserPrivilegesReq);
+ assertEquals(resp.status.getCode(),
TSStatusCode.SUCCESS_STATUS.getStatusCode());
+ assertTrue(
+ toUser
+ ?
resp.getUserInfo().getPermissionInfo().getSysPriSet().contains(sysPriv.ordinal())
+ : resp.getRoleInfo().containsKey(roleName)
+ &&
resp.getRoleInfo().get(roleName).getSysPriSet().contains(sysPriv.ordinal()));
+ if (grantOpt) {
+ assertTrue(
+ toUser
+ ? resp.getUserInfo()
+ .getPermissionInfo()
+ .getSysPriSetGrantOpt()
+ .contains(sysPriv.ordinal())
+ : resp.getRoleInfo().containsKey(roleName)
+ && resp.getRoleInfo()
+ .get(roleName)
+ .getSysPriSetGrantOpt()
+ .contains(sysPriv.ordinal()));
+ }
+ }
+
+ private void grantPrivilegeAndCheck(
+ SyncConfigNodeIServiceClient client,
+ String username,
+ String rolename,
+ boolean toUser,
+ PrivilegeUnion union)
+ throws TException {
+ TSStatus status;
+ AuthorRType type;
+ if (union.isForAny()) {
+ type = toUser ? AuthorRType.GRANT_USER_ANY : AuthorRType.GRANT_ROLE_ANY;
+ } else if (union.getTbName() == null) {
+ type = toUser ? AuthorRType.GRANT_USER_DB : AuthorRType.GRANT_ROLE_DB;
+ } else {
+ type = toUser ? AuthorRType.GRANT_USER_TB : AuthorRType.GRANT_ROLE_TB;
+ }
+ TAuthorizerRelationalReq authorizerRelationalReq =
+ new TAuthorizerRelationalReq(
+ type.ordinal(),
+ toUser ? username : "",
+ toUser ? "" : rolename,
+ "",
+ union.getDBName() == null ? "" : union.getDBName(),
+ union.getTbName() == null ? "" : union.getTbName(),
+ union.getPrivilegeType().ordinal(),
+ union.isGrantOption());
+ status = client.operateRPermission(authorizerRelationalReq);
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
status.getCode());
+ int reqtype = -1;
+ if (union.getPrivilegeType().isRelationalPrivilege()) {
+ reqtype = PrivilegeModelType.RELATIONAL.ordinal();
+ } else if (union.getPrivilegeType().isSystemPrivilege()) {
+ reqtype = PrivilegeModelType.SYSTEM.ordinal();
+ }
+ TCheckUserPrivilegesReq checkUserPrivilegesReq =
+ new TCheckUserPrivilegesReq(
+ username, reqtype, union.getPrivilegeType().ordinal(),
union.isGrantOption());
+ if (union.getDBName() != null) {
+ checkUserPrivilegesReq.setDatabase(union.getDBName());
+ }
+ if (union.getTbName() != null) {
+ checkUserPrivilegesReq.setTable(union.getTbName());
+ }
+
+ TPermissionInfoResp resp =
client.checkUserPrivileges(checkUserPrivilegesReq);
+ assertEquals(resp.status.getCode(),
TSStatusCode.SUCCESS_STATUS.getStatusCode());
+ if (toUser) {
+ TUserResp userInfo = resp.getUserInfo();
+ if (union.isForAny()) {
+ assertTrue(
+ userInfo
+ .getPermissionInfo()
+ .getAnyScopeGrantSet()
+ .contains(union.getPrivilegeType().ordinal()));
+ } else if (union.getTbName() == null) {
+
assertTrue(userInfo.getPermissionInfo().getDbPrivilegeMap().containsKey(union.getDBName()));
+ assertTrue(
+ userInfo
+ .getPermissionInfo()
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getPrivileges()
+ .contains(union.getPrivilegeType().ordinal()));
+ } else {
+
assertTrue(userInfo.getPermissionInfo().getDbPrivilegeMap().containsKey(union.getDBName()));
+ assertTrue(
+ userInfo
+ .getPermissionInfo()
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getTablePrivilegeMap()
+ .containsKey(union.getTbName()));
+ assertTrue(
+ userInfo
+ .getPermissionInfo()
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getTablePrivilegeMap()
+ .get(union.getTbName())
+ .getPrivileges()
+ .contains(union.getPrivilegeType().ordinal()));
+ }
+ } else {
+ assertTrue(resp.getRoleInfo().containsKey(rolename));
+ TRoleResp roleResp = resp.getRoleInfo().get(rolename);
+ if (union.isForAny()) {
+
assertTrue(roleResp.getAnyScopeGrantSet().contains(union.getPrivilegeType().ordinal()));
+ }
+ if (union.getTbName() == null) {
+
assertTrue(roleResp.getDbPrivilegeMap().containsKey(union.getDBName()));
+ assertTrue(
+ roleResp
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getPrivileges()
+ .contains(union.getPrivilegeType().ordinal()));
+ } else {
+
assertTrue(roleResp.getDbPrivilegeMap().containsKey(union.getDBName()));
+ assertTrue(
+ roleResp
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getTablePrivilegeMap()
+ .containsKey(union.getTbName()));
+ assertTrue(
+ roleResp
+ .getDbPrivilegeMap()
+ .get(union.getDBName())
+ .getTablePrivilegeMap()
+ .get(union.getTbName())
+ .getPrivileges()
+ .contains(union.getPrivilegeType().ordinal()));
+ }
+ }
+ }
+
+ private void expectSuccess(TPermissionInfoResp resp) {
+ assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(),
resp.getStatus().getCode());
+ }
+
+ private void expectFailed(TPermissionInfoResp resp) {
+ assertEquals(TSStatusCode.NO_PERMISSION.getStatusCode(),
resp.getStatus().getCode());
+ }
+
+ @Test
+ public void permissionTest()
+ throws TException, ClientManagerException, IOException,
InterruptedException {
+ try (SyncConfigNodeIServiceClient client =
+ (SyncConfigNodeIServiceClient)
EnvFactory.getEnv().getLeaderConfigNodeConnection()) {
+ cleanUserAndRole(client);
+ createUserORRoleAndCheck(client, "user1", true, "password");
+ createUserORRoleAndCheck(client, "role1", false, "");
+ runAndCheck(
+ client,
+ new TAuthorizerRelationalReq(
+ AuthorRType.GRANT_USER_ROLE.ordinal(), "user1", "role1", "", "",
"", -1, false));
+ grantSysPrivilegeAndCheck(client, "user1", "role1", true,
PrivilegeType.MANAGE_USER, false);
+ grantSysPrivilegeAndCheck(client, "user1", "role1", true,
PrivilegeType.MANAGE_ROLE, true);
+ grantSysPrivilegeAndCheck(client, "user1", "role1", false,
PrivilegeType.MAINTAIN, true);
Review Comment:
Better only specify one of user and role to make it clearer…… And in fact we
shall only pass one "name" into the parameter
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]