Get keyStorePassword from file 53/101253/5
authorShadi Haidar <sh1986@att.com>
Thu, 6 Feb 2020 15:48:20 +0000 (10:48 -0500)
committerShadi Haidar <sh1986@att.com>
Tue, 18 Feb 2020 17:15:44 +0000 (12:15 -0500)
Instead of storing actual passsword in config file's keyStorePassword
get the path to the password file from keyStorePassword and create a
new config file with actual password in keyStorePassword then
continue with normal application startup
This also has bug fix for log errors per DCAEGEN2-2086; where logs
dir and files are not getting created due to inventory user not
having permissions to create dir/logs under /opt/logs/

Issue-ID: DCAEGEN2-2017
Issue-ID: DCAEGEN2-2086

Signed-off-by: Shadi Haidar <sh1986@att.com>
Change-Id: I45b5867e9f73b9355c0fa2a0be18cf610291511d
Signed-off-by: Shadi Haidar <sh1986@att.com>
pom.xml
src/main/java/org/onap/dcae/inventory/InventoryApplication.java
src/test/java/org/onap/dcae/inventory/InventoryApplicationTest.java
version.properties

diff --git a/pom.xml b/pom.xml
index b1071ec..a25e0a9 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!--
 ================================================================================
-Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
+Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
 ================================================================================
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
     <groupId>org.onap.dcaegen2.platform</groupId>
     <artifactId>inventory-api</artifactId>
-    <version>3.4.0-SNAPSHOT</version>
+    <version>3.4.1-SNAPSHOT</version>
     <name>dcaegen2-platform-inventory-api</name>
     <!--internal <version>3.0.0</version>-->
 
@@ -198,7 +198,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
-
+                       <version>19.0</version> 
         </dependency>
         <dependency>
             <groupId>org.glassfish.jersey.media</groupId>
@@ -211,6 +211,11 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
             <version>4.12</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+               <groupId>org.json</groupId>
+               <artifactId>json</artifactId>
+               <version>20131018</version>
+               </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-all</artifactId>
@@ -353,6 +358,8 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
                     <runs>
                         <run>addgroup -S inventory</run>
                         <run>adduser -S -G inventory inventory</run>
+                        <run>mkdir -p /opt/logs</run>
+                        <run>chown -R inventory:inventory /opt</run>
                     </runs>
                     <entryPoint>["java", "-jar", "/opt/${project.build.finalName}.jar", "server"]</entryPoint>
                     <resources>
index 7b0911e..5f0104f 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * dcae-inventory
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -51,14 +51,23 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.util.ContextInitializer;
-
 import javax.servlet.DispatcherType;
 import javax.servlet.FilterRegistration;
 import javax.validation.Validator;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.core.Link;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
 import java.util.EnumSet;
 import java.util.Locale;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
 
 
 /**
@@ -68,7 +77,9 @@ public class InventoryApplication extends Application<InventoryConfiguration> {
 
     static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger");
        static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
+       private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
     static boolean shouldRemoteFetchConfig = false;
+    static final String configFile = "/opt/config_active.json";
 
     /**
      * Parses user's args and makes adjustments if necessary
@@ -91,6 +102,16 @@ public class InventoryApplication extends Application<InventoryConfiguration> {
             return customArgs;
         } else {
             // You are here because you want to use the default way of configuring inventory - YAML file.
+               // The config file yaml file however has the path to the file that has the cert jks password in the keyStorePassword filed
+               // Update config file's keyStorePassword to have actual password instead of path to the password file
+               // for junit purposes, it's not possible to do the above with keyStorePassword so return userArgs as we used to do before 
+            if ( "some-junit-yaml.yaml".equals(userArgs[1]) ) { 
+               return userArgs;
+            }
+               debugLogger.debug(String.format("Default configuration file received: %s", userArgs[1]));
+               createConfigFileFromDefault(userArgs[1]);
+               userArgs[1] = configFile;
+               debugLogger.debug(String.format("Active config file that will be used: %s", userArgs[1]));
             return userArgs;
         }
     }
@@ -201,5 +222,50 @@ public class InventoryApplication extends Application<InventoryConfiguration> {
         environment.jersey().register(new ApiListingResource());
         environment.jersey().register(new SwaggerSerializers());
     }
+    
+    
+    private static void createConfigFileFromDefault (String defaultConfigFile) {
+       
+       try {
+                       JSONObject dzConfig = new JSONObject ( new JSONTokener ( new FileInputStream ( new File ( defaultConfigFile ) ) ) );    
+                       JSONObject server = dzConfig.getJSONObject("server");
+                       JSONArray applicationConnectors = server.getJSONArray("applicationConnectors");
+                       String jksPasswdFile = applicationConnectors.getJSONObject(0).getString("keyStorePassword");
+                       if ( jksPasswdFile != null ) {
+                               applicationConnectors.getJSONObject(0).put("keyStorePassword", getFileContents(jksPasswdFile));
+                       }
+                       else {
+                               errorLogger.error(String.format("Exiting due to null value for JKS password file: %s", jksPasswdFile));
+                               System.exit(1);
+                       }                       
+                       FileWriter fileWriter = new FileWriter(configFile);
+                       fileWriter.write(dzConfig.toString());
+                       fileWriter.flush();
+                       fileWriter.close();             
+           } catch (JSONException | FileNotFoundException e) {
+               errorLogger.error(String.format("JSONException | FileNotFoundException while processing default config file: %s; execption: %s", 
+                               defaultConfigFile, e));
+                       System.exit(1);
+               } catch ( Exception e ) {
+                       errorLogger.error(String.format("Exception while processing default config file: %s; execption: %s", 
+                                       defaultConfigFile, e));
+                       System.exit(1);
+               }
+    }
+    
+    public static String getFileContents (String filename) {
+               File f = new File(filename);
+               try {
+                       byte[] bytes = Files.readAllBytes(f.toPath());
+                       return new String(bytes,"UTF-8").trim();
+               } catch (FileNotFoundException e) {
+                       errorLogger.error(String.format("FileNotFoundException for filename: %s; execption: %s", filename, e));
+                       System.exit(1);
+               } catch (IOException e) {
+                       errorLogger.error(String.format("IOException for filename: %s; execption: %s", filename, e));   
+                       System.exit(1);
+               }
+               return null;
+       }
 
 }
index 8011452..aacbd25 100644 (file)
@@ -59,13 +59,7 @@ public class InventoryApplicationTest {
         String userArgs[] = {"server"};
         assertEquals(InventoryApplication.processArgs(userArgs).length, 2);
 
-        userArgs = new String[] {"server some-yaml.yaml"};
-        assertArrayEquals(InventoryApplication.processArgs(userArgs), userArgs);
-
-        userArgs = new String[] {"foo"};
-        assertArrayEquals(InventoryApplication.processArgs(userArgs), userArgs);
-
-        userArgs = new String[] {"foo bar"};
+        userArgs = new String[] {"server", "some-junit-yaml.yaml"};
         assertArrayEquals(InventoryApplication.processArgs(userArgs), userArgs);
     }
 
index 1ba9fb5..5a6199f 100644 (file)
@@ -3,7 +3,7 @@
 # because they are used in Jenkins, whose plug-in doesn't support
 major=3
 minor=4
-patch=0
+patch=1
 base_version=${major}.${minor}.${patch}
 # Release must be completed with git revision # in Jenkins