Skip to content

Commit 328f500

Browse files
[feat](auth) Support MySQL-compatible dual password (RETAIN CURRENT PASSWORD / DISCARD OLD PASSWORD)
Add MySQL 8.0-compatible dual password support so a password can be rotated online without breaking consumers that still hold the previous one, on both ALTER USER and SET PASSWORD: ALTER USER u IDENTIFIED BY 'new' RETAIN CURRENT PASSWORD; ALTER USER u IDENTIFIED BY 'new'; -- secondary password unchanged ALTER USER u DISCARD OLD PASSWORD; -- discard the retained password SET PASSWORD = PASSWORD('new') RETAIN CURRENT PASSWORD; Without an overlap window, every password change is a hard cutover: all clients (BI tools, service accounts, AI/MCP consumers) must switch at the same instant or fail to authenticate. The common workaround - two alternating accounts - fragments audit attribution across two usernames. MySQL solved this with dual passwords (WL#11544). Semantics mirror the MySQL 8.0 manual: - RETAIN CURRENT PASSWORD saves the current primary as the secondary password; both authenticate. Exactly one secondary per user: the next RETAIN replaces it. - Changing the password WITHOUT the clause leaves an existing secondary password unchanged. - An empty new password empties the secondary as well, even with RETAIN. - RETAIN on an account with an empty primary fails. - DISCARD OLD PASSWORD (ALTER USER only, matching MySQL) drops the secondary if one exists; combining it with a password change is rejected. Privileges (revised per review): the dual password clauses are PRIVILEGED even on one's own account. MySQL requires APPLICATION_PASSWORD_ADMIN for RETAIN CURRENT PASSWORD / DISCARD OLD PASSWORD on one's own account, precisely because otherwise anyone who briefly holds a password could park their own in the secondary slot as a persistent, invisible credential. Doris has no dynamic-privilege framework, so the clauses require the GRANT tier (the same tier that already gates ALTER USER on other accounts): - plain self-service SET PASSWORD stays unprivileged (unchanged); - SET PASSWORD ... RETAIN CURRENT PASSWORD requires GRANT even on self; - ALTER USER paths were already GRANT-gated by AlterUserInfo.validate(). A finer-grained APPLICATION_PASSWORD_ADMIN-style privilege (so that low-privilege service accounts can self-rotate with an overlap window without holding GRANT) is left as a follow-up. Design: - Password gains a nullable secondary slot (GSON field "pwd2"): absent in images/journals written before this change, ignored by binaries without it - tolerant in both directions, no meta version bump. - Authentication tries the primary, then the secondary, against ONE snapshot of the Password object, in one shared helper behind both existing check paths (checkPasswordInternal and checkPasswordInternalForUser), so every protocol lane (MySQL scramble, cleartext/LDAP fallback, Arrow Flight, HTTP, stream load) behaves identically. Failed-login policy fires only when both miss. - Password changes swap in a fully-built Password object as a single reference assignment (primary + carried secondary together), so there is no window where the new primary is visible without the retained secondary - the exact window a zero-downtime rotation must not have. - The domain resolver clones BOTH password slots when materializing resolved-IP users, so a resolver refresh after a rotation with RETAIN does not evict the retained password. - Replay: the retain flag rides PrivInfo on the existing OP_SET_PASSWORD journal op. DISCARD OLD PASSWORD also rides OP_SET_PASSWORD (a discard flag in PrivInfo, with passwd = the account's unchanged primary), NOT a new operation value on OP_ALTER_USER: an older FE deserializes an unknown AlterUserOpType as null and fails replay, whereas it replays the PrivInfo form as a plain set-password to the value the account already holds - a harmless no-op. Downgrade after first use is therefore safe. For correct DISCARD semantics across a mixed-version cluster, run all FEs on this version before first using the new clauses (an old-binary follower applies the discard as a no-op until it loads new state). - Observability: authenticating with the retained password logs an INFO line and increments fe metric secondary_password_auth_total, so operators can verify all consumers converged on the new password before rotating again (MySQL itself offers no such signal). The log/metric fire only after account lock/expiration policy passes, so a rejected login never counts as a successful secondary auth. - Grammar: RETAIN/CURRENT/PASSWORD tokens existed; DISCARD and OLD are added as nonReserved keywords (still valid as identifiers). Deliberate differences from MySQL (noted for review): - Privileges: Doris has no dynamic privileges, so the clauses use the GRANT tier instead of a dedicated APPLICATION_PASSWORD_ADMIN (see above; finer-grained privilege left as a follow-up). - No per-user authentication plugins in Doris, so MySQL's plugin-change-discards-secondary rule has no analog. Out of scope: introspection of whether a user currently holds a retained password (fe.audit.log keeps the RETAIN clause; a per-account indicator is a natural follow-up alongside the finer-grained privilege). ### What problem does this PR solve? Related PR: #67527, #67396 Problem Summary: #67527 added seven JUnit 4 `Assert` calls to `ConfigTest` after #67396 migrated the class to JUnit 5, causing `fe-common:testCompile` to fail with `cannot find symbol: Assert`. Use the existing JUnit 5 `Assertions` import. Also migrate the scheduler test introduced by the same PR to JUnit 5 assertions and lifecycle annotations, consistent with the FE test migration. ### Release note None ### Check List (For Author) - Test: `./run-fe-ut.sh --run org.apache.doris.common.ConfigTest,org.apache.doris.cloud.CacheHotspotManagerSchedulerTest` completed with BUILD SUCCESS: ConfigTest 10/10 and CacheHotspotManagerSchedulerTest 4/4 passed, with no failures, errors, or skipped tests. Checkstyle passed for fe-common and fe-core with zero violations; `git diff --check` passed. - Behavior changed: No - Does this need documentation: No Rebased onto current master: the password change paths now carry both the retained-secondary flag and the authentication-integration binding (setPasswordInternal / alterUserInternal take both), the domain resolver materializes resolved-IP users with both password slots, and a bound account shows "No" in the password column. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Raghvendra Singh <[email protected]>
1 parent 7a7e86c commit 328f500

16 files changed

Lines changed: 1237 additions & 35 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/alter/AlterUserOpType.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@ public enum AlterUserOpType {
2525
LOCK_ACCOUNT,
2626
UNLOCK_ACCOUNT,
2727
MODIFY_COMMENT,
28-
SET_TLS_REQUIRE
28+
SET_TLS_REQUIRE,
29+
// MySQL-compatible "DISCARD OLD PASSWORD": evict the retained secondary
30+
// password. NB: this name is NEVER written to the journal — a pre-feature
31+
// binary would deserialize the unknown enum value as null and fail replay.
32+
// The entry is journaled with op = SET_PASSWORD_POLICY (every option
33+
// UNSET, a no-op for such binaries) plus a discard marker; see
34+
// AlterUserOperationLog.discardOldPassword / Auth.discardOldPasswordInternal.
35+
DISCARD_OLD_PASSWORD
2936
}

‎fe/fe-core/src/main/java/org/apache/doris/metric/MetricRepo.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public final class MetricRepo {
121121
public static LongCounterMetric COUNTER_REQUEST_ALL;
122122
public static LongCounterMetric COUNTER_QUERY_ALL;
123123
public static LongCounterMetric COUNTER_QUERY_ERR;
124+
public static LongCounterMetric COUNTER_SECONDARY_PASSWORD_AUTH;
124125
public static LongCounterMetric COUNTER_QUERY_SLOW;
125126
public static LongCounterMetric COUNTER_QUERY_TABLE;
126127
public static LongCounterMetric COUNTER_QUERY_OLAP_TABLE;
@@ -549,6 +550,9 @@ public Long getValue() {
549550
DORIS_METRIC_REGISTER.addMetrics(COUNTER_QUERY_ALL);
550551
COUNTER_QUERY_ERR = new LongCounterMetric("query_err", MetricUnit.REQUESTS, "total error query");
551552
DORIS_METRIC_REGISTER.addMetrics(COUNTER_QUERY_ERR);
553+
COUNTER_SECONDARY_PASSWORD_AUTH = new LongCounterMetric("secondary_password_auth_total",
554+
MetricUnit.REQUESTS, "total authentications with a retained secondary password");
555+
DORIS_METRIC_REGISTER.addMetrics(COUNTER_SECONDARY_PASSWORD_AUTH);
552556
COUNTER_QUERY_SLOW = new LongCounterMetric("query_slow", MetricUnit.REQUESTS, "total slow query");
553557
DORIS_METRIC_REGISTER.addMetrics(COUNTER_QUERY_SLOW);
554558
COUNTER_QUERY_TABLE = new LongCounterMetric("query_table", MetricUnit.REQUESTS, "total query from table");

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

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,24 +1176,40 @@ public void setPassword(UserIdentity userIdentity, byte[] password) throws DdlEx
11761176
false /* set by resolver */, false);
11771177
}
11781178

1179+
// MySQL-compatible SET PASSWORD ... RETAIN CURRENT PASSWORD: keep the
1180+
// previous password valid as the secondary password.
1181+
public void setPassword(UserIdentity userIdentity, byte[] password,
1182+
boolean retainCurrent) throws DdlException {
1183+
setPasswordInternal(userIdentity, password, null, true /* err on non exist */,
1184+
false /* set by resolver */, retainCurrent, false);
1185+
}
1186+
11791187
public void replaySetPassword(PrivInfo info) {
11801188
try {
11811189
setPasswordInternal(info.getUserIdent(), info.getPasswd(), null, true /* err on non exist */,
1182-
false /* set by resolver */, true, true, info.getAuthIntegration(), info.getAuthIdentity());
1190+
false /* set by resolver */, info.isRetainPasswd(), true, true, info.getAuthIntegration(),
1191+
info.getAuthIdentity());
11831192
} catch (DdlException e) {
11841193
LOG.error("should not happened", e);
11851194
}
11861195
}
11871196

