Skip to content

Commit 0f8b492

Browse files
[fix](auth) Dual password: discard the secondary on a binding transition; carry both slots in the immediate domain refresh
Two lifecycle gaps found by the automated review after the rebase onto the authentication-integration bindings: - An authentication-binding transition (into, out of or between integrations) now retires the retained secondary password instead of carrying it, following the MySQL rule that a change of authentication plugin discards the secondary. The bound-user guard refuses native logins only while the account is bound, so a password retained before the external-authentication period came back to life beside the new one once the account returned to native authentication. RETAIN CURRENT PASSWORD is refused across a binding transition: the current password of a bound account is a generated placeholder, not a credential to keep. The decision is derived from the account's state and the journal's binding, so a follower replays the same outcome. - The immediate refresh of a domain account's resolved-IP entries (UserManager.refreshAuthenticationBinding, run right after a password change, an ACCOUNT_LOCK/UNLOCK and a REQUIRE change) copied the primary alone, so a rotation with RETAIN rejected the retained password on those entries until the DomainResolver's next periodic pass rebuilt them. Both refresh paths now copy both slots through Password.copyOf. DualPasswordTest: bind/unbind lifecycle (the pre-binding passwords stay dead), RETAIN refused into and out of a binding, the bind/unbind journals replayed from a retained credential, and the domain account authenticating both slots immediately after RETAIN, ACCOUNT_UNLOCK and REQUIRE without a periodic refresh. Signed-off-by: Raghvendra Singh <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 328f500 commit 0f8b492

4 files changed

