Skip to content

Commit 7685261

Browse files
authored
User userId to check whether the user is admin in ConfigNode (apache#16554)
1 parent 9a25d72 commit 7685261

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed

integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBUserRenameIT.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,23 @@ private void userRenameTest(String dialect) throws SQLException {
110110
adminStmt.execute("ALTER USER root RENAME TO user4");
111111
// We can create another root
112112
adminStmt.execute("CREATE USER root 'IoTDB@2025abc'");
113+
// We can grant and revoke privilege to the new root
114+
if (BaseEnv.TABLE_SQL_DIALECT.equals(dialect)) {
115+
adminStmt.execute("GRANT SYSTEM TO USER root");
116+
adminStmt.execute("REVOKE SYSTEM FROM USER root");
117+
} else {
118+
adminStmt.execute("GRANT SYSTEM ON root.** TO USER root");
119+
adminStmt.execute("REVOKE SYSTEM ON root.** FROM USER root");
120+
}
113121
// Ensure everything works
114-
final String ans = "0,admin,\n" + "10000,user4,\n" + "10001,user2,\n" + "10002,root,\n";
122+
String ans = "0,admin,\n" + "10000,user4,\n" + "10001,user2,\n" + "10002,root,\n";
115123
ResultSet resultSet = adminStmt.executeQuery("LIST USER");
116124
validateResultSet(resultSet, ans);
125+
// Finally, the other root can be deleted
126+
adminStmt.execute("DROP USER root");
127+
ans = "0,admin,\n" + "10000,user4,\n" + "10001,user2,\n";
128+
resultSet = adminStmt.executeQuery("LIST USER");
129+
validateResultSet(resultSet, ans);
117130
}
118131
}
119132
}

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/authorizer/LocalFileAuthorizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void testTreePermission() throws AuthException {
130130
authorizer.grantPrivilegeToUser(
131131
"error", new PrivilegeUnion(nodeName, PrivilegeType.READ_DATA, false));
132132
} catch (AuthException e) {
133-
assertEquals("No such user error", e.getMessage());
133+
assertEquals("User error does not exist", e.getMessage());
134134
}
135135

136136
try {

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.iotdb.commons.auth.role.BasicRoleManager;
2727
import org.apache.iotdb.commons.auth.user.BasicUserManager;
2828
import org.apache.iotdb.commons.conf.CommonDescriptor;
29+
import org.apache.iotdb.commons.conf.IoTDBConstant;
2930
import org.apache.iotdb.commons.exception.StartupException;
3031
import org.apache.iotdb.commons.path.PartialPath;
3132
import org.apache.iotdb.commons.security.encrypt.AsymmetricEncrypt;
@@ -99,8 +100,8 @@ private static class InstanceHolder {
99100
}
100101
}
101102

