Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion buildSrc/src/main/resources/forbidden/jdk-signatures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ java.net.MulticastSocket#<init>(int)
java.net.ServerSocket#<init>(int)
java.net.ServerSocket#<init>(int,int)

@defaultMessage use NetworkAddress format/formatAddress to print IP or IP+ports
@defaultMessage use NetworkAddress format() to print IP or IP+ports
java.net.InetAddress#toString()
java.net.InetAddress#getHostAddress()
java.net.Inet4Address#getHostAddress()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.netty.handler.codec.http.HttpVersion;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -195,11 +196,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 " + NetworkAddress.format(remoteAddress.address()),
bindHttpException.getMessage()
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.netty.handler.codec.http.HttpVersion;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -190,11 +191,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 " + NetworkAddress.format(remoteAddress.address()),
bindHttpException.getMessage()
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.common.network;

import org.elasticsearch.common.transport.PortsRange;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -66,7 +68,7 @@ private NetworkAddress() {}
* @return formatted string
*/
public static String format(InetAddress address) {
return format(address, -1);
return format(address, new PortsRange(""));
}

/**
Expand All @@ -88,21 +90,64 @@ public static String format(InetSocketAddress address) {
return format(address.getAddress(), address.getPort());
}

// note, we don't validate port, because we only allow InetSocketAddress
static String format(InetAddress address, int port) {
/**
* Formats a network address and port for display purposes.
* <p>
* This formats the address with {@link #format(InetAddress)}
* and appends the port number. IPv6 addresses will be bracketed.
* Any host information, if present is ignored.
* <p>
* Example output:
* <ul>
* <li>IPv4: {@code 127.0.0.1:9300}</li>
* <li>IPv6: {@code [::1]:9300}</li>
* </ul>
* @param address IPv4 or IPv6 address
* @param port port
* @return formatted string
*/
public static String format(InetAddress address, int port) {
return format(address, new PortsRange(String.valueOf(port)));
}

/**
* Formats a network address and port range for display purposes.
* <p>
* This formats the address with {@link #format(InetAddress)}
* and appends the port range in brackets. In case there is only one
* port, the result is the same with {@link #format(InetAddress, int)}.
* <p>
* Example output:
* <ul>
* <li>IPv4 no port: {@code 127.0.0.1}</li>
* <li>IPv4 single port: {@code 127.0.0.1:9300}</li>
* <li>IPv4 multiple ports: {@code 127.0.0.1:[9300-9400]}</li>
* <li>IPv6 multiple ports: {@code [::1]:[9300-9400]}</li>
* </ul>
* @param address IPv4 or IPv6 address
* @param portsRange range of ports
* @return formatted string
*/
public static String format(InetAddress address, PortsRange portsRange) {
Objects.requireNonNull(address);

StringBuilder builder = new StringBuilder();

if (port != -1 && address instanceof Inet6Address) {
int numberOfPorts = portsRange.ports().length;

if (numberOfPorts != 0 && address instanceof Inet6Address) {
builder.append(InetAddresses.toUriString(address));
} else {
builder.append(InetAddresses.toAddrString(address));
}

if (port != -1) {
if (numberOfPorts != 0) {
builder.append(':');
builder.append(port);
if (numberOfPorts == 1) {
builder.append(portsRange.getPortRangeString());
} else {
builder.append("[").append(portsRange.getPortRangeString()).append("]");
}
}

return builder.toString();
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
Contributor

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
Contributor

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
Contributor

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 " + NetworkAddress.format(hostAddress, port),
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 " + NetworkAddress.format(hostAddress, portsRange),
lastException.get()
);
}
} finally {
closeLock.writeLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.network.CloseableChannel;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.network.NetworkUtils;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
Expand Down Expand Up @@ -2698,13 +2699,16 @@ 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());
InetSocketAddress inetSocketAddress = serviceA.boundAddress().publishAddress().address();
assertEquals("Failed to bind to " + NetworkAddress.format(inetSocketAddress), bindTransportException.getMessage());
}

public void testChannelCloseWhileConnecting() {
Expand Down