diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/factory/LdapServiceFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/factory/LdapServiceFactory.java index fc5c57cc6f..e26cb57547 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/factory/LdapServiceFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/factory/LdapServiceFactory.java @@ -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); } diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java index 5d58b42e54..5d41d77105 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java @@ -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; @@ -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; @@ -81,7 +83,12 @@ public class KnoxLDAPServerManager { private Set baseDns; KnoxLDAPServerManager(AliasService aliasService) { + this(aliasService, null); + } + + KnoxLDAPServerManager(AliasService aliasService, GatewayServices gatewayServices) { this.aliasService = aliasService; + this.gatewayServices = gatewayServices; } /** @@ -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; } diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPService.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPService.java index b8a919c625..f1ccbb321f 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPService.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPService.java @@ -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; @@ -36,6 +37,7 @@ public class KnoxLDAPService implements Service, GatewayConfigChangeListener { KnoxLDAPServerManager ldapServerManager; AliasService aliasService; + private GatewayServices gatewayServices; private boolean enabled; @Override @@ -48,7 +50,7 @@ public void init(GatewayConfig config, Map 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); @@ -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) { @@ -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(); diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DisabledUserInterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DisabledUserInterceptorFactory.java index f26fbc20bb..17c58aa9b4 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DisabledUserInterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DisabledUserInterceptorFactory.java @@ -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; @@ -26,7 +27,7 @@ public class DisabledUserInterceptorFactory implements KnoxLdapInterceptorFactor public static final String TYPE = "disableduserfilter"; @Override - public Interceptor create(GatewayConfig gatewayConfig, String name, Map config) { + public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map config) { return new DisabledUserInterceptor(name, config); } diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DuplicateUserFilteringInterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DuplicateUserFilteringInterceptorFactory.java index 9af5c58cdd..6dc80e6b0d 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DuplicateUserFilteringInterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DuplicateUserFilteringInterceptorFactory.java @@ -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; @@ -26,7 +27,7 @@ public class DuplicateUserFilteringInterceptorFactory implements KnoxLdapInterce public static final String TYPE = "duplicateuserfilter"; @Override - public Interceptor create(GatewayConfig gatewayConfig, String name, Map interceptorConfig) { + public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map interceptorConfig) { return new DuplicateUserFilteringInterceptor(name); } diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/InterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/InterceptorFactory.java index 3adab3a5a0..e055f99542 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/InterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/InterceptorFactory.java @@ -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; @@ -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 interceptorConfig) throws Exception { final String interceptorType = interceptorConfig.get("interceptorType"); if (interceptorType == null) { @@ -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); } } diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/KnoxLdapInterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/KnoxLdapInterceptorFactory.java index 86964f32f0..0eaddbc8cf 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/KnoxLdapInterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/KnoxLdapInterceptorFactory.java @@ -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; @@ -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 interceptorConfig) throws Exception; + Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map interceptorConfig) throws Exception; /** * Get the type of interceptor this factory creates diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactory.java index 4ab8d903ed..90e84cffe9 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactory.java @@ -30,17 +30,17 @@ public class LDAPRolesLookupInterceptorFactory implements KnoxLdapInterceptorFac private static final String TYPE = "rolesLookup"; @Override - public Interceptor create(GatewayConfig gatewayConfig, String name, Map interceptorConfig) throws Exception { - final LDAPRolesLookupService ldapRolesLookupService = getLDAPRolesLookupService(); + public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map 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 diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/UserSearchInterceptorFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/UserSearchInterceptorFactory.java index f601dd3358..90f62789b3 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/UserSearchInterceptorFactory.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/UserSearchInterceptorFactory.java @@ -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; @@ -26,7 +27,7 @@ public class UserSearchInterceptorFactory implements KnoxLdapInterceptorFactory public static final String TYPE = "backend"; @Override - public Interceptor create(GatewayConfig gatewayConfig, String name, Map interceptorConfig) throws Exception { + public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map interceptorConfig) throws Exception { return new UserSearchInterceptor(name, interceptorConfig); } diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactoryTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactoryTest.java index ca28b7959d..24eae50be7 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactoryTest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactoryTest.java @@ -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; @@ -38,7 +39,7 @@ public void testCreateWithEnabledService() throws Exception { LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() { @Override - protected LDAPRolesLookupService getLDAPRolesLookupService() { + protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) { return mockService; } }; @@ -46,7 +47,7 @@ protected LDAPRolesLookupService getLDAPRolesLookupService() { 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); } @@ -59,7 +60,7 @@ public void testCreateWithDisabledService() throws Exception { LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() { @Override - protected LDAPRolesLookupService getLDAPRolesLookupService() { + protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) { return mockService; } }; @@ -67,14 +68,14 @@ protected LDAPRolesLookupService getLDAPRolesLookupService() { 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; } }; @@ -82,6 +83,6 @@ protected LDAPRolesLookupService getLDAPRolesLookupService() { GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class); EasyMock.replay(mockConfig); - factory.create(mockConfig, "test", Collections.emptyMap()); + factory.create(mockConfig, null, "test", Collections.emptyMap()); } }