11881197
public void setPasswordInternal(UserIdentity userIdent, byte[] password, UserIdentity domainUserIdent,
11891198
boolean errOnNonExist, boolean setByResolver, boolean isReplay) throws DdlException {
1190-
setPasswordInternal(userIdent, password, domainUserIdent, errOnNonExist, setByResolver, isReplay,
1191-
false, null, null);
1199+
setPasswordInternal(userIdent, password, domainUserIdent, errOnNonExist, setByResolver,
1200+
false /* retain current password */, isReplay);
1201+
}
1202+
1203+
public void setPasswordInternal(UserIdentity userIdent, byte[] password, UserIdentity domainUserIdent,
1204+
boolean errOnNonExist, boolean setByResolver, boolean retainCurrent,
1205+
boolean isReplay) throws DdlException {
1206+
setPasswordInternal(userIdent, password, domainUserIdent, errOnNonExist, setByResolver,
1207+
retainCurrent, isReplay, false, null, null);
11921208
}
11931209

11941210
private void setPasswordInternal(UserIdentity userIdent, byte[] password, UserIdentity domainUserIdent,
1195-
boolean errOnNonExist, boolean setByResolver, boolean isReplay, boolean replaceBinding,
1196-
String authIntegration, String authIdentity) throws DdlException {
1211+
boolean errOnNonExist, boolean setByResolver, boolean retainCurrent, boolean isReplay,
1212+
boolean replaceBinding, String authIntegration, String authIdentity) throws DdlException {
11971213
Preconditions.checkArgument(!setByResolver || domainUserIdent != null, setByResolver + ", " + domainUserIdent);
11981214
writeLock();
11991215
try {
@@ -1210,8 +1226,25 @@ private void setPasswordInternal(UserIdentity userIdent, byte[] password, UserId
12101226
ErrorReport.reportDdlException(ErrorCode.ERR_CREDENTIALS_CONTRADICT_TO_HISTORY,
12111227
userIdent.getQualifiedUser(), userIdent.getHost());
12121228
}
1229+
if (retainCurrent) {
1230+
// MySQL-compatible RETAIN CURRENT PASSWORD constraint:
1231+
// "If you specify RETAIN CURRENT PASSWORD for an account
1232+
// that has an empty primary password, the statement
1233+
// fails." Checked here (not only at analysis) so every
1234+
// caller is covered; skipped on replay (the journal
1235+
// already passed this check). NB an EMPTY NEW password is
1236+
// NOT an error — it empties the secondary as well (MySQL
1237+
// semantics, handled in UserManager.setPassword).
1238+
if (user == null || user.getPassword() == null
1239+
|| user.getPassword().getPassword() == null
1240+
|| user.getPassword().getPassword().length == 0) {
1241+
throw new DdlException(
1242+
"Current password cannot be retained for user " + userIdent
1243+
+ " because it does not exist or is empty");
1244+
}
1245+
}
12131246
}
1214-
userManager.setPassword(userIdent, password, errOnNonExist);
1247+
userManager.setPassword(userIdent, password, errOnNonExist, retainCurrent);
12151248
if (replaceBinding && user != null) {
12161249
user.setAuthenticationBinding(authIntegration, authIdentity);
12171250
}
@@ -1227,14 +1260,44 @@ private void setPasswordInternal(UserIdentity userIdent, byte[] password, UserId
12271260
}
12281261

12291262
if (!isReplay) {
1230-
PrivInfo info = new PrivInfo(userIdent, null, password, null, null);
1263+
PrivInfo info = new PrivInfo(userIdent, null, password, null, null, retainCurrent);
12311264
info.setAuthenticationBinding(authIntegration, authIdentity);
12321265
Env.getCurrentEnv().getEditLog().logSetPassword(info);
12331266
}
12341267
} finally {
12351268
writeUnlock();
12361269
}
1237-
LOG.info("finished to set password for {}. is replay: {}", userIdent, isReplay);
1270+
LOG.info("finished to set password for {}. is replay: {}, retain current: {}",
1271+
userIdent, isReplay, retainCurrent);
1272+
}
1273+
1274+
/**
1275+
* MySQL-compatible "ALTER USER ... DISCARD OLD PASSWORD": drop the
1276+
* retained secondary password. Not a password change, so the password
1277+
* policy state (history, creation time) is left untouched.
1278+
*
1279+
* <p>Journaled as an OP_ALTER_USER entry whose carrier op is
1280+
* SET_PASSWORD_POLICY with every option UNSET plus a discard marker
1281+
* ({@link AlterUserOperationLog#discardOldPassword}). An FE binary
1282+
* without this feature ignores the marker and replays a policy update
1283+
* that changes nothing, so the journal stays readable by pre-feature
1284+
* binaries with no side effect. A new AlterUserOpType name is not an
1285+
* option there (an unknown enum name deserializes as null and fails
1286+
* replay), and neither is OP_SET_PASSWORD with the unchanged primary (a
1287+
* pre-feature binary's set-password replay appends that primary to the
1288+
* password history and refreshes the password creation time).
1289+
*/
1290+
private void discardOldPasswordInternal(UserIdentity userIdent, boolean isReplay) throws DdlException {
1291+
writeLock();
1292+
try {
1293+
userManager.discardOldPassword(userIdent, true /* err on non exist */);
1294+
if (!isReplay) {
1295+
Env.getCurrentEnv().getEditLog().logAlterUser(AlterUserOperationLog.discardOldPassword(userIdent));
1296+
}
1297+
} finally {
1298+
writeUnlock();
1299+
}
1300+
LOG.info("finished to discard old password for {}. is replay: {}", userIdent, isReplay);
12381301
}
12391302

