From 77c2a1c59772dfda31845781dcf936159d06ee7d Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Fri, 29 Jul 2022 11:44:55 +0800 Subject: [PATCH] [fix][proxy] Fix client service url Signed-off-by: Zixuan Liu --- .../pulsar/proxy/server/ProxyConnection.java | 4 +++- .../proxy/server/ProxyConnectionTest.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java index 53c8d1bd4186a..0a9375ce699d0 100644 --- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java +++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyConnection.java @@ -565,8 +565,10 @@ protected void handleLookup(CommandLookupTopic lookup) { ClientConfigurationData createClientConfiguration() { ClientConfigurationData initialConf = new ClientConfigurationData(); - initialConf.setServiceUrl(service.getServiceUrl()); ProxyConfiguration proxyConfig = service.getConfiguration(); + initialConf.setServiceUrl( + proxyConfig.isTlsEnabledWithBroker() ? service.getServiceUrlTls() : service.getServiceUrl()); + // Apply all arbitrary configuration. This must be called before setting any fields annotated as // @Secret on the ClientConfigurationData object because of the way they are serialized. // See https://github.com/apache/pulsar/issues/8509 for more information. diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java index 5f533e37d3594..8c07e4b42d797 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyConnectionTest.java @@ -18,8 +18,12 @@ */ package org.apache.pulsar.proxy.server; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; +import org.apache.pulsar.client.impl.conf.ClientConfigurationData; import org.testng.annotations.Test; public class ProxyConnectionTest { @@ -35,4 +39,24 @@ public void testMatchesHostAndPort() { assertFalse(ProxyConnection .matchesHostAndPort("pulsar://", "pulsar://1.2.3.4:12345", "1.2.3.4:1234")); } + @Test + public void testCreateClientConfiguration() { + ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); + proxyConfiguration.setTlsEnabledWithBroker(true); + String proxyUrlTls = "pulsar+ssl://proxy:6651"; + String proxyUrl = "pulsar://proxy:6650"; + + ProxyService proxyService = mock(ProxyService.class); + doReturn(proxyConfiguration).when(proxyService).getConfiguration(); + doReturn(proxyUrlTls).when(proxyService).getServiceUrlTls(); + doReturn(proxyUrl).when(proxyService).getServiceUrl(); + + ProxyConnection proxyConnection = new ProxyConnection(proxyService, null); + ClientConfigurationData clientConfiguration = proxyConnection.createClientConfiguration(); + assertEquals(clientConfiguration.getServiceUrl(), proxyUrlTls); + + proxyConfiguration.setTlsEnabledWithBroker(false); + clientConfiguration = proxyConnection.createClientConfiguration(); + assertEquals(clientConfiguration.getServiceUrl(), proxyUrl); + } }