102-
private void checkAdmin(String username, String errmsg) throws AuthException {
103-
if (isAdmin(username)) {
103+
private void checkAdmin(long userId, String errmsg) throws AuthException {
104+
if (userId == IoTDBConstant.SUPER_USER_ID) {
104105
throw new AuthException(TSStatusCode.NO_PERMISSION, errmsg);
105106
}
106107
}
@@ -177,7 +178,7 @@ public void createUserWithRawPassword(String username, String password) throws A
177178

178179
@Override
179180
public void deleteUser(String username) throws AuthException {
180-
checkAdmin(username, "Default administrator cannot be deleted");
181+
checkAdmin(userManager.getUserId(username), "Default administrator cannot be deleted");
181182
if (!userManager.deleteEntity(username)) {
182183
throw new AuthException(
183184
TSStatusCode.USER_NOT_EXIST, String.format("User %s does not exist", username));
@@ -186,19 +187,25 @@ public void deleteUser(String username) throws AuthException {
186187

187188
@Override
188189
public void grantPrivilegeToUser(String username, PrivilegeUnion union) throws AuthException {
189-
checkAdmin(username, "Invalid operation, administrator already has all privileges");
190+
checkAdmin(
191+
userManager.getUserId(username),
192+
"Invalid operation, administrator already has all privileges");
190193
userManager.grantPrivilegeToEntity(username, union);
191194
}
192195

193196
@Override
194197
public void revokePrivilegeFromUser(String username, PrivilegeUnion union) throws AuthException {
195-
checkAdmin(username, "Invalid operation, administrator must have all privileges");
198+
checkAdmin(
199+
userManager.getUserId(username),
200+
"Invalid operation, administrator must have all privileges");
196201
userManager.revokePrivilegeFromEntity(username, union);
197202
}
198203

199204
@Override
200205
public void revokeAllPrivilegeFromUser(String userName) throws AuthException {
201-
checkAdmin(userName, "Invalid operation, administrator cannot revoke privileges");
206+
checkAdmin(
207+
userManager.getUserId(userName),
208+
"Invalid operation, administrator cannot revoke privileges");
202209
User user = userManager.getEntity(userName);
203210
if (user == null) {
204211
throw new AuthException(
@@ -262,7 +269,8 @@ public void revokeAllPrivilegeFromRole(String roleName) throws AuthException {
262269

263270
@Override
264271
public void grantRoleToUser(String roleName, String userName) throws AuthException {
265-
checkAdmin(userName, "Invalid operation, cannot grant role to administrator");
272+
checkAdmin(
273+
userManager.getUserId(userName), "Invalid operation, cannot grant role to administrator");
266274
Role role = roleManager.getEntity(roleName);
267275
if (role == null) {
268276
throw new AuthException(
@@ -279,7 +287,7 @@ public void grantRoleToUser(String roleName, String userName) throws AuthExcepti
279287

280288
@Override
281289
public void revokeRoleFromUser(String roleName, String userName) throws AuthException {
282-
if (isAdmin(userName)) {
290+
if (userManager.getUserId(userName) == IoTDBConstant.SUPER_USER_ID) {
283291
throw new AuthException(
284292
TSStatusCode.NO_PERMISSION, "Invalid operation, cannot revoke role from administrator ");
285293
}
@@ -333,7 +341,7 @@ private void forceUpdateUserPassword(String userName, String newPassword) throws
333341

334342
@Override
335343
public boolean checkUserPrivileges(String userName, PrivilegeUnion union) throws AuthException {
336-
if (isAdmin(userName)) {
344+
if (userManager.getUserId(userName) == IoTDBConstant.SUPER_USER_ID) {
337345
return true;
338346
}
339347
User user = userManager.getEntity(userName);

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/IAuthorizer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
/** This interface provides all authorization-relative operations. */
3636
public interface IAuthorizer extends SnapshotProcessor {
3737

38-
boolean isAdmin(String userName);
39-
4038
/**
4139
* Login for a user.
4240
*

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/LocalFileAuthorizer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,4 @@ public LocalFileAuthorizer() throws AuthException {
3333
new LocalFileUserManager(config.getUserFolder()),
3434
new LocalFileRoleManager(config.getRoleFolder()));
3535
}
36-
37-
@Override
38-
public boolean isAdmin(String username) {
39-
return config.getDefaultAdminName().equals(username);
40-
}
4136
}

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/OpenIdAuthorizer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ public void deleteUser(String username) {
226226
* @param token Usually the JWT but could also be just the name of the user.
227227
* @return true if the user is an admin
228228
*/
229-
@Override
230229
public boolean isAdmin(String token) {
231230
Claims claims;
232231
if (this.loggedClaims.containsKey(token)) {

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ public User getEntity(long entityId) {
140140
return null;
141141
}
142142

143+
public long getUserId(String username) throws AuthException {
144+
User user = this.getEntity(username);
145+
if (user == null) {
146+
throw new AuthException(
147+
TSStatusCode.USER_NOT_EXIST, String.format("User %s does not exist", username));
148+
}
149+
return user.getUserId();
150+
}
151+
143152
public boolean createUser(
144153
String username, String password, boolean validCheck, boolean enableEncrypt)
145154
throws AuthException {

0 commit comments

Comments
 (0)