Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit fe19dc4

Browse files
authored
Merge pull request #592 from graylog-labs/scrub-server-uris
Scrub server URIs before logging them in AbstractJestClient
2 parents 65139a5 + 2355a8c commit fe19dc4

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

jest-common/src/main/java/io/searchbox/client/AbstractJestClient.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.searchbox.client;
22

33

4+
import com.google.common.annotations.VisibleForTesting;
45
import com.google.common.base.Joiner;
56
import com.google.common.collect.ImmutableList;
67
import com.google.common.collect.ImmutableSet;
@@ -13,6 +14,8 @@
1314
import org.slf4j.LoggerFactory;
1415

1516
import java.io.IOException;
17+
import java.net.URI;
18+
import java.net.URISyntaxException;
1619
import java.util.List;
1720
import java.util.Set;
1821
import java.util.concurrent.atomic.AtomicInteger;
@@ -49,13 +52,13 @@ public void setServers(Set<String> servers) {
4952
if (servers.equals(serverPoolReference.get().getServers())) {
5053
if (log.isDebugEnabled()) {
5154
log.debug("Server pool already contains same list of servers: {}",
52-
Joiner.on(',').join(servers));
55+
Joiner.on(',').join(scrubServerURIs(servers)));
5356
}
5457
return;
5558
}
5659
if (log.isInfoEnabled()) {
5760
log.info("Setting server pool to a list of {} servers: [{}]",
58-
servers.size(), Joiner.on(',').join(servers));
61+
servers.size(), Joiner.on(',').join(scrubServerURIs(servers)));
5962
}
6063
serverPoolReference.set(new ServerPool(servers));
6164

@@ -64,6 +67,27 @@ public void setServers(Set<String> servers) {
6467
}
6568
}
6669

70+
@VisibleForTesting
71+
Set<String> scrubServerURIs(Set<String> servers) {
72+
final ImmutableSet.Builder<String> scrubbedServers = ImmutableSet.builder();
73+
for (String server : servers) {
74+
final URI originalURI = URI.create(server);
75+
try {
76+
final URI scrubbedURI = new URI(originalURI.getScheme(),
77+
null, // Remove user info
78+
originalURI.getHost(),
79+
originalURI.getPort(),
80+
originalURI.getPath(),
81+
originalURI.getQuery(),
82+
originalURI.getFragment());
83+
scrubbedServers.add(scrubbedURI.toString());
84+
} catch (URISyntaxException e) {
85+
log.debug("Couldn't scrub server URI " + originalURI, e);
86+
}
87+
}
88+
return scrubbedServers.build();
89+
}
90+
6791
/**
6892
* {@inheritDoc}
6993
*/

jest-common/src/test/java/io/searchbox/client/AbstractJestClientTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.searchbox.client;
22

3+
import com.google.common.collect.ImmutableSet;
34
import com.google.common.collect.Maps;
45
import io.searchbox.action.Action;
56
import org.junit.Test;
@@ -63,6 +64,39 @@ public void testGetElasticSearchServer() throws Exception {
6364
assertTrue(set.contains("http://localhost:9400"));
6465
}
6566

67+
@Test
68+
public void testGetElasticSearchServerWithCredentials() {
69+
final Set<String> set = ImmutableSet.of(
70+
"http://user:pass@localhost:9200",
71+
"http://user:pass@localhost:9300",
72+
"http://user:pass@localhost:9400");
73+
client.setServers(set);
74+
75+
Set<String> serverList = new HashSet<>();
76+
77+
for (int i = 0; i < set.size(); i++) {
78+
serverList.add(client.getNextServer());
79+
}
80+
81+
assertEquals("round robin does not work", 3, serverList.size());
82+
83+
assertTrue(set.contains("http://user:pass@localhost:9200"));
84+
assertTrue(set.contains("http://user:pass@localhost:9300"));
85+
assertTrue(set.contains("http://user:pass@localhost:9400"));
86+
}
87+
88+
@Test
89+
public void testScrubServerURIs() {
90+
final Set<String> set = ImmutableSet.of(
91+
"http://user:pass@localhost:9200",
92+
"http://localhost:9300/path?query#fragment");
93+
final Set<String> scrubbedURIs = client.scrubServerURIs(set);
94+
95+
assertEquals(2, scrubbedURIs.size());
96+
assertTrue(scrubbedURIs.contains("http://localhost:9200"));
97+
assertTrue(scrubbedURIs.contains("http://localhost:9300/path?query#fragment"));
98+
}
99+
66100
@Test
67101
public void testGetElasticSearchServerIsThreadsafe() throws Exception {
68102
final int NUM_THREADS = 12;

0 commit comments

Comments
 (0)