You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2023/03/01 18:25:30 UTC
[pulsar] branch branch-2.9 updated: [improve] Simplify enabling Broker, WS Proxy hostname verification (#19674)
This is an automated email from the ASF dual-hosted git repository.
mmarshall pushed a commit to branch branch-2.9
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/branch-2.9 by this push:
new c84c3b70837 [improve] Simplify enabling Broker, WS Proxy hostname verification (#19674)
c84c3b70837 is described below
commit c84c3b70837eead15265ddad036d16fd0351e157
Author: Michael Marshall <mm...@apache.org>
AuthorDate: Wed Mar 1 12:21:25 2023 -0600
[improve] Simplify enabling Broker, WS Proxy hostname verification (#19674)
When we merged #15818 in order to make the broker's client configurable, we did not add an explicit config for hostname verification. This PR adds that config to the broker and the websocket proxy. I chose the name `tlsHostnameVerificationEnabled` because that is what is already used in the proxy. It diverges from the function worker's config of `tlsEnableHostnameVerification`.
Before this PR, you would have enabled hostname verification by configuring `brokerClient_tlsHostnameVerificationEnable=true` in the broker and WS proxy configs. (Note that the variable name is slightly different because the `ClientConfiguration` does not have a `d` at the end of its name.
The remaining follow up work will be to update the `ClusterData` objects to configure hostname verification there to make it easier to configure hostname verification for remote clusters.
* Add `tlsHostnameVerificationEnabled` to the `broker.conf` and the `proxy.conf`
* Update all of the relevant locations that were previously only relying on `brokerClient_tlsHostnameVerificationEnable`
I added a single test to ensure that the `WebSocketProxyConfiguration` properly converts to the `ServiceConfiguration` object.
Otherwise, I verified that anywhere we are using `"brokerClient_"`, this PR also adds the right configuration.
This PR introduces a "new" configuration key, but not a new concept. All underlying behaviors are unchanged.
- [x] `doc-not-needed`
Docs are automatically updated by these changes.
(cherry picked from commit 6621fd3451a52bb7447ebeb845c83ac2f310b718)
(cherry picked from commit b8083b0e3f5a63c0fdaaf784cf2976dcff1fd68d)
(cherry picked from commit a3a242cc48e8d7454ee0c5c8fd872ae6f92ae4f7)
---
conf/broker.conf | 6 +++
conf/websocket.conf | 3 ++
.../apache/pulsar/broker/ServiceConfiguration.java | 5 ++
.../org/apache/pulsar/broker/PulsarService.java | 6 ++-
.../pulsar/broker/namespace/NamespaceService.java | 3 +-
.../pulsar/broker/service/BrokerService.java | 27 +++++++----
.../apache/pulsar/compaction/CompactorTool.java | 1 +
.../apache/pulsar/websocket/WebSocketService.java | 1 +
.../service/WebSocketProxyConfiguration.java | 6 ++-
.../websocket/WebSocketProxyConfigurationTest.java | 53 ++++++++++++++++++++++
10 files changed, 99 insertions(+), 12 deletions(-)
diff --git a/conf/broker.conf b/conf/broker.conf
index 329fd399f36..c6901ebebc7 100644
--- a/conf/broker.conf
+++ b/conf/broker.conf
@@ -594,6 +594,9 @@ tlsTrustCertsFilePath=
# though the cert will not be used for client authentication.
tlsAllowInsecureConnection=false
+# Whether the hostname is validated when the broker creates a TLS connection with other brokers
+tlsHostnameVerificationEnabled=false
+
# Specify the tls protocols the broker will use to negotiate during TLS handshake
# (a comma-separated list of protocol names).
# Examples:- [TLSv1.3, TLSv1.2]
@@ -898,6 +901,9 @@ bookkeeperTLSTrustCertsFilePath=
# Tls cert refresh duration at bookKeeper-client in seconds (0 to disable check)
bookkeeperTlsCertFilesRefreshDurationSeconds=300
+# Whether the hostname is validated when the broker creates a TLS connection to a bookkeeper
+bookkeeper_tlsHostnameVerificationEnabled=false
+
# Enable/disable disk weight based placement. Default is false
bookkeeperDiskWeightBasedPlacementEnabled=false
diff --git a/conf/websocket.conf b/conf/websocket.conf
index 4fe6f7e37b6..0376a717d85 100644
--- a/conf/websocket.conf
+++ b/conf/websocket.conf
@@ -92,6 +92,9 @@ brokerClientAuthenticationPlugin=
brokerClientAuthenticationParameters=
brokerClientTrustCertsFilePath=
+# Whether the hostname is validated when connecting to the broker.
+tlsHostnameVerificationEnabled=false
+
# You can add extra configuration options for the Pulsar Client
# by prefixing them with "brokerClient_". These configurations are applied after hard coded configuration
# and before the above brokerClient configurations named above.
diff --git a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
index f2155b4431c..458511a28fc 100644
--- a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
+++ b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
@@ -1161,6 +1161,11 @@ public class ServiceConfiguration implements PulsarConfiguration {
doc = "Accept untrusted TLS certificate from client"
)
private boolean tlsAllowInsecureConnection = false;
+ @FieldContext(
+ category = CATEGORY_TLS,
+ doc = "Whether the hostname is validated when the broker creates a TLS connection with other brokers"
+ )
+ private boolean tlsHostnameVerificationEnabled = false;
@FieldContext(
category = CATEGORY_TLS,
doc = "Specify the tls protocols the broker will use to negotiate during TLS Handshake.\n\n"
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
index 67f3b55cbfc..efcb2840556 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
@@ -1405,6 +1405,7 @@ public class PulsarService implements AutoCloseable, ShutdownService {
conf.setTlsCiphers(this.getConfiguration().getBrokerClientTlsCiphers());
conf.setTlsProtocols(this.getConfiguration().getBrokerClientTlsProtocols());
conf.setTlsAllowInsecureConnection(this.getConfiguration().isTlsAllowInsecureConnection());
+ conf.setTlsHostnameVerificationEnable(this.getConfiguration().isTlsHostnameVerificationEnabled());
if (this.getConfiguration().isBrokerClientTlsEnabledWithKeyStore()) {
conf.setUseKeyStoreTls(true);
conf.setTlsTrustStoreType(this.getConfiguration().getBrokerClientTlsTrustStoreType());
@@ -1468,7 +1469,8 @@ public class PulsarService implements AutoCloseable, ShutdownService {
} else {
builder.tlsTrustCertsFilePath(conf.getBrokerClientTrustCertsFilePath());
}
- builder.allowTlsInsecureConnection(conf.isTlsAllowInsecureConnection());
+ builder.allowTlsInsecureConnection(conf.isTlsAllowInsecureConnection())
+ .enableTlsHostnameVerification(conf.isTlsHostnameVerificationEnabled());
}
// most of the admin request requires to make zk-call so, keep the max read-timeout based on
@@ -1711,7 +1713,7 @@ public class PulsarService implements AutoCloseable, ShutdownService {
workerConfig.setTlsAllowInsecureConnection(brokerConfig.isTlsAllowInsecureConnection());
workerConfig.setTlsEnabled(brokerConfig.isTlsEnabled());
- workerConfig.setTlsEnableHostnameVerification(false);
+ workerConfig.setTlsEnableHostnameVerification(brokerConfig.isTlsHostnameVerificationEnabled());
workerConfig.setBrokerClientTrustCertsFilePath(brokerConfig.getTlsTrustCertsFilePath());
// client in worker will use this config to authenticate with broker
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
index 933e750242e..13f9cc98821 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
@@ -1297,7 +1297,8 @@ public class NamespaceService implements AutoCloseable {
? cluster.getBrokerServiceUrlTls() : cluster.getServiceUrlTls())
.enableTls(true)
.tlsTrustCertsFilePath(pulsar.getConfiguration().getBrokerClientTrustCertsFilePath())
- .allowTlsInsecureConnection(pulsar.getConfiguration().isTlsAllowInsecureConnection());
+ .allowTlsInsecureConnection(pulsar.getConfiguration().isTlsAllowInsecureConnection())
+ .enableTlsHostnameVerification(pulsar.getConfiguration().isTlsHostnameVerificationEnabled());
} else {
clientBuilder.serviceUrl(isNotBlank(cluster.getBrokerServiceUrl())
? cluster.getBrokerServiceUrl() : cluster.getServiceUrl());
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
index 123afa4c426..a9e5a769aac 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
@@ -1158,7 +1158,9 @@ public class BrokerService implements Closeable {
configTlsSettings(clientBuilder, serviceUrlTls,
data.isBrokerClientTlsEnabledWithKeyStore(), data.isTlsAllowInsecureConnection(),
data.getBrokerClientTlsTrustStoreType(), data.getBrokerClientTlsTrustStore(),
- data.getBrokerClientTlsTrustStorePassword(), data.getBrokerClientTrustCertsFilePath());
+ data.getBrokerClientTlsTrustStorePassword(), data.getBrokerClientTrustCertsFilePath(),
+ pulsar.getConfiguration().isTlsHostnameVerificationEnabled()
+ );
} else if (pulsar.getConfiguration().isBrokerClientTlsEnabled()) {
configTlsSettings(clientBuilder, serviceUrlTls,
pulsar.getConfiguration().isBrokerClientTlsEnabledWithKeyStore(),
@@ -1166,7 +1168,9 @@ public class BrokerService implements Closeable {
pulsar.getConfiguration().getBrokerClientTlsTrustStoreType(),
pulsar.getConfiguration().getBrokerClientTlsTrustStore(),
pulsar.getConfiguration().getBrokerClientTlsTrustStorePassword(),
- pulsar.getConfiguration().getBrokerClientTrustCertsFilePath());
+ pulsar.getConfiguration().getBrokerClientTrustCertsFilePath(),
+ pulsar.getConfiguration().isTlsHostnameVerificationEnabled()
+ );
} else {
clientBuilder.serviceUrl(
isNotBlank(data.getBrokerServiceUrl()) ? data.getBrokerServiceUrl() : data.getServiceUrl());
@@ -1192,11 +1196,14 @@ public class BrokerService implements Closeable {
private void configTlsSettings(ClientBuilder clientBuilder, String serviceUrl,
boolean brokerClientTlsEnabledWithKeyStore, boolean isTlsAllowInsecureConnection,
String brokerClientTlsTrustStoreType, String brokerClientTlsTrustStore,
- String brokerClientTlsTrustStorePassword, String brokerClientTrustCertsFilePath) {
+ String brokerClientTlsTrustStorePassword, String brokerClientTrustCertsFilePath,
+ boolean isTlsHostnameVerificationEnabled) {
clientBuilder
.serviceUrl(serviceUrl)
.enableTls(true)
- .allowTlsInsecureConnection(isTlsAllowInsecureConnection);
+ .allowTlsInsecureConnection(isTlsAllowInsecureConnection)
+ .enableTlsHostnameVerification(isTlsHostnameVerificationEnabled);
+
if (brokerClientTlsEnabledWithKeyStore) {
clientBuilder.useKeyStoreTls(true)
.tlsTrustStoreType(brokerClientTlsTrustStoreType)
@@ -1212,7 +1219,8 @@ public class BrokerService implements Closeable {
String brokerClientTlsTrustStoreType,
String brokerClientTlsTrustStore,
String brokerClientTlsTrustStorePassword,
- String brokerClientTrustCertsFilePath) {
+ String brokerClientTrustCertsFilePath,
+ boolean isTlsHostnameVerificationEnabled) {
if (brokerClientTlsEnabledWithKeyStore) {
adminBuilder.useKeyStoreTls(true)
.tlsTrustStoreType(brokerClientTlsTrustStoreType)
@@ -1221,7 +1229,8 @@ public class BrokerService implements Closeable {
} else {
adminBuilder.tlsTrustCertsFilePath(brokerClientTrustCertsFilePath);
}
- adminBuilder.allowTlsInsecureConnection(isTlsAllowInsecureConnection);
+ adminBuilder.allowTlsInsecureConnection(isTlsAllowInsecureConnection)
+ .enableTlsHostnameVerification(isTlsHostnameVerificationEnabled);
}
public PulsarAdmin getClusterPulsarAdmin(String cluster, Optional<ClusterData> clusterDataOp) {
@@ -1265,7 +1274,8 @@ public class BrokerService implements Closeable {
data.getBrokerClientTlsTrustStoreType(),
data.getBrokerClientTlsTrustStore(),
data.getBrokerClientTlsTrustStorePassword(),
- data.getBrokerClientTrustCertsFilePath()
+ data.getBrokerClientTrustCertsFilePath(),
+ conf.isTlsHostnameVerificationEnabled()
);
} else if (conf.isBrokerClientTlsEnabled()) {
configAdminTlsSettings(builder,
@@ -1274,7 +1284,8 @@ public class BrokerService implements Closeable {
conf.getBrokerClientTlsTrustStoreType(),
conf.getBrokerClientTlsTrustStore(),
conf.getBrokerClientTlsTrustStorePassword(),
- conf.getBrokerClientTrustCertsFilePath()
+ conf.getBrokerClientTrustCertsFilePath(),
+ conf.isTlsHostnameVerificationEnabled()
);
}
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
index 35ca089e5df..0764fb02ea9 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
@@ -123,6 +123,7 @@ public class CompactorTool {
clientBuilder
.serviceUrl(internalListener.getBrokerServiceUrlTls().toString())
.allowTlsInsecureConnection(brokerConfig.isTlsAllowInsecureConnection())
+ .enableTlsHostnameVerification(brokerConfig.isTlsHostnameVerificationEnabled())
.tlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());
} else {
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
index ad30d9dfabb..843e3a8ec82 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
@@ -179,6 +179,7 @@ public class WebSocketService implements Closeable {
.statsInterval(0, TimeUnit.SECONDS) //
.enableTls(config.isTlsEnabled()) //
.allowTlsInsecureConnection(config.isTlsAllowInsecureConnection()) //
+ .enableTlsHostnameVerification(config.isTlsHostnameVerificationEnabled())
.tlsTrustCertsFilePath(config.getBrokerClientTrustCertsFilePath()) //
.ioThreads(config.getWebSocketNumIoThreads()) //
.connectionsPerBroker(config.getWebSocketConnectionsPerBroker());
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/service/WebSocketProxyConfiguration.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/service/WebSocketProxyConfiguration.java
index b05b0cc0e32..f1c6cc5e989 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/service/WebSocketProxyConfiguration.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/service/WebSocketProxyConfiguration.java
@@ -119,6 +119,10 @@ public class WebSocketProxyConfiguration implements PulsarConfiguration {
@FieldContext(doc = "Path for the trusted TLS certificate file for outgoing connection to a server (broker)")
private String brokerClientTrustCertsFilePath = "";
+ // Note: name matches the ServiceConfiguration name to ensure correct mapping
+ @FieldContext(doc = "Enable TLS hostname verification when connecting to broker")
+ private boolean tlsHostnameVerificationEnabled = false;
+
@FieldContext(doc = "Number of IO threads in Pulsar client used in WebSocket proxy")
private int webSocketNumIoThreads = Runtime.getRuntime().availableProcessors();
@@ -153,7 +157,7 @@ public class WebSocketProxyConfiguration implements PulsarConfiguration {
@FieldContext(doc = "Path for the trusted TLS certificate file")
private String tlsTrustCertsFilePath = "";
- @FieldContext(doc = "Accept untrusted TLS certificate from client")
+ @FieldContext(doc = "Accept untrusted TLS certificate from client and broker")
private boolean tlsAllowInsecureConnection = false;
@FieldContext(doc = "Specify whether client certificates are required for "
diff --git a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/WebSocketProxyConfigurationTest.java b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/WebSocketProxyConfigurationTest.java
new file mode 100644
index 00000000000..3f8b1a3f533
--- /dev/null
+++ b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/WebSocketProxyConfigurationTest.java
@@ -0,0 +1,53 @@
+/**
+ * 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.websocket;
+
+
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
+import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration;
+import org.testng.annotations.Test;
+import org.testng.Assert;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+
+import static org.testng.Assert.assertEquals;
+
+public class WebSocketProxyConfigurationTest {
+
+ @Test
+ public void testConfigurationConversionUsedByWebSocketProxyStarter() {
+ WebSocketProxyConfiguration config = new WebSocketProxyConfiguration();
+ // Use non-default values for testing
+ config.setTlsAllowInsecureConnection(true);
+ Assert.assertFalse(config.isTlsHostnameVerificationEnabled(), "Update me when default changes.");
+ config.setTlsHostnameVerificationEnabled(true);
+ ServiceConfiguration brokerConf = PulsarConfigurationLoader.convertFrom(config);
+ Assert.assertTrue(brokerConf.isTlsAllowInsecureConnection(),
+ "TlsAllowInsecureConnection should convert correctly");
+ Assert.assertTrue(brokerConf.isTlsHostnameVerificationEnabled(),
+ "TlsHostnameVerificationEnabled should convert correctly");
+ }
+}