Skip to content
Snippets Groups Projects

feat: Integrate Apache LDAP API

5 files
+ 76
74
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -11,27 +11,25 @@
**********************************************************************/
package org.eclipsefoundation.profile.services.impl;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.eclipsefoundation.core.service.CachingService;
import org.eclipsefoundation.profile.config.LDAPConnectionConfig;
import org.eclipsefoundation.profile.models.LdapResult;
import org.eclipsefoundation.profile.namespace.ProfileAPIParameterNames;
import org.eclipsefoundation.profile.services.LDAPService;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;
@ApplicationScoped
public class DefaultLDAPService implements LDAPService {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLDAPService.class);
@@ -41,15 +39,39 @@ public class DefaultLDAPService implements LDAPService {
@Inject
LDAPConnectionConfig config;
@Inject
CachingService cache;
@Override
public LdapResult searchLdapByUsername(String efUsername) {
return searchLdap(Filter.createEqualityFilter(ProfileAPIParameterNames.UID.getName(), efUsername));
public Optional<LdapResult> searchLdapByUsername(String efUsername) {
LOGGER.debug("Searching LDAP for user: {}", efUsername);
String filter = "(uid=" + efUsername + ")";
Optional<LdapResult> result = cache.get(efUsername, new MultivaluedMapImpl<>(), LdapResult.class,
() -> searchLdap(filter));
if (result.isEmpty()) {
LOGGER.warn("LDAP - no user with name: {}", efUsername);
}
return result;
}
@Override
public LdapResult searchLdapByGhHandle(String ghHandle) {
return searchLdap(
Filter.createEqualityFilter(EMPLOYEE_TYPE, "GITHUB:" + ghHandle));
public Optional<LdapResult> searchLdapByGhHandle(String ghHandle) {
LOGGER.debug("Searching LDAP for GH handle: {}", ghHandle);
String filter = "(employeeType=GITHUB:" + ghHandle + ")";
Optional<LdapResult> result = cache.get(ghHandle, new MultivaluedMapImpl<>(), LdapResult.class,
() -> searchLdap(filter));
if (result.isEmpty()) {
LOGGER.debug("LDAP - no user with GH id: {}", ghHandle);
}
return result;
}
/**
@@ -57,40 +79,37 @@ public class DefaultLDAPService implements LDAPService {
* creates a request, and constructs an LdapResult entity from the entry if
* found. Returns an empty LdapResult object if no results were found or if
* there was a conneciton error.
*
*
* @param searchFilter The search filter used in the search
* @return
*/
private LdapResult searchLdap(Filter searchFilter) {
try (LDAPConnection connection = new LDAPConnection(config.host(), config.port())) {
private LdapResult searchLdap(String searchFilter) {
try (LdapConnection connection = new LdapNetworkConnection(config.host(), config.port(), true)) {
connection.bind();
LOGGER.debug("Successfully connected to Eclipse LDAP Server");
// Create a search request with base dn, scope, and filter
DN dn = new DN(config.baseDn());
SearchRequest request = new SearchRequest(dn, SearchScope.SUB, searchFilter);
// Perform search and get cursor with entry
EntryCursor cursor = connection.search(config.baseDn(), searchFilter, SearchScope.SUBTREE, "*");
LOGGER.debug("LDAP REQUEST: {}", request);
// Place cursor in front of 1st entry if it exists
if (cursor.next()) {
// Perform search and get entries
SearchResult result = connection.search(request);
List<SearchResultEntry> searchEntries = result.getSearchEntries();
Entry result = cursor.get();
LOGGER.debug("LDAP RESULT: {}", result);
if (searchEntries == null || searchEntries.isEmpty()) {
LOGGER.error("No results found using filter: {}", searchFilter);
return null;
return LdapResult.builder()
.setUid(result.get(ProfileAPIParameterNames.UID.getName()).get().getString())
.setMail(result.get(ProfileAPIParameterNames.MAIL.getName()).get().getString())
.setGithubId(isolateGhHandle(result.get(EMPLOYEE_TYPE).get().getString()))
.build();
}
// Only using equality filters on uid and employeeType. 1 result expected
SearchResultEntry entry = searchEntries.get(0);
LOGGER.debug("LDAP RESULT: {}", entry);
return LdapResult.builder()
.setUid(entry.getAttributeValue(ProfileAPIParameterNames.UID.getName()))
.setMail(entry.getAttributeValue(ProfileAPIParameterNames.MAIL.getName()))
.setGithubId(isolateGhHandle(entry.getAttributeValue(EMPLOYEE_TYPE)))
.build();
LOGGER.error("No results found using filter: {}", searchFilter);
return null;
} catch (LDAPException e) {
} catch (Exception e) {
LOGGER.error("Error performing user search on LDAP server", e);
return null;
}
Loading