Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

LdapSettingsManagerTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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() {
  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. */
  65. @Test
  66. public void testContextFactoriesWithMultipleLdap() {
  67. LdapSettingsManager settingsManager = new LdapSettingsManager(
  68. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  69. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  70. // We do it twice to make sure the settings keep the same.
  71. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  72. }
  73. @Test
  74. public void testAutodiscover() {
  75. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  76. LdapSrvRecord ldap1 = new LdapSrvRecord("ldap://localhost:189", 1, 1);
  77. LdapSrvRecord ldap2 = new LdapSrvRecord("ldap://localhost:1899", 1, 1);
  78. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Arrays.asList(ldap1, ldap2));
  79. LdapSettingsManager settingsManager = new LdapSettingsManager(
  80. generateAutodiscoverSettings(), ldapAutodiscovery);
  81. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  82. }
  83. @Test
  84. public void testAutodiscoverFailed() {
  85. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  86. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Collections.emptyList());
  87. LdapSettingsManager settingsManager = new LdapSettingsManager(
  88. generateAutodiscoverSettings(), ldapAutodiscovery);
  89. thrown.expect(LdapException.class);
  90. thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server.");
  91. settingsManager.getContextFactories();
  92. }
  93. /**
  94. * Test there are 2 @link{org.sonar.plugins.ldap.LdapUserMapping}s found.
  95. *
  96. */
  97. @Test
  98. public void testUserMappings() {
  99. LdapSettingsManager settingsManager = new LdapSettingsManager(
  100. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  101. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  102. // We do it twice to make sure the settings keep the same.
  103. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  104. }
  105. /**
  106. * Test there are 2 @link{org.sonar.plugins.ldap.LdapGroupMapping}s found.
  107. *
  108. */
  109. @Test
  110. public void testGroupMappings() {
  111. LdapSettingsManager settingsManager = new LdapSettingsManager(
  112. generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery());
  113. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  114. // We do it twice to make sure the settings keep the same.
  115. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  116. }
  117. /**
  118. * Test what happens when no configuration is set.
  119. * Normally there will be a contextFactory, but the autodiscovery doesn't work for the test server.
  120. */
  121. @Test
  122. public void testEmptySettings() {
  123. LdapSettingsManager settingsManager = new LdapSettingsManager(
  124. new MapSettings(), new LdapAutodiscovery());
  125. thrown.expect(LdapException.class);
  126. thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery.");
  127. settingsManager.getContextFactories();
  128. }
  129. private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() {
  130. MapSettings settings = new MapSettings();
  131. settings.setProperty("ldap.servers", "example,infosupport");
  132. settings.setProperty("ldap.example.url", "/users.example.org.ldif")
  133. .setProperty("ldap.example.user.baseDn", "ou=users,dc=example,dc=org")
  134. .setProperty("ldap.example.group.baseDn", "ou=groups,dc=example,dc=org")
  135. .setProperty("ldap.example.group.request",
  136. "(&(objectClass=posixGroup)(memberUid={uid}))");
  137. settings.setProperty("ldap.infosupport.url", "/users.infosupport.com.ldif")
  138. .setProperty("ldap.infosupport.user.baseDn",
  139. "ou=users,dc=infosupport,dc=com")
  140. .setProperty("ldap.infosupport.group.baseDn",
  141. "ou=groups,dc=infosupport,dc=com")
  142. .setProperty("ldap.infosupport.group.request",
  143. "(&(objectClass=posixGroup)(memberUid={uid}))");
  144. return settings;
  145. }
  146. private MapSettings generateSingleLdapSettingsWithUserAndGroupMapping() {
  147. MapSettings settings = new MapSettings();
  148. settings.setProperty("ldap.url", "/users.example.org.ldif")
  149. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  150. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  151. .setProperty("ldap.group.request",
  152. "(&(objectClass=posixGroup)(memberUid={uid}))");
  153. return settings;
  154. }
  155. private MapSettings generateAutodiscoverSettings() {
  156. MapSettings settings = new MapSettings();
  157. settings.setProperty("ldap.realm", "example.org")
  158. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  159. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  160. .setProperty("ldap.group.request",
  161. "(&(objectClass=posixGroup)(memberUid={uid}))");
  162. return settings;
  163. }
  164. }