31
31
import io .prometheus .client .Gauge ;
32
32
import io .prometheus .client .Gauge .Child ;
33
33
import io .prometheus .client .hotspot .DefaultExports ;
34
+ import java .io .IOException ;
34
35
import java .text .DateFormat ;
35
36
import java .text .SimpleDateFormat ;
36
37
import java .util .Collection ;
37
38
import java .util .Collections ;
38
39
import java .util .Date ;
40
+ import java .util .Objects ;
39
41
import java .util .function .Consumer ;
40
42
import lombok .Getter ;
41
43
import org .apache .logging .log4j .LogManager ;
45
47
import org .apache .pulsar .broker .authentication .AuthenticationService ;
46
48
import org .apache .pulsar .broker .stats .prometheus .PrometheusMetricsServlet ;
47
49
import org .apache .pulsar .broker .web .plugin .servlet .AdditionalServletWithClassLoader ;
50
+ import org .apache .pulsar .client .api .Authentication ;
51
+ import org .apache .pulsar .client .api .AuthenticationFactory ;
52
+ import org .apache .pulsar .client .api .PulsarClientException ;
53
+ import org .apache .pulsar .client .impl .auth .AuthenticationDisabled ;
48
54
import org .apache .pulsar .common .configuration .PulsarConfigurationLoader ;
49
55
import org .apache .pulsar .common .configuration .VipStatus ;
50
56
import org .apache .pulsar .common .policies .data .ClusterData ;
@@ -99,6 +105,9 @@ public class ProxyServiceStarter {
99
105
100
106
private ProxyConfiguration config ;
101
107
108
+ @ Getter
109
+ private Authentication proxyClientAuthentication ;
110
+
102
111
@ Getter
103
112
private ProxyService proxyService ;
104
113
@@ -239,8 +248,27 @@ public static void main(String[] args) throws Exception {
239
248
public void start () throws Exception {
240
249
AuthenticationService authenticationService = new AuthenticationService (
241
250
PulsarConfigurationLoader .convertFrom (config ));
251
+
252
+ if (config .getBrokerClientAuthenticationPlugin () != null ) {
253
+ proxyClientAuthentication = AuthenticationFactory .create (config .getBrokerClientAuthenticationPlugin (),
254
+ config .getBrokerClientAuthenticationParameters ());
255
+ Objects .requireNonNull (proxyClientAuthentication , "No supported auth found for proxy" );
256
+ try {
257
+ proxyClientAuthentication .start ();
258
+ } catch (Exception e ) {
259
+ try {
260
+ proxyClientAuthentication .close ();
261
+ } catch (IOException ioe ) {
262
+ log .error ("Failed to close the authentication service" , ioe );
263
+ }
264
+ throw new PulsarClientException .InvalidConfigurationException (e .getMessage ());
265
+ }
266
+ } else {
267
+ proxyClientAuthentication = AuthenticationDisabled .INSTANCE ;
268
+ }
269
+
242
270
// create proxy service
243
- proxyService = new ProxyService (config , authenticationService );
271
+ proxyService = new ProxyService (config , authenticationService , proxyClientAuthentication );
244
272
// create a web-service
245
273
server = new WebServer (config , authenticationService );
246
274
@@ -287,7 +315,8 @@ public double get() {
287
315
metricsInitialized = true ;
288
316
}
289
317
290
- addWebServerHandlers (server , config , proxyService , proxyService .getDiscoveryProvider ());
318
+ addWebServerHandlers (server , config , proxyService , proxyService .getDiscoveryProvider (),
319
+ proxyClientAuthentication );
291
320
292
321
// start web-service
293
322
server .start ();
@@ -301,6 +330,9 @@ public void close() {
301
330
if (server != null ) {
302
331
server .stop ();
303
332
}
333
+ if (proxyClientAuthentication != null ) {
334
+ proxyClientAuthentication .close ();
335
+ }
304
336
} catch (Exception e ) {
305
337
log .warn ("server couldn't stop gracefully {}" , e .getMessage (), e );
306
338
} finally {
@@ -311,9 +343,10 @@ public void close() {
311
343
}
312
344
313
345
public static void addWebServerHandlers (WebServer server ,
314
- ProxyConfiguration config ,
315
- ProxyService service ,
316
- BrokerDiscoveryProvider discoveryProvider ) throws Exception {
346
+ ProxyConfiguration config ,
347
+ ProxyService service ,
348
+ BrokerDiscoveryProvider discoveryProvider ,
349
+ Authentication proxyClientAuthentication ) throws Exception {
317
350
// We can make 'status.html' publicly accessible without authentication since
318
351
// it does not contain any sensitive data.
319
352
server .addRestResource ("/" , VipStatus .ATTRIBUTE_STATUS_FILE_PATH , config .getStatusFilePath (),
@@ -330,7 +363,8 @@ public static void addWebServerHandlers(WebServer server,
330
363
}
331
364
}
332
365
333
- AdminProxyHandler adminProxyHandler = new AdminProxyHandler (config , discoveryProvider );
366
+ AdminProxyHandler adminProxyHandler = new AdminProxyHandler (config , discoveryProvider ,
367
+ proxyClientAuthentication );
334
368
ServletHolder servletHolder = new ServletHolder (adminProxyHandler );
335
369
server .addServlet ("/admin" , servletHolder );
336
370
server .addServlet ("/lookup" , servletHolder );
0 commit comments