Skip to content

Commit

Permalink
Merge pull request #119 from luan-cestari/LDAPIntegrationTest#116
Browse files Browse the repository at this point in the history
Fixes #116 - Ldap integration test
  • Loading branch information
jewzaam committed Mar 19, 2015
2 parents aed9cc7 + 8391878 commit efcbc52
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ crud-cert-auth/overlays
rhc-port-forward-status
etc/rhc-port-forward-status
*versionsBackup
auth/apacheds-work
9 changes: 9 additions & 0 deletions auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
<groupId>com.redhat.lightblue</groupId>
<artifactId>lightblue-core-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-integ</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import com.redhat.lightblue.rest.auth.LightblueRoleProvider;
import com.redhat.lightblue.rest.auth.ldap.LightblueLdapRoleProvider;
import org.slf4j.LoggerFactory;

/**
* @author dhaynes
Expand All @@ -48,7 +49,7 @@
*
*/
public class CertLdapLoginModule extends CertRolesLoginModule {
private final Logger LOGGER = Logger.getLogger(CertLdapLoginModule.class);
private final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CertLdapLoginModule.class);

public static final String AUTH_ROLE_NAME = "authRoleName";
public static final String LDAP_SERVER = "ldapServer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.jboss.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.naming.directory.SearchResult;
Expand All @@ -17,7 +18,7 @@
* Created by lcestari on 2/23/15.
*/
public class LDAPCache {
private static final Logger LOGGER = Logger.getLogger(LDAPCache.class);
private static final Logger LOGGER = LoggerFactory.getLogger(LDAPCache.class);
private static final Cache<LDAPCacheKey, SearchResult> ldapCacheSession; // non-persisted cache
private static final Cache<LDAPCacheKey, List<String>> userRolesCacheSession; // non-persisted cache

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redhat.lightblue.rest.auth.ldap;

import org.jboss.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
Expand All @@ -11,7 +12,7 @@
* Created by lcestari on 2/23/15.
*/
public class LDAPSearcher {
private static final Logger LOGGER = Logger.getLogger(LDAPSearcher.class);
private static final Logger LOGGER = LoggerFactory.getLogger(LDAPSearcher.class);

public static SearchResult searchLDAPServer(LDAPCacheKey ldapCacheKey) throws NamingException, LDAPUserNotFoundException, LDAPMultipleUserFoundException {
LOGGER.debug("LDAPSearcher#searchLDAPServer was invoked and it will call the remote LDAP server");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.redhat.lightblue.hystrix.ServoGraphiteSetup;
import org.jboss.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
Expand All @@ -20,7 +21,7 @@
public class LdapFindUserByUidCommand extends HystrixCommand<SearchResult> {
public static final String GROUPKEY = "ldap";
private static final String INVALID_PARAM = "%s is null or empty";
private static final Logger LOGGER = Logger.getLogger(LightblueLdapRoleProvider.class);
private static final Logger LOGGER = LoggerFactory.getLogger(LightblueLdapRoleProvider.class);

static {
LOGGER.debug("Invoking ServoGraphiteSetup#initialize on a static block");
Expand Down Expand Up @@ -94,7 +95,7 @@ protected SearchResult getFallback() {
Use the cache in case tje LDAP Server was not available and also to we have metrics around the fallback
*/
private static class FallbackViaLDAPServerProblemCommand extends HystrixCommand<SearchResult> {
private static final Logger LOGGER = Logger.getLogger(FallbackViaLDAPServerProblemCommand.class);
private static final Logger LOGGER = LoggerFactory.getLogger(FallbackViaLDAPServerProblemCommand.class);

private final LDAPCacheKey cacheKey;
private final Throwable failedExecutionThrowable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.redhat.lightblue.rest.auth.LightblueRoleProvider;
import org.jboss.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
Expand All @@ -31,13 +32,10 @@
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.*;

public class LightblueLdapRoleProvider implements LightblueRoleProvider {
private final Logger LOGGER = Logger.getLogger(LightblueLdapRoleProvider.class);
private final Logger LOGGER = LoggerFactory.getLogger(LightblueLdapRoleProvider.class);

LdapContext ldapContext;
String ldapSearchBase;
Expand All @@ -64,8 +62,12 @@ public List<String> getUserRoles(String userName) {
LOGGER.debug("Invoking LightblueLdapRoleProvider#getUserRoles");
List<String> userRoles = new ArrayList<>();
try {
userRoles.addAll(getUserRolesFromCache(userName));
List<String> userRolesFromCache = getUserRolesFromCache(userName);
if( userRolesFromCache != null && !userRolesFromCache.isEmpty() ) {
userRoles.addAll(userRolesFromCache);
}

// Not found on cache due it expired or it wasn't search for this user yet (assuming the user exist)
if (userRoles.isEmpty()) {
SearchResult searchResult = new LdapFindUserByUidCommand(ldapContext, ldapSearchBase, userName).execute();
userRoles.addAll(getUserRolesFromLdap(searchResult));
Expand Down Expand Up @@ -103,7 +105,8 @@ public void flushUserCache(String userName) {
private List<String> getUserRolesFromCache(String userName) {
LOGGER.debug("Invoking LightblueLdapRoleProvider#getUserRolesFromCache");
LDAPCacheKey cacheKey = new LDAPCacheKey(userName, ldapContext, ldapSearchBase, "(uid=" + userName + ")", SearchControls.SUBTREE_SCOPE);
return LDAPCache.getUserRolesCacheSession().getIfPresent(cacheKey);
List<String> ifPresent = LDAPCache.getUserRolesCacheSession().getIfPresent(cacheKey);
return ifPresent;
}

private List<String> getUserRolesFromLdap(SearchResult ldapUser) throws NamingException {
Expand Down
Loading

0 comments on commit efcbc52

Please sign in to comment.