forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIP-60] [Proxy-Server] Support SNI routing to support various proxy-…
…server in pulsar (apache#6566) ### Motivation Implementation of [PIP-60](https://github.com/apache/pulsar/wiki/PIP-60:-Support-Proxy-server-with-SNI-routing) A proxy server is a go‑between or intermediary server that forwards requests from multiple clients to different servers across the Internet. The proxy server can act as a “traffic cop,” in both forward and reverse proxy scenarios, and adds various benefits in your system such as load balancing, performance, security, auto-scaling, etc.. There are already many proxy servers already available in the market which are fast, scalable and more importantly covers various essential security aspects that are needed by the large organization to securely share their confidential data over the network. Pulsar already provides proxy implementation PIP-1 which acts as a reverse proxy and creates a gateway in front of brokers. However, pulsar doesn’t provide support to use other proxies such as Apache traffic server (ATS), HAProxy, Nginx, Envoy those are more scalable and secured. Most of these proxy-servers support SNI ROUTING which can route traffic to a destination without having to terminate the SSL connection. Routing at layer 4 gives greater transparency because the outbound connection is determined by examining the destination address in the client TCP packets. [Netty supports sending SNI header on TLS handshake](netty/netty#3801 (comment)) and this PR uses that Netty feature to send SNI header while connecting to proxy. ### Modification https://github.com/apache/pulsar/wiki/PIP-60:-Support-Proxy-server-with-SNI-routing#changes **Note:** we have fully tested this changes with ATS proxy for both forward and reverse proxy scenarios. And I have also shared e2e example in PIP to use ATS proxy for client and broker integration.
- Loading branch information
1 parent
8730dbe
commit fba2a94
Showing
12 changed files
with
351 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
pulsar-broker/src/test/java/org/apache/pulsar/client/api/ProxyProtocolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.api; | ||
|
||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.fail; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.pulsar.client.impl.auth.AuthenticationTls; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testng.annotations.Test; | ||
|
||
import lombok.Cleanup; | ||
|
||
public class ProxyProtocolTest extends TlsProducerConsumerBase { | ||
private static final Logger log = LoggerFactory.getLogger(ProxyProtocolTest.class); | ||
|
||
@Test | ||
public void testSniProxyProtocol() throws Exception { | ||
|
||
// Client should try to connect to proxy and pass broker-url as SNI header | ||
String proxyUrl = pulsar.getBrokerServiceUrlTls(); | ||
String brokerServiceUrl = "pulsar+ssl://1.1.1.1:6651"; | ||
String topicName = "persistent://my-property/use/my-ns/my-topic1"; | ||
|
||
ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(brokerServiceUrl) | ||
.tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false) | ||
.proxyServiceUrl(proxyUrl, ProxyProtocol.SNI).operationTimeout(1000, TimeUnit.MILLISECONDS); | ||
Map<String, String> authParams = new HashMap<>(); | ||
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH); | ||
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH); | ||
clientBuilder.authentication(AuthenticationTls.class.getName(), authParams); | ||
|
||
@Cleanup | ||
PulsarClient pulsarClient = clientBuilder.build(); | ||
|
||
// should be able to create producer successfully | ||
pulsarClient.newProducer().topic(topicName).create(); | ||
} | ||
|
||
@Test | ||
public void testSniProxyProtocolWithInvalidProxyUrl() throws Exception { | ||
|
||
// Client should try to connect to proxy and pass broker-url as SNI header | ||
String brokerServiceUrl = "pulsar+ssl://1.1.1.1:6651"; | ||
String proxyHost = "invalid-url"; | ||
String proxyUrl = "pulsar+ssl://" + proxyHost + ":5555"; | ||
String topicName = "persistent://my-property/use/my-ns/my-topic1"; | ||
|
||
ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(brokerServiceUrl) | ||
.tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false) | ||
.proxyServiceUrl(proxyUrl, ProxyProtocol.SNI).operationTimeout(1000, TimeUnit.MILLISECONDS); | ||
Map<String, String> authParams = new HashMap<>(); | ||
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH); | ||
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH); | ||
clientBuilder.authentication(AuthenticationTls.class.getName(), authParams); | ||
|
||
@Cleanup | ||
PulsarClient pulsarClient = clientBuilder.build(); | ||
|
||
try { | ||
pulsarClient.newProducer().topic(topicName).create(); | ||
fail("should have failed due to invalid url"); | ||
} catch (PulsarClientException e) { | ||
assertTrue(e.getMessage().contains(proxyHost)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSniProxyProtocolWithoutTls() throws Exception { | ||
// Client should try to connect to proxy and pass broker-url as SNI header | ||
String proxyUrl = pulsar.getBrokerServiceUrl(); | ||
String brokerServiceUrl = "pulsar+ssl://1.1.1.1:6651"; | ||
String topicName = "persistent://my-property/use/my-ns/my-topic1"; | ||
|
||
ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(brokerServiceUrl) | ||
.proxyServiceUrl(proxyUrl, ProxyProtocol.SNI).operationTimeout(1000, TimeUnit.MILLISECONDS); | ||
|
||
@Cleanup | ||
PulsarClient pulsarClient = clientBuilder.build(); | ||
|
||
try { | ||
pulsarClient.newProducer().topic(topicName).create(); | ||
fail("should have failed due to non-tls url"); | ||
} catch (PulsarClientException e) { | ||
// Ok | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/ProxyProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.api; | ||
|
||
/** | ||
* Protcol type to determine type of proxy routing when client connects to proxy using | ||
* {@link ClientBuilder::proxyServiceUrl}. | ||
*/ | ||
public enum ProxyProtocol { | ||
/** | ||
* Follows SNI-routing | ||
* https://docs.trafficserver.apache.org/en/latest/admin-guide/layer-4-routing.en.html#sni-routing. | ||
**/ | ||
SNI | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.