You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/02/07 17:24:19 UTC

[GitHub] merlimat closed pull request #1147: Add support for new configration interface to AuthenticaionTls

merlimat closed pull request #1147: Add support for new configration interface to AuthenticaionTls
URL: https://github.com/apache/incubator-pulsar/pull/1147
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-auth-athenz/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationAthenz.java b/pulsar-client-auth-athenz/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationAthenz.java
index d833e950d..277f4a1f2 100644
--- a/pulsar-client-auth-athenz/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationAthenz.java
+++ b/pulsar-client-auth-athenz/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationAthenz.java
@@ -27,20 +27,17 @@
 import java.net.URISyntaxException;
 import java.security.PrivateKey;
 import java.util.Base64;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.pulsar.client.api.Authentication;
 import org.apache.pulsar.client.api.AuthenticationDataProvider;
+import org.apache.pulsar.client.api.AuthenticationUtil;
 import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
 import org.apache.pulsar.client.api.PulsarClientException;
 import org.apache.pulsar.client.api.PulsarClientException.GettingAuthenticationDataException;
-import org.apache.pulsar.common.util.ObjectMapperFactory;
 
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.base.Splitter;
 import com.yahoo.athenz.auth.ServiceIdentityProvider;
 import com.yahoo.athenz.auth.impl.SimpleServiceIdentityProvider;
@@ -108,15 +105,10 @@ public void configure(String encodedAuthParamString) {
             throw new IllegalArgumentException("authParams must not be empty");
         }
 
-        // Convert JSON to Map
         try {
-            ObjectMapper jsonMapper = ObjectMapperFactory.create();
-            Map<String, String> authParamsMap = jsonMapper.readValue(encodedAuthParamString,
-                    new TypeReference<HashMap<String, String>>() {
-                    });
-            setAuthParams(authParamsMap);
+            setAuthParams(AuthenticationUtil.configureFromJsonString(encodedAuthParamString));
         } catch (IOException e) {
-            throw new IllegalArgumentException("Failed to parse authParams");
+            throw new IllegalArgumentException("Failed to parse authParams", e);
         }
     }
 
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/api/Authentication.java b/pulsar-client/src/main/java/org/apache/pulsar/client/api/Authentication.java
index 981b9997c..bd564f591 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/api/Authentication.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/api/Authentication.java
@@ -43,7 +43,11 @@
      * Configure the authentication plugins with the supplied parameters
      *
      * @param authParams
+     * @deprecated This method will be deleted on version 2.0, instead please use configure(String
+     *             encodedAuthParamString) which is in EncodedAuthenticationParameterSupport for now and will be
+     *             integrated into this interface.
      */
