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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.internal.MapSettings;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.when;
  30. import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord;
  31. public class LdapSettingsManagerTest {
  32. @Rule
  33. public ExpectedException thrown = ExpectedException.none();
  34. @Test
  35. public void shouldFailWhenNoLdapUrl() {
  36. MapSettings settings = generateMultipleLdapSettingsWithUserAndGroupMapping();
  37. settings.removeProperty("ldap.example.url");
  38. LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery());
  39. thrown.expect(LdapException.class);
  40. thrown.expectMessage("The property 'ldap.example.url' property is empty while it is mandatory.");
  41. settingsManager.getContextFactories();
  42. }
  43. @Test
  44. public void shouldFailWhenMixingSingleAndMultipleConfiguration() {
  45. MapSettings settings = generateMultipleLdapSettingsWithUserAndGroupMapping();
  46. settings.setProperty("ldap.url", "ldap://foo");
  47. LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery());
  48. thrown.expect(LdapException.class);
  49. thrown
  50. .expectMessage(
  51. "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', ...");
  52. settingsManager.getContextFactories();
  53. }
  54. @Test
  55. public void testContextFactoriesWithSingleLdap() {
  56. LdapSettingsManager settingsManager = new LdapSettingsManager(
  57. generateSingleLdapSettingsWithUserAndGroupMapping().asConfig(), new LdapAutodiscovery());
  58. assertThat(settingsManager.getContextFactories().size()).isEqualTo(1);
  59. }
  60. /**
  61. * Test there are 2 @link{org.sonar.plugins.ldap.LdapContextFactory}s found.
  62. *
  63. */
  64. @Test
  65. public void testContextFactoriesWithMultipleLdap() {
  66. LdapSettingsManager settingsManager = new LdapSettingsManager(
  67. generateMultipleLdapSettingsWithUserAndGroupMapping().asConfig(), new LdapAutodiscovery());
  68. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  69. // We do it twice to make sure the settings keep the same.
  70. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  71. }
  72. @Test
  73. public void testAutodiscover() {
  74. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  75. LdapSrvRecord ldap1 = new LdapSrvRecord("ldap://localhost:189", 1, 1);
  76. LdapSrvRecord ldap2 = new LdapSrvRecord("ldap://localhost:1899", 1, 1);
  77. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Arrays.asList(ldap1, ldap2));
  78. LdapSettingsManager settingsManager = new LdapSettingsManager(
  79. generateAutodiscoverSettings().asConfig(), ldapAutodiscovery);
  80. assertThat(settingsManager.getContextFactories().size()).isEqualTo(2);
  81. }
  82. @Test
  83. public void testAutodiscoverFailed() {
  84. LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class);
  85. when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Collections.emptyList());
  86. LdapSettingsManager settingsManager = new LdapSettingsManager(
  87. generateAutodiscoverSettings().asConfig(), ldapAutodiscovery);
  88. thrown.expect(LdapException.class);
  89. thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server.");
  90. settingsManager.getContextFactories();
  91. }
  92. /**
  93. * Test there are 2 @link{org.sonar.plugins.ldap.LdapUserMapping}s found.
  94. *
  95. */
  96. @Test
  97. public void testUserMappings() {
  98. LdapSettingsManager settingsManager = new LdapSettingsManager(
  99. generateMultipleLdapSettingsWithUserAndGroupMapping().asConfig(), new LdapAutodiscovery());
  100. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  101. // We do it twice to make sure the settings keep the same.
  102. assertThat(settingsManager.getUserMappings().size()).isEqualTo(2);
  103. }
  104. /**
  105. * Test there are 2 @link{org.sonar.plugins.ldap.LdapGroupMapping}s found.
  106. *
  107. */
  108. @Test
  109. public void testGroupMappings() {
  110. LdapSettingsManager settingsManager = new LdapSettingsManager(
  111. generateMultipleLdapSettingsWithUserAndGroupMapping().asConfig(), new LdapAutodiscovery());
  112. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  113. // We do it twice to make sure the settings keep the same.
  114. assertThat(settingsManager.getGroupMappings().size()).isEqualTo(2);
  115. }
  116. /**
  117. * Test what happens when no configuration is set.
  118. * Normally there will be a contextFactory, but the autodiscovery doesn't work for the test server.
  119. */
  120. @Test
  121. public void testEmptySettings() {
  122. LdapSettingsManager settingsManager = new LdapSettingsManager(
  123. new MapSettings().asConfig(), new LdapAutodiscovery());
  124. thrown.expect(LdapException.class);
  125. thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery.");
  126. settingsManager.getContextFactories();
  127. }
  128. private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() {
  129. MapSettings settings = new MapSettings();
  130. settings.setProperty("ldap.servers", "example,infosupport");
  131. settings.setProperty("ldap.example.url", "/users.example.org.ldif")
  132. .setProperty("ldap.example.user.baseDn", "ou=users,dc=example,dc=org")
  133. .setProperty("ldap.example.group.baseDn", "ou=groups,dc=example,dc=org")
  134. .setProperty("ldap.example.group.request",
  135. "(&(objectClass=posixGroup)(memberUid={uid}))");
  136. settings.setProperty("ldap.infosupport.url", "/users.infosupport.com.ldif")
  137. .setProperty("ldap.infosupport.user.baseDn",
  138. "ou=users,dc=infosupport,dc=com")
  139. .setProperty("ldap.infosupport.group.baseDn",
  140. "ou=groups,dc=infosupport,dc=com")
  141. .setProperty("ldap.infosupport.group.request",
  142. "(&(objectClass=posixGroup)(memberUid={uid}))");
  143. return settings;
  144. }
  145. private MapSettings generateSingleLdapSettingsWithUserAndGroupMapping() {
  146. MapSettings settings = new MapSettings();
  147. settings.setProperty("ldap.url", "/users.example.org.ldif")
  148. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  149. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  150. .setProperty("ldap.group.request",
  151. "(&(objectClass=posixGroup)(memberUid={uid}))");
  152. return settings;
  153. }
  154. private MapSettings generateAutodiscoverSettings() {
  155. MapSettings settings = new MapSettings();
  156. settings.setProperty("ldap.realm", "example.org")
  157. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  158. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  159. .setProperty("ldap.group.request",
  160. "(&(objectClass=posixGroup)(memberUid={uid}))");
  161. return settings;
  162. }
  163. }