merlimat commented on a change in pull request #10574:
URL: https://github.com/apache/pulsar/pull/10574#discussion_r632136929



##########
File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/SerializableAuthenticationToken.java
##########
@@ -0,0 +1,108 @@
+/**
+ * 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.impl.auth;
+
+import com.google.common.base.Charsets;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonSyntaxException;
+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;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+/**
+ * Serializable token based authentication provider.
+ *
+ * This class is like {@link 
org.apache.pulsar.client.impl.auth.AuthenticationDataToken}
+ * but without the option to set a token supplier function, which might not be
+ * serializable when distributing throughout a cluster. Since the token is 
loaded
+ * only once to the class, when the provided token source (eg. file) changes, 
this
+ * class will _not_ be updated.
+ */
+public class SerializableAuthenticationToken implements Authentication, 
EncodedAuthenticationParameterSupport, Serializable {
+
+    private static final long serialVersionUID = 192088880489182317L;
+
+    private String token;
+
+    public SerializableAuthenticationToken() {}
+
+    public SerializableAuthenticationToken(final String token) {
+        configure(token);
+    }
+
+    @Override
+    public String getAuthMethodName() {
+        return "token";
+    }
+
+    @Override
+    public void configure(String encodedAuthParamString) {
+        // Interpret the whole param string as the token. If the string 
contains the notation `token:xxxxx` then strip
+        // the prefix
+        if (encodedAuthParamString.startsWith("token:")) {
+            this.token = encodedAuthParamString.substring("token:".length());
+        } else if (encodedAuthParamString.startsWith("file:")) {
+            // Read token from a file
+            URI filePath = URI.create(encodedAuthParamString);
+            try {
+                this.token = new 
String(Files.readAllBytes(Paths.get(filePath)), Charsets.UTF_8).trim();

Review comment:
       This has a problem that it only reads the content of the file once, at 
startup. When the token file gets refreshed, the client will still be operating 
with the old credentials, and the broker will disconnect it when the token 
expires




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to