This is an automated email from the ASF dual-hosted git repository.
adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 42ddc7a1446 HDDS-17728. Bind secure RPC client to local address with
family matching the server's (#11970)
42ddc7a1446 is described below
commit 42ddc7a144630baa34459f326ab9c8988a82418a
Author: Siyao Meng <[email protected]>
AuthorDate: Sat Nov 7 07:19:33 2026 -0800
HDDS-17728. Bind secure RPC client to local address with family matching
the server's (#11970)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../main/java/org/apache/hadoop/ipc_/Client.java | 29 ++++++++-
.../java/org/apache/hadoop/ipc_/TestClient.java | 76 ++++++++++++++++++----
2 files changed, 90 insertions(+), 15 deletions(-)
diff --git
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ipc_/Client.java
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ipc_/Client.java
index 28f6b5ed4cb..1219dda87e4 100644
--- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ipc_/Client.java
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ipc_/Client.java
@@ -227,6 +227,33 @@ Call createCall(RPC.RpcKind rpcKind, Writable rpcRequest) {
return new Call(rpcKind, rpcRequest);
}
+ /**
+ * Returns a local address of {@code host} to bind the client socket to, in
the address family of
+ * {@code server}, or null if there is none. On a dual-stack host the first
address of the host can be
+ * of the other family, and binding to it would make the connection fail.
+ */
+ static InetAddress getLocalBindAddress(String host, InetAddress server)
throws SocketException {
+ InetAddress localAddr = NetUtils.getLocalInetAddress(host);
+ if (localAddr == null || localAddr.getClass() == server.getClass()) {
+ return localAddr;
+ }
+ try {
+ return getLocalAddressOfFamily(InetAddress.getAllByName(host), server);
+ } catch (UnknownHostException e) {
+ return null;
+ }
+ }
+
+ /** Returns the first local address in {@code candidates} in the address
family of {@code server}. */
+ static InetAddress getLocalAddressOfFamily(InetAddress[] candidates,
InetAddress server) throws SocketException {
+ for (InetAddress candidate : candidates) {
+ if (candidate.getClass() == server.getClass() &&
NetworkInterface.getByInetAddress(candidate) != null) {
+ return candidate;
+ }
+ }
+ return null;
+ }
+
/**
* Class that represents an RPC call
*/
@@ -600,7 +627,7 @@ private synchronized void setupConnection(
String principal = ticket.getUserName();
String host = SecurityUtil.getHostFromPrincipal(principal);
// If host name is a valid local address then bind socket to it
- InetAddress localAddr = NetUtils.getLocalInetAddress(host);
+ InetAddress localAddr = getLocalBindAddress(host,
server.getAddress());
if (localAddr != null) {
this.socket.setReuseAddress(true);
InetAddress bindTo = bindToWildCardAddress ? null : localAddr;
diff --git
a/hadoop-hdds/common/src/test/java/org/apache/hadoop/ipc_/TestClient.java
b/hadoop-hdds/common/src/test/java/org/apache/hadoop/ipc_/TestClient.java
index 168a5406dd2..9482bc878c2 100644
--- a/hadoop-hdds/common/src/test/java/org/apache/hadoop/ipc_/TestClient.java
+++ b/hadoop-hdds/common/src/test/java/org/apache/hadoop/ipc_/TestClient.java
@@ -17,17 +17,23 @@
package org.apache.hadoop.ipc_;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import com.google.common.net.InetAddresses;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.net.NetworkInterface;
import java.net.Socket;
import java.util.function.Consumer;
import javax.net.SocketFactory;
@@ -36,6 +42,7 @@
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.security.KerberosInfo;
import org.apache.hadoop.security.UserGroupInformation;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
@@ -43,6 +50,11 @@
/** Regression tests for the copied Hadoop RPC client. */
class TestClient {
+ private static final InetAddress IPV4_LOOPBACK =
InetAddresses.forString("127.0.0.1");
+ private static final InetAddress IPV6_LOOPBACK =
InetAddresses.forString("::1");
+ // Documentation prefix (RFC 3849), never assigned to a local interface.
+ private static final InetAddress IPV6_REMOTE =
InetAddresses.forString("2001:db8::1");
+
@KerberosInfo(serverPrincipal = "test.server.principal")
private interface TestProtocol {
}
@@ -52,13 +64,54 @@ private interface TestProtocol {
void testKerberosSocketBinding(boolean wildcard) throws Exception {
Configuration conf = new Configuration(false);
conf.setBoolean("ipc.client.bind.wildcard.addr", wildcard);
- InetAddress localAddress = InetAddress.getByName("127.0.0.1");
- InetSocketAddress server = new InetSocketAddress(localAddress, 12345);
+ InetSocketAddress server = new InetSocketAddress(IPV4_LOOPBACK, 12345);
+ Socket socket = mock(Socket.class);
+ when(socket.getLocalAddress()).thenReturn(IPV4_LOOPBACK);
+
+ setupConnection(conf, "client/[email protected]", server, socket);
+
+ ArgumentCaptor<InetSocketAddress> boundAddress =
ArgumentCaptor.forClass(InetSocketAddress.class);
+ verify(socket).bind(boundAddress.capture());
+ assertEquals(wildcard,
boundAddress.getValue().getAddress().isAnyLocalAddress());
+ if (!wildcard) {
+ assertEquals(IPV4_LOOPBACK, boundAddress.getValue().getAddress());
+ }
+ assertEquals(0, boundAddress.getValue().getPort());
+ verify(socket).connect(eq(server), anyInt());
+ }
+
+ @Test
+ void testNoBindToAddressOfOtherFamily() throws Exception {
+ assumeIPv6LoopbackIsLocal();
+ InetSocketAddress server = new InetSocketAddress(IPV4_LOOPBACK, 12345);
+ Socket socket = mock(Socket.class);
+ when(socket.getLocalAddress()).thenReturn(IPV4_LOOPBACK);
+
+ // The principal host is a local IPv6 address only, so there is nothing to
bind to for an IPv4 server.
+ setupConnection(new Configuration(false), "client/::[email protected]",
server, socket);
+
+ verify(socket, never()).bind(any());
+ verify(socket).connect(eq(server), anyInt());
+ }
+
+ @Test
+ void testLocalAddressOfFamily() throws Exception {
+ assumeIPv6LoopbackIsLocal();
+ InetAddress[] dualStack = {IPV4_LOOPBACK, IPV6_LOOPBACK};
+
+ assertThat(Client.getLocalAddressOfFamily(dualStack,
IPV6_LOOPBACK)).isEqualTo(IPV6_LOOPBACK);
+ assertThat(Client.getLocalAddressOfFamily(dualStack,
IPV4_LOOPBACK)).isEqualTo(IPV4_LOOPBACK);
+ assertThat(Client.getLocalAddressOfFamily(new InetAddress[] {IPV6_REMOTE,
IPV6_LOOPBACK}, IPV6_REMOTE))
+ .isEqualTo(IPV6_LOOPBACK);
+ assertThat(Client.getLocalAddressOfFamily(new InetAddress[]
{IPV4_LOOPBACK}, IPV6_LOOPBACK)).isNull();
+ assertThat(Client.getLocalAddressOfFamily(new InetAddress[] {IPV6_REMOTE},
IPV6_LOOPBACK)).isNull();
+ }
+
+ private static void setupConnection(Configuration conf, String principal,
InetSocketAddress server,
+ Socket socket) throws Exception {
UserGroupInformation ticket = mock(UserGroupInformation.class);
when(ticket.hasKerberosCredentials()).thenReturn(true);
- when(ticket.getUserName()).thenReturn("client/[email protected]");
- Socket socket = mock(Socket.class);
- when(socket.getLocalAddress()).thenReturn(localAddress);
+ when(ticket.getUserName()).thenReturn(principal);
SocketFactory factory = mock(SocketFactory.class);
when(factory.createSocket()).thenReturn(socket);
Client client = new Client(LongWritable.class, conf, factory);
@@ -74,18 +127,13 @@ void testKerberosSocketBinding(boolean wildcard) throws
Exception {
Method setupConnection =
connectionClass.getDeclaredMethod("setupConnection",
UserGroupInformation.class);
setupConnection.setAccessible(true);
setupConnection.invoke(connection, ticket);
-
- ArgumentCaptor<InetSocketAddress> boundAddress =
ArgumentCaptor.forClass(InetSocketAddress.class);
- verify(socket).bind(boundAddress.capture());
- assertEquals(wildcard,
boundAddress.getValue().getAddress().isAnyLocalAddress());
- if (!wildcard) {
- assertEquals(localAddress, boundAddress.getValue().getAddress());
- }
- assertEquals(0, boundAddress.getValue().getPort());
- verify(socket).connect(eq(server), anyInt());
} finally {
socket.close();
client.stop();
}
}
+
+ private static void assumeIPv6LoopbackIsLocal() throws Exception {
+ assumeTrue(NetworkInterface.getByInetAddress(IPV6_LOOPBACK) != null, "IPv6
loopback is not available");
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]