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.
[improve][proxy] Add TLS transport encryption for broker client (apac…
- Loading branch information
Showing
4 changed files
with
269 additions
and
3 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
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
138 changes: 138 additions & 0 deletions
138
pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyMutualTlsTest.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,138 @@ | ||
/** | ||
* 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.proxy.server; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.testng.Assert.assertThrows; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
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.api.Producer; | ||
import org.apache.pulsar.client.api.PulsarClient; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.client.api.Schema; | ||
import org.apache.pulsar.client.impl.auth.AuthenticationTls; | ||
import org.apache.pulsar.common.configuration.PulsarConfigurationLoader; | ||
import org.apache.pulsar.metadata.impl.ZKMetadataStore; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
public class ProxyMutualTlsTest extends MockedPulsarServiceBaseTest { | ||
|
||
private final String TLS_TRUST_CERT_FILE_PATH = "./src/test/resources/authentication/tls/cacert.pem"; | ||
private final String TLS_PROXY_CERT_FILE_PATH = "./src/test/resources/authentication/tls/server-cert.pem"; | ||
private final String TLS_PROXY_KEY_FILE_PATH = "./src/test/resources/authentication/tls/server-key.pem"; | ||
private final String TLS_CLIENT_CERT_FILE_PATH = "./src/test/resources/authentication/tls/client-cert.pem"; | ||
private final String TLS_CLIENT_KEY_FILE_PATH = "./src/test/resources/authentication/tls/client-key.pem"; | ||
|
||
private ProxyService proxyService; | ||
private ProxyConfiguration proxyConfig = new ProxyConfiguration(); | ||
|
||
@Override | ||
@BeforeClass | ||
protected void setup() throws Exception { | ||
internalSetup(); | ||
|
||
proxyConfig.setServicePort(Optional.of(0)); | ||
proxyConfig.setBrokerProxyAllowedTargetPorts("*"); | ||
proxyConfig.setServicePortTls(Optional.of(0)); | ||
proxyConfig.setWebServicePort(Optional.of(0)); | ||
proxyConfig.setWebServicePortTls(Optional.of(0)); | ||
proxyConfig.setTlsCertificateFilePath(TLS_PROXY_CERT_FILE_PATH); | ||
proxyConfig.setTlsKeyFilePath(TLS_PROXY_KEY_FILE_PATH); | ||
proxyConfig.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH); | ||
proxyConfig.setMetadataStoreUrl(DUMMY_VALUE); | ||
proxyConfig.setConfigurationMetadataStoreUrl(GLOBAL_DUMMY_VALUE); | ||
proxyConfig.setTlsRequireTrustedClientCertOnConnect(true); | ||
proxyConfig.setTlsAllowInsecureConnection(false); | ||
|
||
proxyService = Mockito.spy(new ProxyService(proxyConfig, new AuthenticationService( | ||
PulsarConfigurationLoader.convertFrom(proxyConfig)))); | ||
doReturn(new ZKMetadataStore(mockZooKeeper)).when(proxyService).createLocalMetadataStore(); | ||
doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(proxyService).createConfigurationMetadataStore(); | ||
|
||
proxyService.start(); | ||
} | ||
|
||
@Override | ||
@AfterClass(alwaysRun = true) | ||
protected void cleanup() throws Exception { | ||
internalCleanup(); | ||
|
||
proxyService.close(); | ||
} | ||
|
||
@Test | ||
public void testProducerByTlsTransport() throws Exception { | ||
@Cleanup | ||
PulsarClient client = PulsarClient.builder() | ||
.serviceUrl(proxyService.getServiceUrlTls()) | ||
.allowTlsInsecureConnection(false) | ||
.tlsKeyFilePath(TLS_CLIENT_KEY_FILE_PATH) | ||
.tlsCertificateFilePath(TLS_CLIENT_CERT_FILE_PATH) | ||
.tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH) | ||
.operationTimeout(3, TimeUnit.SECONDS) | ||
.build(); | ||
@Cleanup | ||
Producer<byte[]> producer = | ||
client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/"+ UUID.randomUUID()).create(); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
producer.send("test".getBytes()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testProducerByAuthenticationTls() throws Exception { | ||
@Cleanup | ||
PulsarClient client = PulsarClient.builder() | ||
.serviceUrl(proxyService.getServiceUrlTls()) | ||
.allowTlsInsecureConnection(false) | ||
.tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH) | ||
.authentication(new AuthenticationTls(TLS_CLIENT_CERT_FILE_PATH, TLS_CLIENT_KEY_FILE_PATH)) | ||
.operationTimeout(3, TimeUnit.SECONDS) | ||
.build(); | ||
@Cleanup | ||
Producer<byte[]> producer = | ||
client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/"+ UUID.randomUUID()).create(); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
producer.send("test".getBytes()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testProducerNegative() throws Exception { | ||
@Cleanup | ||
PulsarClient client = PulsarClient.builder() | ||
.serviceUrl(proxyService.getServiceUrlTls()) | ||
.allowTlsInsecureConnection(false) | ||
.tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH) | ||
.operationTimeout(3, TimeUnit.SECONDS) | ||
.build(); | ||
|
||
assertThrows(PulsarClientException.class, | ||
() -> client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/"+ UUID.randomUUID()).create()); | ||
} | ||
} |
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