+    @Deprecated
     void configure(Map<String, String> authParams);
 
     /**
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationFactory.java b/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationFactory.java
index 80e5dd0cb..f955a5695 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationFactory.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationFactory.java
@@ -19,7 +19,6 @@
 package org.apache.pulsar.client.api;
 
 import java.util.Map;
-import java.util.HashMap;
 
 import static org.apache.commons.lang3.StringUtils.isNotBlank;
 import org.apache.pulsar.client.api.PulsarClientException.UnsupportedAuthenticationException;
@@ -28,21 +27,6 @@
 
 public final class AuthenticationFactory {
 
-    private static Map<String, String> parseAuthParamsString(String authParamsString) {
-        Map<String, String> authParams = new HashMap<>();
-
-        if (isNotBlank(authParamsString)) {
-            String[] params = authParamsString.split(",");
-            for (String p : params) {
-                String[] kv = p.split(":");
-                if (kv.length == 2) {
-                    authParams.put(kv[0], kv[1]);
-                }
-            }
-        }
-        return authParams;
-    }
-
     /**
      * Create an instance of the Authentication-Plugin
      *
@@ -63,7 +47,7 @@ public static final Authentication create(String authPluginClassName, String aut
                     ((EncodedAuthenticationParameterSupport) auth).configure(authParamsString);
                 } else {
                     // Parse parameters by default parse logic.
-                    auth.configure(parseAuthParamsString(authParamsString));
+                    auth.configure(AuthenticationUtil.configureFromPulsar1AuthParamString(authParamsString));
                 }
                 return auth;
             } else {
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationUtil.java b/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationUtil.java
new file mode 100644
index 000000000..1103e1ab0
--- /dev/null
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/api/AuthenticationUtil.java
@@ -0,0 +1,53 @@
+/**
+ * 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.client.api;
+
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.pulsar.common.util.ObjectMapperFactory;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class AuthenticationUtil {
+	public static Map<String, String> configureFromJsonString(String authParamsString) throws IOException {
+		ObjectMapper jsonMapper = ObjectMapperFactory.create();
+		return jsonMapper.readValue(authParamsString, new TypeReference<HashMap<String, String>>() {
+		});
+	}
+
+	public static Map<String, String> configureFromPulsar1AuthParamString(String authParamsString) {
+		Map<String, String> authParams = new HashMap<>();
+
+		if (isNotBlank(authParamsString)) {
+			String[] params = authParamsString.split(",");
+			for (String p : params) {
+				String[] kv = p.split(":");
+				if (kv.length == 2) {
+					authParams.put(kv[0], kv[1]);
+				}
+			}
+		}
+		return authParams;
+	}
+}
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationDisabled.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationDisabled.java
index f1afd87da..c74a38e77 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationDisabled.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationDisabled.java
@@ -23,9 +23,10 @@
 
 import org.apache.pulsar.client.api.Authentication;
 import org.apache.pulsar.client.api.AuthenticationDataProvider;
+import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
 import org.apache.pulsar.client.api.PulsarClientException;
 
-public class AuthenticationDisabled implements Authentication {
+public class AuthenticationDisabled implements Authentication, EncodedAuthenticationParameterSupport {
 
     protected final AuthenticationDataProvider nullData = new AuthenticationDataNull();
     /**
@@ -47,6 +48,11 @@ public AuthenticationDataProvider getAuthData() throws PulsarClientException {
     }
 
     @Override
+    public void configure(String encodedAuthParamString) {
+    }
+
+    @Override
+    @Deprecated
     public void configure(Map<String, String> authParams) {
     }
 
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationTls.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationTls.java
index 1d8f0f7cf..b2ad87544 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationTls.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationTls.java
@@ -23,6 +23,8 @@
 
 import org.apache.pulsar.client.api.Authentication;
 import org.apache.pulsar.client.api.AuthenticationDataProvider;
+import org.apache.pulsar.client.api.AuthenticationUtil;
+import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
 import org.apache.pulsar.client.api.PulsarClientException;
 
 /**
@@ -32,7 +34,7 @@
  * tlsCertFile: A file path for a client certificate. tlsKeyFile: A file path for a client private key.
  *
  */
-public class AuthenticationTls implements Authentication {
+public class AuthenticationTls implements Authentication, EncodedAuthenticationParameterSupport {
 
     private static final long serialVersionUID = 1L;
 
@@ -59,9 +61,14 @@ public AuthenticationDataProvider getAuthData() throws PulsarClientException {
     }
 
     @Override
+    public void configure(String encodedAuthParamString) {
+        setAuthParams(AuthenticationUtil.configureFromPulsar1AuthParamString(encodedAuthParamString));
+    }
+
+    @Override
+    @Deprecated
     public void configure(Map<String, String> authParams) {
-        certFilePath = authParams.get("tlsCertFile");
-        keyFilePath = authParams.get("tlsKeyFile");
+        setAuthParams(authParams);
     }
 
     @Override
@@ -69,4 +76,9 @@ public void start() throws PulsarClientException {
         // noop
     }
 
+    private void setAuthParams(Map<String, String> authParams) {
+        certFilePath = authParams.get("tlsCertFile");
+        keyFilePath = authParams.get("tlsKeyFile");
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services