Skip to content

Commit 3f2ed2e

Browse files
authored
KNOX-3375: Wire in gatewayservices for LDAPRolesLookupInterceptorFactory so KnoxCLI is able to load it (#1303)
1 parent 6dbdc94 commit 3f2ed2e

10 files changed

Lines changed: 43 additions & 21 deletions

File tree

gateway-server/src/main/java/org/apache/knox/gateway/services/factory/LdapServiceFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected Service createService(GatewayServices gatewayServices, ServiceType ser
3838
if (shouldCreateService(implementation)) {
3939
service = new KnoxLDAPService();
4040
service.setAliasService(getAliasService(gatewayServices));
41+
service.setGatewayServices(gatewayServices);
4142
GatewayServer.registerConfigChangeListener(service);
4243
logServiceUsage(service.getClass().getName(), serviceType);
4344
}

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
4545
import org.apache.knox.gateway.config.GatewayConfig;
4646
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
47+
import org.apache.knox.gateway.services.GatewayServices;
4748
import org.apache.knox.gateway.services.ldap.control.RolesLookupBypassControlFactory;
4849
import org.apache.knox.gateway.services.ldap.control.RolesLookupBypassControlImpl;
4950
import org.apache.knox.gateway.services.ldap.interceptor.InterceptorFactory;
@@ -67,6 +68,7 @@ public class KnoxLDAPServerManager {
6768
private static final LdapMessages LOG = MessagesFactory.get(LdapMessages.class);
6869
private static final String LDAP_BIND_PASSWORD_ALIAS = "gateway_ldap_bind_password";
6970
private final AliasService aliasService;
71+
private final GatewayServices gatewayServices;
7072

7173
@VisibleForTesting
7274
DirectoryService directoryService;
@@ -81,7 +83,12 @@ public class KnoxLDAPServerManager {
8183
private Set<String> baseDns;
8284

8385
KnoxLDAPServerManager(AliasService aliasService) {
86+
this(aliasService, null);
87+
}
88+
89+
KnoxLDAPServerManager(AliasService aliasService, GatewayServices gatewayServices) {
8490
this.aliasService = aliasService;
91+
this.gatewayServices = gatewayServices;
8592
}
8693

8794
/**
@@ -133,7 +140,7 @@ private void createInterceptors(GatewayConfig config) throws Exception {
133140
}
134141
}
135142

136-
interceptors.add(InterceptorFactory.createInterceptor(config, interceptorName, interceptorConfig));
143+
interceptors.add(InterceptorFactory.createInterceptor(config, gatewayServices, interceptorName, interceptorConfig));
137144
}
138145
this.interceptors = interceptors;
139146
}

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.knox.gateway.config.GatewayConfig;
2121
import org.apache.knox.gateway.config.GatewayConfigChangeListener;
2222
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
23+
import org.apache.knox.gateway.services.GatewayServices;
2324
import org.apache.knox.gateway.services.Service;
2425
import org.apache.knox.gateway.services.ServiceLifecycleException;
2526
import org.apache.knox.gateway.services.security.AliasService;
@@ -36,6 +37,7 @@ public class KnoxLDAPService implements Service, GatewayConfigChangeListener {
3637

3738
KnoxLDAPServerManager ldapServerManager;
3839
AliasService aliasService;
40+
private GatewayServices gatewayServices;
3941
private boolean enabled;
4042

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

4951
try {
5052
// Initialize the LDAP server manager with configuration
51-
ldapServerManager = new KnoxLDAPServerManager(aliasService);
53+
ldapServerManager = new KnoxLDAPServerManager(aliasService, gatewayServices);
5254
ldapServerManager.initialize(config);
5355
} catch (Exception e) {
5456
throw new ServiceLifecycleException("Failed to initialize LDAP service", e);
@@ -59,6 +61,10 @@ public void setAliasService(AliasService aliasService) {
5961
this.aliasService = aliasService;
6062
}
6163

64+
public void setGatewayServices(GatewayServices gatewayServices) {
65+
this.gatewayServices = gatewayServices;
66+
}
67+
6268
@Override
6369
public void start() throws ServiceLifecycleException {
6470
if (!enabled) {
@@ -95,7 +101,7 @@ public void onGatewayConfigChanged(GatewayConfig config) {
95101
this.enabled = config.isLDAPEnabled();
96102

97103
if (this.enabled) {
98-
this.ldapServerManager = this.ldapServerManager == null ? new KnoxLDAPServerManager(aliasService) : this.ldapServerManager;
104+
this.ldapServerManager = this.ldapServerManager == null ? new KnoxLDAPServerManager(aliasService, gatewayServices) : this.ldapServerManager;
99105
ldapServerManager.stop();
100106
ldapServerManager.initialize(config);
101107
ldapServerManager.start();

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DisabledUserInterceptorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import org.apache.directory.server.core.api.interceptor.Interceptor;
2121
import org.apache.knox.gateway.config.GatewayConfig;
22+
import org.apache.knox.gateway.services.GatewayServices;
2223

2324
import java.util.Map;
2425

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

2829
@Override
29-
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> config) {
30+
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> config) {
3031
return new DisabledUserInterceptor(name, config);
3132
}
3233

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/DuplicateUserFilteringInterceptorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import org.apache.directory.server.core.api.interceptor.Interceptor;
2121
import org.apache.knox.gateway.config.GatewayConfig;
22+
import org.apache.knox.gateway.services.GatewayServices;
2223

2324
import java.util.Map;
2425

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

2829
@Override
29-
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) {
30+
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) {
3031
return new DuplicateUserFilteringInterceptor(name);
3132
}
3233

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/InterceptorFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.directory.server.core.api.interceptor.Interceptor;
2121
import org.apache.knox.gateway.config.GatewayConfig;
2222
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
23+
import org.apache.knox.gateway.services.GatewayServices;
2324
import org.apache.knox.gateway.services.ldap.LdapMessages;
2425

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

36-
public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, final String interceptorName,
37+
public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, final GatewayServices gatewayServices,
38+
final String interceptorName,
3739
final Map<String, String> interceptorConfig) throws Exception {
3840
final String interceptorType = interceptorConfig.get("interceptorType");
3941
if (interceptorType == null) {
@@ -49,7 +51,7 @@ public static Interceptor createInterceptor(final GatewayConfig gatewayConfig, f
4951
for (KnoxLdapInterceptorFactory interceptorFactory : loader) {
5052
if (interceptorFactory.getType().equalsIgnoreCase(interceptorType)) {
5153
LOG.ldapInterceptorCreating(interceptorType, "ServiceLoader");
52-
return interceptorFactory.create(gatewayConfig, interceptorName, interceptorConfig);
54+
return interceptorFactory.create(gatewayConfig, gatewayServices, interceptorName, interceptorConfig);
5355
}
5456
}
5557

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/KnoxLdapInterceptorFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.directory.server.core.api.interceptor.Interceptor;
2121
import org.apache.knox.gateway.config.GatewayConfig;
22+
import org.apache.knox.gateway.services.GatewayServices;
2223

2324
import java.util.Map;
2425

@@ -27,13 +28,14 @@
2728
*/
2829
public interface KnoxLdapInterceptorFactory {
2930
/**
30-
* Instantiate and interceptor
31+
* Instantiate an interceptor
3132
* @param gatewayConfig the Knox Gateway configuration
33+
* @param gatewayServices the active GatewayServices registry (may be null)
3234
* @param name the name of the interceptor
3335
* @param interceptorConfig the configuration for the interceptor
3436
* @return the interceptor
3537
*/
36-
Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) throws Exception;
38+
Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) throws Exception;
3739

3840
/**
3941
* Get the type of interceptor this factory creates

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ public class LDAPRolesLookupInterceptorFactory implements KnoxLdapInterceptorFac
3030
private static final String TYPE = "rolesLookup";
3131

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

41-
protected LDAPRolesLookupService getLDAPRolesLookupService() {
42-
final GatewayServices gatewayServices = GatewayServer.getGatewayServices();
43-
return gatewayServices.getService(ServiceType.LDAP_ROLES_LOOKUP_SERVICE);
41+
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
42+
final GatewayServices services = gatewayServices != null ? gatewayServices : GatewayServer.getGatewayServices();
43+
return services == null ? null : services.getService(ServiceType.LDAP_ROLES_LOOKUP_SERVICE);
4444
}
4545

4646
@Override

gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/UserSearchInterceptorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import org.apache.directory.server.core.api.interceptor.Interceptor;
2121
import org.apache.knox.gateway.config.GatewayConfig;
22+
import org.apache.knox.gateway.services.GatewayServices;
2223

2324
import java.util.Map;
2425

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

2829
@Override
29-
public Interceptor create(GatewayConfig gatewayConfig, String name, Map<String, String> interceptorConfig) throws Exception {
30+
public Interceptor create(GatewayConfig gatewayConfig, GatewayServices gatewayServices, String name, Map<String, String> interceptorConfig) throws Exception {
3031
return new UserSearchInterceptor(name, interceptorConfig);
3132
}
3233

gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptorFactoryTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

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

3940
LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
4041
@Override
41-
protected LDAPRolesLookupService getLDAPRolesLookupService() {
42+
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
4243
return mockService;
4344
}
4445
};
4546

4647
GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
4748
EasyMock.replay(mockConfig);
4849

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

6061
LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
6162
@Override
62-
protected LDAPRolesLookupService getLDAPRolesLookupService() {
63+
protected LDAPRolesLookupService getLDAPRolesLookupService(GatewayServices gatewayServices) {
6364
return mockService;
6465
}
6566
};
6667

6768
GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
6869
EasyMock.replay(mockConfig);
6970

70-
factory.create(mockConfig, "test", Collections.emptyMap());
71+
factory.create(mockConfig, null, "test", Collections.emptyMap());
7172
}
7273

7374
@Test(expected = ServiceLifecycleException.class)
7475
public void testCreateWithNullService() throws Exception {
7576
LDAPRolesLookupInterceptorFactory factory = new LDAPRolesLookupInterceptorFactory() {
7677
@Override
77-
protected LDAPRolesLookupService getLDAPRolesLookupService() {
78+
protected LDAPRolesLookupService getLDAPRolesLookupService(org.apache.knox.gateway.services.GatewayServices gatewayServices) {
7879
return null;
7980
}
8081
};
8182

8283
GatewayConfig mockConfig = EasyMock.createMock(GatewayConfig.class);
8384
EasyMock.replay(mockConfig);
8485

85-
factory.create(mockConfig, "test", Collections.emptyMap());
86+
factory.create(mockConfig, null, "test", Collections.emptyMap());
8687
}
8788
}

0 commit comments

Comments
 (0)