Skip to content

Commit 1717aea

Browse files
wenyanshi-123shiwenyan
authored andcommitted
Connection limit function. (#16462)
1 parent 3cc6ee1 commit 1717aea

File tree

10 files changed

+182
-21
lines changed

10 files changed

+182
-21
lines changed

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public enum ConfigPhysicalPlanType {
131131
@Deprecated
132132
ListRoleUsers((short) 637),
133133
CreateUserWithRawPassword((short) 638),
134+
UpdateUserMaxSession((short) 639),
135+
UpdateUserMinSession((short) 640),
134136

135137
/** Table Author */
136138
RCreateUser((short) 641),
@@ -164,6 +166,8 @@ public enum ConfigPhysicalPlanType {
164166
RListRole((short) 670),
165167
RListUserPrivilege((short) 671),
166168
RListRolePrivilege((short) 672),
169+
RUpdateUserMaxSession((short) 673),
170+
RUpdateUserMinSession((short) 674),
167171

168172
/** Function. */
169173
CreateFunction((short) 700),

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/auth/AuthorPlan.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public abstract class AuthorPlan extends ConfigPhysicalReadPlan {
3131
protected String newPassword;
3232
protected String userName;
3333
protected boolean grantOpt;
34+
protected int maxSessionPerUser;
35+
protected int minSessionPerUser;
3436

3537
public AuthorPlan(final ConfigPhysicalPlanType type) {
3638
super(type);
@@ -44,13 +46,17 @@ public AuthorPlan(
4446
String roleName,
4547
String password,
4648
String newPassword,
47-
boolean grantOpt) {
49+
boolean grantOpt,
50+
int MaxSessionPerUser,
51+
int MinSessionPerUser) {
4852
super(type);
4953
this.userName = userName;
5054
this.roleName = roleName;
5155
this.password = password;
5256
this.newPassword = newPassword;
5357
this.grantOpt = grantOpt;
58+
this.maxSessionPerUser = MaxSessionPerUser;
59+
this.minSessionPerUser = MinSessionPerUser;
5460
}
5561

5662
public ConfigPhysicalPlanType getAuthorType() {
@@ -69,6 +75,22 @@ public String getPassword() {
6975
return password;
7076
}
7177

78+
public int getMaxSessionPerUser() {
79+
return maxSessionPerUser;
80+
}
81+
82+
public void setMaxSessionPerUser(final int maxSessionPerUser) {
83+
this.maxSessionPerUser = maxSessionPerUser;
84+
}
85+
86+
public int getMinSessionPerUser() {
87+
return minSessionPerUser;
88+
}
89+
90+
public void setMinSessionPerUser(final int minSessionPerUser) {
91+
this.maxSessionPerUser = minSessionPerUser;
92+
}
93+
7294
public void setPassword(final String password) {
7395
this.password = password;
7496
}

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/auth/AuthorRelationalPlan.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ public AuthorRelationalPlan(final ConfigPhysicalPlanType authorType) {
4141
super(authorType);
4242
}
4343

44+
public AuthorRelationalPlan(
45+
final ConfigPhysicalPlanType authorType,
46+
final String userName,
47+
final String roleName,
48+
final String databaseName,
49+
final String tableName,
50+
final Set<Integer> permissions,
51+
final boolean grantOpt,
52+
final String password,
53+
final int maxSessionPerUser,
54+
final int minSessionPerUser) {
55+
super(
56+
authorType,
57+
userName,
58+
roleName,
59+
password,
60+
"",
61+
grantOpt,
62+
maxSessionPerUser,
63+
minSessionPerUser);
64+
65+
this.databaseName = databaseName;
66+
this.tableName = tableName;
67+
this.permissions = permissions;
68+
}
69+
4470
public AuthorRelationalPlan(
4571
final ConfigPhysicalPlanType authorType,
4672
final String userName,
@@ -50,7 +76,8 @@ public AuthorRelationalPlan(
5076
final Set<Integer> permissions,
5177
final boolean grantOpt,
5278
final String password) {
53-
super(authorType, userName, roleName, password, "", grantOpt);
79+
super(authorType, userName, roleName, password, "", grantOpt, -1, -1);
80+
5481
this.databaseName = databaseName;
5582
this.tableName = tableName;
5683
this.permissions = permissions;
@@ -64,7 +91,7 @@ public AuthorRelationalPlan(
6491
final String tableName,
6592
final int permission,
6693
final boolean grantOpt) {
67-
super(authorType, userName, roleName, "", "", grantOpt);
94+
super(authorType, userName, roleName, "", "", grantOpt, -1, -1);
6895
this.databaseName = databaseName;
6996
this.tableName = tableName;
7097
this.permissions = Collections.singleton(permission);
@@ -140,6 +167,11 @@ protected void serializeImpl(DataOutputStream stream) throws IOException {
140167
BasicStructureSerDeUtil.write(userName, stream);
141168
BasicStructureSerDeUtil.write(roleName, stream);
142169
BasicStructureSerDeUtil.write(password, stream);
170+
if (getAuthorType() == ConfigPhysicalPlanType.UpdateUserMaxSession
171+
|| getAuthorType() == ConfigPhysicalPlanType.UpdateUserMinSession) {
172+
BasicStructureSerDeUtil.write(maxSessionPerUser, stream);
173+
BasicStructureSerDeUtil.write(minSessionPerUser, stream);
174+
}
143175
BasicStructureSerDeUtil.write(databaseName, stream);
144176
BasicStructureSerDeUtil.write(tableName, stream);
145177
stream.writeInt(permissions.size());
@@ -155,6 +187,11 @@ protected void deserializeImpl(ByteBuffer buffer) {
155187
userName = BasicStructureSerDeUtil.readString(buffer);
156188
roleName = BasicStructureSerDeUtil.readString(buffer);
157189
password = BasicStructureSerDeUtil.readString(buffer);
190+
if (getAuthorType() == ConfigPhysicalPlanType.UpdateUserMaxSession
191+
|| getAuthorType() == ConfigPhysicalPlanType.UpdateUserMinSession) {
192+
maxSessionPerUser = buffer.getInt();
193+
minSessionPerUser = buffer.getInt();
194+
}
158195
databaseName = BasicStructureSerDeUtil.readString(buffer);
159196
tableName = BasicStructureSerDeUtil.readString(buffer);
160197
permissions = new HashSet<>();

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/auth/AuthorTreePlan.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ public AuthorTreePlan(final ConfigPhysicalPlanType type) {
4343
super(type);
4444
}
4545

46+
/**
47+
* {@link AuthorTreePlan} Constructor.
48+
*
49+
* @param authorType author type
50+
* @param userName user name
51+
* @param roleName role name
52+
* @param password password
53+
* @param permissions permissions
54+
* @param grantOpt with grant option, only grant statement can set grantOpt = true
55+
* @param maxSessioPerUser maxSessionPerUser of user
56+
* @param minSessionPerUser minSessionPerUser of user
57+
*/
58+
public AuthorTreePlan(
59+
final ConfigPhysicalPlanType authorType,
60+
final String userName,
61+
final String roleName,
62+
final String password,
63+
final String newPassword,
64+
final Set<Integer> permissions,
65+
final boolean grantOpt,
66+
final List<PartialPath> nodeNameList,
67+
final Integer maxSessioPerUser,
68+
final Integer minSessionPerUser) {
69+
super(
70+
authorType,
71+
userName,
72+
roleName,
73+
password,
74+
newPassword,
75+
grantOpt,
76+
maxSessioPerUser,
77+
minSessionPerUser);
78+
this.permissions = permissions;
79+
this.nodeNameList = nodeNameList;
80+
}
81+
4682
/**
4783
* {@link AuthorTreePlan} Constructor.
4884
*
@@ -63,7 +99,7 @@ public AuthorTreePlan(
6399
final Set<Integer> permissions,
64100
final boolean grantOpt,
65101
final List<PartialPath> nodeNameList) {
66-
super(authorType, userName, roleName, password, newPassword, grantOpt);
102+
super(authorType, userName, roleName, password, newPassword, grantOpt, -1, -1);
67103
this.permissions = permissions;
68104
this.nodeNameList = nodeNameList;
69105
}
@@ -128,6 +164,11 @@ protected void serializeImpl(DataOutputStream stream) throws IOException {
128164
BasicStructureSerDeUtil.write(roleName, stream);
129165
BasicStructureSerDeUtil.write(password, stream);
130166
BasicStructureSerDeUtil.write(newPassword, stream);
167+
if (getAuthorType() == ConfigPhysicalPlanType.UpdateUserMaxSession
168+
|| getAuthorType() == ConfigPhysicalPlanType.UpdateUserMinSession) {
169+
BasicStructureSerDeUtil.write(maxSessionPerUser, stream);
170+
BasicStructureSerDeUtil.write(minSessionPerUser, stream);
171+
}
131172
if (permissions == null) {
132173
stream.write((byte) 0);
133174
} else {
@@ -150,6 +191,11 @@ protected void deserializeImpl(ByteBuffer buffer) {
150191
roleName = BasicStructureSerDeUtil.readString(buffer);
151192
password = BasicStructureSerDeUtil.readString(buffer);
152193
newPassword = BasicStructureSerDeUtil.readString(buffer);
194+
if (getAuthorType() == ConfigPhysicalPlanType.UpdateUserMaxSession
195+
|| getAuthorType() == ConfigPhysicalPlanType.UpdateUserMinSession) {
196+
maxSessionPerUser = buffer.getInt();
197+
minSessionPerUser = buffer.getInt();
198+
}
153199
if (buffer.get() == (byte) 0) {
154200
this.permissions = null;
155201
} else {

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import java.io.IOException;
6565
import java.nio.ByteBuffer;
6666
import java.util.ArrayList;
67-
import java.util.Collections;
6867
import java.util.HashMap;
6968
import java.util.HashSet;
7069
import java.util.Iterator;
@@ -512,7 +511,8 @@ public PermissionInfoResp executeListUsers(final AuthorPlan plan) throws AuthExc
512511
userList = new ArrayList<>(1);
513512
userList.add(plan.getUserName());
514513
User user = authorizer.getUser(plan.getUserName());
515-
userInfoList = Collections.singletonList(user.convertToListUserInfo());
514+
userInfoList = new ArrayList<>(1);
515+
userInfoList.add(user.convertToListUserInfo());
516516
} else {
517517
userList = authorizer.listAllUsers();
518518
userInfoList = authorizer.listAllUsersInfo();
@@ -536,13 +536,6 @@ public PermissionInfoResp executeListUsers(final AuthorPlan plan) throws AuthExc
536536
}
537537
}
538538
userInfoList.removeIf(info -> toRemove.contains(info.username));
539-
final Iterator<TListUserInfo> userInfoitr = userInfoList.iterator();
540-
while (itr.hasNext()) {
541-
User userObj = authorizer.getUser(userInfoitr.next().getUsername());
542-
if (userObj == null || !userObj.hasRole(plan.getRoleName())) {
543-
itr.remove();
544-
}
545-
}
546539
}
547540
result.setTag(ColumnHeaderConstant.USER);
548541
result.setMemberInfo(userList);

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ public TSStatus executeNonQueryPlan(ConfigPhysicalPlan physicalPlan)
467467
case RevokeRole:
468468
case RevokeRoleFromUser:
469469
case UpdateUser:
470+
case UpdateUserMaxSession:
471+
case UpdateUserMinSession:
470472
case CreateUserWithRawPassword:
471473
case CreateUserDep:
472474
case CreateRoleDep:
@@ -484,6 +486,8 @@ public TSStatus executeNonQueryPlan(ConfigPhysicalPlan physicalPlan)
484486
case RDropUser:
485487
case RDropRole:
486488
case RUpdateUser:
489+
case RUpdateUserMaxSession:
490+
case RUpdateUserMinSession:
487491
case RGrantUserRole:
488492
case RGrantRoleAny:
489493
case RGrantUserAny:

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/CNPhysicalPlanGenerator.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ private void generateUserRolePhysicalPlan(final boolean isUser) {
199199
try (final DataInputStream dataInputStream =
200200
new DataInputStream(new BufferedInputStream(inputStream))) {
201201
int tag = dataInputStream.readInt();
202-
boolean fromOldVersion = tag < 0;
203202
String user;
204203
if (tag < 0) {
205204
user = readString(dataInputStream, STRING_ENCODING, strBufferLocal, -1 * tag);
@@ -221,6 +220,23 @@ private void generateUserRolePhysicalPlan(final boolean isUser) {
221220
createUser.setPermissions(new HashSet<>());
222221
createUser.setNodeNameList(new ArrayList<>());
223222
planDeque.add(createUser);
223+
if (tag == 2) {
224+
final AuthorTreePlan updateUserMaxSession =
225+
new AuthorTreePlan(ConfigPhysicalPlanType.UpdateUserMaxSession);
226+
updateUserMaxSession.setMaxSessionPerUser(dataInputStream.readInt());
227+
updateUserMaxSession.setUserName(user);
228+
updateUserMaxSession.setPermissions(new HashSet<>());
229+
updateUserMaxSession.setNodeNameList(new ArrayList<>());
230+
planDeque.add(updateUserMaxSession);
231+
final AuthorTreePlan updateUserMinSession =
232+
new AuthorTreePlan(ConfigPhysicalPlanType.UpdateUserMinSession);
233+
updateUserMinSession.setMinSessionPerUser(dataInputStream.readInt());
234+
updateUserMinSession.setUserName(user);
235+
updateUserMinSession.setPermissions(new HashSet<>());
236+
updateUserMinSession.setNodeNameList(new ArrayList<>());
237+
planDeque.add(updateUserMinSession);
238+
}
239+
224240
} else {
225241
final AuthorTreePlan createRole = new AuthorTreePlan(ConfigPhysicalPlanType.CreateRole);
226242
createRole.setRoleName(user);

iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/persistence/CNPhysicalPlanGeneratorTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ public void userGeneratorTest() throws Exception {
206206
plan.setNodeNameList(new ArrayList<>());
207207
answerSet.add(plan.hashCode());
208208

209+
plan = new AuthorTreePlan(ConfigPhysicalPlanType.UpdateUserMaxSession);
210+
plan.setUserName(userName);
211+
plan.setPermissions(new HashSet<>());
212+
plan.setNodeNameList(new ArrayList<>());
213+
plan.setMaxSessionPerUser(-1);
214+
authorInfo.authorNonQuery(plan);
215+
answerSet.add(plan.hashCode());
216+
217+
plan = new AuthorTreePlan(ConfigPhysicalPlanType.UpdateUserMinSession);
218+
plan.setUserName(userName);
219+
plan.setPermissions(new HashSet<>());
220+
plan.setNodeNameList(new ArrayList<>());
221+
plan.setMinSessionPerUser(-1);
222+
authorInfo.authorNonQuery(plan);
223+
answerSet.add(plan.hashCode());
224+
225+
plan = new AuthorTreePlan(ConfigPhysicalPlanType.CreateUserWithRawPassword);
226+
plan.setPassword(AuthUtils.encryptPassword("password123456"));
227+
plan.setUserName(userName);
228+
plan.setPermissions(new HashSet<>());
229+
plan.setNodeNameList(new ArrayList<>());
230+
answerSet.add(plan.hashCode());
231+
209232
plan = new AuthorTreePlan(ConfigPhysicalPlanType.CreateRole);
210233
plan.setRoleName("role1");
211234
plan.setPermissions(new HashSet<>());
@@ -271,7 +294,7 @@ public void userGeneratorTest() throws Exception {
271294
Assert.assertTrue(answerSet.contains(authPlan.hashCode()));
272295
count++;
273296
}
274-
Assert.assertEquals(4, count);
297+
Assert.assertEquals(6, count);
275298
final File roleListProfile =
276299
SystemFileFactory.INSTANCE.getFile(
277300
snapshotDir

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ protected void loadPrivileges(DataInputStream dataInputStream, Role role)
150150
role.setObjectPrivilegeMap(objectPrivilegeMap);
151151
}
152152

153+
protected void saveSessionPerUser(BufferedOutputStream outputStream, Role role)
154+
throws IOException {
155+
// Just used in LocalFileUserAccessor.java.
156+
// Do nothing.
157+
}
158+
153159
protected void saveRoles(Role role) throws IOException {
154160
// Just used in LocalFileUserAccessor.java.
155161
// Do nothing.
@@ -191,13 +197,9 @@ public Role loadEntity(String entityName) throws IOException {
191197
FileInputStream inputStream = new FileInputStream(entityFile);
192198
try (DataInputStream dataInputStream =
193199
new DataInputStream(new BufferedInputStream(inputStream))) {
194-
boolean fromOldVersion = false;
195200
int tag = dataInputStream.readInt();
196-
if (tag < 0) {
197-
fromOldVersion = true;
198-
}
199201

200-
if (fromOldVersion) {
202+
if (tag < 0) {
201203
String name =
202204
IOUtils.readString(dataInputStream, STRING_ENCODING, strBufferLocal, -1 * tag);
203205
Role role = new Role(name);
@@ -209,6 +211,11 @@ public Role loadEntity(String entityName) throws IOException {
209211
}
210212
role.setPrivilegeList(pathPrivilegeList);
211213
return role;
214+
} else if (tag == 1) {
215+
entityName = IOUtils.readString(dataInputStream, STRING_ENCODING, strBufferLocal);
216+
Role role = new Role(entityName);
217+
loadPrivileges(dataInputStream, role);
218+
return role;
212219
} else {
213220
assert tag == VERSION;
214221
entityName = IOUtils.readString(dataInputStream, STRING_ENCODING, strBufferLocal);
@@ -266,6 +273,7 @@ public void saveEntity(Role entity) throws IOException {
266273
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream)) {
267274
saveEntityVersion(outputStream);
268275
saveEntityName(outputStream, entity);
276+
saveSessionPerUser(outputStream, entity);
269277
savePrivileges(outputStream, entity);
270278
outputStream.flush();
271279
fileOutputStream.getFD().sync();

0 commit comments

Comments
 (0)