You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/10/20 16:26:10 UTC

(pulsar) branch branch-4.2 updated: [fix][proxy] Fix Proxy Protocol forwarding for UNKNOWN and IPv6 (#26999)

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 069d048b1e60 [fix][proxy] Fix Proxy Protocol forwarding for UNKNOWN and IPv6 (#26999)
069d048b1e60 is described below

commit 069d048b1e604266c1126e09eb84a8077d3b2f25
Author: sinan liu <li...@gmail.com>
AuthorDate: Tue Oct 20 23:12:41 2026 +0800

    [fix][proxy] Fix Proxy Protocol forwarding for UNKNOWN and IPv6 (#26999)
    
    (cherry picked from commit fff1b119fe1b23f88be58c3567f747f405a73775)
---
 .../pulsar/proxy/server/DirectProxyHandler.java    |  8 ++-
 .../server/ProxyEnableHAProxyProtocolTest.java     | 73 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java
index 773cbc5fbcd9..d81c8c4eb7c7 100644
--- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java
+++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java
@@ -42,6 +42,7 @@ import io.netty.handler.flush.FlushConsolidationHandler;
 import io.netty.handler.ssl.SslHandler;
 import io.netty.handler.timeout.ReadTimeoutHandler;
 import io.netty.util.CharsetUtil;
+import java.net.Inet6Address;
 import java.net.InetSocketAddress;
 import java.util.Arrays;
 import java.util.Map;
@@ -225,8 +226,10 @@ public class DirectProxyHandler {
                 InetSocketAddress proxyAddress = (InetSocketAddress) inboundChannel.localAddress();
                 String destinationAddress = proxyAddress.getAddress().getHostAddress();
                 int destinationPort = proxyAddress.getPort();
+                HAProxyProxiedProtocol protocol = clientAddress.getAddress() instanceof Inet6Address
+                        ? HAProxyProxiedProtocol.TCP6 : HAProxyProxiedProtocol.TCP4;
                 HAProxyMessage msg = new HAProxyMessage(HAProxyProtocolVersion.V1, HAProxyCommand.PROXY,
-                        HAProxyProxiedProtocol.TCP4, sourceAddress, destinationAddress, sourcePort,
+                        protocol, sourceAddress, destinationAddress, sourcePort,
                         destinationPort);
                 final ByteBuf encodedMsg = encodeProxyProtocolMessage(msg);
                 writeAndFlush(encodedMsg);
@@ -238,6 +241,9 @@ public class DirectProxyHandler {
 
 
     private ByteBuf encodeProxyProtocolMessage(HAProxyMessage msg) {
+        if (msg.proxiedProtocol() == HAProxyProxiedProtocol.UNKNOWN) {
+            return Unpooled.copiedBuffer("PROXY UNKNOWN\r\n", CharsetUtil.US_ASCII);
+        }
         // Max length of v1 version proxy protocol message is 108
         ByteBuf out = Unpooled.buffer(108);
         out.writeBytes(TEXT_PREFIX);
diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyEnableHAProxyProtocolTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyEnableHAProxyProtocolTest.java
index 5d4d8324953e..d1f122a8ff0f 100644
--- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyEnableHAProxyProtocolTest.java
+++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyEnableHAProxyProtocolTest.java
@@ -18,18 +18,33 @@
  */
 package org.apache.pulsar.proxy.server;
 
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.doReturn;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.haproxy.HAProxyCommand;
+import io.netty.handler.codec.haproxy.HAProxyMessage;
+import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
+import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol;
+import java.util.HexFormat;
 import java.util.Optional;
+import java.util.concurrent.TimeUnit;
 import lombok.Cleanup;
 import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
 import org.apache.pulsar.broker.authentication.AuthenticationService;
 import org.apache.pulsar.client.admin.PulsarAdminException;
 import org.apache.pulsar.client.api.Authentication;
 import org.apache.pulsar.client.api.AuthenticationFactory;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.InjectedClientCnxClientBuilder;
 import org.apache.pulsar.client.api.PulsarClient;
 import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.impl.ClientBuilderImpl;
+import org.apache.pulsar.client.impl.ClientCnx;
 import org.apache.pulsar.client.impl.ConsumerImpl;
 import org.apache.pulsar.client.impl.ProducerImpl;
+import org.apache.pulsar.client.impl.metrics.InstrumentProvider;
 import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
 import org.apache.pulsar.common.policies.data.SubscriptionStats;
 import org.apache.pulsar.common.policies.data.TopicStats;
@@ -40,6 +55,7 @@ import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 public class ProxyEnableHAProxyProtocolTest extends MockedPulsarServiceBaseTest {
@@ -88,6 +104,63 @@ public class ProxyEnableHAProxyProtocolTest extends MockedPulsarServiceBaseTest
         }
     }
 
+    @DataProvider
+    public Object[][] proxyProtocolHeaders() {
+        return new Object[][] {
+                {"v1-unknown", "PROXY UNKNOWN\r\n".getBytes(US_ASCII), HAProxyProxiedProtocol.UNKNOWN},
+                {"v2-local", HexFormat.of().parseHex("0d0a0d0a000d0a515549540a20000000"),
+                        HAProxyProxiedProtocol.UNKNOWN},
+                {"v2-unknown", HexFormat.of().parseHex("0d0a0d0a000d0a515549540a21000000"),
+                        HAProxyProxiedProtocol.UNKNOWN},
+                {"v1-tcp4", "PROXY TCP4 198.51.100.22 203.0.113.7 35646 80\r\n".getBytes(US_ASCII),
+                        HAProxyProxiedProtocol.TCP4},
+                {"v1-tcp6", "PROXY TCP6 2001:db8::1 2001:db8::2 35646 80\r\n".getBytes(US_ASCII),
+                        HAProxyProxiedProtocol.TCP6},
+                {"v2-tcp4-tlv", HexFormat.of().parseHex(
+                        "0d0a0d0a000d0a515549540a21110011c6336416cb0071078b3e00500100026832"),
+                        HAProxyProxiedProtocol.TCP4},
+        };
+    }
+
+    @Test(dataProvider = "proxyProtocolHeaders")
+    public void testProxyProtocolForwarding(String name, byte[] header, HAProxyProxiedProtocol expectedProtocol)
+            throws Exception {
+        ClientBuilderImpl builder = (ClientBuilderImpl) PulsarClient.builder()
+                .serviceUrl(proxyService.getServiceUrl()).operationTimeout(5, TimeUnit.SECONDS);
+        // Supply only the bytes a load balancer would prepend; use the real client/proxy/broker handshake.
+        @Cleanup
+        PulsarClient client = InjectedClientCnxClientBuilder.create(builder,
+                (clientConf, eventLoopGroup) -> new ClientCnx(InstrumentProvider.NOOP, clientConf, eventLoopGroup) {
+                    @Override
+                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
+                        ctx.writeAndFlush(Unpooled.wrappedBuffer(header));
+                        super.channelActive(ctx);
+                    }
+                });
+        String topic = "persistent://public/default/proxy-protocol-" + name;
+        @Cleanup
+        Consumer<byte[]> consumer = client.newConsumer().topic(topic)
+                .subscriptionName("sub").subscribe();
+        HAProxyMessage forwarded = pulsar.getBrokerService().getTopicReference(topic).orElseThrow()
+                .getSubscription("sub").getConsumers().get(0).cnx().getHAProxyMessage();
+        assertThat(forwarded).isNotNull();
+        assertThat(forwarded.protocolVersion()).isEqualTo(HAProxyProtocolVersion.V1);
+        assertThat(forwarded.command()).isEqualTo(HAProxyCommand.PROXY);
+        assertThat(forwarded.proxiedProtocol()).isEqualTo(expectedProtocol);
+        if (expectedProtocol == HAProxyProxiedProtocol.UNKNOWN) {
+            assertThat(forwarded.sourceAddress()).isNull();
+            assertThat(forwarded.destinationAddress()).isNull();
+            assertThat(forwarded.sourcePort()).isZero();
+            assertThat(forwarded.destinationPort()).isZero();
+        } else {
+            boolean ipv6 = expectedProtocol == HAProxyProxiedProtocol.TCP6;
+            assertThat(forwarded.sourceAddress()).isEqualTo(ipv6 ? "2001:db8::1" : "198.51.100.22");
+            assertThat(forwarded.destinationAddress()).isEqualTo(ipv6 ? "2001:db8::2" : "203.0.113.7");
+            assertThat(forwarded.sourcePort()).isEqualTo(35646);
+            assertThat(forwarded.destinationPort()).isEqualTo(80);
+        }
+    }
+
     @Test
     public void testSimpleProduceAndConsume() throws PulsarClientException, PulsarAdminException {
         @Cleanup