Skip to content

Commit c5017be

Browse files
committed
[test](fe) Add LDAP default role coverage
### What problem does this PR solve? Issue Number: N/A Related PR: N/A Problem Summary: Add FE unit coverage for LDAP default role behavior. The tests verify that blank LDAP default role entries are ignored while valid LDAP group and default roles are preserved, and that online updates of ldap_default_roles refresh LDAP user cache through Env.setMutableConfigWithCallback. ### Release note None ### Check List (For Author) - Test: Unit Test - Ran `env PATH=/private/tmp/doris-brew-shim:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin FE_UT_PARALLEL=1 JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home CUSTOM_MVN=/Users/zhanggen/.m2/wrapper/dists/apache-maven-3.9.5-bin/32db9c34/apache-maven-3.9.5/bin/mvn ./run-fe-ut.sh --coverage --run 'org.apache.doris.mysql.authenticate.ldap.LdapManagerTest,org.apache.doris.catalog.EnvTest'` - Behavior changed: No - Does this need documentation: No
1 parent ecff045 commit c5017be

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

fe/fe-core/src/test/java/org/apache/doris/catalog/EnvTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
package org.apache.doris.catalog;
1919

20+
import org.apache.doris.common.ConfigBase;
2021
import org.apache.doris.common.FeConstants;
22+
import org.apache.doris.common.LdapConfig;
2123
import org.apache.doris.common.io.CountingDataOutputStream;
2224
import org.apache.doris.meta.MetaContext;
25+
import org.apache.doris.mysql.authenticate.ldap.LdapManager;
26+
import org.apache.doris.mysql.privilege.Auth;
2327
import org.apache.doris.persist.meta.MetaHeader;
2428

2529
import org.junit.After;
@@ -36,6 +40,9 @@
3640
import java.io.FileOutputStream;
3741
import java.io.FileWriter;
3842
import java.io.IOException;
43+
import java.lang.reflect.Field;
44+
import java.util.HashMap;
45+
import java.util.Map;
3946
import java.util.Random;
4047

4148
public class EnvTest {
@@ -146,4 +153,34 @@ public void testSaveLoadHeader() throws Exception {
146153

147154
deleteDir(dir);
148155
}
156+
157+
@Test
158+
public void testSetLdapDefaultRolesConfigRefreshesLdapCache() throws Exception {
159+
Env env = Mockito.spy(new Env(false));
160+
Auth auth = Mockito.mock(Auth.class);
161+
LdapManager ldapManager = Mockito.mock(LdapManager.class);
162+
Mockito.doReturn(auth).when(env).getAuth();
163+
Mockito.when(auth.getLdapManager()).thenReturn(ldapManager);
164+
165+
Map<String, Field> oldConfFields = ConfigBase.confFields;
166+
Field oldLdapDefaultRolesField = ConfigBase.ldapConfFields.put("ldap_default_roles",
167+
LdapConfig.class.getField("ldap_default_roles"));
168+
String[] oldLdapDefaultRoles = LdapConfig.ldap_default_roles;
169+
try {
170+
ConfigBase.confFields = new HashMap<>();
171+
172+
env.setMutableConfigWithCallback("ldap_default_roles", "role1,role2");
173+
174+
Assert.assertArrayEquals(new String[] {"role1", "role2"}, LdapConfig.ldap_default_roles);
175+
Mockito.verify(ldapManager).refresh(true, null);
176+
} finally {
177+
ConfigBase.confFields = oldConfFields;
178+
if (oldLdapDefaultRolesField == null) {
179+
ConfigBase.ldapConfFields.remove("ldap_default_roles");
180+
} else {
181+
ConfigBase.ldapConfFields.put("ldap_default_roles", oldLdapDefaultRolesField);
182+
}
183+
LdapConfig.ldap_default_roles = oldLdapDefaultRoles;
184+
}
185+
}
149186
}

fe/fe-core/src/test/java/org/apache/doris/mysql/authenticate/ldap/LdapManagerTest.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ private void mockClient(boolean userExist, boolean passwd, ArrayList<String> gro
6060
}
6161

