Skip to content

Commit db31e65

Browse files
authored
[fix](auth) fix SHOW GRANTS FOR always showing RequireSan=NULL (apache#7733)
## Problem `SHOW GRANTS FOR '<user>'@'%'` always displayed `RequireSan=NULL` even when `REQUIRE SAN` was set and actively enforced. `SHOW ALL GRANTS` showed the correct value. Reported on: selectdb-4.1.0-c574af9014e, fra03 dev-doritos and iad03 abuse-prevention-platform. ## Root Cause `Auth.getUserAuthInfo()` computed `requireSan` from the `UserIdentity` passed in by the caller. For `SHOW GRANTS FOR <user>`, the SQL parser constructs a **fresh** `UserIdentity` with only `user` and `host` populated — the `san` field is never set. By contrast, `SHOW ALL GRANTS` iterates directly over stored `User` objects whose `UserIdentity` carries the persisted `san` value. ## Fix In `getUserAuthInfo()`, look up the stored `User` via `userManager.getUserByUserIdentity()` **before** computing `requireSan`, and derive the value from the persisted `UserIdentity` instead of the parameter. `UserIdentity.equals()` compares only `(user, host, isDomain)`, so the lookup correctly matches the parser-created identity to the stored record. As a side effect, the redundant second `getUserByUserIdentity` call in the non-LDAP branch is eliminated. ## Files Changed - `fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java` — fix in `getUserAuthInfo()` - `fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java` — regression test `testShowGrantsForUserDisplaysRequireSan()` ## Test Added `CloudAuthTest#testShowGrantsForUserDisplaysRequireSan()`: creates a user with `REQUIRE SAN`, then calls `SHOW GRANTS FOR` with a parser-simulated `UserIdentity` (no `san` set) and asserts the `RequireSan` column returns the correct SAN value instead of `\N`.
1 parent fcb970e commit db31e65

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,9 +1333,15 @@ private void getUserAuthInfo(List<List<String>> userAuthInfos, UserIdentity user
13331333
List<String> userAuthInfo = Lists.newArrayList();
13341334
// ================= UserIdentity =======================
13351335
userAuthInfo.add(userIdent.toString());
1336-
String requireSan = Strings.isNullOrEmpty(userIdent.getSan())
1336+
// Look up the stored User first so we can derive TLS fields (e.g. san) from the
1337+
// persisted UserIdentity. The passed-in userIdent may have been constructed by the
1338+
// SQL parser (SHOW GRANTS FOR <user>) and therefore lacks fields that are not part
1339+
// of the user/host identity key, such as san.
1340+
User storedUser = userManager.getUserByUserIdentity(userIdent);
1341+
UserIdentity storedIdent = (storedUser != null) ? storedUser.getUserIdentity() : userIdent;
1342+
String requireSan = Strings.isNullOrEmpty(storedIdent.getSan())
13371343
? FeConstants.null_string
1338-
: userIdent.getSan();
1344+
: storedIdent.getSan();
13391345
if (isLdapAuthEnabled() && ldapManager.doesUserExist(userIdent.getQualifiedUser())) {
13401346
// ============== Comment ==============
13411347
userAuthInfo.add(FeConstants.null_string);
@@ -1348,17 +1354,16 @@ private void getUserAuthInfo(List<List<String>> userAuthInfos, UserIdentity user
13481354
userAuthInfo.add(ldapUserInfo.getRoles().stream().map(role -> role.getRoleName())
13491355
.collect(Collectors.joining(",")));
13501356
} else {
1351-
User user = userManager.getUserByUserIdentity(userIdent);
1352-
if (user == null) {
1357+
if (storedUser == null) {
13531358
userAuthInfo.add(FeConstants.null_string);
13541359
userAuthInfo.add(FeConstants.null_string);
13551360
userAuthInfo.add(FeConstants.null_string);
13561361
userAuthInfo.add(FeConstants.null_string);
13571362
} else {
13581363
// ============== Comment ==============
1359-
userAuthInfo.add(user.getComment());
1364+
userAuthInfo.add(storedUser.getComment());
13601365
// ============== Password ==============
1361-
userAuthInfo.add(user.hasPassword() ? "Yes" : "No");
1366+
userAuthInfo.add(storedUser.hasPassword() ? "Yes" : "No");
13621367
// ============== RequireSan ==============
13631368
userAuthInfo.add(requireSan);
13641369
// ============== Roles ==============

‎fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.doris.analysis.RevokeStmt;
2929
import org.apache.doris.analysis.ShowGrantsStmt;
3030
import org.apache.doris.analysis.TablePattern;
31+
import org.apache.doris.analysis.TlsOptions;
3132
import org.apache.doris.analysis.UserDesc;
3233
import org.apache.doris.analysis.UserIdentity;
3334
import org.apache.doris.catalog.AccessPrivilege;
@@ -38,6 +39,7 @@
3839
import org.apache.doris.cloud.system.CloudSystemInfoService;
3940
import org.apache.doris.common.AnalysisException;
4041
import org.apache.doris.common.ExceptionChecker;
42+
import org.apache.doris.common.Pair;
4143
import org.apache.doris.common.UserException;
4244
import org.apache.doris.datasource.InternalCatalog;
4345
import org.apache.doris.persist.EditLog;
@@ -620,4 +622,35 @@ public void testVirtualComputeGroup() throws UserException {
620622
// drop user
621623
dropUser(userIdentity);
622624
}
625+
626+
/**
627+
* Regression test for: SHOW GRANTS FOR '<user>'@'%' always shows RequireSan=NULL even when
628+
* REQUIRE SAN is set. The bug was that getUserAuthInfo() computed requireSan from the
629+
* parser-created UserIdentity (which lacks the san field) instead of the stored one.
630+
*/
631+
@Test
632+
public void testShowGrantsForUserDisplaysRequireSan() throws UserException, AnalysisException {
633+
String san = "DNS:ca-doris-cluster-doris-test";
634+
UserIdentity userIdentity = new UserIdentity("sanUser", "%");
635+
TlsOptions tlsOptions = TlsOptions.of(
636+
java.util.Collections.singletonList(Pair.of("SAN", san)));
637+
UserDesc userDesc = new UserDesc(userIdentity, "passwd", true);
638+
CreateUserStmt createUserStmt = new CreateUserStmt(false, userDesc, null, null, null, tlsOptions);
639+
createUserStmt.analyze(analyzer);
640+
auth.createUser(createUserStmt);
641+
642+
// Simulate the parser: SHOW GRANTS FOR 'sanUser'@'%' creates a fresh UserIdentity
643+
// without the san field populated. In production, ShowGrantsStmt.analyze() calls
644+
// userIdent.analyze() before the statement reaches ShowExecutor, so we do the same.
645+
UserIdentity parserCreatedIdent = new UserIdentity("sanUser", "%");
646+
parserCreatedIdent.analyze();
647+
ShowResultSet resultSet = testShowGrants(parserCreatedIdent);
648+
649+
Assert.assertEquals(1, resultSet.getResultRows().size());
650+
List<String> row = resultSet.getResultRows().get(0);
651+
// RequireSan is the 4th column (index 3): UserIdentity, Comment, Password, RequireSan, ...
652+
Assert.assertEquals("RequireSan should be shown by SHOW GRANTS FOR, not NULL", san, row.get(3));
653+
654+
dropUser(userIdentity);
655+
}
623656
}

0 commit comments

Comments
 (0)