• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9733

pending completion
#9733

push

travis_ci

web-flow
[To rel/1.2] Add compression and encoding type check for FastCompactionPerformer (#10712)

32 of 32 new or added lines in 4 files covered. (100.0%)

79232 of 165563 relevant lines covered (47.86%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.56
/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/BasicRoleManager.java
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
package org.apache.iotdb.commons.auth.role;
20

21
import org.apache.iotdb.commons.auth.AuthException;
22
import org.apache.iotdb.commons.auth.entity.Role;
23
import org.apache.iotdb.commons.concurrent.HashLock;
24
import org.apache.iotdb.commons.path.PartialPath;
25
import org.apache.iotdb.commons.utils.AuthUtils;
26
import org.apache.iotdb.rpc.TSStatusCode;
27

28
import java.io.IOException;
29
import java.util.HashMap;
30
import java.util.HashSet;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Map.Entry;
34
import java.util.Set;
35

36
/**
37
 * This class reads roles from local files through LocalFileRoleAccessor and manages them in a hash
38
 * map.
39
 */
40
public abstract class BasicRoleManager implements IRoleManager {
41

42
  protected Map<String, Role> roleMap;
43
  protected IRoleAccessor accessor;
44
  protected HashLock lock;
45

46
  BasicRoleManager(LocalFileRoleAccessor accessor) {
1✔
47
    this.roleMap = new HashMap<>();
1✔
48
    this.accessor = accessor;
1✔
49
    this.lock = new HashLock();
1✔
50
  }
1✔
51

52
  @Override
53
  public Role getRole(String rolename) throws AuthException {
54
    lock.readLock(rolename);
1✔
55
    Role role = roleMap.get(rolename);
1✔
56
    try {
57
      if (role == null) {
1✔
58
        role = accessor.loadRole(rolename);
1✔
59
        if (role != null) {
1✔
60
          roleMap.put(rolename, role);
×
61
        }
62
      }
63
    } catch (IOException e) {
×
64
      throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
65
    } finally {
66
      lock.readUnlock(rolename);
1✔
67
    }
68
    return role;
1✔
69
  }
70

71
  @Override
72
  public boolean createRole(String rolename) throws AuthException {
73
    AuthUtils.validateRolename(rolename);
1✔
74

75
    Role role = getRole(rolename);
1✔
76
    if (role != null) {
1✔
77
      return false;
1✔
78
    }
79
    lock.writeLock(rolename);
1✔
80
    try {
81
      role = new Role(rolename);
1✔
82
      accessor.saveRole(role);
1✔
83
      roleMap.put(rolename, role);
1✔
84
      return true;
1✔
85
    } catch (IOException e) {
×
86
      throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
87
    } finally {
88
      lock.writeUnlock(rolename);
1✔
89
    }
90
  }
91

92
  @Override
93
  public boolean deleteRole(String rolename) throws AuthException {
94
    lock.writeLock(rolename);
1✔
95
    try {
96
      if (accessor.deleteRole(rolename)) {
1✔
97
        roleMap.remove(rolename);
1✔
98
        return true;
1✔
99
      } else {
100
        return false;
1✔
101
      }
102
    } catch (IOException e) {
×
103
      throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
104
    } finally {
105
      lock.writeUnlock(rolename);
1✔
106
    }
107
  }
108

109
  @Override
110
  public boolean grantPrivilegeToRole(String rolename, PartialPath path, int privilegeId)
111
      throws AuthException {
112
    AuthUtils.validatePrivilegeOnPath(path, privilegeId);
1✔
113
    lock.writeLock(rolename);
1✔
114
    try {
115
      Role role = getRole(rolename);
1✔
116
      if (role == null) {
1✔
117
        throw new AuthException(
1✔
118
            TSStatusCode.ROLE_NOT_EXIST, String.format("No such role %s", rolename));
1✔
119
      }
120
      if (role.hasPrivilege(path, privilegeId)) {
1✔
121
        return false;
1✔
122
      }
123
      Set<Integer> privilegesCopy = new HashSet<>(role.getPrivileges(path));
1✔
124
      role.addPrivilege(path, privilegeId);
1✔
125
      try {
126
        accessor.saveRole(role);
1✔
127
      } catch (IOException e) {
×
128
        role.setPrivileges(path, privilegesCopy);
×
129
        throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
130
      }
1✔
131
      return true;
1✔
132
    } finally {
133
      lock.writeUnlock(rolename);
1✔
134
    }
135
  }
136

137
  @Override
138
  public boolean revokePrivilegeFromRole(String rolename, PartialPath path, int privilegeId)
139
      throws AuthException {
140
    AuthUtils.validatePrivilegeOnPath(path, privilegeId);
1✔
141
    lock.writeLock(rolename);
1✔
142
    try {
143
      Role role = getRole(rolename);
1✔
144
      if (role == null) {
1✔
145
        throw new AuthException(
1✔
146
            TSStatusCode.ROLE_NOT_EXIST, String.format("No such role %s", rolename));
1✔
147
      }
148
      if (!role.hasPrivilege(path, privilegeId)) {
1✔
149
        return false;
1✔
150
      }
151
      role.removePrivilege(path, privilegeId);
1✔
152
      try {
153
        accessor.saveRole(role);
1✔
154
      } catch (IOException e) {
×
155
        role.addPrivilege(path, privilegeId);
×
156
        throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
157
      }
1✔
158
      return true;
1✔
159
    } finally {
160
      lock.writeUnlock(rolename);
1✔
161
    }
162
  }
163

164
  @Override
165
  public void reset() {
166
    accessor.reset();
1✔
167
    roleMap.clear();
1✔
168
  }
1✔
169

170
  @Override
171
  public List<String> listAllRoles() {
172
    List<String> rtlist = accessor.listAllRoles();
1✔
173
    rtlist.sort(null);
1✔
174
    return rtlist;
1✔
175
  }
176

177
  @Override
178
  public void replaceAllRoles(Map<String, Role> roles) throws AuthException {
179
    synchronized (this) {
1✔
180
      reset();
1✔
181
      roleMap = roles;
1✔
182

183
      for (Entry<String, Role> entry : roleMap.entrySet()) {
1✔
184
        Role role = entry.getValue();
1✔
185
        try {
186
          accessor.saveRole(role);
1✔
187
        } catch (IOException e) {
×
188
          throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
×
189
        }
1✔
190
      }
1✔
191
    }
1✔
192
  }
1✔
193
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc