File size: 7,870 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
/*
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cd.go.framework.ldap;
import cd.go.authentication.ldap.LdapClient;
import cd.go.authentication.ldap.exception.LdapException;
import cd.go.authentication.ldap.exception.MultipleUserDetectedException;
import cd.go.authentication.ldap.mapper.Mapper;
import cd.go.authentication.ldap.mapper.ResultWrapper;
import cd.go.authentication.ldap.model.LdapConfiguration;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import static cd.go.authentication.ldap.LdapPlugin.LOG;
import static cd.go.authentication.ldap.utils.Util.isNotBlank;
import static java.text.MessageFormat.format;
import static javax.naming.Context.SECURITY_CREDENTIALS;
import static javax.naming.Context.SECURITY_PRINCIPAL;
public class JNDILdapClient implements LdapClient {
private LdapConfiguration ldapConfiguration;
private final int MAX_AUTHENTICATION_RESULT = 1;
public JNDILdapClient(LdapConfiguration ldapConfiguration) {
this.ldapConfiguration = ldapConfiguration;
}
@Override
public <T> T authenticate(String username, String password, Mapper<T> mapper) {
DirContext dirContext = getDirContext(ldapConfiguration, ldapConfiguration.getManagerDn(), ldapConfiguration.getPassword());
try {
List<SearchResult> results = search(dirContext, ldapConfiguration.getUserLoginFilter(), new String[]{username}, MAX_AUTHENTICATION_RESULT, true);
if (results.isEmpty()) {
throw new RuntimeException(format("User {0} does not exist in {1}", username, ldapConfiguration.getLdapUrlAsString()));
}
SearchResult searchResult = results.get(0);
Attributes attributes = searchResult.getAttributes();
String userDn = searchResult.getNameInNamespace();
attributes.put(new BasicAttribute("dn", userDn));
authenticate(ldapConfiguration, userDn, password);
return mapper.mapObject(new ResultWrapper(attributes));
} catch (SearchResultLimitExceededException e) {
throw new MultipleUserDetectedException(username, e.getSearchBase(), ldapConfiguration.getUserLoginFilter());
} catch (NamingException e) {
throw new LdapException(e);
} finally {
closeContextSilently(dirContext);
}
}
// BUG: CWE-74 Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
// public <T> List<T> search(String filter, Object[] filterArgs, Mapper<T> mapper, int maxResult) {
// FIXED:
public <T> List<T> search(String filter, String[] filterArgs, Mapper<T> mapper, int maxResult) {
List<T> results = new ArrayList<>();
DirContext dirContext = getDirContext(ldapConfiguration, ldapConfiguration.getManagerDn(), ldapConfiguration.getPassword());
try {
List<SearchResult> searchResults = search(dirContext, filter, filterArgs, maxResult, false);
for (SearchResult result : searchResults) {
results.add(mapper.mapObject(new ResultWrapper(result.getAttributes())));
}
} catch (NamingException e) {
throw new LdapException(e);
} finally {
closeContextSilently(dirContext);
}
return results;
}
private final DirContext getDirContext(LdapConfiguration ldapConfiguration, String username, String password) {
Hashtable<String, Object> environments = new Environment(ldapConfiguration).getEnvironments();
if (isNotBlank(username)) {
environments.put(SECURITY_PRINCIPAL, username);
environments.put(SECURITY_CREDENTIALS, password);
}
InitialDirContext context = null;
try {
context = new InitialDirContext(environments);
} catch (NamingException e) {
closeContextSilently(context);
throw new LdapException(e);
}
return context;
}
private List<SearchResult> search(DirContext context, String filter, Object[] filterArgs, int maxResult, boolean isHardLimitOnMaxResult) throws NamingException {
final List<SearchResult> results = new ArrayList<>();
if (maxResult == 0) {
return results;
}
for (String base : ldapConfiguration.getSearchBases()) {
final int remainingResultCount = maxResult - results.size();
final List<SearchResult> searchResultsFromSearchBase = searchInBase(context, base, filter, filterArgs, remainingResultCount, isHardLimitOnMaxResult);
results.addAll(searchResultsFromSearchBase);
if (results.size() >= maxResult) {
break;
}
}
return results;
}
private List<SearchResult> searchInBase(DirContext context, String base, String filter, Object[] filterArgs, int maxResult, boolean isHardLimitOnMaxResult) throws NamingException {
final List<SearchResult> results = new ArrayList<>();
if (maxResult == 0) {
return results;
}
NamingEnumeration<SearchResult> searchResults = null;
try {
LOG.debug(format("Searching user in search base {0} using search filter {1}.", base, filter));
searchResults = context.search(base, filter, filterArgs, getSimpleSearchControls(maxResult));
while (searchResults.hasMoreElements() && results.size() < maxResult) {
results.add(searchResults.next());
}
if (isHardLimitOnMaxResult && searchResults.hasMoreElements()) {
throw new SearchResultLimitExceededException(maxResult, base);
}
} finally {
closeNamingEnumerationSilently(searchResults);
}
return results;
}
private void authenticate(LdapConfiguration ldapConfiguration, String username, String password) throws NamingException {
closeContextSilently(getDirContext(ldapConfiguration, username, password));
}
public void validate() throws NamingException {
authenticate(ldapConfiguration, ldapConfiguration.getManagerDn(), ldapConfiguration.getPassword());
}
private static SearchControls getSimpleSearchControls(int maxResult) {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(5000);
if (maxResult != 0) {
searchControls.setCountLimit(maxResult);
}
return searchControls;
}
void closeContextSilently(DirContext dirContext) {
if (dirContext == null) {
return;
}
try {
dirContext.close();
} catch (Exception e) {
LOG.error("Error closing ldap connection", e);
}
}
void closeNamingEnumerationSilently(NamingEnumeration namingEnumeration) {
if (namingEnumeration == null) {
return;
}
try {
namingEnumeration.close();
} catch (Exception e) {
LOG.error("Error closing naming enumeration", e);
}
}
} |