|
17 | 17 |
|
18 | 18 | package org.apache.doris.mysql.authenticate.ldap; |
19 | 19 |
|
| 20 | +import org.apache.doris.catalog.Env; |
20 | 21 | import org.apache.doris.common.Config; |
| 22 | +import org.apache.doris.common.LdapConfig; |
21 | 23 | import org.apache.doris.common.jmockit.Deencapsulation; |
| 24 | +import org.apache.doris.mysql.privilege.Auth; |
| 25 | +import org.apache.doris.mysql.privilege.Role; |
22 | 26 |
|
23 | 27 | import org.junit.Assert; |
24 | 28 | import org.junit.Before; |
25 | 29 | import org.junit.Test; |
| 30 | +import org.mockito.MockedStatic; |
26 | 31 | import org.mockito.Mockito; |
27 | 32 |
|
28 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.Arrays; |
29 | 35 |
|
30 | 36 | public class LdapManagerTest { |
31 | 37 |
|
32 | 38 | private static final String USER1 = "user1"; |
33 | 39 | private static final String USER2 = "user2"; |
| 40 | + private static final String LDAP_GROUP_ROLE = "ldap_group_role"; |
| 41 | + private static final String LDAP_DEFAULT_ROLE = "ldap_default_role"; |
| 42 | + private static final String MISSING_LDAP_DEFAULT_ROLE = "missing_ldap_default_role"; |
34 | 43 |
|
35 | 44 | private LdapClient ldapClient = Mockito.mock(LdapClient.class); |
36 | 45 |
|
37 | 46 | @Before |
38 | 47 | public void setUp() { |
39 | 48 | Config.authentication_type = "ldap"; |
| 49 | + LdapConfig.ldap_default_roles = new String[0]; |
40 | 50 | } |
41 | 51 |
|
42 | 52 | private void mockClient(boolean userExist, boolean passwd) { |
| 53 | + mockClient(userExist, passwd, new ArrayList<>()); |
| 54 | + } |
| 55 | + |
| 56 | + private void mockClient(boolean userExist, boolean passwd, ArrayList<String> groups) { |
43 | 57 | Mockito.when(ldapClient.doesUserExist(Mockito.anyString())).thenReturn(userExist); |
44 | 58 | Mockito.when(ldapClient.checkPassword(Mockito.anyString(), Mockito.anyString())).thenReturn(passwd); |
45 | | - Mockito.when(ldapClient.getGroups(Mockito.anyString())).thenReturn(new ArrayList<>()); |
| 59 | + Mockito.when(ldapClient.getGroups(Mockito.anyString())).thenReturn(groups); |
| 60 | + } |
| 61 | + |
| 62 | + 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) { |
| 68 | + Env env = Mockito.mock(Env.class); |
| 69 | + Auth auth = Mockito.mock(Auth.class); |
| 70 | + envMockedStatic.when(Env::getCurrentEnv).thenReturn(env); |
| 71 | + Mockito.when(env.getAuth()).thenReturn(auth); |
| 72 | + Mockito.when(auth.doesRoleExist(LDAP_GROUP_ROLE)).thenReturn(ldapGroupRoleExists); |
| 73 | + if (ldapGroupRoleExists) { |
| 74 | + Mockito.when(auth.getRoleByName(LDAP_GROUP_ROLE)).thenReturn(ldapGroupRole); |
| 75 | + } |
| 76 | + Mockito.when(auth.doesRoleExist(LDAP_DEFAULT_ROLE)).thenReturn(true); |
| 77 | + Mockito.when(auth.getRoleByName(LDAP_DEFAULT_ROLE)).thenReturn(ldapDefaultRole); |
| 78 | + Mockito.when(auth.doesRoleExist(MISSING_LDAP_DEFAULT_ROLE)).thenReturn(false); |
46 | 79 | } |
47 | 80 |
|
48 | 81 | @Test |
@@ -74,4 +107,80 @@ public void testCheckUserPasswd() { |
74 | 107 | mockClient(true, false); |
75 | 108 | Assert.assertFalse(ldapManager.checkUserPasswd(USER2, "123")); |
76 | 109 | } |
| 110 | + |
| 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 | + |
| 168 | + @Test |
| 169 | + public void testGetUserInfoWithLdapDefaultRoles() { |
| 170 | + LdapManager ldapManager = new LdapManager(); |
| 171 | + Deencapsulation.setField(ldapManager, "ldapClient", ldapClient); |
| 172 | + LdapConfig.ldap_default_roles = new String[] {LDAP_DEFAULT_ROLE, MISSING_LDAP_DEFAULT_ROLE}; |
| 173 | + Role ldapGroupRole = new Role(LDAP_GROUP_ROLE); |
| 174 | + Role ldapDefaultRole = new Role(LDAP_DEFAULT_ROLE); |
| 175 | + mockClient(true, true, new ArrayList<>(Arrays.asList(LDAP_GROUP_ROLE))); |
| 176 | + try (MockedStatic<Env> envMockedStatic = Mockito.mockStatic(Env.class)) { |
| 177 | + mockAuth(envMockedStatic, ldapGroupRole, ldapDefaultRole); |
| 178 | + |
| 179 | + LdapUserInfo ldapUserInfo = ldapManager.getUserInfo(USER1); |
| 180 | + Assert.assertNotNull(ldapUserInfo); |
| 181 | + Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapGroupRole)); |
| 182 | + Assert.assertTrue(ldapUserInfo.getRoles().contains(ldapDefaultRole)); |
| 183 | + Assert.assertEquals(3, ldapUserInfo.getRoles().size()); |
| 184 | + } |
| 185 | + } |
77 | 186 | } |
0 commit comments