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.

LdapSettingsManager.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.server.ServerSide;
  27. import org.sonar.api.utils.log.Logger;
  28. import org.sonar.api.utils.log.Loggers;
  29. import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord;
  30. /**
  31. * The LdapSettingsManager will parse the settings.
  32. * This class is also responsible to cope with multiple ldap servers.
  33. */
  34. @ServerSide
  35. public class LdapSettingsManager {
  36. private static final Logger LOG = Loggers.get(LdapSettingsManager.class);
  37. private static final String LDAP_SERVERS_PROPERTY = "ldap.servers";
  38. private static final String LDAP_PROPERTY_PREFIX = "ldap";
  39. private static final String DEFAULT_LDAP_SERVER_KEY = "<default>";
  40. private final Configuration config;
  41. private final LdapAutodiscovery ldapAutodiscovery;
  42. private Map<String, LdapUserMapping> userMappings = null;
  43. private Map<String, LdapGroupMapping> groupMappings = null;
  44. private Map<String, LdapContextFactory> contextFactories;
  45. /**
  46. * Create an instance of the settings manager.
  47. *
  48. * @param config The config to use.
  49. */
  50. public LdapSettingsManager(Configuration config, LdapAutodiscovery ldapAutodiscovery) {
  51. this.config = config;
  52. this.ldapAutodiscovery = ldapAutodiscovery;
  53. }
  54. /**
  55. * Get all the @link{LdapUserMapping}s available in the settings.
  56. *
  57. * @return A @link{Map} with all the @link{LdapUserMapping} objects.
  58. * The key is the server key used in the settings (ldap for old single server notation).
  59. */
  60. public Map<String, LdapUserMapping> getUserMappings() {
  61. if (userMappings == null) {
  62. // Use linked hash map to preserve order
  63. userMappings = new LinkedHashMap<>();
  64. String[] serverKeys = config.getStringArray(LDAP_SERVERS_PROPERTY);
  65. if (serverKeys.length > 0) {
  66. for (String serverKey : serverKeys) {
  67. LdapUserMapping userMapping = new LdapUserMapping(config, LDAP_PROPERTY_PREFIX + "." + serverKey);
  68. if (StringUtils.isNotBlank(userMapping.getBaseDn())) {
  69. LOG.info("User mapping for server {}: {}", serverKey, userMapping);
  70. userMappings.put(serverKey, userMapping);
  71. } else {
  72. LOG.info("Users will not be synchronized for server {}, because property 'ldap.{}.user.baseDn' is empty.", serverKey, serverKey);
  73. }
  74. }
  75. } else {
  76. // Backward compatibility with single server configuration
  77. LdapUserMapping userMapping = new LdapUserMapping(config, LDAP_PROPERTY_PREFIX);
  78. if (StringUtils.isNotBlank(userMapping.getBaseDn())) {
  79. LOG.info("User mapping: {}", userMapping);
  80. userMappings.put(DEFAULT_LDAP_SERVER_KEY, userMapping);
  81. } else {
  82. LOG.info("Users will not be synchronized, because property 'ldap.user.baseDn' is empty.");
  83. }
  84. }
  85. }
  86. return userMappings;
  87. }
  88. /**
  89. * Get all the @link{LdapGroupMapping}s available in the settings.
  90. *
  91. * @return A @link{Map} with all the @link{LdapGroupMapping} objects.
  92. * The key is the server key used in the settings (ldap for old single server notation).
  93. */
  94. public Map<String, LdapGroupMapping> getGroupMappings() {
  95. if (groupMappings == null) {
  96. // Use linked hash map to preserve order
  97. groupMappings = new LinkedHashMap<>();
  98. String[] serverKeys = config.getStringArray(LDAP_SERVERS_PROPERTY);
  99. if (serverKeys.length > 0) {
  100. for (String serverKey : serverKeys) {
  101. LdapGroupMapping groupMapping = new LdapGroupMapping(config, LDAP_PROPERTY_PREFIX + "." + serverKey);
  102. if (StringUtils.isNotBlank(groupMapping.getBaseDn())) {
  103. LOG.info("Group mapping for server {}: {}", serverKey, groupMapping);
  104. groupMappings.put(serverKey, groupMapping);
  105. } else {
  106. LOG.info("Groups will not be synchronized for server {}, because property 'ldap.{}.group.baseDn' is empty.", serverKey, serverKey);
  107. }
  108. }
  109. } else {
  110. // Backward compatibility with single server configuration
  111. LdapGroupMapping groupMapping = new LdapGroupMapping(config, LDAP_PROPERTY_PREFIX);
  112. if (StringUtils.isNotBlank(groupMapping.getBaseDn())) {
  113. LOG.info("Group mapping: {}", groupMapping);
  114. groupMappings.put(DEFAULT_LDAP_SERVER_KEY, groupMapping);
  115. } else {
  116. LOG.info("Groups will not be synchronized, because property 'ldap.group.baseDn' is empty.");
  117. }
  118. }
  119. }
  120. return groupMappings;
  121. }
  122. /**
  123. * Get all the @link{LdapContextFactory}s available in the settings.
  124. *
  125. * @return A @link{Map} with all the @link{LdapContextFactory} objects.
  126. * The key is the server key used in the settings (ldap for old single server notation).
  127. */
  128. public Map<String, LdapContextFactory> getContextFactories() {
  129. if (contextFactories == null) {
  130. // Use linked hash map to preserve order
  131. contextFactories = new LinkedHashMap<>();
  132. String[] serverKeys = config.getStringArray(LDAP_SERVERS_PROPERTY);
  133. if (serverKeys.length > 0) {
  134. initMultiLdapConfiguration(serverKeys);
  135. } else {
  136. initSimpleLdapConfiguration();
  137. }
  138. }
  139. return contextFactories;
  140. }
  141. private void initSimpleLdapConfiguration() {
  142. String realm = config.get(LDAP_PROPERTY_PREFIX + ".realm").orElse(null);
  143. String ldapUrlKey = LDAP_PROPERTY_PREFIX + ".url";
  144. String ldapUrl = config.get(ldapUrlKey).orElse(null);
  145. if (ldapUrl == null && realm != null) {
  146. LOG.warn("Auto-discovery feature is deprecated, please use '{}' to specify LDAP url", ldapUrlKey);
  147. List<LdapSrvRecord> ldapServers = ldapAutodiscovery.getLdapServers(realm);
  148. if (ldapServers.isEmpty()) {
  149. throw new LdapException(String.format("The property '%s' is empty and SonarQube is not able to auto-discover any LDAP server.", ldapUrlKey));
  150. }
  151. int index = 1;
  152. for (LdapSrvRecord ldapSrvRecord : ldapServers) {
  153. if (StringUtils.isNotBlank(ldapSrvRecord.getServerUrl())) {
  154. LOG.info("Detected server: {}", ldapSrvRecord.getServerUrl());
  155. LdapContextFactory contextFactory = new LdapContextFactory(config, LDAP_PROPERTY_PREFIX, ldapSrvRecord.getServerUrl());
  156. contextFactories.put(DEFAULT_LDAP_SERVER_KEY + index, contextFactory);
  157. index++;
  158. }
  159. }
  160. } else {
  161. if (StringUtils.isBlank(ldapUrl)) {
  162. throw new LdapException(String.format("The property '%s' is empty and no realm configured to try auto-discovery.", ldapUrlKey));
  163. }
  164. LdapContextFactory contextFactory = new LdapContextFactory(config, LDAP_PROPERTY_PREFIX, ldapUrl);
  165. contextFactories.put(DEFAULT_LDAP_SERVER_KEY, contextFactory);
  166. }
  167. }
  168. private void initMultiLdapConfiguration(String[] serverKeys) {
  169. if (config.hasKey("ldap.url") || config.hasKey("ldap.realm")) {
  170. throw new LdapException("When defining multiple LDAP servers with the property '" + LDAP_SERVERS_PROPERTY + "', "
  171. + "all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ...");
  172. }
  173. for (String serverKey : serverKeys) {
  174. String prefix = LDAP_PROPERTY_PREFIX + "." + serverKey;
  175. String ldapUrlKey = prefix + ".url";
  176. String ldapUrl = config.get(ldapUrlKey).orElse(null);
  177. if (StringUtils.isBlank(ldapUrl)) {
  178. throw new LdapException(String.format("The property '%s' property is empty while it is mandatory.", ldapUrlKey));
  179. }
  180. LdapContextFactory contextFactory = new LdapContextFactory(config, prefix, ldapUrl);
  181. contextFactories.put(serverKey, contextFactory);
  182. }
  183. }
  184. }