Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public NettyChannelBuilder build() {
case LocationSchemes.GRPC_TLS:
{
final int port = location.getUri().getPort();
if (port < 0 || port > 65535) {
if (port < 1 || port > 65535) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

URI.getPort() returns -1 to mean "no port present in the URI", not "the user asked for port -1".
Folding it into this range check produces Invalid port -1: must be between 1 and 65535 (a number that the user never defined).

That path is reachable: a Location like grpc+tls://myhost with no port, and also ArrowFlightSqlClientHandler.getStreams(), which does withPort(endpointUri.getPort()) on an endpoint URI that may carry no port.

Since this PR is rewriting the message anyway, this seems like the moment to split
the two cases:

if (port == -1) {
  throw new IllegalArgumentException(
      "No port specified in location URI: " + location.getUri());
}
if (port < 1 || port > 65535) {
  throw new IllegalArgumentException(
      "Invalid port " + port + ": must be between 1 and 65535.");
}

throw new IllegalArgumentException(
"Invalid port " + port + ": must be between 0 and 65535.");
"Invalid port " + port + ": must be between 1 and 65535.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This narrows the accepted input range of published public API: NettyClientBuilder,
and transitively FlightClient.Builder. Third-party code that today calls FlightClient.builder(allocator, someLocation).build() with a port-0 location
(a server location captured before start(), or a placeholder resolved later) gets a lazily-failing channel today and an immediate IllegalArgumentException after this.

The change itself is right, but neither NettyClientBuilder.build() nor
FlightClient.Builder.build() documents the precondition.

Could we add an @throws IllegalArgumentException javadoc stating the accepted range, so callers can
discover it without hitting it in production?

}
builder = NettyChannelBuilder.forAddress(location.getUri().getHost(), port);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception {
try (ArrowFlightSqlClientHandler client =
new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_EXTENSION.getHost())
.withPort(FLIGHT_SERVER_TEST_EXTENSION.getPort())
.withTlsRootCertificates(tlsRootCertsPath)
.withClientCertificate(clientMTlsCertPath)
.withClientKey(clientMTlsKeyPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/** Tests for {@link Connection}. */
public class ConnectionTest {
Expand Down Expand Up @@ -204,21 +206,20 @@ public void testGetBasicClientAuthenticatedShouldOpenConnection() throws Excepti
}

/**
* Checks if the exception IllegalArgumentException is thrown when trying to establish an
* unencrypted connection providing with an invalid port.
*
* @throws SQLException on error.
* Checks if a SQLException is thrown when trying to establish an unencrypted connection with an
* invalid port.
*/
@Test
public void testUnencryptedConnectionProvidingInvalidPort() throws Exception {
@ParameterizedTest
@ValueSource(ints = {0, -1, 65536, 65537})
public void testUnencryptedConnectionProvidingInvalidPort(int invalidPort) {
final Properties properties = new Properties();

properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost");
properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest);
properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false);
final String invalidUrl =
"jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_EXTENSION.getHost() + ":" + 65537;
"jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_EXTENSION.getHost() + ":" + invalidPort;

assertThrows(
SQLException.class,
Expand All @@ -240,6 +241,7 @@ public void testGetBasicClientNoAuthShouldOpenConnection() throws Exception {
try (ArrowFlightSqlClientHandler client =
new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_EXTENSION.getHost())
.withPort(FLIGHT_SERVER_TEST_EXTENSION.getPort())
.withBufferAllocator(allocator)
.withEncryption(false)
.build()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception {
try (ArrowFlightSqlClientHandler client =
new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_EXTENSION.getHost())
.withPort(FLIGHT_SERVER_TEST_EXTENSION.getPort())
.withTlsRootCertificates(tlsRootCertsPath)
.withBufferAllocator(allocator)
.withEncryption(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception {
try (ArrowFlightSqlClientHandler client =
new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_EXTENSION.getHost())
.withPort(FLIGHT_SERVER_TEST_EXTENSION.getPort())
.withSystemTrustStore(false)
.withTrustStorePath(trustStorePath)
.withTrustStorePassword(trustStorePass)
Expand Down
Loading