Skip to content

Commit a22994a

Browse files
CR: Only validate form of seed address not hostname lookup
1 parent f2e8f9b commit a22994a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

server/src/main/java/org/elasticsearch/transport/RemoteClusterAware.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class RemoteClusterAware extends AbstractComponent {
5656
key, Collections.emptyList(),
5757
s -> {
5858
// validate seed address
59-
RemoteClusterAware.parseSeedAddress(s);
59+
parsePort(s);
6060
return s;
6161
},
6262
Setting.Property.NodeScope, Setting.Property.Dynamic
@@ -151,28 +151,36 @@ public void listenForUpdates(ClusterSettings clusterSettings) {
151151
}
152152

153153
protected static InetSocketAddress parseSeedAddress(String remoteHost) {
154-
int portSeparator = remoteHost.lastIndexOf(':'); // in case we have a IPv6 address ie. [::1]:9300
155-
if (portSeparator == -1 || portSeparator == remoteHost.length()) {
156-
throw new IllegalArgumentException("remote hosts need to be configured as [host:port], found [" + remoteHost + "] instead");
157-
}
158-
String host = remoteHost.substring(0, portSeparator);
154+
String host = remoteHost.substring(0, indexOfPortSeparator(remoteHost));
159155
InetAddress hostAddress;
160156
try {
161157
hostAddress = InetAddress.getByName(host);
162158
} catch (UnknownHostException e) {
163159
throw new IllegalArgumentException("unknown host [" + host + "]", e);
164160
}
161+
return new InetSocketAddress(hostAddress, parsePort(remoteHost));
162+
}
163+
164+
private static int parsePort(String remoteHost) {
165165
try {
166-
int port = Integer.valueOf(remoteHost.substring(portSeparator + 1));
166+
int port = Integer.valueOf(remoteHost.substring(indexOfPortSeparator(remoteHost) + 1));
167167
if (port <= 0) {
168168
throw new IllegalArgumentException("port number must be > 0 but was: [" + port + "]");
169169
}
170-
return new InetSocketAddress(hostAddress, port);
170+
return port;
171171
} catch (NumberFormatException e) {
172172
throw new IllegalArgumentException("port must be a number", e);
173173
}
174174
}
175175

176+
private static int indexOfPortSeparator(String remoteHost) {
177+
int portSeparator = remoteHost.lastIndexOf(':'); // in case we have a IPv6 address ie. [::1]:9300
178+
if (portSeparator == -1 || portSeparator == remoteHost.length()) {
179+
throw new IllegalArgumentException("remote hosts need to be configured as [host:port], found [" + remoteHost + "] instead");
180+
}
181+
return portSeparator;
182+
}
183+
176184
public static String buildRemoteIndexName(String clusterAlias, String indexName) {
177185
return clusterAlias != null ? clusterAlias + REMOTE_CLUSTER_INDEX_SEPARATOR + indexName : indexName;
178186
}

0 commit comments

Comments
 (0)