Skip to content

Commit dd5dddb

Browse files
committed
[grid] Using non-loopback address when default config is used
The `--hub` flag was introduced just before the 4.0.0 release, and this implicitly made the use of `0.0.0.0` as the default IP for the Hub and hence the Grid. When someone tried to connect to a remote Grid, through the `RemoteWebDriver.builder()`, an error was happening since the `Augmenter` makes the `RemoteWebDriver` connect to `se:cdp`. Given that the Grid url was `http://0.0.0.0:4444`, the client was trying to connect to `ws://0.0.0.0:4444/session/...`, which caused a failure. This fixes that by retrieving the non-loopback address of the machine, under the assumption that Hub and Nodes are on the same machine. If the Hub and Nodes are on different machines, or inside Docker containers, the users will need to configure the Grid url themselves (through `--grid-url`). Fixes #9976
1 parent 7213e08 commit dd5dddb

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

java/src/org/openqa/selenium/grid/node/config/NodeOptions.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.openqa.selenium.Platform;
3030
import org.openqa.selenium.SessionNotCreatedException;
3131
import org.openqa.selenium.WebDriver;
32+
import org.openqa.selenium.WebDriverException;
3233
import org.openqa.selenium.WebDriverInfo;
3334
import org.openqa.selenium.grid.config.Config;
3435
import org.openqa.selenium.grid.config.ConfigException;
@@ -37,6 +38,7 @@
3738
import org.openqa.selenium.internal.Require;
3839
import org.openqa.selenium.json.Json;
3940
import org.openqa.selenium.json.JsonOutput;
41+
import org.openqa.selenium.net.NetworkUtils;
4042
import org.openqa.selenium.net.Urls;
4143
import org.openqa.selenium.remote.service.DriverService;
4244

@@ -120,7 +122,22 @@ public Optional<URI> getPublicGridUri() {
120122
baseUri.getQuery(),
121123
baseUri.getFragment());
122124
}
123-
125+
String nonLoopbackAddress = "0.0.0.0";
126+
if (nonLoopbackAddress.equals(baseUri.getHost())) {
127+
try {
128+
nonLoopbackAddress = new NetworkUtils().getNonLoopbackAddressOfThisMachine();
129+
} catch (WebDriverException ignore) {
130+
// ignore this path as we still use "0.0.0.0"
131+
}
132+
baseUri = new URI(
133+
baseUri.getScheme(),
134+
baseUri.getUserInfo(),
135+
nonLoopbackAddress,
136+
baseUri.getPort(),
137+
baseUri.getPath(),
138+
baseUri.getQuery(),
139+
baseUri.getFragment());
140+
}
124141
return Optional.of(baseUri);
125142
} catch (URISyntaxException e) {
126143
throw new ConfigException("Unable to construct public URL: " + base);

java/test/org/openqa/selenium/grid/node/config/NodeOptionsTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
package org.openqa.selenium.grid.node.config;
1919

20+
import static java.util.Collections.emptyMap;
21+
import static java.util.Collections.emptySet;
22+
import static java.util.Collections.singletonMap;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.junit.Assert.fail;
25+
import static org.junit.Assume.assumeFalse;
26+
import static org.junit.Assume.assumeTrue;
27+
2028
import com.google.common.collect.ImmutableMap;
29+
2130
import org.assertj.core.api.Condition;
2231
import org.junit.Test;
2332
import org.mockito.Mockito;
@@ -38,6 +47,7 @@
3847
import org.openqa.selenium.grid.node.SessionFactory;
3948
import org.openqa.selenium.internal.Either;
4049
import org.openqa.selenium.json.Json;
50+
import org.openqa.selenium.net.NetworkUtils;
4151

4252
import java.io.StringReader;
4353
import java.net.URI;
@@ -48,14 +58,6 @@
4858
import java.util.Map;
4959
import java.util.Optional;
5060

51-
import static java.util.Collections.emptyMap;
52-
import static java.util.Collections.emptySet;
53-
import static java.util.Collections.singletonMap;
54-
import static org.assertj.core.api.Assertions.assertThat;
55-
import static org.junit.Assert.fail;
56-
import static org.junit.Assume.assumeFalse;
57-
import static org.junit.Assume.assumeTrue;
58-
5961
@SuppressWarnings("DuplicatedCode")
6062
public class NodeOptionsTest {
6163

@@ -472,6 +474,20 @@ public void settingTheHubFlagSetsTheGridUrlAndEventBusFlags() {
472474
.isEqualTo(Optional.of(URI.create("http://cheese.com:4444")));
473475
}
474476

477+
@Test
478+
public void settingTheHubWithDefaultValueSetsTheGridUrlToTheNonLoopbackAddress() {
479+
String[] rawConfig = new String[]{
480+
"[node]",
481+
"hub = \"http://0.0.0.0:4444\"",
482+
};
483+
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
484+
String nonLoopbackAddress = new NetworkUtils().getNonLoopbackAddressOfThisMachine();
485+
String nonLoopbackAddressUrl = String.format("http://%s:4444", nonLoopbackAddress);
486+
NodeOptions nodeOptions = new NodeOptions(config);
487+
assertThat(nodeOptions.getPublicGridUri())
488+
.isEqualTo(Optional.of(URI.create(nonLoopbackAddressUrl)));
489+
}
490+
475491
private Condition<? super List<? extends Capabilities>> supporting(String name) {
476492
return new Condition<>(
477493
caps -> caps.stream().anyMatch(cap -> name.equals(cap.getBrowserName())),

0 commit comments

Comments
 (0)