You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by si...@apache.org on 2020/09/11 04:45:18 UTC

[pulsar] branch master updated: Fix the null exception when starting the proxy service (#8019)

This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 248ee49  Fix the null exception when starting the proxy service (#8019)
248ee49 is described below

commit 248ee49fc267d99bd9e2fd4ef742b38dcf69ed27
Author: Yong Zhang <zh...@gmail.com>
AuthorDate: Fri Sep 11 12:45:05 2020 +0800

    Fix the null exception when starting the proxy service (#8019)
    
    * Fix the null exception when starting the proxy service
    ---
    
    Fixes #7960
    
    *Motivation*
    
    When enable the broker tls and enable broker client authentication with oauth2 plugin,
    the proxy service will exit with an unexpected null exception.
    Because there are some methods call the authentication before initializing the flow
    which will cause the token client isn't initialized before using.
    
    * Address comments
---
 .../impl/auth/oauth2/ClientCredentialsFlow.java    |  6 ++
 pulsar-proxy/pom.xml                               |  7 ++
 .../pulsar/proxy/server/ProxyTlsTestWithAuth.java  | 94 ++++++++++++++++++++++
 3 files changed, 107 insertions(+)

diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/ClientCredentialsFlow.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/ClientCredentialsFlow.java
index a7b30ab..8d82cc2 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/ClientCredentialsFlow.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/ClientCredentialsFlow.java
@@ -54,6 +54,8 @@ class ClientCredentialsFlow extends FlowBase {
 
     private transient ClientCredentialsExchanger exchanger;
 
+    private boolean initialized = false;
+
     @Builder
     public ClientCredentialsFlow(URL issuerUrl, String audience, String privateKey) {
         super(issuerUrl);
@@ -68,6 +70,7 @@ class ClientCredentialsFlow extends FlowBase {
 
         URL tokenUrl = this.metadata.getTokenEndpoint();
         this.exchanger = new TokenClient(tokenUrl);
+        initialized = true;
     }
 
     public TokenResult authenticate() throws PulsarClientException {
@@ -86,6 +89,9 @@ class ClientCredentialsFlow extends FlowBase {
                 .audience(this.audience)
                 .build();
         TokenResult tr;
+        if (!initialized) {
+            initialize();
+        }
         try {
             tr = this.exchanger.exchangeClientCredentials(req);
         } catch (TokenExchangeException | IOException e) {
diff --git a/pulsar-proxy/pom.xml b/pulsar-proxy/pom.xml
index c5f4ecb..ebdb32f 100644
--- a/pulsar-proxy/pom.xml
+++ b/pulsar-proxy/pom.xml
@@ -171,5 +171,12 @@
       <groupId>com.beust</groupId>
       <artifactId>jcommander</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>bouncy-castle-bc</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTestWithAuth.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTestWithAuth.java
new file mode 100644
index 0000000..9273a63
--- /dev/null
+++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTestWithAuth.java
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.proxy.server;
+
+import static org.mockito.Mockito.doReturn;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.util.Optional;
+
+import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
+import org.apache.pulsar.broker.authentication.AuthenticationService;
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class ProxyTlsTestWithAuth extends MockedPulsarServiceBaseTest {
+
+    private final String TLS_TRUST_CERT_FILE_PATH = "./src/test/resources/authentication/tls/cacert.pem";
+    private final String TLS_PROXY_CERT_FILE_PATH = "./src/test/resources/authentication/tls/server-cert.pem";
+    private final String TLS_PROXY_KEY_FILE_PATH = "./src/test/resources/authentication/tls/server-key.pem";
+    private final String DUMMY_VALUE = "DUMMY_VALUE";
+
+    private ProxyService proxyService;
+    private ProxyConfiguration proxyConfig = new ProxyConfiguration();
+
+    @Override
+    @BeforeClass
+    protected void setup() throws Exception {
+        internalSetup();
+
+        File tempFile = File.createTempFile("oauth2", ".tmp");
+        tempFile.deleteOnExit();
+        FileWriter writer = new FileWriter(tempFile);
+        writer.write("{\n" +
+            "  \"client_id\":\"Xd23RHsUnvUlP7wchjNYOaIfazgeHd9x\",\n" +
+            "  \"client_secret\":\"rT7ps7WY8uhdVuBTKWZkttwLdQotmdEliaM5rLfmgNibvqziZ-g07ZH52N_poGAb\"\n" +
+            "}");
+        writer.flush();
+        writer.close();
+
+        proxyConfig.setServicePort(Optional.of(0));
+        proxyConfig.setServicePortTls(Optional.of(0));
+        proxyConfig.setWebServicePort(Optional.of(0));
+        proxyConfig.setWebServicePortTls(Optional.of(0));
+        proxyConfig.setTlsEnabledWithBroker(true);
+        proxyConfig.setTlsCertificateFilePath(TLS_PROXY_CERT_FILE_PATH);
+        proxyConfig.setTlsKeyFilePath(TLS_PROXY_KEY_FILE_PATH);
+        proxyConfig.setZookeeperServers(DUMMY_VALUE);
+        proxyConfig.setConfigurationStoreServers(DUMMY_VALUE);
+        proxyConfig.setBrokerClientAuthenticationPlugin("org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2");
+        proxyConfig.setBrokerClientAuthenticationParameters("{\"grant_type\":\"client_credentials\"," +
+            " \"issuerUrl\":\"https://dev-kt-aa9ne.us.auth0.com\"," +
+            " \"audience\": \"https://dev-kt-aa9ne.us.auth0.com/api/v2/\"," +
+            " \"privateKey\":\"file://" + tempFile.getAbsolutePath() + "\"}");
+
+        proxyService = Mockito.spy(new ProxyService(proxyConfig, new AuthenticationService(
+            PulsarConfigurationLoader.convertFrom(proxyConfig))));
+        doReturn(mockZooKeeperClientFactory).when(proxyService).getZooKeeperClientFactory();
+
+        proxyService.start();
+    }
+
+    @Override
+    @AfterClass
+    protected void cleanup() throws Exception {
+        internalCleanup();
+
+        proxyService.close();
+    }
+
+    @Test
+    public void testServiceStartup() {
+        // this tests is only for verify the proxy setup with oauth2 authentication plugin
+    }
+}