Skip to content

Commit 2577fd8

Browse files
authored
Support authorization for java proxy (#74)
Fixed #58 ## Motivation At first, I thought I needed to define the authorization cache configuration class when obtaining authorization, for example, by implementing a class similar to `ConfigurationCacheService`. However, after my research, I found that the current the class `AuthenticationService` value receives class `ConfigurationCacheService` as an argument, and this class uses zookeeper as a caching service. java-proxy runs in the same place as the pulsar cluster and inherits the function_worker configuration, so it can also use zookeeper as a caching service. For the authentication plugin, the custom logic is implemented in jetty's filter, so there is no need to update the
1 parent c68c021 commit 2577fd8

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

java-proxy/src/main/java/io/streamnative/function/mesh/proxy/FunctionMeshProxyService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public class FunctionMeshProxyService implements WorkerService {
6464
private CustomObjectsApi customObjectsApi;
6565
private ApiClient apiClient;
6666

67+
private AuthenticationService authenticationService;
68+
private AuthorizationService authorizationService;
69+
6770
public FunctionMeshProxyService() {
6871

6972
}
@@ -102,7 +105,8 @@ private void initKubernetesClient() throws IOException {
102105
public void start(AuthenticationService authenticationService,
103106
AuthorizationService authorizationService,
104107
ErrorNotifier errorNotifier) {
105-
// https://github.com/streamnative/function-mesh/issues/58
108+
this.authenticationService = authenticationService;
109+
this.authorizationService = authorizationService;
106110
}
107111

108112
public void stop() {

java-proxy/src/main/java/io/streamnative/function/mesh/proxy/FunctionMeshProxyWorker.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,40 @@
1818
*/
1919
package io.streamnative.function.mesh.proxy;
2020

21+
import io.netty.util.concurrent.DefaultThreadFactory;
2122
import lombok.extern.slf4j.Slf4j;
23+
import org.apache.bookkeeper.common.util.OrderedExecutor;
2224
import org.apache.pulsar.broker.PulsarServerException;
2325
import org.apache.pulsar.broker.ServiceConfiguration;
2426
import org.apache.pulsar.broker.authentication.AuthenticationService;
2527
import org.apache.pulsar.broker.authorization.AuthorizationService;
28+
import org.apache.pulsar.broker.cache.ConfigurationCacheService;
2629
import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
2730
import org.apache.pulsar.functions.worker.ErrorNotifier;
2831
import org.apache.pulsar.functions.worker.Worker;
2932
import org.apache.pulsar.functions.worker.WorkerConfig;
3033
import org.apache.pulsar.functions.worker.WorkerService;
3134
import org.apache.pulsar.functions.worker.rest.WorkerServer;
35+
import org.apache.pulsar.zookeeper.GlobalZooKeeperCache;
36+
import org.apache.pulsar.zookeeper.ZooKeeperClientFactory;
37+
import org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl;
38+
39+
import java.io.IOException;
40+
import java.util.concurrent.Executors;
41+
import java.util.concurrent.ScheduledExecutorService;
3242

3343
/**
3444
* This class for test.
3545
*/
3646
@Slf4j
3747
public class FunctionMeshProxyWorker {
3848

49+
private ZooKeeperClientFactory zkClientFactory = null;
50+
private final OrderedExecutor orderedExecutor = OrderedExecutor.newBuilder().numThreads(8).name("zk-cache-ordered").build();
51+
private final ScheduledExecutorService cacheExecutor = Executors.newScheduledThreadPool(10,
52+
new DefaultThreadFactory("zk-cache-callback"));
53+
private GlobalZooKeeperCache globalZkCache;
54+
private ConfigurationCacheService configurationCacheService;
3955
private final WorkerConfig workerConfig;
4056
private final WorkerService workerService;
4157
private final ErrorNotifier errorNotifier;
@@ -50,8 +66,7 @@ public FunctionMeshProxyWorker(WorkerConfig workerConfig) {
5066

5167
protected void start() throws Exception {
5268
workerService.initAsStandalone(workerConfig);
53-
// To do add authorization and authentication
54-
workerService.start(getAuthenticationService(), null, errorNotifier);
69+
workerService.start(getAuthenticationService(), getAuthorizationService(), errorNotifier);
5570
server = new WorkerServer(workerService, getAuthenticationService());
5671
server.start();
5772
log.info("/** Started worker server on port={} **/", this.workerConfig.getWorkerPort());
@@ -64,6 +79,37 @@ protected void start() throws Exception {
6479
}
6580
}
6681

82+
public ZooKeeperClientFactory getZooKeeperClientFactory() {
83+
if (zkClientFactory == null) {
84+
zkClientFactory = new ZookeeperBkClientFactoryImpl(orderedExecutor);
85+
}
86+
// Return default factory
87+
return zkClientFactory;
88+
}
89+
90+
private AuthorizationService getAuthorizationService() throws PulsarServerException {
91+
if (this.workerConfig.isAuthorizationEnabled()) {
92+
log.info("starting configuration cache service");
93+
94+
this.globalZkCache = new GlobalZooKeeperCache(getZooKeeperClientFactory(),
95+
(int) workerConfig.getZooKeeperSessionTimeoutMillis(),
96+
workerConfig.getZooKeeperOperationTimeoutSeconds(),
97+
workerConfig.getConfigurationStoreServers(),
98+
orderedExecutor, cacheExecutor,
99+
workerConfig.getZooKeeperOperationTimeoutSeconds());
100+
try {
101+
this.globalZkCache.start();
102+
} catch (IOException e) {
103+
throw new PulsarServerException(e);
104+
}
105+
106+
this.configurationCacheService = new ConfigurationCacheService(
107+
this.globalZkCache, this.workerConfig.getPulsarFunctionsCluster());
108+
return new AuthorizationService(getServiceConfiguration(), this.configurationCacheService);
109+
}
110+
return null;
111+
}
112+
67113
private AuthenticationService getAuthenticationService() throws PulsarServerException {
68114
return new AuthenticationService(getServiceConfiguration());
69115
}
@@ -79,5 +125,12 @@ protected void stop() {
79125
this.server.stop();
80126
}
81127
workerService.stop();
128+
if (this.globalZkCache != null) {
129+
try {
130+
this.globalZkCache.close();
131+
} catch (IOException e) {
132+
log.warn("Failed to close global zk cache ", e);
133+
}
134+
}
82135
}
83136
}

0 commit comments

Comments
 (0)