Skip to content

Commit

Permalink
[BugFix] Fix show property for non-existed user error result (backport
Browse files Browse the repository at this point in the history
…#41351) (#41482)

Co-authored-by: yiming <[email protected]>
  • Loading branch information
mergify[bot] and nshangyiming authored Feb 23, 2024
1 parent f381f7d commit 40c9893
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.starrocks.privilege.UserPrivilegeCollectionV2;
import com.starrocks.qe.ConnectContext;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.CreateUserStmt;
import com.starrocks.sql.ast.DropUserStmt;
import com.starrocks.sql.ast.UserIdentity;
Expand Down Expand Up @@ -173,11 +174,31 @@ public boolean doesUserExist(UserIdentity userIdentity) {
}
}

/**
* Get max connection number of the user, if the user is ephemeral, i.e. the user is saved in SR,
* but some external system, like LDAP, return default max connection number
* @param currUserIdentity user identity of current connection
* @return max connection number of the user
*/
public long getMaxConn(UserIdentity currUserIdentity) {
if (currUserIdentity.isEphemeral()) {
return DEFAULT_MAX_CONNECTION_FOR_EXTERNAL_USER;
} else {
String userName = currUserIdentity.getUser();
return getMaxConn(userName);
}
}

/**
* Get max connection number based on plain username, the user should be an internal user,
* if the user doesn't exist in SR, it will throw an exception.
* @param userName plain username saved in SR
* @return max connection number of the user
*/
public long getMaxConn(String userName) {
UserProperty userProperty = userNameToProperty.get(userName);
if (userProperty == null) {
// TODO(yiming): find a better way to specify max connections for external user, like ldap, kerberos etc.
return DEFAULT_MAX_CONNECTION_FOR_EXTERNAL_USER;
throw new SemanticException("Unknown user: " + userName);
} else {
return userNameToProperty.get(userName).getMaxConn();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public boolean registerConnection(ConnectContext ctx) {
if (connCountByUser.get(ctx.getQualifiedUser()) == null) {
connCountByUser.put(ctx.getQualifiedUser(), new AtomicInteger(0));
}
int currentConns = connCountByUser.get(ctx.getQualifiedUser()).get();
long currentMaxConns = ctx.getGlobalStateMgr().getAuthenticationMgr().getMaxConn(ctx.getQualifiedUser());
if (currentConns >= currentMaxConns) {
int currentConn = connCountByUser.get(ctx.getQualifiedUser()).get();
long currentMaxConn = ctx.getGlobalStateMgr().getAuthenticationMgr().getMaxConn(ctx.getCurrentUserIdentity());
if (currentConn >= currentMaxConn) {
return false;
}
numberConnection.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.DDLStmtExecutor;
import com.starrocks.qe.SetDefaultRoleExecutor;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.AlterUserStmt;
import com.starrocks.sql.ast.CreateRoleStmt;
import com.starrocks.sql.ast.CreateUserStmt;
Expand Down Expand Up @@ -299,8 +300,7 @@ public void testDropAlterUser() throws Exception {
Assert.assertFalse(manager.doesUserExist(testUserWithIp));

// can't get max connection after all test user are dropped
Assert.assertEquals(AuthenticationMgr.DEFAULT_MAX_CONNECTION_FOR_EXTERNAL_USER,
manager.getMaxConn("test"));
Assert.assertThrows(SemanticException.class, () -> manager.getMaxConn("test"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,10 +1123,12 @@ public void testShowProcessList(@Mocked MysqlChannel channel,

ConnectContext ctx1 = new ConnectContext(socketChannel);
ctx1.setQualifiedUser("test");
ctx1.setCurrentUserIdentity(new UserIdentity("test", "%"));
ctx1.setGlobalStateMgr(GlobalStateMgr.getCurrentState());
ctx1.setConnectionId(1);
ConnectContext ctx2 = new ConnectContext(socketChannel);
ctx2.setQualifiedUser("test2");
ctx2.setCurrentUserIdentity(new UserIdentity("test2", "%"));
ctx2.setGlobalStateMgr(GlobalStateMgr.getCurrentState());
ctx2.setConnectionId(2);

Expand Down

0 comments on commit 40c9893

Please sign in to comment.