6262
private void mockAuth(MockedStatic<Env> envMockedStatic, Role ldapGroupRole, Role ldapDefaultRole) {
63+
mockAuth(envMockedStatic, ldapGroupRole, ldapDefaultRole, true);
64+
}
65+
66+
private void mockAuth(MockedStatic<Env> envMockedStatic, Role ldapGroupRole, Role ldapDefaultRole,
67+
boolean ldapGroupRoleExists) {
6368
Env env = Mockito.mock(Env.class);
6469
Auth auth = Mockito.mock(Auth.class);
6570
envMockedStatic.when(Env::getCurrentEnv).thenReturn(env);
6671
Mockito.when(env.getAuth()).thenReturn(auth);
67-
Mockito.when(auth.doesRoleExist(LDAP_GROUP_ROLE)).thenReturn(true);
68-
Mockito.when(auth.getRoleByName(LDAP_GROUP_ROLE)).thenReturn(ldapGroupRole);
72+
Mockito.when(auth.doesRoleExist(LDAP_GROUP_ROLE)).thenReturn(ldapGroupRoleExists);
73+
if (ldapGroupRoleExists) {
74+
Mockito.when(auth.getRoleByName(LDAP_GROUP_ROLE)).thenReturn(ldapGroupRole);
75+
}
6976
Mockito.when(auth.doesRoleExist(LDAP_DEFAULT_ROLE)).thenReturn(true);
7077
Mockito.when(auth.getRoleByName(LDAP_DEFAULT_ROLE)).thenReturn(ldapDefaultRole);
7178
Mockito.when(auth.doesRoleExist(MISSING_LDAP_DEFAULT_ROLE)).thenReturn(false);
@@ -101,6 +108,63 @@ public void testCheckUserPasswd() {
101108
Assert.assertFalse(ldapManager.checkUserPasswd(USER2, "123"));
102109
}
103110

111+
@Test
112+
public void testGetUserInfoWithLdapDefaultRolesWithoutLdapGroups() {
113+
LdapManager ldapManager = new LdapManager();
114+
Deencapsulation.setField(ldapManager, "ldapClient", ldapClient);
115+
LdapConfig.ldap_default_roles = new String[] {LDAP_DEFAULT_ROLE, MISSING_LDAP_DEFAULT_ROLE};
116+
Role ldapGroupRole = new Role(LDAP_GROUP_ROLE);
117+
Role ldapDefaultRole = new Role(LDAP_DEFAULT_ROLE);
118+
mockClient(true, true, new ArrayList<>());
119+
try (MockedStatic<Env> envMockedStatic = Mockito.mockStatic(Env.class)) {
120+
mockAuth(envMockedStatic, ldapGroupRole, ldapDefaultRole);
121+
122+
LdapUserInfo ldapUserInfo = ldapManager.getUserInfo(USER1);
123+
Assert.assertNotNull(ldapUserInfo);
124+
Assert.assertFalse(ldapUserInfo.getRoles().contains(ldapGroupRole));
125+
Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapDefaultRole));
126+
Assert.assertEquals(2, ldapUserInfo.getRoles().size());
127+
}
128+
}
129+
130+
@Test
131+
public void testGetUserInfoWithLdapDefaultRolesWhenLdapGroupRoleMissing() {
132+
LdapManager ldapManager = new LdapManager();
133+
Deencapsulation.setField(ldapManager, "ldapClient", ldapClient);
134+
LdapConfig.ldap_default_roles = new String[] {LDAP_DEFAULT_ROLE, MISSING_LDAP_DEFAULT_ROLE};
135+
Role ldapGroupRole = new Role(LDAP_GROUP_ROLE);
136+
Role ldapDefaultRole = new Role(LDAP_DEFAULT_ROLE);
137+
mockClient(true, true, new ArrayList<>(Arrays.asList(LDAP_GROUP_ROLE)));
138+
try (MockedStatic<Env> envMockedStatic = Mockito.mockStatic(Env.class)) {
139+
mockAuth(envMockedStatic, ldapGroupRole, ldapDefaultRole, false);
140+
141+
LdapUserInfo ldapUserInfo = ldapManager.getUserInfo(USER1);
142+
Assert.assertNotNull(ldapUserInfo);
143+
Assert.assertFalse(ldapUserInfo.getRoles().contains(ldapGroupRole));
144+
Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapDefaultRole));
145+
Assert.assertEquals(2, ldapUserInfo.getRoles().size());
146+
}
147+
}
148+
149+
@Test
150+
public void testGetUserInfoWithBlankLdapDefaultRoles() {
151+
LdapManager ldapManager = new LdapManager();
152+
Deencapsulation.setField(ldapManager, "ldapClient", ldapClient);
153+
LdapConfig.ldap_default_roles = new String[] {null, "", " ", LDAP_DEFAULT_ROLE};
154+
Role ldapGroupRole = new Role(LDAP_GROUP_ROLE);
155+
Role ldapDefaultRole = new Role(LDAP_DEFAULT_ROLE);
156+
mockClient(true, true, new ArrayList<>(Arrays.asList(LDAP_GROUP_ROLE)));
157+
try (MockedStatic<Env> envMockedStatic = Mockito.mockStatic(Env.class)) {
158+
mockAuth(envMockedStatic, ldapGroupRole, ldapDefaultRole);
159+
160+
LdapUserInfo ldapUserInfo = ldapManager.getUserInfo(USER1);
161+
Assert.assertNotNull(ldapUserInfo);
162+
Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapGroupRole));
163+
Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapDefaultRole));
164+
Assert.assertEquals(3, ldapUserInfo.getRoles().size());
165+
}
166+
}
167+
104168
@Test
105169
public void testGetUserInfoWithLdapDefaultRoles() {
106170
LdapManager ldapManager = new LdapManager();

0 commit comments

Comments
 (0)