You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/12/16 06:54:25 UTC

[GitHub] [pulsar] Shoothzj opened a new pull request #13354: [security] Allow to config web server's cipher and protocols

Shoothzj opened a new pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354


   ### Motivation
   For the security, people want to config the support `ciphers` and `protocols`
   
   ### Modifications
   - Allow the broker and proxy config web server's `ciphers` and `protocols`
   - By default, `ciphers` and `protocols` are null, same as before.
   
   ### Documentation
   
   Check the box below and label this PR (if you have committer privilege).
   
   Need to update docs? 
     
   - [x] `doc` 
     
     this PR contains config file change. IMHO, the doc will be auto generated.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999626182






-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997161649


   > Could you please add a test to cover this?
   
   Yes, add tests to test ciphers and protocols on jetty server.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999218249


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999218249


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999355130


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r776909537



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,186 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        connector.setPort(0);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + connector.getLocalPort());
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        connector.setPort(0);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();

Review comment:
       fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003242425


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997806604


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 @hangc0276 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1000011779


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003257296


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999955608


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997806604


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 @hangc0276 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999385334


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002814976


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002834917


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003281417


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli merged pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
eolivelli merged pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli merged pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
eolivelli merged pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
eolivelli commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773700522



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);

Review comment:
       can we remove this and use Awaiatility ?

##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       we already have a utility method for this

##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);

Review comment:
       and this




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999626284


   @eolivelli PTAL, again, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
eolivelli commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773876688



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       you can pass 0 and Jetty will get an ephemeral port, then you can ask to Jetty the actual LocalPort

##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       no need to add this method




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997170680


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
eolivelli commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r776768732



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,186 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        connector.setPort(0);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + connector.getLocalPort());
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        connector.setPort(0);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();

Review comment:
       Please stop all the servers and close all the clients




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997170141


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999385334


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773812984



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       @eolivelli I fould `getFreePort` in `PulsarMultiListenersWithInternalListenerNameTest` and `PulsarMultiHostClientTest`.
   But now we don't have module like `test-common` or `test-util`. So I suggest now we put this method here. And start a new PR for `test-util` WDYT?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002411169


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997524372


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 @hangc0276 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997170680


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999976361


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999955608


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999355130


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002115201


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002411169


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003257296


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1005446157


   @eolivelli PTAL, again, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773715911



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       @eolivelli Could you please tell me, where the method exists? I have searched for `unusedPort` and `freePort` etc. :)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773938114



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, null, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidCipher() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, new HashSet<String>() {
+                    {
+                        this.add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
+                    }
+                },
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.2");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(),
+                new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    private static SSLContext getClientSslContext() {
+        return getSslContext(Resources.getResource("ssl/jetty_client_key.jks").getPath(),
+                "jetty_client_pwd",
+                Resources.getResource("ssl/jetty_client_trust.jks").getPath(),
+                "jetty_client_pwd");
+    }
+
+    private static SSLContext getSslContext(String keyStorePath, String keyStorePassword,
+                                            String trustStorePath, String trustStorePassword) {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            // key store
+            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(keyStorePath)) {
+                keyStore.load(inputStream, keyStorePassword.toCharArray());
+            }
+            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
+            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
+            // trust store
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            try (FileInputStream inputStream = new FileInputStream(trustStorePath)) {
+                trustStore.load(inputStream, trustStorePassword.toCharArray());
+            }
+            trustManagerFactory.init(trustStore);
+            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
+            return sslContext;
+        } catch (Exception e) {
+            log.error("load ssl context error ", e);
+            return null;
+        }
+    }
+
+    private static int getUnusedPort() throws Exception {

Review comment:
       fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002814976


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002834917


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002889009


   @eolivelli PTAL, again, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003242425


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003295575


   @eolivelli PTAL, again, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1003295575


   @eolivelli PTAL, again, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997170141


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-997524372


   @codelipenghui @lhotari @michaeljmarshall @merlimat @315157973 @hangc0276 PTAL, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-999976361


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj commented on a change in pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj commented on a change in pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#discussion_r773813916



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/util/keystoretls/JettySslContextFactoryWithAutoRefreshTest.java
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.common.util.keystoretls;
+
+import com.google.common.io.Resources;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.net.ServerSocket;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@Slf4j
+public class JettySslContextFactoryWithAutoRefreshTest {
+
+    @Test
+    public void testJettyTlsServerTls() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                null, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);
+        // client connect
+        HttpClientBuilder httpClientBuilder = HttpClients.custom();
+        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
+        registryBuilder.register("https", new SSLConnectionSocketFactory(getClientSslContext(), new NoopHostnameVerifier()));
+        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registryBuilder.build());
+        httpClientBuilder.setConnectionManager(cm);
+        CloseableHttpClient httpClient = httpClientBuilder.build();
+        HttpGet httpGet = new HttpGet("https://localhost:" + unusedPort);
+        httpClient.execute(httpGet);
+    }
+
+    @Test(expectedExceptions = SSLHandshakeException.class)
+    public void testJettyTlsServerInvalidTlsProtocol() throws Exception {
+        Configurator.setRootLevel(Level.INFO);
+        Server server = new Server();
+        List<ServerConnector> connectors = new ArrayList<>();
+        SslContextFactory.Server factory = KeyStoreSSLContext.createSslContextFactory(null,
+                "JKS", Resources.getResource("ssl/jetty_server_key.jks").getPath(),
+                "jetty_server_pwd", false, "JKS",
+                Resources.getResource("ssl/jetty_server_trust.jks").getPath(),
+                "jetty_server_pwd", true, null,
+                new HashSet<String>() {
+                    {
+                        this.add("TLSv1.3");
+                    }
+                }, 600);
+        factory.setHostnameVerifier((s, sslSession) -> true);
+        ServerConnector connector = new ServerConnector(server, factory);
+        int unusedPort = getUnusedPort();
+        connector.setPort(unusedPort);
+        connectors.add(connector);
+        server.setConnectors(connectors.toArray(new ServerConnector[0]));
+        server.start();
+        Thread.sleep(5000);

Review comment:
       I verified the code, It looks wo don't need sleep, the server is ready when `server.start()` returns. Remove the sleep.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1002115201


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] Shoothzj removed a comment on pull request #13354: [security] Allow to config web server's cipher and protocols

Posted by GitBox <gi...@apache.org>.
Shoothzj removed a comment on pull request #13354:
URL: https://github.com/apache/pulsar/pull/13354#issuecomment-1000011779


   /pulsarbot run-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org