Skip to content

Commit

Permalink
Fixed reading from file with newlines (apache#3864)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored Mar 24, 2019
1 parent 044daf8 commit f93650f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void configure(String encodedAuthParamString) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ public void testAuthTokenConfigFromFile() throws Exception {
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();
Expand Down

0 comments on commit f93650f

Please sign in to comment.