You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LdapSettingsManagerTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.ldap;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.sonar.api.config.Settings;
  27. import org.sonar.api.config.internal.MapSettings;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord;
  32. public class LdapSettingsManagerTest {
  33. @Rule
  34. public ExpectedException thrown = ExpectedException.none();
  35. @Test
  36. public void shouldFailWhenNoLdapUrl() {
  37. Settings settings = generateMultipleLdapSettingsWithUserAndGroupMapping();
  38. settings.removeProperty("ldap.example.url");
  39. LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery());
  40. thrown.expect(LdapException.class);
  41. thrown.expectMessage("The property 'ldap.example.url' property is empty while it is mandatory.");
  42. settingsManager.getContextFactories();
  43. }
  44. @Test
  45. public void shouldFailWhenMixingSingleAndMultipleConfiguration() {
  46. Settings settings = generateMultipleLdapSettingsWithUserAndGroupMapping();
  47. settings.setProperty("ldap.url", "ldap://foo");
  48. LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery());
  49. thrown.expect(LdapException.class);
  50. thrown
  51. .expectMessage(
  52. "When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ...");
  53. settingsManager.getContextFactories();
  54. }
  55. @Test
  56. public void testContextFactoriesWithSingleLdap() throws Exception {
  57. LdapSettingsManager settingsManager = new LdapSettingsManager(
  58. generateSingleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  59. assertThat(settingsManager.getContextFactories().size()).isEqualTo(1);
  60. }
  61. /**
  62. * Test there are 2 @link{org.sonar.plugins.ldap.LdapContextFactory}s found.
  63. *
  64. * @throws Exception
  65. * This is not expected.
  66. */
  67. @Test
  68. public void testContextFactoriesWithMultipleLdap() throws Exception {
  69. LdapSettingsManager settingsManager = new LdapSettingsManager(
  70. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  71. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  72. // We do it twice to make sure the settings keep the same.
  73. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  74. }
  75. @Test
  76. public void testAutodiscover() throws Exception {
  77. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  78. LdapSrvRecord ldap1 = new LdapSrvRecord("ldap://localhost:189", 1, 1);
  79. LdapSrvRecord ldap2 = new LdapSrvRecord("ldap://localhost:1899", 1, 1);
  80. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Arrays.asList(ldap1, ldap2));
  81. LdapSettingsManager settingsManager = new LdapSettingsManager(
  82. generateAutodiscoverSettings(), ldapAutodiscovery);
  83. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  84. }
  85. @Test
  86. public void testAutodiscoverFailed() throws Exception {
  87. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  88. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Collections.<LdapSrvRecord>emptyList());
  89. LdapSettingsManager settingsManager = new LdapSettingsManager(
  90. generateAutodiscoverSettings(), ldapAutodiscovery);
  91. thrown.expect(LdapException.class);
  92. thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server.");
  93. settingsManager.getContextFactories();
  94. }
  95. /**
  96. * Test there are 2 @link{org.sonar.plugins.ldap.LdapUserMapping}s found.
  97. *
  98. * @throws Exception
  99. * This is not expected.
  100. */
  101. @Test
  102. public void testUserMappings() throws Exception {
  103. LdapSettingsManager settingsManager = new LdapSettingsManager(
  104. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  105. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  106. // We do it twice to make sure the settings keep the same.
  107. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  108. }
  109. /**
  110. * Test there are 2 @link{org.sonar.plugins.ldap.LdapGroupMapping}s found.
  111. *
  112. * @throws Exception
  113. * This is not expected.
  114. */
  115. @Test
  116. public void testGroupMappings() throws Exception {
  117. LdapSettingsManager settingsManager = new LdapSettingsManager(
  118. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  119. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  120. // We do it twice to make sure the settings keep the same.
  121. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  122. }
  123. /**
  124. * Test what happens when no configuration is set.
  125. * Normally there will be a contextFactory, but the autodiscovery doesn't work for the test server.
  126. * @throws Exception
  127. */
  128. @Test
  129. public void testEmptySettings() throws Exception {
  130. LdapSettingsManager settingsManager = new LdapSettingsManager(
  131. new MapSettings(), new LdapAutodiscovery());
  132. thrown.expect(LdapException.class);
  133. thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery.");
  134. settingsManager.getContextFactories();
  135. }
  136. private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() {
  137. MapSettings settings = new MapSettings();
  138. settings.setProperty("ldap.servers", "example,infosupport");
  139. settings.setProperty("ldap.example.url", "/users.example.org.ldif")
  140. .setProperty("ldap.example.user.baseDn", "ou=users,dc=example,dc=org")
  141. .setProperty("ldap.example.group.baseDn", "ou=groups,dc=example,dc=org")
  142. .setProperty("ldap.example.group.request",
  143. "(&(objectClass=posixGroup)(memberUid={uid}))");
  144. settings.setProperty("ldap.infosupport.url", "/users.infosupport.com.ldif")
  145. .setProperty("ldap.infosupport.user.baseDn",
  146. "ou=users,dc=infosupport,dc=com")
  147. .setProperty("ldap.infosupport.group.baseDn",
  148. "ou=groups,dc=infosupport,dc=com")
  149. .setProperty("ldap.infosupport.group.request",
  150. "(&(objectClass=posixGroup)(memberUid={uid}))");
  151. return settings;
  152. }
  153. private MapSettings generateSingleLdapSettingsWithUserAndGroupMapping() {
  154. MapSettings settings = new MapSettings();
  155. settings.setProperty("ldap.url", "/users.example.org.ldif")
  156. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  157. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  158. .setProperty("ldap.group.request",
  159. "(&(objectClass=posixGroup)(memberUid={uid}))");
  160. return settings;
  161. }
  162. private MapSettings generateAutodiscoverSettings() {
  163. MapSettings settings = new MapSettings();
  164. settings.setProperty("ldap.realm", "example.org")
  165. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  166. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  167. .setProperty("ldap.group.request",
  168. "(&(objectClass=posixGroup)(memberUid={uid}))");
  169. return settings;
  170. }
  171. }