924060929 commented on code in PR #66115:
URL: https://github.com/apache/doris/pull/66115#discussion_r4581516019
##########
fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/UserManager.java:
##########
@@ -370,14 +438,93 @@ public Map<String, List<User>> getNameToUsers() {
}
public void setPassword(UserIdentity userIdentity, byte[] password,
boolean errOnNonExist) throws DdlException {
+ setPassword(userIdentity, password, errOnNonExist, false);
+ }
+
+ /**
+ * Set the user's password, with MySQL-compatible dual password semantics:
+ * with {@code retainCurrent} ("RETAIN CURRENT PASSWORD") the previous
+ * primary password becomes the secondary password and remains valid for
+ * authentication; without it an existing secondary password remains
+ * UNCHANGED (MySQL: "If an account has a secondary password and you
+ * change its primary password without specifying RETAIN CURRENT PASSWORD,
+ * the secondary password remains unchanged."). Setting an EMPTY password
+ * empties the secondary password as well, even with retain (also MySQL).
+ */
+ public void setPassword(UserIdentity userIdentity, byte[] password,
boolean errOnNonExist,
+ boolean retainCurrent) throws DdlException {
+ setPassword(userIdentity, password, errOnNonExist, retainCurrent,
false);
+ }
+
+ /**
+ * As above; with {@code discardSecondary} the retained secondary password
+ * is dropped instead of carried, whatever {@code retainCurrent} says. Used
+ * for an authentication-binding transition (into, out of or between
+ * authentication integrations): MySQL discards the secondary password
+ * when the authentication plugin changes, and a password retained before
+ * an external-authentication period must not come back to life beside
+ * the new one when the account returns to native authentication (the
+ * bound-user guard refuses native logins only WHILE the account is bound).
+ */
+ public void setPassword(UserIdentity userIdentity, byte[] password,
boolean errOnNonExist,
+ boolean retainCurrent, boolean discardSecondary) throws
DdlException {
User user = getUserByUserIdentity(userIdentity);
if (user == null) {
if (errOnNonExist) {
throw new DdlException("user " + userIdentity + " does not
exist");
}
return;
}
- user.setPassword(password);
+ Password oldPassword = user.getPassword();
+ byte[] carried;
+ if (discardSecondary || password == null || password.length == 0) {
+ // a binding transition retires the secondary; an empty new
password
+ // empties it as well, even with RETAIN CURRENT PASSWORD (MySQL
+ // semantics)
+ carried = null;
+ } else if (retainCurrent) {
+ carried = oldPassword == null ? null : oldPassword.getPassword();
+ } else {
+ carried = oldPassword == null ? null :
oldPassword.getSecondaryPassword();
+ }
+ // Build the full Password first and swap it in as ONE reference
+ // assignment: authentication reads a single Password snapshot, so
+ // there must never be a window where the new primary is visible
+ // without the carried secondary — that window would reject exactly
+ // the old-password consumers this feature keeps alive.
+ Password newPassword = new Password(password);
+ newPassword.setSecondaryPassword(carried);
+ user.setPassword(newPassword);
+ }
+
+ /**
+ * MySQL-compatible "ALTER USER ... DISCARD OLD PASSWORD": drop the
+ * retained secondary password. A user without one is a silent no-op
+ * (MySQL behavior).
+ *
+ * <p>Swaps in a new {@link Password} holding the primary alone instead of
+ * clearing the slot in place, so that, like a password change, it never
+ * mutates a Password object authentication may be reading:
+ * matchUserPassword compares against one snapshot per generation.
+ *
+ * <p>A domain account ({@code u@['example.com']}) is cleared here in its
+ * domain entry only; the resolved-IP entries the DomainResolver derives
+ * from it keep their copy until its next refresh (10 s) rebuilds them, so
+ * the discarded password stays valid for up to that long - the same lag a
+ * plain password change on a domain account has.
+ */
+ public void discardOldPassword(UserIdentity userIdentity, boolean
errOnNonExist) throws DdlException {
+ User user = getUserByUserIdentity(userIdentity);
+ if (user == null) {
+ if (errOnNonExist) {
+ throw new DdlException("user " + userIdentity + " does not
exist");
+ }
+ return;
+ }
+ Password current = user.getPassword();
+ if (current != null && current.hasSecondaryPassword()) {
+ user.setPassword(new Password(current.getPassword()));
Review Comment:
[P2] Refresh resolved domain-account entries when discarding the secondary
password
For a domain account whose IP entries have already been materialized by
DomainResolver, rotate from p1 to p2 with RETAIN CURRENT PASSWORD, then execute
DISCARD OLD PASSWORD. This method replaces only the domain user's Password; the
resolved-IP users still hold their copied (p2, p1) credentials.
checkPasswordInternal() skips the domain entry and matchUserPassword() checks
the resolved-IP entry, so a new connection using p1 still succeeds after
DISCARD returns successfully, until a subsequent resolver refresh updates that
entry. The resolver's 10-second scheduling interval is not a strict upper bound
on this delay.
Unlike the comment above suggests, ordinary password changes already refresh
these entries immediately through Auth.setPasswordInternal() ->
refreshAuthenticationBinding(). Could we do the same for DISCARD: after
clearing the secondary in Auth.discardOldPasswordInternal(), call
userManager.refreshAuthenticationBinding(userIdent) for a domain identity while
holding the existing Auth write lock? Keeping this in the shared
execution/replay path would also make followers apply the same update. Please
add a test that materializes a domain account's IP entry, retains the old
password, discards it, and verifies immediately (without invoking the periodic
resolver refresh) that the old password fails while the primary still works.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]