12401303
public void setLdapPassword(String ldapPassword) {
@@ -1567,6 +1630,18 @@ public List<List<String>> getAuthInfo(UserIdentity specifiedUserIdent) {
15671630
return userAuthInfos;
15681631
}
15691632

1633+
/**
1634+
* The Password column of SHOW ALL GRANTS and SHOW PROC '/auth': "Yes (dual)" while the account also holds
1635+
* a retained secondary password (RETAIN CURRENT PASSWORD), so an administrator can list every account whose
1636+
* previous password still authenticates without reading the audit log for RETAIN clauses.
1637+
*/
1638+
private static String passwordColumnOf(User user) {
1639+
if (!user.hasPassword()) {
1640+
return "No";
1641+
}
1642+
return user.hasSecondaryPassword() ? "Yes (dual)" : "Yes";
1643+
}
1644+
15701645
private void getUserAuthInfo(List<List<String>> userAuthInfos, UserIdentity userIdent) {
15711646
// AuthProcDir.TITLE_NAMES
15721647
List<String> userAuthInfo = Lists.newArrayList();
@@ -1600,7 +1675,7 @@ private void getUserAuthInfo(List<List<String>> userAuthInfos, UserIdentity user
16001675
// ============== Comment ==============
16011676
userAuthInfo.add(user.getComment());
16021677
// ============== Password ==============
1603-
userAuthInfo.add(user.hasPassword() && !user.isAuthenticationBound() ? "Yes" : "No");
1678+
userAuthInfo.add(user.isAuthenticationBound() ? "No" : passwordColumnOf(user));
16041679
// ============== RequireSan ==============
16051680
userAuthInfo.add(requireSan);
16061681
userAuthInfo.add(Strings.nullToEmpty(user.getAuthIntegration()));
@@ -2107,15 +2182,23 @@ public void alterUser(AlterUserInfo info) throws DdlException {
21072182
integration, info.getAuthIdentity(), info.getPasswordOptions());
21082183
byte[] password = integration == null ? info.getPassword() : newBoundPassword();
21092184
alterUserInternal(info.isIfExist(), info.getOpType(), info.getUserIdent(), password, null,
2110-
info.getPasswordOptions(), info.getComment(), false, integration, identity);
2185+
info.getPasswordOptions(), info.getComment(), info.isRetainCurrentPassword(), false,
2186+
integration, identity);
21112187
} finally {
21122188
integrations.readUnlock();
21132189
}
21142190
}
21152191

