You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2023/01/02 04:42:59 UTC

[incubator-eventmesh] branch master updated: fix issue2706

This is an automated email from the ASF dual-hosted git repository.

mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 7fffa1020 fix issue2706
     new cea6096a1 Merge pull request #2770 from jonyangx/issue2706
7fffa1020 is described below

commit 7fffa1020d3b1efdf37180bce4e625b75994257f
Author: jonyangx <jo...@gmail.com>
AuthorDate: Sat Dec 31 23:43:00 2022 +0800

    fix issue2706
---
 .../eventmesh/runtime/boot/AbstractHTTPServer.java | 11 ++---
 .../eventmesh/runtime/boot/SSLContextFactory.java  | 50 +++++++++-------------
 2 files changed, 26 insertions(+), 35 deletions(-)

diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
index 0c3feaf0a..c870bcf31 100644
--- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
+++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
@@ -169,12 +169,13 @@ public abstract class AbstractHTTPServer extends AbstractRemotingServer {
     public void start() throws Exception {
         Runnable r = () -> {
             ServerBootstrap b = new ServerBootstrap();
-            SSLContext sslContext = useTLS ? SSLContextFactory.getSslContext(eventMeshHttpConfiguration) : null;
-            b.group(this.getBossGroup(), this.getWorkerGroup())
-                    .channel(NioServerSocketChannel.class)
-                    .childHandler(new HttpsServerInitializer(sslContext))
-                    .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
             try {
+                SSLContext sslContext = useTLS ? SSLContextFactory.getSslContext(eventMeshHttpConfiguration) : null;
+                b.group(this.getBossGroup(), this.getWorkerGroup())
+                        .channel(NioServerSocketChannel.class)
+                        .childHandler(new HttpsServerInitializer(sslContext))
+                        .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
+
                 httpServerLogger.info("HTTPServer[port={}] started......", this.getPort());
                 ChannelFuture future = b.bind(this.getPort()).sync();
                 future.channel().closeFuture().sync();
diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java
index 7338320df..3132c094b 100644
--- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java
+++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/SSLContextFactory.java
@@ -30,7 +30,12 @@ import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
+import java.security.KeyManagementException;
 import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
 
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
@@ -39,49 +44,34 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class SSLContextFactory {
-    private static Logger httpLogger = LoggerFactory.getLogger("http");
-
     private static String protocol = "TLSv1.1";
 
     private static String fileName;
 
-    private static String pass;
-
+    private static String password;
 
-    public static SSLContext getSslContext(EventMeshHTTPConfiguration eventMeshHttpConfiguration) {
+    public static SSLContext getSslContext(final EventMeshHTTPConfiguration eventMeshHttpConfiguration)
+            throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
+            UnrecoverableKeyException, KeyManagementException {
         SSLContext sslContext;
-        InputStream inputStream = null;
-        try {
-            protocol = eventMeshHttpConfiguration.eventMeshServerSSLProtocol;
 
+        try (InputStream inputStream = Files.newInputStream(Paths.get(EventMeshConstants.EVENTMESH_CONF_HOME
+                + File.separator
+                + fileName), StandardOpenOption.READ)) {
+            protocol = eventMeshHttpConfiguration.eventMeshServerSSLProtocol;
             fileName = eventMeshHttpConfiguration.eventMeshServerSSLCer;
+            password = eventMeshHttpConfiguration.eventMeshServerSSLPass;
 
-            char[] filePass = null;
-            pass = eventMeshHttpConfiguration.eventMeshServerSSLPass;
-            if (StringUtils.isNotBlank(pass)) {
-                filePass = pass.toCharArray();
-            }
-            sslContext = SSLContext.getInstance(protocol);
-            KeyStore keyStore = KeyStore.getInstance("JKS");
-            inputStream = Files.newInputStream(Paths.get(EventMeshConstants.EVENTMESH_CONF_HOME
-                + File.separator
-                + fileName), StandardOpenOption.READ);
+            char[] filePass = StringUtils.isNotBlank(password) ? password.toCharArray() : new char[0];
+            final KeyStore keyStore = KeyStore.getInstance("JKS");
             keyStore.load(inputStream, filePass);
-            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+            final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
             kmf.init(keyStore, filePass);
+
+            sslContext = SSLContext.getInstance(protocol);
             sslContext.init(kmf.getKeyManagers(), null, null);
-        } catch (Exception e) {
-            httpLogger.warn("sslContext init failed", e);
-            sslContext = null;
-        } finally {
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (IOException e) {
-                    httpLogger.warn("IOException found", e);
-                }
-            }
         }
+
         return sslContext;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org