1717
1818package org .apache .doris .mysql .authenticate .ldap ;
1919
20+ import org .apache .doris .catalog .Env ;
2021import org .apache .doris .common .Config ;
22+ import org .apache .doris .common .LdapConfig ;
23+ import org .apache .doris .common .jmockit .Deencapsulation ;
2124import org .apache .doris .mysql .authenticate .TestLogAppender ;
25+ import org .apache .doris .mysql .privilege .Auth ;
26+ import org .apache .doris .mysql .privilege .Role ;
2227
23- import mockit .Expectations ;
24- import mockit .Mocked ;
2528import org .apache .logging .log4j .Level ;
2629import org .junit .Assert ;
2730import org .junit .Before ;
2831import org .junit .Test ;
32+ import org .mockito .MockedStatic ;
33+ import org .mockito .Mockito ;
2934
3035import java .util .ArrayList ;
36+ import java .util .Arrays ;
3137
3238public class LdapManagerTest {
3339
3440 private static final String USER1 = "user1" ;
3541 private static final String USER2 = "user2" ;
42+ private static final String LDAP_GROUP_ROLE = "ldap_group_role" ;
43+ private static final String LDAP_DEFAULT_ROLE = "ldap_default_role" ;
44+ private static final String MISSING_LDAP_DEFAULT_ROLE = "missing_ldap_default_role" ;
3645
37- @ Mocked
38- private LdapClient ldapClient ;
46+ private LdapClient ldapClient = Mockito .mock (LdapClient .class );
3947
4048 @ Before
4149 public void setUp () {
4250 Config .authentication_type = "ldap" ;
51+ LdapConfig .ldap_default_roles = new String [0 ];
4352 }
4453
4554 private void mockClient (boolean userExist , boolean passwd ) {
46- new Expectations () {
47- {
48- ldapClient .doesUserExist (anyString );
49- minTimes = 0 ;
50- result = userExist ;
51-
52- ldapClient .checkPassword (anyString , anyString );
53- minTimes = 0 ;
54- result = passwd ;
55-
56- ldapClient .getGroups (anyString );
57- minTimes = 0 ;
58- result = new ArrayList <>();
59- }
60- };
55+ mockClient (userExist , passwd , new ArrayList <>());
56+ }
57+
58+ private void mockClient (boolean userExist , boolean passwd , ArrayList <String > groups ) {
59+ Mockito .when (ldapClient .doesUserExist (Mockito .anyString ())).thenReturn (userExist );
60+ Mockito .when (ldapClient .checkPassword (Mockito .anyString (), Mockito .anyString ())).thenReturn (passwd );
61+ Mockito .when (ldapClient .getGroups (Mockito .anyString ())).thenReturn (groups );
62+ }
63+
64+ private void mockAuth (MockedStatic <Env > envMockedStatic , Role ldapGroupRole , Role ldapDefaultRole ) {
65+ mockAuth (envMockedStatic , ldapGroupRole , ldapDefaultRole , true );
66+ }
67+
68+ private void mockAuth (MockedStatic <Env > envMockedStatic , Role ldapGroupRole , Role ldapDefaultRole ,
69+ boolean ldapGroupRoleExists ) {
70+ Env env = Mockito .mock (Env .class );
71+ Auth auth = Mockito .mock (Auth .class );
72+ envMockedStatic .when (Env ::getCurrentEnv ).thenReturn (env );
73+ Mockito .when (env .getAuth ()).thenReturn (auth );
74+ Mockito .when (auth .doesRoleExist (LDAP_GROUP_ROLE )).thenReturn (ldapGroupRoleExists );
75+ if (ldapGroupRoleExists ) {
76+ Mockito .when (auth .getRoleByName (LDAP_GROUP_ROLE )).thenReturn (ldapGroupRole );
77+ }
78+ Mockito .when (auth .doesRoleExist (LDAP_DEFAULT_ROLE )).thenReturn (true );
79+ Mockito .when (auth .getRoleByName (LDAP_DEFAULT_ROLE )).thenReturn (ldapDefaultRole );
80+ Mockito .when (auth .doesRoleExist (MISSING_LDAP_DEFAULT_ROLE )).thenReturn (false );
6181 }
6282
6383 @ Test
6484 public void testGetUserInfo () {
6585 LdapManager ldapManager = new LdapManager ();
86+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
6687 mockClient (true , true );
6788 LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
6889 Assert .assertNotNull (ldapUserInfo );
@@ -77,6 +98,7 @@ public void testGetUserInfo() {
7798 @ Test
7899 public void testCheckUserPasswd () {
79100 LdapManager ldapManager = new LdapManager ();
101+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
80102 mockClient (true , true );
81103 Assert .assertTrue (ldapManager .checkUserPasswd (USER1 , "123" ));
82104 LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
@@ -91,6 +113,7 @@ public void testCheckUserPasswd() {
91113 @ Test
92114 public void testCheckUserPasswdCachedPasswdMatchLogsInfoWithoutThreshold () {
93115 LdapManager ldapManager = new LdapManager ();
116+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
94117 mockClient (true , true );
95118 Assert .assertTrue (ldapManager .checkUserPasswd (USER1 , "123" ));
96119
@@ -106,6 +129,7 @@ public void testCheckUserPasswdCachedPasswdMatchLogsInfoWithoutThreshold() {
106129 @ Test
107130 public void testGetUserInfoLogsInfoWithoutThreshold () {
108131 LdapManager ldapManager = new LdapManager ();
132+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
109133 mockClient (true , true );
110134
111135 try (TestLogAppender appender = TestLogAppender .attach (LdapManager .class )) {
@@ -116,4 +140,80 @@ public void testGetUserInfoLogsInfoWithoutThreshold() {
116140 "LdapManager.getUserInfo slow: user=user1" ));
117141 }
118142 }
143+
144+ @ Test
145+ public void testGetUserInfoWithLdapDefaultRolesWithoutLdapGroups () {
146+ LdapManager ldapManager = new LdapManager ();
147+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
148+ LdapConfig .ldap_default_roles = new String [] {LDAP_DEFAULT_ROLE , MISSING_LDAP_DEFAULT_ROLE };
149+ Role ldapGroupRole = new Role (LDAP_GROUP_ROLE );
150+ Role ldapDefaultRole = new Role (LDAP_DEFAULT_ROLE );
151+ mockClient (true , true , new ArrayList <>());
152+ try (MockedStatic <Env > envMockedStatic = Mockito .mockStatic (Env .class )) {
153+ mockAuth (envMockedStatic , ldapGroupRole , ldapDefaultRole );
154+
155+ LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
156+ Assert .assertNotNull (ldapUserInfo );
157+ Assert .assertFalse (ldapUserInfo .getRoles ().contains (ldapGroupRole ));
158+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapDefaultRole ));
159+ Assert .assertEquals (2 , ldapUserInfo .getRoles ().size ());
160+ }
161+ }
162+
163+ @ Test
164+ public void testGetUserInfoWithLdapDefaultRolesWhenLdapGroupRoleMissing () {
165+ LdapManager ldapManager = new LdapManager ();
166+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
167+ LdapConfig .ldap_default_roles = new String [] {LDAP_DEFAULT_ROLE , MISSING_LDAP_DEFAULT_ROLE };
168+ Role ldapGroupRole = new Role (LDAP_GROUP_ROLE );
169+ Role ldapDefaultRole = new Role (LDAP_DEFAULT_ROLE );
170+ mockClient (true , true , new ArrayList <>(Arrays .asList (LDAP_GROUP_ROLE )));
171+ try (MockedStatic <Env > envMockedStatic = Mockito .mockStatic (Env .class )) {
172+ mockAuth (envMockedStatic , ldapGroupRole , ldapDefaultRole , false );
173+
174+ LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
175+ Assert .assertNotNull (ldapUserInfo );
176+ Assert .assertFalse (ldapUserInfo .getRoles ().contains (ldapGroupRole ));
177+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapDefaultRole ));
178+ Assert .assertEquals (2 , ldapUserInfo .getRoles ().size ());
179+ }
180+ }
181+
182+ @ Test
183+ public void testGetUserInfoWithBlankLdapDefaultRoles () {
184+ LdapManager ldapManager = new LdapManager ();
185+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
186+ LdapConfig .ldap_default_roles = new String [] {null , "" , " " , LDAP_DEFAULT_ROLE };
187+ Role ldapGroupRole = new Role (LDAP_GROUP_ROLE );
188+ Role ldapDefaultRole = new Role (LDAP_DEFAULT_ROLE );
189+ mockClient (true , true , new ArrayList <>(Arrays .asList (LDAP_GROUP_ROLE )));
190+ try (MockedStatic <Env > envMockedStatic = Mockito .mockStatic (Env .class )) {
191+ mockAuth (envMockedStatic , ldapGroupRole , ldapDefaultRole );
192+
193+ LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
194+ Assert .assertNotNull (ldapUserInfo );
195+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapGroupRole ));
196+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapDefaultRole ));
197+ Assert .assertEquals (3 , ldapUserInfo .getRoles ().size ());
198+ }
199+ }
200+
201+ @ Test
202+ public void testGetUserInfoWithLdapDefaultRoles () {
203+ LdapManager ldapManager = new LdapManager ();
204+ Deencapsulation .setField (ldapManager , "ldapClient" , ldapClient );
205+ LdapConfig .ldap_default_roles = new String [] {LDAP_DEFAULT_ROLE , MISSING_LDAP_DEFAULT_ROLE };
206+ Role ldapGroupRole = new Role (LDAP_GROUP_ROLE );
207+ Role ldapDefaultRole = new Role (LDAP_DEFAULT_ROLE );
208+ mockClient (true , true , new ArrayList <>(Arrays .asList (LDAP_GROUP_ROLE )));
209+ try (MockedStatic <Env > envMockedStatic = Mockito .mockStatic (Env .class )) {
210+ mockAuth (envMockedStatic , ldapGroupRole , ldapDefaultRole );
211+
212+ LdapUserInfo ldapUserInfo = ldapManager .getUserInfo (USER1 );
213+ Assert .assertNotNull (ldapUserInfo );
214+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapGroupRole ));
215+ Assert .assertTrue (ldapUserInfo .getRoles ().contains (ldapDefaultRole ));
216+ Assert .assertEquals (3 , ldapUserInfo .getRoles ().size ());
217+ }
218+ }
119219}
0 commit comments