2323import org .apache .doris .analysis .UserDesc ;
2424import org .apache .doris .analysis .UserIdentity ;
2525import org .apache .doris .authentication .AuthenticationIntegrationMgr ;
26+ import org .apache .doris .authentication .AuthenticationIntegrationRuntime ;
2627import org .apache .doris .catalog .Env ;
2728import org .apache .doris .common .AnalysisException ;
2829import org .apache .doris .common .AuthenticationException ;
5556import org .mockito .Mockito ;
5657
5758import java .lang .reflect .Field ;
59+ import java .util .Collections ;
5860import java .util .List ;
5961import 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