Lines changed: 207 additions & 9 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,13 +1214,23 @@ private void setPasswordInternal(UserIdentity userIdent, byte[] password, UserId
12141214
writeLock();
12151215
try {
12161216
User user = userManager.getUserByUserIdentity(userIdent);
1217-
if (!isReplay && !replaceBinding && user != null && user.isAuthenticationBound()) {
1217+
boolean wasBound = user != null && user.isAuthenticationBound();
1218+
if (!isReplay && !replaceBinding && wasBound) {
12181219
throw new DdlException("SET PASSWORD is not supported for authentication-bound users; "
12191220
+ "use ALTER USER ... IDENTIFIED BY");
12201221
}
12211222
if (!isReplay && authIntegration != null) {
12221223
userManager.checkDomainAuthenticationBinding(userIdent);
12231224
}
1225+
if (!isReplay && retainCurrent && (wasBound || authIntegration != null)) {
1226+
// The "current password" of an authentication-bound account is a
1227+
// generated placeholder nobody holds: there is nothing to retain
1228+
// on the way out of a binding, and nothing worth keeping on the
1229+
// way in (native authentication is refused while bound).
1230+
throw new DdlException("Current password cannot be retained for user " + userIdent
1231+
+ (wasBound ? " because the account is bound to an authentication integration"
1232+
: " while binding it to an authentication integration"));
1233+
}
12241234
if (!isReplay && authIntegration == null) {
12251235
if (!passwdPolicyManager.checkPasswordHistory(userIdent, password)) {
12261236
ErrorReport.reportDdlException(ErrorCode.ERR_CREDENTIALS_CONTRADICT_TO_HISTORY,
@@ -1244,7 +1254,18 @@ private void setPasswordInternal(UserIdentity userIdent, byte[] password, UserId
12441254
}
12451255
}
12461256
}
1247-
userManager.setPassword(userIdent, password, errOnNonExist, retainCurrent);
1257+
// An authentication-binding transition (into, out of or between
1258+
// integrations) retires the retained secondary password instead of
1259+
// carrying it: MySQL discards the secondary password when the
1260+
// authentication plugin changes, and the bound-user guard refuses
1261+
// native logins only WHILE the account is bound, so a password
1262+
// retained before the external-authentication period would
1263+
// otherwise come back to life beside the new one once the account
1264+
// returns to native authentication. Derived from the account's
1265+
// state and the journal's binding, so a follower replays the same
1266+
// outcome.
1267+
boolean bindingChanges = wasBound || authIntegration != null;
1268+
userManager.setPassword(userIdent, password, errOnNonExist, retainCurrent, bindingChanges);
12481269
if (replaceBinding && user != null) {
12491270
user.setAuthenticationBinding(authIntegration, authIdentity);
12501271
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public Password(byte[] password) {
3838
this.password = password;
3939
}
4040

41+
/**
42+
* A new Password holding the same two slots. Every copy of an account's
43+
* credentials (the resolved-IP entries the DomainResolver derives from a
44+
* domain account, and their immediate refresh after an ALTER) must carry
45+
* the retained secondary password as well: a copy built from the primary
46+
* alone stops authenticating the old password on that entry right after
47+
* a rotation with RETAIN CURRENT PASSWORD.
48+
*/
49+
public static Password copyOf(Password source) {
50+
Password copy = new Password(source.getPassword());
51+
copy.setSecondaryPassword(source.getSecondaryPassword());
52+
return copy;
53+
}
54+
4155
public byte[] getPassword() {
4256
return password;
4357
}

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,21 @@ public void setPassword(UserIdentity userIdentity, byte[] password, boolean errO
453453
*/
454454
public void setPassword(UserIdentity userIdentity, byte[] password, boolean errOnNonExist,
455455
boolean retainCurrent) throws DdlException {
456+
setPassword(userIdentity, password, errOnNonExist, retainCurrent, false);
457+
}
458+
459+
/**
460+
* As above; with {@code discardSecondary} the retained secondary password
461+
* is dropped instead of carried, whatever {@code retainCurrent} says. Used
462+
* for an authentication-binding transition (into, out of or between
463+
* authentication integrations): MySQL discards the secondary password
464+
* when the authentication plugin changes, and a password retained before
465+
* an external-authentication period must not come back to life beside
466+
* the new one when the account returns to native authentication (the
467+
* bound-user guard refuses native logins only WHILE the account is bound).
468+
*/
469+
public void setPassword(UserIdentity userIdentity, byte[] password, boolean errOnNonExist,
470+
boolean retainCurrent, boolean discardSecondary) throws DdlException {
456471
User user = getUserByUserIdentity(userIdentity);
457472
if (user == null) {
458473
if (errOnNonExist) {
@@ -462,9 +477,10 @@ public void setPassword(UserIdentity userIdentity, byte[] password, boolean errO
462477
}
463478
Password oldPassword = user.getPassword();
464479
byte[] carried;
465-
if (password == null || password.length == 0) {
466-
// an empty new password empties the secondary as well, even with
467-
// RETAIN CURRENT PASSWORD (MySQL semantics)
480+
if (discardSecondary || password == null || password.length == 0) {
481+
// a binding transition retires the secondary; an empty new password
482+
// empties it as well, even with RETAIN CURRENT PASSWORD (MySQL
483+
// semantics)
468484
carried = null;
469485
} else if (retainCurrent) {
470486
carried = oldPassword == null ? null : oldPassword.getPassword();
@@ -520,7 +536,12 @@ public void refreshAuthenticationBinding(UserIdentity identity) {
520536
for (User user : nameToUsers.get(identity.getQualifiedUser())) {
521537
if (user.isSetByDomainResolver() && identity.equals(user.getDomainUserIdentity())) {
522538
user.setAuthenticationBinding(domain.getAuthIntegration(), domain.getAuthIdentity());
523-
user.setPassword(domain.getPassword().getPassword());
539+
// both slots: a primary-only copy would stop authenticating the
540+
// retained password on every resolved-IP entry right after a
541+
// rotation with RETAIN CURRENT PASSWORD (and after an
542+
// ACCOUNT_UNLOCK or REQUIRE change), until the DomainResolver's
543+
// next periodic pass rebuilt them
544+
user.setPassword(Password.copyOf(domain.getPassword()));
524545
user.setUserIdentity(createResolvedUserIdentity(identity.getQualifiedUser(),
525546
user.getUserIdentity().getHost(), domain.getUserIdentity()));
526547
user.setDomainUserIdentity(domain.getUserIdentity());
@@ -593,8 +614,7 @@ public void addUserPrivEntriesByResolvedIPs(Map<String, Set<String>> resolvedIPs
593614
// the retained secondary password (dual password) on
594615
// every resolver refresh after a rotation with
595616
// RETAIN CURRENT PASSWORD
596-
Password ipPassword = new Password(domainPassword.getPassword());
597-
ipPassword.setSecondaryPassword(domainPassword.getSecondaryPassword());
617+
Password ipPassword = Password.copyOf(domainPassword);
598618
try {
599619
User resolved = createUserWithoutLock(userIdent, ipPassword,
600620
domainUser.getUserIdentity(), true, "");

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

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.doris.analysis.UserDesc;
2424
import org.apache.doris.analysis.UserIdentity;
2525
import org.apache.doris.authentication.AuthenticationIntegrationMgr;
26+
import org.apache.doris.authentication.AuthenticationIntegrationRuntime;
2627
import org.apache.doris.catalog.Env;
2728
import org.apache.doris.common.AnalysisException;
2829
import org.apache.doris.common.AuthenticationException;
@@ -55,6 +56,7 @@
5556
import org.mockito.Mockito;
5657

5758
import java.lang.reflect.Field;
59+
import java.util.Collections;
5860
import java.util.List;
5961
import java.util.Map;
6062

@@ -71,6 +73,8 @@ public class DualPasswordTest {
7173
private EditLog editLog = Mockito.mock(EditLog.class);
7274
private AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class);
7375
private InternalCatalog internalCatalog = Mockito.mock(InternalCatalog.class);
76+
private AuthenticationIntegrationMgr integrations = new AuthenticationIntegrationMgr();
77+
private AuthenticationIntegrationRuntime runtime = Mockito.mock(AuthenticationIntegrationRuntime.class);
7478
private MockedStatic<Env> mockedEnvStatic;
7579

7680
@BeforeEach
@@ -80,7 +84,11 @@ public void setUp() throws Exception {
8084
mockedEnvStatic.when(Env::getCurrentEnv).thenReturn(env);
8185
Mockito.when(env.getAuth()).thenReturn(auth);
8286
// createUser / alterUser take the authentication-integration read lock
83-
Mockito.when(env.getAuthenticationIntegrationMgr()).thenReturn(new AuthenticationIntegrationMgr());
87+
Mockito.when(env.getAuthenticationIntegrationMgr()).thenReturn(integrations);
88+
// binding an account normalizes its AS identity through the runtime
89+
Mockito.when(env.getAuthenticationIntegrationRuntime()).thenReturn(runtime);
90+
Mockito.when(runtime.normalizeBoundIdentity(Mockito.any(), Mockito.any()))
91+
.thenAnswer(invocation -> invocation.getArgument(1));
8492
Mockito.when(env.getEditLog()).thenReturn(editLog);
8593
Mockito.when(env.getAccessManager()).thenReturn(accessManager);
8694
// ConnectContext.setEnv reads the internal catalog name
@@ -125,6 +133,21 @@ private boolean canLogin(String user, String plainPassword) {
125133
}
126134
}
127135

136+
/** An authentication integration accounts can be bound to (IDENTIFIED WITH AUTHENTICATION INTEGRATION). */
137+
private void createIntegration(String name) throws DdlException {
138+
integrations.createAuthenticationIntegration(name, false,
139+
Collections.singletonMap("type", "dual_password_test"), "", "root");
140+
}
141+
142+
private void alterUser(ConnectContext ctx, String sql) throws Exception {
143+
AlterUserCommand cmd = (AlterUserCommand) new NereidsParser().parseSingle(sql);
144+
cmd.doRun(ctx, null);
145+
}
146+
147+
private User userOf(UserIdentity identity) {
148+
return auth.getUserManager().getUserByUserIdentity(identity);
149+
}
150+
128151
@Test
129152
public void testRetainEvictAndDiscard() throws DdlException {
130153
UserIdentity user = createUser("rot");
@@ -598,6 +621,126 @@ public void testShowGrantsPasswordColumnTellsRetainedApart() throws DdlException
598621
Assertions.assertEquals("Yes", passwordColumnOf(user));
599622
}
600623

624+
@Test
625+
public void testBindingTransitionDiscardsRetainedPassword() throws Exception {
626+
// MySQL discards the secondary password when the authentication
627+
// plugin changes. Here the bound-user guard refuses native logins only
628+
// WHILE the account is bound, so a password retained before the
629+
// binding would otherwise come back to life, beside the new one, when
630+
// the account returns to native authentication.
631+
createIntegration("bound");
632+
UserIdentity admin = createUser("adm4");
633+
UserIdentity user = createUser("rebound");
634+
auth.setPassword(user, MysqlPassword.makeScrambledPassword("p1"));
635+
auth.setPassword(user, MysqlPassword.makeScrambledPassword("p2"), true /* retain */);
636+
ConnectContext ctx = ctxFor(admin);
637+
grantPriv(true);
638+
Assertions.assertTrue(canLogin("rebound", "p1"));
639+
Assertions.assertEquals("Yes (dual)", passwordColumnOf(user));
640+
641+
// into the binding: the retained password is dropped, not parked
642+
alterUser(ctx, "ALTER USER 'rebound'@'%' IDENTIFIED WITH AUTHENTICATION INTEGRATION bound AS 'external'");
643+
Assertions.assertTrue(userOf(user).isAuthenticationBound());
644+
Assertions.assertFalse(userOf(user).getPassword().hasSecondaryPassword());
645+
Assertions.assertFalse(canLogin("rebound", "p1"));
646+
Assertions.assertFalse(canLogin("rebound", "p2"));
647+
648+
// out of the binding: only the new password authenticates
649+
alterUser(ctx, "ALTER USER 'rebound'@'%' IDENTIFIED BY 'p3'");
650+
Assertions.assertFalse(userOf(user).isAuthenticationBound());
651+
Assertions.assertTrue(canLogin("rebound", "p3"));
652+
Assertions.assertFalse(canLogin("rebound", "p1"));
653+
Assertions.assertFalse(canLogin("rebound", "p2"));
654+
Assertions.assertEquals("Yes", passwordColumnOf(user));
655+
}
656+
657+
@Test
658+
public void testRetainIsRefusedAcrossBindingTransitions() throws Exception {
659+
// the "current password" of a bound account is a generated placeholder
660+
// nobody holds: nothing to retain on the way out of a binding, nothing
661+
// worth keeping on the way in
662+
createIntegration("bound");
663+
UserIdentity admin = createUser("adm5");
664+
UserIdentity user = createUser("placeholder");
665+
auth.setPassword(user, MysqlPassword.makeScrambledPassword("p1"));
666+
ConnectContext ctx = ctxFor(admin);
667+
grantPriv(true);
668+
669+
// into a binding: RETAIN needs a password change (rejected at validation)
670+
Assertions.assertThrows(AnalysisException.class, () -> alterUser(ctx,
671+
"ALTER USER 'placeholder'@'%' IDENTIFIED WITH AUTHENTICATION INTEGRATION bound"
672+
+ " RETAIN CURRENT PASSWORD"));
673+
Assertions.assertFalse(userOf(user).isAuthenticationBound());
674+
Assertions.assertTrue(canLogin("placeholder", "p1"));
675+
676+
// out of a binding: the placeholder is not a credential to keep
677+
alterUser(ctx, "ALTER USER 'placeholder'@'%' IDENTIFIED WITH AUTHENTICATION INTEGRATION bound");
678+
Assertions.assertThrows(DdlException.class, () -> alterUser(ctx,
679+
"ALTER USER 'placeholder'@'%' IDENTIFIED BY 'p2' RETAIN CURRENT PASSWORD"));
680+
Assertions.assertTrue(userOf(user).isAuthenticationBound());
681+
Assertions.assertFalse(canLogin("placeholder", "p2"));
682+
Assertions.assertFalse(canLogin("placeholder", "p1"));
683+
}
684+
685+
@Test
686+
public void testBindingTransitionReplayDiscardsRetainedPassword() throws Exception {
687+
// a follower replays the bind and the unbind from their journals and
688+
// must reach the same single-password state as the master
689+
UserIdentity user = createUser("rebound_replay");
690+
auth.setPassword(user, MysqlPassword.makeScrambledPassword("p1"));
691+
auth.replaySetPassword(new PrivInfo(user, null,
692+
MysqlPassword.makeScrambledPassword("p2"), null, null, true /* retain */));
693+
Assertions.assertTrue(canLogin("rebound_replay", "p1"));
694+
695+
// the bind journals the generated placeholder plus the binding
696+
PrivInfo bind = new PrivInfo(user, null, MysqlPassword.makeScrambledPassword("placeholder"), null, null);
697+
bind.setAuthenticationBinding("bound", "external");
698+
auth.replaySetPassword(bind);
699+
Assertions.assertTrue(userOf(user).isAuthenticationBound());
700+
Assertions.assertFalse(userOf(user).getPassword().hasSecondaryPassword());
701+
702+
// the unbind journals a plain password change without a binding
703+
auth.replaySetPassword(new PrivInfo(user, null, MysqlPassword.makeScrambledPassword("p3"), null, null));
704+
Assertions.assertFalse(userOf(user).isAuthenticationBound());
705+
Assertions.assertTrue(canLogin("rebound_replay", "p3"));
706+
Assertions.assertFalse(canLogin("rebound_replay", "p1"));
707+
Assertions.assertFalse(canLogin("rebound_replay", "p2"));
708+
}
709+
710+
@Test
711+
public void testDomainRetainAuthenticatesBothSlotsBeforeTheResolverTick() throws Exception {
712+
// the resolved-IP entries of a domain account are rebuilt at once by
713+
// UserManager.refreshAuthenticationBinding, long before the
714+
// DomainResolver's periodic pass: that copy must carry both slots too,
715+
// or the retained password is refused for up to a resolver tick right
716+
// after the rotation (and again after an ACCOUNT_UNLOCK or REQUIRE
717+
// change)
718+
UserIdentity admin = createUser("adm6");
719+
UserIdentity domainIdent = new UserIdentity("domuser2", "mydomain.example", true);
720+
domainIdent.setIsAnalyzed();
721+
auth.createUser(new CreateUserCommand(new CreateUserInfo(new UserDesc(domainIdent))).getInfo());
722+
auth.setPassword(domainIdent, MysqlPassword.makeScrambledPassword("p1"));
723+
auth.refreshUserPrivEntriesByResovledIPs(
724+
ImmutableMap.of("mydomain.example", Sets.newHashSet("192.168.1.1")));
725+
Assertions.assertTrue(canLogin("domuser2", "p1"));
726+
727+
// rotate with RETAIN and check at once: no periodic refresh in between
728+
auth.setPasswordInternal(domainIdent, MysqlPassword.makeScrambledPassword("p2"), null,
729+
true, false, true /* retain */, false);
730+
Assertions.assertTrue(canLogin("domuser2", "p2"));
731+
Assertions.assertTrue(canLogin("domuser2", "p1"));
732+
733+
// the same immediate refresh runs on ACCOUNT_UNLOCK and REQUIRE changes
734+
ConnectContext ctx = ctxFor(admin);
735+
grantPriv(true);
736+
alterUser(ctx, "ALTER USER 'domuser2'@('mydomain.example') ACCOUNT_UNLOCK");
737+
Assertions.assertTrue(canLogin("domuser2", "p1"));
738+
alterUser(ctx, "ALTER USER 'domuser2'@('mydomain.example') REQUIRE NONE");
739+
Assertions.assertTrue(canLogin("domuser2", "p2"));
740+
Assertions.assertTrue(canLogin("domuser2", "p1"));
741+
Assertions.assertFalse(canLogin("domuser2", "p0"));
742+
}
743+
601744
private String passwordColumnOf(UserIdentity user) {
602745
List<List<String>> rows = auth.getAuthInfo(user);
603746
Assertions.assertEquals(1, rows.size());

0 commit comments

Comments
 (0)