Fix NPE when loading certificate 30/87530/6
authorgvrangan <vgovindarajan@luminanetworks.com>
Mon, 10 Feb 2020 10:55:44 +0000 (10:55 +0000)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 23 Feb 2020 08:42:20 +0000 (09:42 +0100)
This Patch fixes the NPE when using a certificate from a
non-default location.

JIRA: AAA-191
Change-Id: If12d49d45d2e914bdc901ee692c02790bf9b51b9
Signed-off-by: gvrangan <vgovindarajan@luminanetworks.com>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
aaa-cert/src/main/java/org/opendaylight/aaa/cert/impl/KeyStoreConstant.java
aaa-cert/src/main/java/org/opendaylight/aaa/cert/impl/ODLKeyTool.java

index 99aee61377cbd35636b0a3d9ad33ef3bf1c4fc0b..78edae2d51f3adde37380d0d3e464893993129dd 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.aaa.cert.impl;
 
 import java.io.BufferedWriter;
@@ -46,9 +45,13 @@ public final class KeyStoreConstant {
 
     }
 
+    public static File toAbsoluteFile(final String fileName, final String basePath) {
+        final File file = new File(fileName);
+        return file.isAbsolute() ? file : new File(basePath + fileName);
+    }
+
     public static boolean checkKeyStoreFile(final String fileName) {
-        final File file = new File(KEY_STORE_PATH + fileName);
-        return file.exists();
+        return toAbsoluteFile(fileName, KEY_STORE_PATH).exists();
     }
 
     public static String createDir(final String dir) {
@@ -65,8 +68,7 @@ public final class KeyStoreConstant {
         if (certFile == null || certFile.isEmpty()) {
             return null;
         }
-
-        final String path = KEY_STORE_PATH + certFile;
+        final File path = toAbsoluteFile(certFile, KEY_STORE_PATH);
         try (FileInputStream fInputStream = new FileInputStream(path)) {
             final int available = fInputStream.available();
             final byte[] certBytes = new byte[available];
@@ -84,9 +86,9 @@ public final class KeyStoreConstant {
         if (fileName == null || fileName.isEmpty()) {
             return false;
         }
-
+        final File path = toAbsoluteFile(fileName, KEY_STORE_PATH);
         try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
-                new FileOutputStream(KEY_STORE_PATH + fileName), StandardCharsets.UTF_8))) {
+                new FileOutputStream(path), StandardCharsets.UTF_8))) {
             out.write(cert);
             return true;
         } catch (final IOException e) {
index 5fa3110e43b964ecfcc40e946a3861a3105bbfc5..c97d6a5016e0052ff0d4d06cb46bb18d7de3f25a 100644 (file)
@@ -5,11 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.aaa.cert.impl;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -46,17 +46,14 @@ import org.slf4j.LoggerFactory;
  * such as create, generate, add and delete certificates.
  *
  * @author mserngawy
- *
  */
 public class ODLKeyTool {
-
     private static final Logger LOG = LoggerFactory.getLogger(ODLKeyTool.class);
 
     private final String workingDir;
 
     protected ODLKeyTool() {
-        workingDir = KeyStoreConstant.KEY_STORE_PATH;
-        KeyStoreConstant.createDir(workingDir);
+        this(KeyStoreConstant.KEY_STORE_PATH);
     }
 
     public ODLKeyTool(final String workingDirectory) {
@@ -225,7 +222,9 @@ public class ODLKeyTool {
         if (keystore == null) {
             return false;
         }
-        try (FileOutputStream fOutputStream = new FileOutputStream(workingDir + fileName)) {
+
+        final File realPath = KeyStoreConstant.toAbsoluteFile(fileName, workingDir);
+        try (FileOutputStream fOutputStream = new FileOutputStream(realPath)) {
             keystore.store(fOutputStream, keystorePassword.toCharArray());
             return true;
         } catch (final KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
@@ -393,7 +392,8 @@ public class ODLKeyTool {
      * @return keystore object otherwise return null if it fails to load.
      */
     public KeyStore loadKeyStore(final String keyStoreName, final String keystorePassword) {
-        try (FileInputStream fInputStream = new FileInputStream(workingDir + keyStoreName)) {
+        final File realPath = KeyStoreConstant.toAbsoluteFile(keyStoreName, workingDir);
+        try (FileInputStream fInputStream = new FileInputStream(realPath)) {
             final KeyStore keyStore = KeyStore.getInstance("JKS");
             keyStore.load(fInputStream, keystorePassword.toCharArray());
             return keyStore;