21162192
public void replayAlterUser(AlterUserOperationLog log) {
21172193
try {
2118-
alterUserInternal(true, log.getOp(), log.getUserIdent(), log.getPassword(), log.getRole(),
2194+
// A DISCARD OLD PASSWORD entry carries op = SET_PASSWORD_POLICY
2195+
// (a no-op for pre-feature binaries) plus the discard marker; the
2196+
// marker decides what this binary replays.
2197+
AlterUserOpType opType = log.isDiscardOldPassword() ? AlterUserOpType.DISCARD_OLD_PASSWORD : log.getOp();
2198+
// retainCurrent is always false here: the SET_PASSWORD
2199+
// branch (the only one it applies to) journals via
2200+
// logSetPassword/PrivInfo, which carries the flag itself.
2201+
alterUserInternal(true, opType, log.getUserIdent(), log.getPassword(), log.getRole(),
21192202
log.getPasswordOptions(), log.getComment(), true);
21202203
} catch (DdlException e) {
21212204
LOG.error("should not happen", e);
@@ -2125,12 +2208,13 @@ public void replayAlterUser(AlterUserOperationLog log) {
21252208
private void alterUserInternal(boolean ifExists, AlterUserOpType opType, UserIdentity userIdent, byte[] password,
21262209
String role, PasswordOptions passwordOptions, String comment,
21272210
boolean isReplay) throws DdlException {
2128-
alterUserInternal(ifExists, opType, userIdent, password, role, passwordOptions, comment, isReplay, null, null);
2211+
alterUserInternal(ifExists, opType, userIdent, password, role, passwordOptions, comment,
2212+
false /* retain current password */, isReplay, null, null);
21292213
}
21302214

21312215
private void alterUserInternal(boolean ifExists, AlterUserOpType opType, UserIdentity userIdent, byte[] password,
2132-
String role, PasswordOptions passwordOptions, String comment, boolean isReplay,
2133-
String authIntegration, String authIdentity) throws DdlException {
2216+
String role, PasswordOptions passwordOptions, String comment, boolean retainCurrent,
2217+
boolean isReplay, String authIntegration, String authIdentity) throws DdlException {
21342218
writeLock();
21352219
try {
21362220
if (!doesUserExist(userIdent)) {
@@ -2141,13 +2225,22 @@ private void alterUserInternal(boolean ifExists, AlterUserOpType opType, UserIde
21412225
}
21422226
switch (opType) {
21432227
case SET_PASSWORD:
2144-
setPasswordInternal(userIdent, password, null, false, false, isReplay, true,
2145-
authIntegration, authIdentity);
2228+
setPasswordInternal(userIdent, password, null, false, false, retainCurrent, isReplay,
2229+
true, authIntegration, authIdentity);
21462230
if (!isReplay && authIntegration != null && ConnectContext.get() != null) {
21472231
ConnectContext.get().getState().setOk(0, 1,
21482232
"Local password policies and password history cleared");
21492233
}
21502234
break;
2235+
case DISCARD_OLD_PASSWORD:
2236+
// MySQL-compatible "ALTER USER ... DISCARD OLD PASSWORD":
2237+
// drop the retained secondary password now rather than at
2238+
// the next password change (a domain account's resolved-IP
2239+
// entries follow at the next resolver refresh, see
2240+
// UserManager.discardOldPassword). Journals its own
2241+
// OP_ALTER_USER entry (see discardOldPasswordInternal).
2242+
discardOldPasswordInternal(userIdent, isReplay);
2243+
break;
21512244
case SET_ROLE:
21522245
setRoleToUser(userIdent, role);
21532246
break;
@@ -2179,9 +2272,13 @@ private void alterUserInternal(boolean ifExists, AlterUserOpType opType, UserIde
21792272
|| opType == AlterUserOpType.UNLOCK_ACCOUNT)) {
21802273
userManager.refreshAuthenticationBinding(userIdent);
21812274
}
2182-
if (opType != AlterUserOpType.SET_PASSWORD && !isReplay) {
2275+
if (opType != AlterUserOpType.SET_PASSWORD && opType != AlterUserOpType.DISCARD_OLD_PASSWORD
2276+
&& !isReplay) {
21832277
// For SET_PASSWORD:
21842278
// the edit log is wrote in "setPasswordInternal"
2279+
// For DISCARD_OLD_PASSWORD:
2280+
// the edit log is wrote in "discardOldPasswordInternal"
2281+
// (a no-op carrier for pre-feature binaries)
21852282
AlterUserOperationLog log = new AlterUserOperationLog(opType, userIdent, password, role,
21862283
passwordOptions, comment);
21872284
Env.getCurrentEnv().getEditLog().logAlterUser(log);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public class Password {
2323
@SerializedName(value = "pwd")
2424
private byte[] password;
2525

26+
// MySQL-compatible dual password: the previous (retained) password, kept
27+
// valid for authentication until the next password change without
28+
// "RETAIN CURRENT PASSWORD", or an explicit "DISCARD OLD PASSWORD".
29+
// Nullable; absent in images written before this feature (GSON tolerant
30+
// in both directions).
31+
@SerializedName(value = "pwd2")
32+
private byte[] secondaryPassword;
33+
2634
public Password() {
2735
}
2836

@@ -37,4 +45,16 @@ public byte[] getPassword() {
3745
public void setPassword(byte[] password) {
3846
this.password = password;
3947
}
48+
49+
public byte[] getSecondaryPassword() {
50+
return secondaryPassword;
51+
}
52+
53+
public void setSecondaryPassword(byte[] secondaryPassword) {
54+
this.secondaryPassword = secondaryPassword;
55+
}
56+
57+
public boolean hasSecondaryPassword() {
58+
return secondaryPassword != null && secondaryPassword.length > 0;
59+
}
4060
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public boolean hasPassword() {
168168
return password != null && password.getPassword() != null && password.getPassword().length != 0;
169169
}
170170

171+
/** Whether the account also holds a retained secondary password (RETAIN CURRENT PASSWORD). */
172+
public boolean hasSecondaryPassword() {
173+
return password != null && password.hasSecondaryPassword();
174+
}
175+
171176
public String getComment() {
172177
return comment;
173178
}

0 commit comments

Comments
 (0)