Skip to content

Commit 5cfd207

Browse files
authored
Use CLI provided hostname instead ip address for requests to downstream Eth node
1 parent 971b910 commit 5cfd207

File tree

4 files changed

+19
-33
lines changed

4 files changed

+19
-33
lines changed

ethsigner/commandline/src/main/java/tech/pegasys/ethsigner/EthSignerBaseCommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public class EthSignerBaseCommand implements Config {
5151
"Logging verbosity levels: OFF, FATAL, WARN, INFO, DEBUG, TRACE, ALL (default: INFO)")
5252
private final Level logLevel = Level.INFO;
5353

54+
@SuppressWarnings("FieldMayBeFinal") // Because PicoCLI requires Strings to not be final.
5455
@Option(
5556
names = "--downstream-http-host",
5657
description =
5758
"The endpoint to which received requests are forwarded (default: ${DEFAULT-VALUE})",
5859
arity = "1")
59-
private final InetAddress downstreamHttpHost = InetAddress.getLoopbackAddress();
60+
private String downstreamHttpHost = InetAddress.getLoopbackAddress().getHostAddress();
6061

62+
@SuppressWarnings("FieldMayBeFinal") // Because PicoCLI requires Strings to not be final.
6163
@Option(
6264
names = "--downstream-http-port",
6365
description = "The endpoint to which received requests are forwarded",
@@ -78,7 +80,7 @@ public class EthSignerBaseCommand implements Config {
7880
names = {"--http-listen-host"},
7981
description = "Host for JSON-RPC HTTP to listen on (default: ${DEFAULT-VALUE})",
8082
arity = "1")
81-
private final InetAddress httpListenHost = InetAddress.getLoopbackAddress();
83+
private String httpListenHost = InetAddress.getLoopbackAddress().getHostAddress();
8284

8385
@Option(
8486
names = {"--http-listen-port"},
@@ -106,7 +108,7 @@ public Level getLogLevel() {
106108
}
107109

108110
@Override
109-
public InetAddress getDownstreamHttpHost() {
111+
public String getDownstreamHttpHost() {
110112
return downstreamHttpHost;
111113
}
112114

@@ -116,7 +118,7 @@ public Integer getDownstreamHttpPort() {
116118
}
117119

118120
@Override
119-
public InetAddress getHttpListenHost() {
121+
public String getHttpListenHost() {
120122
return httpListenHost;
121123
}
122124

ethsigner/commandline/src/test/java/tech/pegasys/ethsigner/CommandlineParserTest.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public void fullyPopulatedCommandLineParsesIntoVariables() throws UnknownHostExc
7979
assertThat(result).isTrue();
8080

8181
assertThat(config.getLogLevel()).isEqualTo(Level.INFO);
82-
assertThat(config.getDownstreamHttpHost()).isEqualTo(InetAddress.getByName("8.8.8.8"));
82+
assertThat(config.getDownstreamHttpHost()).isEqualTo("8.8.8.8");
8383
assertThat(config.getDownstreamHttpPort()).isEqualTo(5000);
8484
assertThat(config.getDownstreamHttpRequestTimeout()).isEqualTo(Duration.ofSeconds(10));
85-
assertThat(config.getHttpListenHost()).isEqualTo(InetAddress.getByName("localhost"));
85+
assertThat(config.getHttpListenHost()).isEqualTo("localhost");
8686
assertThat(config.getHttpListenPort()).isEqualTo(5001);
8787
}
8888

@@ -139,7 +139,9 @@ public void missingLoggingDefaultsToInfoLevel() {
139139
@Test
140140
public void missingDownStreamHostDefaultsToLoopback() {
141141
missingOptionalParameterIsValidAndMeetsDefault(
142-
"downstream-http-host", config::getDownstreamHttpHost, InetAddress.getLoopbackAddress());
142+
"downstream-http-host",
143+
config::getDownstreamHttpHost,
144+
InetAddress.getLoopbackAddress().getHostAddress());
143145
}
144146

145147
@Test
@@ -159,7 +161,9 @@ public void missingDownstreamTimeoutDefaultsToFiveSeconds() {
159161
@Test
160162
public void missingListenHostDefaultsToLoopback() {
161163
missingOptionalParameterIsValidAndMeetsDefault(
162-
"http-listen-host", config::getHttpListenHost, InetAddress.getLoopbackAddress());
164+
"http-listen-host",
165+
config::getHttpListenHost,
166+
InetAddress.getLoopbackAddress().getHostAddress());
163167
}
164168

165169
@Test
@@ -204,22 +208,6 @@ private <T> void missingOptionalParameterIsValidAndMeetsDefault(
204208
assertThat(commandOutput.toString()).isEmpty();
205209
}
206210

207-
@Test
208-
public void domainNamesDecodeIntoAnInetAddress() {
209-
final String input =
210-
"--downstream-http-host=google.com "
211-
+ "--downstream-http-port=5000 "
212-
+ "--downstream-http-request-timeout=10000 "
213-
+ "--http-listen-port=5001 "
214-
+ "--http-listen-host=localhost "
215-
+ "--chain-id=6 "
216-
+ "--logging=INFO";
217-
final String[] inputArgs = input.split(" ");
218-
219-
parser.parseCommandLine(inputArgs);
220-
assertThat(config.getDownstreamHttpHost().getHostName()).isEqualTo("google.com");
221-
}
222-
223211
@Test
224212
public void creatingSignerThrowsDisplaysFailureToCreateSignerText() {
225213
subCommand = new NullSignerSubCommand(true);

ethsigner/core/src/main/java/tech/pegasys/ethsigner/core/Config.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import tech.pegasys.ethsigner.core.signing.ChainIdProvider;
1616

17-
import java.net.InetAddress;
1817
import java.nio.file.Path;
1918
import java.time.Duration;
2019

@@ -24,13 +23,13 @@ public interface Config {
2423

2524
Level getLogLevel();
2625

27-
InetAddress getDownstreamHttpHost();
26+
String getDownstreamHttpHost();
2827

2928
Integer getDownstreamHttpPort();
3029

3130
Duration getDownstreamHttpRequestTimeout();
3231

33-
InetAddress getHttpListenHost();
32+
String getHttpListenHost();
3433

3534
Integer getHttpListenPort();
3635

ethsigner/core/src/main/java/tech/pegasys/ethsigner/core/EthSigner.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public void run() {
6262
final WebClientOptions clientOptions =
6363
new WebClientOptions()
6464
.setDefaultPort(config.getDownstreamHttpPort())
65-
.setDefaultHost(config.getDownstreamHttpHost().getHostAddress());
65+
.setDefaultHost(config.getDownstreamHttpHost());
6666
final HttpServerOptions serverOptions =
6767
new HttpServerOptions()
6868
.setPort(config.getHttpListenPort())
69-
.setHost(config.getHttpListenHost().getHostAddress())
69+
.setHost(config.getHttpListenHost())
7070
.setReuseAddress(true)
7171
.setReusePort(true);
7272
final Path dataPath = config.getDataPath();
@@ -96,10 +96,7 @@ public static JsonDecoder createJsonDecoder() {
9696

9797
private HttpService createWeb3jHttpService() {
9898
final String downstreamUrl =
99-
"http://"
100-
+ config.getDownstreamHttpHost().getHostName()
101-
+ ":"
102-
+ config.getDownstreamHttpPort();
99+
"http://" + config.getDownstreamHttpHost() + ":" + config.getDownstreamHttpPort();
103100
LOG.info("Downstream URL = {}", downstreamUrl);
104101

105102
final OkHttpClient.Builder builder = new OkHttpClient.Builder();

0 commit comments

Comments
 (0)