You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/03/11 12:45:54 UTC

[dubbo] branch master updated: [dubbo-6756]ssl support load from classpath (#6756)

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

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 5602191  [dubbo-6756]ssl support load from classpath (#6756)
5602191 is described below

commit 5602191629356dc7b5e1d97137767ad3a67a1e47
Author: Owen.Cai <89...@qq.com>
AuthorDate: Thu Mar 11 20:45:35 2021 +0800

    [dubbo-6756]ssl support load from classpath (#6756)
    
    * ssl support load from classpath
    
    * delete no need import
    
    * check dubbo compile bug
---
 .../dubbo/common/constants/CommonConstants.java    |  3 ++
 .../org/apache/dubbo/common/utils/IOUtils.java     | 38 ++++++++++++++++++++++
 .../java/org/apache/dubbo/config/SslConfig.java    | 28 ++++++++--------
 3 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index 4d81bf9..316f00b 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -365,5 +365,8 @@ public interface CommonConstants {
 
     String CLUSTER_REDIS = "cluster";
 
+    /** Pseudo URL prefix for loading from the class path: "classpath:". */
+    String CLASSPATH_URL_PREFIX = "classpath:";
+
     String DEFAULT_VERSION = "0.0.0";
 }
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
index 02ec00b..1e44924 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.common.utils;
 
+import org.apache.dubbo.common.constants.CommonConstants;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
@@ -30,6 +31,9 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.io.FileNotFoundException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -228,4 +232,38 @@ public class IOUtils {
         writeLines(new FileOutputStream(file, true), lines);
     }
 
+
+    /**
+     * use like spring code
+     * @param resourceLocation
+     * @return
+     */
+    public static URL getURL(String resourceLocation) throws FileNotFoundException {
+        Assert.notNull(resourceLocation, "Resource location must not be null");
+        if (resourceLocation.startsWith(CommonConstants.CLASSPATH_URL_PREFIX)) {
+            String path = resourceLocation.substring(CommonConstants.CLASSPATH_URL_PREFIX.length());
+            ClassLoader cl = ClassUtils.getClassLoader();
+            URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
+            if (url == null) {
+                String description = "class path resource [" + path + "]";
+                throw new FileNotFoundException(description +
+                        " cannot be resolved to URL because it does not exist");
+            }
+            return url;
+        }
+        try {
+            // try URL
+            return new URL(resourceLocation);
+        }
+        catch (MalformedURLException ex) {
+            // no URL -> treat as file path
+            try {
+                return new File(resourceLocation).toURI().toURL();
+            }
+            catch (MalformedURLException ex2) {
+                throw new FileNotFoundException("Resource location [" + resourceLocation +
+                        "] is neither a URL not a well-formed file path");
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java
index 3be7d3e..83f8659 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java
@@ -18,10 +18,10 @@ package org.apache.dubbo.config;
 
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.IOUtils;
 import org.apache.dubbo.config.support.Parameter;
 
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -120,9 +120,9 @@ public class SslConfig extends AbstractConfig {
         this.clientTrustCertCollectionPath = clientTrustCertCollectionPath;
     }
 
-    public InputStream getServerKeyCertChainPathStream() throws FileNotFoundException {
+    public InputStream getServerKeyCertChainPathStream() throws IOException {
         if (serverKeyCertChainPath != null) {
-            serverKeyCertChainPathStream = new FileInputStream(serverKeyCertChainPath);
+            serverKeyCertChainPathStream = IOUtils.getURL(serverKeyCertChainPath).openStream();
         }
         return serverKeyCertChainPathStream;
     }
@@ -131,9 +131,9 @@ public class SslConfig extends AbstractConfig {
         this.serverKeyCertChainPathStream = serverKeyCertChainPathStream;
     }
 
-    public InputStream getServerPrivateKeyPathStream() throws FileNotFoundException {
+    public InputStream getServerPrivateKeyPathStream() throws IOException {
         if (serverPrivateKeyPath != null) {
-            serverPrivateKeyPathStream = new FileInputStream(serverPrivateKeyPath);
+            serverPrivateKeyPathStream = IOUtils.getURL(serverPrivateKeyPath).openStream();
         }
         return serverPrivateKeyPathStream;
     }
@@ -142,9 +142,9 @@ public class SslConfig extends AbstractConfig {
         this.serverPrivateKeyPathStream = serverPrivateKeyPathStream;
     }
 
-    public InputStream getServerTrustCertCollectionPathStream() throws FileNotFoundException {
+    public InputStream getServerTrustCertCollectionPathStream() throws IOException {
         if (serverTrustCertCollectionPath != null) {
-            serverTrustCertCollectionPathStream = new FileInputStream(serverTrustCertCollectionPath);
+            serverTrustCertCollectionPathStream = IOUtils.getURL(serverTrustCertCollectionPath).openStream();
         }
         return serverTrustCertCollectionPathStream;
     }
@@ -153,9 +153,9 @@ public class SslConfig extends AbstractConfig {
         this.serverTrustCertCollectionPathStream = serverTrustCertCollectionPathStream;
     }
 
-    public InputStream getClientKeyCertChainPathStream() throws FileNotFoundException {
+    public InputStream getClientKeyCertChainPathStream() throws IOException {
         if (clientKeyCertChainPath != null) {
-            clientKeyCertChainPathStream = new FileInputStream(clientKeyCertChainPath);
+            clientKeyCertChainPathStream = IOUtils.getURL(clientKeyCertChainPath).openStream();
         }
         return clientKeyCertChainPathStream;
     }
@@ -164,9 +164,9 @@ public class SslConfig extends AbstractConfig {
         this.clientKeyCertChainPathStream = clientKeyCertChainPathStream;
     }
 
-    public InputStream getClientPrivateKeyPathStream() throws FileNotFoundException {
+    public InputStream getClientPrivateKeyPathStream() throws IOException {
         if (clientPrivateKeyPath != null) {
-            clientPrivateKeyPathStream = new FileInputStream(clientPrivateKeyPath);
+            clientPrivateKeyPathStream = IOUtils.getURL(clientPrivateKeyPath).openStream();
         }
         return clientPrivateKeyPathStream;
     }
@@ -175,9 +175,9 @@ public class SslConfig extends AbstractConfig {
         this.clientPrivateKeyPathStream = clientPrivateKeyPathStream;
     }
 
-    public InputStream getClientTrustCertCollectionPathStream() throws FileNotFoundException {
+    public InputStream getClientTrustCertCollectionPathStream() throws IOException {
         if (clientTrustCertCollectionPath != null) {
-            clientTrustCertCollectionPathStream = new FileInputStream(clientTrustCertCollectionPath);
+            clientTrustCertCollectionPathStream = IOUtils.getURL(clientTrustCertCollectionPath).openStream();
         }
         return clientTrustCertCollectionPathStream;
     }