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

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