[GitHub] [iotdb] JackieTien97 commented on a diff in pull request #10939: IOTDB. auth refine.

2023-08-30 Thread via GitHub


JackieTien97 commented on code in PR #10939:
URL: https://github.com/apache/iotdb/pull/10939#discussion_r1309802630


##
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java:
##
@@ -292,4 +376,9 @@ public void reset() {
   public String getDirPath() {
 return userDirPath;
   }
+
+  @Override
+  public void cleanUserFolder() {
+SystemFileFactory.INSTANCE.getFile(userDirPath).delete();

Review Comment:
   ```suggestion
   
FileUtils.cleanDirectory(SystemFileFactory.INSTANCE.getFile(userDirPath));
   ```



##
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/entity/Role.java:
##
@@ -36,62 +37,192 @@
 public class Role {
 
   private String name;
-  private List privilegeList;
+  private List pathPrivilegeList;
+
+  private Set sysPrivilegeSet;
+
+  private Set sysPriGrantOpt;
+
+  private static final int SYS_PRI_SIZE = PrivilegeType.getSysPriCount();
 
   public Role() {
 // empty constructor
   }
 
   public Role(String name) {
 this.name = name;
-this.privilegeList = new ArrayList<>();
+this.pathPrivilegeList = new ArrayList<>();
+this.sysPrivilegeSet = new HashSet<>();
+this.sysPriGrantOpt = new HashSet<>();
   }
 
+  /** - get func -* */
   public String getName() {
 return name;
   }
 
-  public void setName(String name) {
-this.name = name;
+  public List getPathPrivilegeList() {
+return pathPrivilegeList;
   }
 
-  public List getPrivilegeList() {
-return privilegeList;
+  public Set getSysPrivilege() {
+return sysPrivilegeSet;
   }
 
-  public void setPrivilegeList(List privilegeList) {
-this.privilegeList = privilegeList;
+  public Set getPathPrivileges(PartialPath path) throws AuthException 
{
+return AuthUtils.getPrivileges(path, pathPrivilegeList);
   }
 
-  public boolean hasPrivilege(PartialPath path, int privilegeId) {
-return AuthUtils.hasPrivilege(path, privilegeId, privilegeList);
+  public Set getSysPriGrantOpt() {
+return sysPriGrantOpt;
   }
 
-  public void addPrivilege(PartialPath path, int privilegeId) {
-AuthUtils.addPrivilege(path, privilegeId, privilegeList);
+  public int getAllSysPrivileges() {
+int privs = 0;
+for (Integer sysPri : sysPrivilegeSet) {
+  privs |= (0b1 << sysPriTopos(sysPri));
+}
+for (Integer sysPriGrantOpt : sysPriGrantOpt) {
+  privs |= 0b1 << (sysPriTopos(sysPriGrantOpt) + 16);

Review Comment:
   ```suggestion
 privs |= 1 << (sysPriTopos(sysPriGrantOpt) + 16);
   ```



##
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java:
##
@@ -317,20 +302,109 @@ public boolean checkUserPrivileges(String username, 
PartialPath path, int privil
   throw new AuthException(
   TSStatusCode.USER_NOT_EXIST, String.format(NO_SUCH_USER_EXCEPTION, 
username));
 }
-// get privileges of the user
-if (user.checkPrivilege(path, privilegeId)) {
-  return true;
-}
-// merge the privileges of the roles of the user
-for (String roleName : user.getRoleList()) {
-  Role role = roleManager.getRole(roleName);
-  if (role.checkPrivilege(path, privilegeId)) {
+if (path != null) {
+  // get privileges of the user
+  if (user.checkPathPrivilege(path, privilegeId)) {
+return true;
+  }
+  // merge the privileges of the roles of the user
+  for (String roleName : user.getRoleList()) {
+Role role = roleManager.getRole(roleName);
+if (role.checkPathPrivilege(path, privilegeId)) {
+  return true;
+}
+  }
+} else {
+  if (user.checkSysPrivilege(privilegeId)) {
 return true;
   }
+  for (String roleName : user.getRoleList()) {
+Role role = roleManager.getRole(roleName);
+if (role.checkSysPrivilege(privilegeId)) {
+  return true;
+}
+  }
 }
+
 return false;
   }
 
+  public boolean checkUserPrivilegeGrantOpt(String username, PartialPath path, 
int privilegeId)
+  throws AuthException {
+User user = userManager.getUser(username);
+if (user == null) {
+  throw new AuthException(
+  TSStatusCode.USER_NOT_EXIST, String.format(NO_SUCH_USER_EXCEPTION, 
username));
+}
+if (path == null) {
+  if (user.checkSysPrivilege(privilegeId)) {
+if (user.getSysPriGrantOpt().contains(privilegeId)) {
+  return true;
+}
+  }
+  if (user.getRoleList().isEmpty()) {
+throw new AuthException(
+TSStatusCode.NOT_HAS_PRIVILEGE,
+String.format(
+"Dont have privilege: %s to grant",
+PrivilegeType.values()[privilegeId].toString()));
+  }
+  for (String roleName : user.getRoleList()) {
+Role role = roleManager.getRole(roleName);
+if (role.checkSysPrivilege(privilegeId) && 

[GitHub] [iotdb] JackieTien97 commented on a diff in pull request #10939: IOTDB. auth refine.

2023-08-30 Thread via GitHub


JackieTien97 commented on code in PR #10939:
URL: https://github.com/apache/iotdb/pull/10939#discussion_r1307423753


##
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java:
##
@@ -33,6 +35,9 @@ public LocalFileUserManager(String userDirPath) throws 
AuthException {
 
   @Override
   public boolean processTakeSnapshot(File snapshotDir) throws TException, 
IOException {
+for (Map.Entry entry : userMap.entrySet()) {
+  accessor.saveUser(entry.getValue());
+}

Review Comment:
   you can put this call into snapshot take.



##
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ConfigManager.java:
##
@@ -992,6 +994,46 @@ public TPermissionInfoResp checkUserPrivileges(
 }
   }
 
+  public TAuthizedPatternTreeResp fetchAuthizedPatternTree(String username, 
int permission) {
+TSStatus status = confirmLeader();
+if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+  try {
+return permissionManager.fetchAuthizedPTree(username, permission);
+  } catch (AuthException e) {
+TAuthizedPatternTreeResp resp = new TAuthizedPatternTreeResp();
+status
+.setCode(TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode())
+.setMessage(e.getMessage());
+resp.setStatus(status);
+return resp;
+  }
+} else {
+  TAuthizedPatternTreeResp resp = new TAuthizedPatternTreeResp();
+  resp.setStatus(status);
+  return resp;
+}
+  }
+
+  public TPermissionInfoResp checkUserPrivilegeGrantOpt(
+  String username, PartialPath path, int permission) {
+TSStatus status = confirmLeader();
+TPermissionInfoResp resp = new TPermissionInfoResp();
+if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+  try {
+resp = permissionManager.checkUserPrivilegeGrantOpt(username, path, 
permission);
+  } catch (AuthException e) {
+status
+.setCode(TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode())

Review Comment:
   use the statusCode in AuthException



##
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/state/AuthOperationProcedureState.java:
##
@@ -0,0 +1,27 @@
+/*
+ * 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.confignode.procedure.state;
+
+public enum AuthOperationProcedureState {
+  INIT,
+  CONFIGNODE_AUTH_ALTER,

Review Comment:
   ```suggestion
   ```



-- 
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: reviews-unsubscr...@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org