jiazhai commented on a change in pull request #6760: [pulsar-client] Add 
support to load tls certs/key dynamically from inputstream
URL: https://github.com/apache/pulsar/pull/6760#discussion_r410696679
 
 

 ##########
 File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/SecurityUtility.java
 ##########
 @@ -238,24 +240,53 @@ public static SSLContext createSslContext(boolean 
allowInsecureConnection, Certi
         }
 
         try (FileInputStream input = new FileInputStream(certFilePath)) {
-            CertificateFactory cf = CertificateFactory.getInstance("X.509");
-            Collection<X509Certificate> collection = 
(Collection<X509Certificate>) cf.generateCertificates(input);
-            certificates = collection.toArray(new 
X509Certificate[collection.size()]);
+            certificates = loadCertificatesFromPemStream(input);
         } catch (GeneralSecurityException | IOException e) {
             throw new KeyManagementException("Certificate loading error", e);
         }
 
         return certificates;
     }
 
+    public static X509Certificate[] loadCertificatesFromPemStream(InputStream 
inStream) throws KeyManagementException  {
+        if (inStream == null) {
+            return null;
+        }
+        CertificateFactory cf;
+        try {
+            cf = CertificateFactory.getInstance("X.509");
+            Collection<X509Certificate> collection = 
(Collection<X509Certificate>) cf.generateCertificates(inStream);
+            return collection.toArray(new X509Certificate[collection.size()]);
+        } catch (CertificateException e) {
+            throw new KeyManagementException("Certificate loading error", e);
+        }
+    }
+
     public static PrivateKey loadPrivateKeyFromPemFile(String keyFilePath) 
throws KeyManagementException {
         PrivateKey privateKey = null;
 
         if (keyFilePath == null || keyFilePath.isEmpty()) {
             return privateKey;
         }
 
-        try (BufferedReader reader = new BufferedReader(new 
FileReader(keyFilePath))) {
+        try (FileInputStream input = new FileInputStream(keyFilePath)) {
+            privateKey = loadPrivateKeyFromPemStream(input);
+        } catch (IOException e) {
+            throw new KeyManagementException("Private key loading error", e);
+        }
+
+        return privateKey;
+    }
+
+    public static PrivateKey loadPrivateKeyFromPemStream(InputStream inStream) 
throws KeyManagementException {
+        PrivateKey privateKey = null;
+
+        if (inStream == null) {
+            return privateKey;
+        }
+
+        //TODO: check if bufferReader should be closed or not
 
 Review comment:
   This could be removed?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to