You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2019/03/24 19:33:47 UTC

[pulsar] branch master updated: Fixed reading from file with newlines (#3864)

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

mmerli 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 f93650f  Fixed reading from file with newlines (#3864)
f93650f is described below

commit f93650fced6595c46d97a2022d37a862985f4f11
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Sun Mar 24 12:33:41 2019 -0700

    Fixed reading from file with newlines (#3864)
---
 .../client/impl/auth/AuthenticationToken.java      |  2 +-
 .../client/impl/auth/AuthenticationTokenTest.java  | 28 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationToken.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationToken.java
index 9e49016..bd13401 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationToken.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/AuthenticationToken.java
@@ -77,7 +77,7 @@ public class AuthenticationToken implements Authentication, EncodedAuthenticatio
             URI filePath = URI.create(encodedAuthParamString);
             this.tokenSupplier = () -> {
                 try {
-                    return new String(Files.readAllBytes(Paths.get(filePath)), Charsets.UTF_8);
+                    return new String(Files.readAllBytes(Paths.get(filePath)), Charsets.UTF_8).trim();
                 } catch (IOException e) {
                     throw new RuntimeException("Failed to read token from file", e);
                 }
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/auth/AuthenticationTokenTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/auth/AuthenticationTokenTest.java
index b2f3a7f..6063a02 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/auth/AuthenticationTokenTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/auth/AuthenticationTokenTest.java
@@ -90,6 +90,34 @@ public class AuthenticationTokenTest {
         authToken.close();
     }
 
+    /**
+     * File can have spaces and newlines before or after the token. We should be able to read
+     * the token correctly anyway.
+     */
+    @Test
+    public void testAuthTokenConfigFromFileWithNewline() throws Exception {
+        File tokenFile = File.createTempFile("pular-test-token", ".key");
+        tokenFile.deleteOnExit();
+        FileUtils.write(tokenFile, "  my-test-token-string  \r\n", Charsets.UTF_8);
+
+        AuthenticationToken authToken = new AuthenticationToken();
+        authToken.configure("file://" + tokenFile);
+        assertEquals(authToken.getAuthMethodName(), "token");
+
+        AuthenticationDataProvider authData = authToken.getAuthData();
+        assertTrue(authData.hasDataFromCommand());
+        assertEquals(authData.getCommandData(), "my-test-token-string");
+
+        // Ensure if the file content changes, the token will get refreshed as well
+        FileUtils.write(tokenFile, "other-token", Charsets.UTF_8);
+
+        AuthenticationDataProvider authData2 = authToken.getAuthData();
+        assertTrue(authData2.hasDataFromCommand());
+        assertEquals(authData2.getCommandData(), "other-token");
+
+        authToken.close();
+    }
+
     @Test
     public void testAuthTokenConfigNoPrefix() throws Exception {
         AuthenticationToken authToken = new AuthenticationToken();