|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.persistence.auth; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TSStatus; |
| 23 | +import org.apache.iotdb.commons.auth.AuthException; |
| 24 | +import org.apache.iotdb.commons.auth.authorizer.BasicAuthorizer; |
| 25 | +import org.apache.iotdb.commons.auth.authorizer.IAuthorizer; |
| 26 | +import org.apache.iotdb.commons.auth.entity.ModelType; |
| 27 | +import org.apache.iotdb.commons.auth.entity.PrivilegeUnion; |
| 28 | +import org.apache.iotdb.commons.conf.CommonConfig; |
| 29 | +import org.apache.iotdb.commons.conf.CommonDescriptor; |
| 30 | +import org.apache.iotdb.commons.snapshot.SnapshotProcessor; |
| 31 | +import org.apache.iotdb.commons.utils.FileUtils; |
| 32 | +import org.apache.iotdb.commons.utils.TestOnly; |
| 33 | +import org.apache.iotdb.confignode.consensus.request.write.auth.AuthorPlan; |
| 34 | +import org.apache.iotdb.confignode.consensus.request.write.auth.AuthorRelationalPlan; |
| 35 | +import org.apache.iotdb.confignode.consensus.request.write.auth.AuthorTreePlan; |
| 36 | +import org.apache.iotdb.confignode.consensus.response.auth.PermissionInfoResp; |
| 37 | +import org.apache.iotdb.confignode.rpc.thrift.TAuthizedPatternTreeResp; |
| 38 | +import org.apache.iotdb.confignode.rpc.thrift.TPermissionInfoResp; |
| 39 | + |
| 40 | +import org.apache.thrift.TException; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | + |
| 44 | +import java.io.File; |
| 45 | +import java.io.IOException; |
| 46 | + |
| 47 | +public class AuthorInfo implements SnapshotProcessor { |
| 48 | + |
| 49 | + // Works at config node. |
| 50 | + private static final Logger LOGGER = LoggerFactory.getLogger(AuthorInfo.class); |
| 51 | + public static final CommonConfig COMMON_CONFIG = CommonDescriptor.getInstance().getConfig(); |
| 52 | + public static final String NO_USER_MSG = "No such user : "; |
| 53 | + |
| 54 | + private IAuthorizer authorizer; |
| 55 | + private volatile AuthorPlanExecutor authorPlanExecutor; |
| 56 | + |
| 57 | + public AuthorInfo() { |
| 58 | + try { |
| 59 | + authorizer = BasicAuthorizer.getInstance(); |
| 60 | + authorPlanExecutor = new AuthorPlanExecutor(authorizer); |
| 61 | + } catch (AuthException e) { |
| 62 | + LOGGER.error("get user or role permissionInfo failed because ", e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void setAuthorQueryPlanExecutor(AuthorPlanExecutor authorPlanExecutor) { |
| 67 | + this.authorPlanExecutor = authorPlanExecutor; |
| 68 | + } |
| 69 | + |
| 70 | + public TPermissionInfoResp login(String username, String password) { |
| 71 | + return authorPlanExecutor.login(username, password); |
| 72 | + } |
| 73 | + |
| 74 | + public String login4Pipe(final String username, final String password) { |
| 75 | + return authorPlanExecutor.login4Pipe(username, password); |
| 76 | + } |
| 77 | + |
| 78 | + public TPermissionInfoResp checkUserPrivileges(String username, PrivilegeUnion union) { |
| 79 | + return authorPlanExecutor.checkUserPrivileges(username, union); |
| 80 | + } |
| 81 | + |
| 82 | + public TSStatus authorNonQuery(AuthorPlan authorPlan) { |
| 83 | + if (authorPlan instanceof AuthorTreePlan) { |
| 84 | + return authorNonQuery((AuthorTreePlan) authorPlan); |
| 85 | + } else { |
| 86 | + return authorNonQuery((AuthorRelationalPlan) authorPlan); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public TSStatus authorNonQuery(AuthorTreePlan authorPlan) { |
| 91 | + return authorPlanExecutor.executeAuthorNonQuery(authorPlan); |
| 92 | + } |
| 93 | + |
| 94 | + public TSStatus authorNonQuery(AuthorRelationalPlan authorPlan) { |
| 95 | + return authorPlanExecutor.executeRelationalAuthorNonQuery(authorPlan); |
| 96 | + } |
| 97 | + |
| 98 | + public PermissionInfoResp executeListUsers(final AuthorPlan plan) throws AuthException { |
| 99 | + return authorPlanExecutor.executeListUsers(plan); |
| 100 | + } |
| 101 | + |
| 102 | + public PermissionInfoResp executeListRoles(final AuthorPlan plan) throws AuthException { |
| 103 | + return authorPlanExecutor.executeListRoles(plan); |
| 104 | + } |
| 105 | + |
| 106 | + public PermissionInfoResp executeListRolePrivileges(final AuthorPlan plan) throws AuthException { |
| 107 | + return authorPlanExecutor.executeListRolePrivileges(plan); |
| 108 | + } |
| 109 | + |
| 110 | + public PermissionInfoResp executeListUserPrivileges(final AuthorPlan plan) throws AuthException { |
| 111 | + return authorPlanExecutor.executeListUserPrivileges(plan); |
| 112 | + } |
| 113 | + |
| 114 | + public TAuthizedPatternTreeResp generateAuthorizedPTree(String username, int permission) |
| 115 | + throws AuthException { |
| 116 | + return authorPlanExecutor.generateAuthorizedPTree(username, permission); |
| 117 | + } |
| 118 | + |
| 119 | + public TPermissionInfoResp checkRoleOfUser(String username, String roleName) |
| 120 | + throws AuthException { |
| 121 | + return authorPlanExecutor.checkRoleOfUser(username, roleName); |
| 122 | + } |
| 123 | + |
| 124 | + public TPermissionInfoResp getUser(String username) throws AuthException { |
| 125 | + return authorPlanExecutor.getUser(username); |
| 126 | + } |
| 127 | + |
| 128 | + public String getUserName(long userId) throws AuthException { |
| 129 | + return authorPlanExecutor.getUserName(userId); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean processTakeSnapshot(File snapshotDir) throws TException, IOException { |
| 134 | + return authorizer.processTakeSnapshot(snapshotDir); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void processLoadSnapshot(File snapshotDir) throws TException, IOException { |
| 139 | + authorizer.processLoadSnapshot(snapshotDir); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Save the user's permission information,Bring back the DataNode for caching |
| 144 | + * |
| 145 | + * @param username The username of the user that needs to be cached |
| 146 | + */ |
| 147 | + public TPermissionInfoResp getUserPermissionInfo(String username, ModelType type) |
| 148 | + throws AuthException { |
| 149 | + return authorPlanExecutor.getUserPermissionInfo(username, type); |
| 150 | + } |
| 151 | + |
| 152 | + @TestOnly |
| 153 | + public void clear() throws AuthException { |
| 154 | + File userFolder = new File(COMMON_CONFIG.getUserFolder()); |
| 155 | + if (userFolder.exists()) { |
| 156 | + FileUtils.deleteFileOrDirectory(userFolder); |
| 157 | + } |
| 158 | + File roleFolder = new File(COMMON_CONFIG.getRoleFolder()); |
| 159 | + if (roleFolder.exists()) { |
| 160 | + FileUtils.deleteFileOrDirectory(roleFolder); |
| 161 | + } |
| 162 | + authorizer.reset(); |
| 163 | + } |
| 164 | +} |
0 commit comments