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.

LdapGroupMapping.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 javax.naming.NamingException;
  23. import javax.naming.directory.Attribute;
  24. import javax.naming.directory.SearchResult;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.sonar.api.config.Configuration;
  27. /**
  28. * @author Evgeny Mandrikov
  29. */
  30. public class LdapGroupMapping {
  31. private static final String DEFAULT_ID_ATTRIBUTE = "cn";
  32. private static final String DEFAULT_REQUEST = "(&(objectClass=groupOfUniqueNames)(uniqueMember={dn}))";
  33. private final String baseDn;
  34. private final String idAttribute;
  35. private final String request;
  36. private final String[] requiredUserAttributes;
  37. /**
  38. * Constructs mapping from Sonar settings.
  39. */
  40. public LdapGroupMapping(Configuration config, String settingsPrefix) {
  41. this.baseDn = config.get(settingsPrefix + ".group.baseDn").orElse(null);
  42. this.idAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".group.idAttribute").orElse(null), DEFAULT_ID_ATTRIBUTE);
  43. String req = StringUtils.defaultString(config.get(settingsPrefix + ".group.request").orElse(null), DEFAULT_REQUEST);
  44. this.requiredUserAttributes = StringUtils.substringsBetween(req, "{", "}");
  45. for (int i = 0; i < requiredUserAttributes.length; i++) {
  46. req = StringUtils.replace(req, "{" + requiredUserAttributes[i] + "}", "{" + i + "}");
  47. }
  48. this.request = req;
  49. }
  50. /**
  51. * Search for this mapping.
  52. */
  53. public LdapSearch createSearch(LdapContextFactory contextFactory, SearchResult user) {
  54. String[] attrs = getRequiredUserAttributes();
  55. String[] parameters = new String[attrs.length];
  56. for (int i = 0; i < parameters.length; i++) {
  57. String attr = attrs[i];
  58. if ("dn".equals(attr)) {
  59. parameters[i] = user.getNameInNamespace();
  60. } else {
  61. parameters[i] = getAttributeValue(user, attr);
  62. }
  63. }
  64. return new LdapSearch(contextFactory)
  65. .setBaseDn(getBaseDn())
  66. .setRequest(getRequest())
  67. .setParameters(parameters)
  68. .returns(getIdAttribute());
  69. }
  70. private static String getAttributeValue(SearchResult user, String attributeId) {
  71. Attribute attribute = user.getAttributes().get(attributeId);
  72. if (attribute == null) {
  73. return null;
  74. }
  75. try {
  76. return (String) attribute.get();
  77. } catch (NamingException e) {
  78. throw new IllegalArgumentException(e);
  79. }
  80. }
  81. /**
  82. * Base DN. For example "ou=groups,o=mycompany".
  83. */
  84. public String getBaseDn() {
  85. return baseDn;
  86. }
  87. /**
  88. * Group ID Attribute. For example "cn".
  89. */
  90. public String getIdAttribute() {
  91. return idAttribute;
  92. }
  93. /**
  94. * Request. For example:
  95. * <pre>
  96. * (&(objectClass=groupOfUniqueNames)(uniqueMember={0}))
  97. * (&(objectClass=posixGroup)(memberUid={0}))
  98. * (&(|(objectClass=groupOfUniqueNames)(objectClass=posixGroup))(|(uniqueMember={0})(memberUid={1})))
  99. * </pre>
  100. */
  101. public String getRequest() {
  102. return request;
  103. }
  104. /**
  105. * Attributes of user required for search of groups.
  106. */
  107. public String[] getRequiredUserAttributes() {
  108. return requiredUserAttributes;
  109. }
  110. @Override
  111. public String toString() {
  112. return getClass().getSimpleName() + "{" +
  113. "baseDn=" + getBaseDn() +
  114. ", idAttribute=" + getIdAttribute() +
  115. ", requiredUserAttributes=" + Arrays.toString(getRequiredUserAttributes()) +
  116. ", request=" + getRequest() +
  117. "}";
  118. }
  119. }