Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected Service createService(GatewayServices gatewayServices, ServiceType ser
if (shouldCreateService(implementation)) {
service = new KnoxLDAPService();
service.setAliasService(getAliasService(gatewayServices));
service.setGatewayServices(gatewayServices);
GatewayServer.registerConfigChangeListener(service);
logServiceUsage(service.getClass().getName(), serviceType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.ldap.control.RolesLookupBypassControlFactory;
import org.apache.knox.gateway.services.ldap.control.RolesLookupBypassControlImpl;
import org.apache.knox.gateway.services.ldap.interceptor.InterceptorFactory;
Expand All @@ -67,6 +68,7 @@ public class KnoxLDAPServerManager {
private static final LdapMessages LOG = MessagesFactory.get(LdapMessages.class);
private static final String LDAP_BIND_PASSWORD_ALIAS = "gateway_ldap_bind_password";
private final AliasService aliasService;
private final GatewayServices gatewayServices;

@VisibleForTesting
DirectoryService directoryService;
Expand All @@ -81,7 +83,12 @@ public class KnoxLDAPServerManager {
private Set<String> baseDns;

KnoxLDAPServerManager(AliasService aliasService) {
this(aliasService, null);
}

KnoxLDAPServerManager(AliasService aliasService, GatewayServices gatewayServices) {
this.aliasService = aliasService;
this.gatewayServices = gatewayServices;
}

/**
Expand Down Expand Up @@ -133,7 +140,7 @@ private void createInterceptors(GatewayConfig config) throws Exception {
}
}

interceptors.add(InterceptorFactory.createInterceptor(config, interceptorName, interceptorConfig));
interceptors.add(InterceptorFactory.createInterceptor(config, gatewayServices, interceptorName, interceptorConfig));
}
this.interceptors = interceptors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.config.GatewayConfigChangeListener;
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.Service;
import org.apache.knox.gateway.services.ServiceLifecycleException;
import org.apache.knox.gateway.services.security.AliasService;
Expand All @@ -36,6 +37,7 @@ public class KnoxLDAPService implements Service, GatewayConfigChangeListener {

KnoxLDAPServerManager ldapServerManager;
AliasService aliasService;
private GatewayServices gatewayServices;
private boolean enabled;

@Override
Expand All @@ -48,7 +50,7 @@ public void init(GatewayConfig config, Map<String, String> options) throws Servi

try {
// Initialize the LDAP server manager with configuration
ldapServerManager = new KnoxLDAPServerManager(aliasService);
ldapServerManager = new KnoxLDAPServerManager(aliasService, gatewayServices);
ldapServerManager.initialize(config);
} catch (Exception e) {
throw new ServiceLifecycleException("Failed to initialize LDAP service", e);
Expand All @@ -59,6 +61,10 @@ public void setAliasService(AliasService aliasService) {
this.aliasService = aliasService;
}

public void setGatewayServices(GatewayServices gatewayServices) {
this.gatewayServices = gatewayServices;
}

@Override
public void start() throws ServiceLifecycleException {
if (!enabled) {
Expand Down Expand Up @@ -95,7 +101,7 @@ public void onGatewayConfigChanged(GatewayConfig config) {
this.enabled = config.isLDAPEnabled();

if (this.enabled) {
this.ldapServerManager = this.ldapServerManager == null ? new KnoxLDAPServerManager(aliasService) : this.ldapServerManager;
this.ldapServerManager = this.ldapServerManager == null ? new KnoxLDAPServerManager(aliasService, gatewayServices) : this.ldapServerManager;
ldapServerManager.stop();
ldapServerManager.initialize(config);
ldapServerManager.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.GatewayServices;

import java.util.Map;

public class DisabledUserInterceptorFactory implements KnoxLdapInterceptorFactory {
public static final String TYPE = "disableduserfilter";

@Override
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> config) {
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> config) {
return new DisabledUserInterceptor(name, config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.GatewayServices;

import java.util.Map;

public class DuplicateUserFilteringInterceptorFactory implements KnoxLdapInterceptorFactory {
public static final String TYPE = "duplicateuserfilter";

@Override
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) {
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) {
return new DuplicateUserFilteringInterceptor(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.ldap.LdapMessages;

import java.util.Map;
Expand All @@ -33,7 +34,8 @@
public class InterceptorFactory {
private static final LdapMessages LOG = MessagesFactory.get(LdapMessages.class);

public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, final String interceptorName,
public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, final GatewayServices gatewayServices,
final String interceptorName,
final Map<String, String> interceptorConfig) throws Exception {
final String interceptorType = interceptorConfig.get("interceptorType");
if (interceptorType == null) {
Expand All @@ -49,7 +51,7 @@ public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, f
for (KnoxLdapInterceptorFactory interceptorFactory : loader) {
if (interceptorFactory.getType().equalsIgnoreCase(interceptorType)) {
LOG.ldapInterceptorCreating(interceptorType, "ServiceLoader");
return interceptorFactory.create(gatewayConfig, interceptorName, interceptorConfig);
return interceptorFactory.create(gatewayConfig, gatewayServices, interceptorName, interceptorConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.GatewayServices;

import java.util.Map;

Expand All @@ -27,13 +28,14 @@
*/
public interface KnoxLdapInterceptorFactory {
/**
* Instantiate and interceptor
* Instantiate an interceptor
* @param gatewayConfig the Knox Gateway configuration
* @param gatewayServices the active GatewayServices registry (may be null)
* @param name the name of the interceptor
* @param interceptorConfig the configuration for the interceptor
* @return the interceptor
*/
Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) throws Exception;
Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) throws Exception;

/**
* Get the type of interceptor this factory creates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class LDAPRolesLookupInterceptorFactory implements KnoxLdapInterceptorFac
private static final String TYPE = "rolesLookup";

@Override
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) throws Exception {
final LDAPRolesLookupService ldapRolesLookupService = getLDAPRolesLookupService();
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) throws Exception {
final LDAPRolesLookupService ldapRolesLookupService = getLDAPRolesLookupService(gatewayServices);
if (ldapRolesLookupService == null || !ldapRolesLookupService.enabled()) {
throw new ServiceLifecycleException("LDAP roles lookup service not found or disabled");
}
return new LDAPRolesLookupInterceptor(ldapRolesLookupService);
}

protected LDAPRolesLookupService getLDAPRolesLookupService() {
final GatewayServices gatewayServices = GatewayServer.getGatewayServices();
return gatewayServices.getService(ServiceType.LDAP_ROLES_LOOKUP_SERVICE);
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
final GatewayServices services = gatewayServices != null ? gatewayServices : GatewayServer.getGatewayServices();
return services == null ? null : services.getService(ServiceType.LDAP_ROLES_LOOKUP_SERVICE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.GatewayServices;

import java.util.Map;

public class UserSearchInterceptorFactory implements KnoxLdapInterceptorFactory {
public static final String TYPE = "backend";

@Override
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) throws Exception {
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) throws Exception {
return new UserSearchInterceptor(name, interceptorConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.directory.server.core.api.interceptor.Interceptor;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.ServiceLifecycleException;
import org.apache.knox.gateway.services.ldap.LDAPRolesLookupService;
import org.easymock.EasyMock;
Expand All @@ -38,15 +39,15 @@ public void testCreateWithEnabledService() throws Exception {

LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
@Override
protected LDAPRolesLookupService getLDAPRolesLookupService() {
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
return mockService;
}
};

GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
EasyMock.replay(mockConfig);

Interceptor interceptor = factory.create(mockConfig, "test", Collections.emptyMap());
Interceptor interceptor = factory.create(mockConfig, null, "test", Collections.emptyMap());
assertNotNull(interceptor);
assertTrue(interceptor instanceof LDAPRolesLookupInterceptor);
}
Expand All @@ -59,29 +60,29 @@ public void testCreateWithDisabledService() throws Exception {

LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
@Override
protected LDAPRolesLookupService getLDAPRolesLookupService() {
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
return mockService;
}
};

GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
EasyMock.replay(mockConfig);

factory.create(mockConfig, "test", Collections.emptyMap());
factory.create(mockConfig, null, "test", Collections.emptyMap());
}

@Test(expected = ServiceLifecycleException.class)
public void testCreateWithNullService() throws Exception {
LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
@Override
protected LDAPRolesLookupService getLDAPRolesLookupService() {
protected LDAPRolesLookupService getLDAPRolesLookupService(org.apache.knox.gateway.services.GatewayServices gatewayServices) {
return null;
}
};

GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
EasyMock.replay(mockConfig);

factory.create(mockConfig, "test", Collections.emptyMap());
factory.create(mockConfig, null, "test", Collections.emptyMap());
}
}
Loading