Skip to content

Add host address to BindTransportException message #51269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,17 @@ public void testBindUnavailableAddress() {
xContentRegistry(), new NullDispatcher())) {
transport.start();
TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).build();
Settings settings = Settings.builder()
.put("http.port", remoteAddress.getPort())
.put("network.host", remoteAddress.getAddress())
.build();
try (Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool,
xContentRegistry(), new NullDispatcher())) {
BindHttpException bindHttpException = expectThrows(BindHttpException.class, otherTransport::start);
assertEquals("Failed to bind to [" + remoteAddress.getPort() + "]", bindHttpException.getMessage());
assertEquals(
"Failed to bind to [" + remoteAddress.getPort() + "] on " + remoteAddress.address().getAddress().getHostAddress(),
bindHttpException.getMessage()
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ public void testBindUnavailableAddress() {
threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger))) {
transport.start();
TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).build();
Settings settings = Settings.builder()
.put("http.port", remoteAddress.getPort())
.put("network.host", remoteAddress.getAddress())
.build();
try (NioHttpServerTransport otherTransport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler,
threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger))) {
BindHttpException bindHttpException = expectThrows(BindHttpException.class, () -> otherTransport.start());
assertEquals("Failed to bind to [" + remoteAddress.getPort() + "]", bindHttpException.getMessage());
assertEquals(
"Failed to bind to [" + remoteAddress.getPort() + "] on " + remoteAddress.address().getAddress().getHostAddress(),
bindHttpException.getMessage()
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ private TransportAddress bindAddress(final InetAddress hostAddress) {
return true;
});
if (!success) {
throw new BindHttpException("Failed to bind to [" + port.getPortRangeString() + "]", lastException.get());
throw new BindHttpException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is failing forbidden APIs checks, see the logs:

Forbidden method invocation: java.net.InetAddress#getHostAddress() [use NetworkAddress format/formatAddress to print IP or IP+ports]
  in org.elasticsearch.transport.TcpTransport (TcpTransport.java:381)
Forbidden method invocation: java.net.InetAddress#getHostAddress() [use NetworkAddress format/formatAddress to print IP or IP+ports]
  in org.elasticsearch.http.AbstractHttpServerTransport (AbstractHttpServerTransport.java:178)
Scanned 5715 class file(s) for forbidden API invocations (in 5.20s), 2 error(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @original-brownbear, thanks for reviewing this. I ran the tests but forgot to run the check task. Will fix this ASAP.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mariaral FYI, you can just run these simple static code checks via:

./gradlew precommit 

no need to run a full check for those

Copy link
Contributor Author

@mariaral mariaral Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @original-brownbear for the tip. I didn't know of the precommit task.

I'm thinking of extending NetworkAddress.format() to be able to handle PortsRange objects and use that instead of coping the same message in five places. I propose using the following format:

    IPv4 no port: 127.0.0.1
    IPv4 single port: 127.0.0.1:9300
    IPv4 multiple ports: 127.0.0.1:[9300-9400]
    IPv6 multiple ports: [::1]:[9300-9400]

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only count two spots where we print the PortsRange but fine by me to keep it consistent via a utility method :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a new commit. Please take a look.

"Failed to bind to [" + port.getPortRangeString() + "] on " + hostAddress.getHostAddress(),
lastException.get()
);
}

if (logger.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ private InetSocketAddress bindToPort(final String name, final InetAddress hostAd
return true;
});
if (!success) {
throw new BindTransportException("Failed to bind to [" + port + "]", lastException.get());
throw new BindTransportException(
"Failed to bind to [" + port + "] on " + hostAddress.getHostAddress(),
lastException.get()
);
}
} finally {
closeLock.writeLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2698,13 +2698,15 @@ public void testProfilesIncludesDefault() {

public void testBindUnavailableAddress() {
int port = serviceA.boundAddress().publishAddress().getPort();
String address = serviceA.boundAddress().publishAddress().getAddress();
Settings settings = Settings.builder()
.put(Node.NODE_NAME_SETTING.getKey(), "foobar")
.put(TransportSettings.HOST.getKey(), address)
.put(TransportSettings.PORT.getKey(), port)
.build();
BindTransportException bindTransportException = expectThrows(BindTransportException.class,
() -> buildService("test", Version.CURRENT, settings));
assertEquals("Failed to bind to ["+ port + "]", bindTransportException.getMessage());
assertEquals("Failed to bind to [" + port + "] on " + address, bindTransportException.getMessage());
}

public void testChannelCloseWhileConnecting() {
Expand Down