mirror of https://github.com/hyperledger/besu
Ethstat retry fix (#5301)
* Fix ethstats retry logic. Attempt to connect with and without ssl. If --ethstats is specified without a port, use port 443 (for wss://) and port 80 (for ws://) as defaults instead of 3000. * Introduced optional --ethstats-cacert-file to specify root CA for ethstats server. Signed-off-by: Usman Saleem <usman@usmans.info>pull/5313/head
parent
a55e824eaa
commit
b5a5bded90
@ -0,0 +1,110 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethstats.util; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
||||
|
||||
import java.nio.file.Path; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.jupiter.params.ParameterizedTest; |
||||
import org.junit.jupiter.params.provider.ValueSource; |
||||
|
||||
public class EthStatsConnectOptionsTest { |
||||
|
||||
private final String VALID_NETSTATS_URL = "Dev-Node-1:secret-with-dashes@127.0.0.1:3001"; |
||||
|
||||
private final String CONTACT = "contact@mail.fr"; |
||||
|
||||
private final String ERROR_MESSAGE = |
||||
"Invalid netstats URL syntax. Netstats URL should have the following format 'nodename:secret@host:port' or 'nodename:secret@host'."; |
||||
|
||||
@Test |
||||
public void buildWithValidParams() { |
||||
final Path caCert = Path.of("./test.pem"); |
||||
final EthStatsConnectOptions ethStatsConnectOptions = |
||||
EthStatsConnectOptions.fromParams(VALID_NETSTATS_URL, CONTACT, caCert); |
||||
assertThat(ethStatsConnectOptions.getHost()).isEqualTo("127.0.0.1"); |
||||
assertThat(ethStatsConnectOptions.getNodeName()).isEqualTo("Dev-Node-1"); |
||||
assertThat(ethStatsConnectOptions.getPort()).isEqualTo(3001); |
||||
assertThat(ethStatsConnectOptions.getSecret()).isEqualTo("secret-with-dashes"); |
||||
assertThat(ethStatsConnectOptions.getContact()).isEqualTo(CONTACT); |
||||
assertThat(ethStatsConnectOptions.getCaCert()).isEqualTo(caCert); |
||||
} |
||||
|
||||
@ParameterizedTest(name = "#{index} - With Host {0}") |
||||
@ValueSource(strings = {"url-test.test.com", "url.test.com", "test.com", "10.10.10.15"}) |
||||
public void buildWithValidHost(final String host) { |
||||
final EthStatsConnectOptions ethStatsConnectOptions = |
||||
EthStatsConnectOptions.fromParams("Dev-Node-1:secret@" + host + ":3001", CONTACT, null); |
||||
assertThat(ethStatsConnectOptions.getHost()).isEqualTo(host); |
||||
} |
||||
|
||||
@ParameterizedTest(name = "#{index} - With Host {0}") |
||||
@ValueSource(strings = {"url-test.test.com", "url.test.com", "test.com", "10.10.10.15"}) |
||||
public void buildWithValidHostWithoutPort(final String host) { |
||||
final EthStatsConnectOptions ethStatsConnectOptions = |
||||
EthStatsConnectOptions.fromParams("Dev-Node-1:secret@" + host, CONTACT, null); |
||||
assertThat(ethStatsConnectOptions.getHost()).isEqualTo(host); |
||||
assertThat(ethStatsConnectOptions.getPort()).isEqualTo(-1); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectEmptyParams() { |
||||
assertThatThrownBy(() -> EthStatsConnectOptions.fromParams("", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectMissingParams() { |
||||
// missing node name
|
||||
assertThatThrownBy( |
||||
() -> EthStatsConnectOptions.fromParams("secret@127.0.0.1:3001", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// missing host
|
||||
assertThatThrownBy(() -> EthStatsConnectOptions.fromParams("Dev-Node-1:secret@", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// missing port
|
||||
assertThatThrownBy( |
||||
() -> EthStatsConnectOptions.fromParams("Dev-Node-1:secret@127.0.0.1:", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectInvalidParams() { |
||||
// invalid host
|
||||
assertThatThrownBy( |
||||
() -> |
||||
EthStatsConnectOptions.fromParams( |
||||
"Dev-Node-1:secret@127.0@0.1:3001", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// invalid port
|
||||
assertThatThrownBy( |
||||
() -> |
||||
EthStatsConnectOptions.fromParams( |
||||
"Dev-Node-1:secret@127.0.0.1:A001", CONTACT, null)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
} |
@ -1,100 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethstats.util; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
public class NetstatsUrlTest { |
||||
|
||||
private final String VALID_NETSTATS_URL = "Dev-Node-1:secret-with-dashes@127.0.0.1:3001"; |
||||
|
||||
private final String CONTACT = "contact@mail.fr"; |
||||
|
||||
private final String ERROR_MESSAGE = |
||||
"Invalid netstats URL syntax. Netstats URL should have the following format 'nodename:secret@host:port' or 'nodename:secret@host'."; |
||||
|
||||
@Test |
||||
public void buildWithValidParams() { |
||||
final NetstatsUrl netstatsUrl = NetstatsUrl.fromParams(VALID_NETSTATS_URL, CONTACT); |
||||
assertThat(netstatsUrl.getHost()).isEqualTo("127.0.0.1"); |
||||
assertThat(netstatsUrl.getNodeName()).isEqualTo("Dev-Node-1"); |
||||
assertThat(netstatsUrl.getPort()).isEqualTo(3001); |
||||
assertThat(netstatsUrl.getSecret()).isEqualTo("secret-with-dashes"); |
||||
assertThat(netstatsUrl.getContact()).isEqualTo(CONTACT); |
||||
} |
||||
|
||||
@Test |
||||
public void buildWithValidHost() { |
||||
final String[] validHosts = |
||||
new String[] {"url-test.test.com", "url.test.com", "test.com", "10.10.10.15"}; |
||||
for (String host : validHosts) { |
||||
final NetstatsUrl netstatsUrl = |
||||
NetstatsUrl.fromParams("Dev-Node-1:secret@" + host + ":3001", CONTACT); |
||||
assertThat(netstatsUrl.getHost()).isEqualTo(host); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void buildWithValidHostWithoutPort() { |
||||
final String[] validHosts = |
||||
new String[] {"url-test.test.com", "url.test.com", "test.com", "10.10.10.15"}; |
||||
for (String host : validHosts) { |
||||
final NetstatsUrl netstatsUrl = NetstatsUrl.fromParams("Dev-Node-1:secret@" + host, CONTACT); |
||||
assertThat(netstatsUrl.getHost()).isEqualTo(host); |
||||
assertThat(netstatsUrl.getPort()).isEqualTo(3000); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectEmptyParams() { |
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectMissingParams() { |
||||
// missing node name
|
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("secret@127.0.0.1:3001", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// missing host
|
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("Dev-Node-1:secret@", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// missing port
|
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("Dev-Node-1:secret@127.0.0.1:", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldDetectInvalidParams() { |
||||
// invalid host
|
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("Dev-Node-1:secret@127.0@0.1:3001", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
|
||||
// invalid port
|
||||
assertThatThrownBy(() -> NetstatsUrl.fromParams("Dev-Node-1:secret@127.0.0.1:A001", CONTACT)) |
||||
.isInstanceOf(IllegalArgumentException.class) |
||||
.hasMessageEndingWith(ERROR_MESSAGE); |
||||
} |
||||
} |
Loading…
Reference in new issue