Correct directory permission check in apex 35/111535/2
authorliamfallon <liam.fallon@est.tech>
Fri, 21 Aug 2020 11:58:04 +0000 (12:58 +0100)
committerliamfallon <liam.fallon@est.tech>
Mon, 24 Aug 2020 08:44:17 +0000 (09:44 +0100)
Apex checked if the current directory is writable when it starts, it is corrected
to just check that the directory is readable and executable.

Issue-ID: POLICY-2791
Change-Id: I794f836cabfde0f6f78392b3c9e1de364d76e5f6
Signed-off-by: liamfallon <liam.fallon@est.tech>
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArguments.java

index eee77c9..89c47d6 100644 (file)
@@ -1,19 +1,20 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modification Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
@@ -37,16 +38,19 @@ import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
 
 /**
- * This class reads and handles command line parameters for the Apex main program.
+ * This class reads and handles command line parameters for the Apex main
+ * program.
  *
  * @author Liam Fallon (liam.fallon@ericsson.com)
  */
 public class ApexCommandLineArguments {
-    // A system property holding the root directory for relative paths in the configuration file
-    public static final String RELATIVE_FILE_ROOT = "APEX_RELATIVE_FILE_ROOT";
+    // A system property holding the root directory for relative paths in the
+    // configuration file
+    public static final String APEX_RELATIVE_FILE_ROOT = "APEX_RELATIVE_FILE_ROOT";
 
     // Recurring string constants
     private static final String FILE_PREAMBLE = " file \"";
+    private static final String RELATIVE_FILE_ROOT = "relative file root \"";
     private static final int HELP_LINE_LENGTH = 120;
 
     // Apache Commons CLI options
@@ -90,7 +94,7 @@ public class ApexCommandLineArguments {
                         .desc("the root file path for relative file paths specified in the Apex configuration file, "
                                         + "defaults to the current directory from where Apex is executed")
                         .hasArg()
-                        .argName(RELATIVE_FILE_ROOT)
+                        .argName(APEX_RELATIVE_FILE_ROOT)
                         .required(false)
                         .type(String.class)
                         .build());
@@ -123,7 +127,8 @@ public class ApexCommandLineArguments {
      * Parse the command line options.
      *
      * @param args The command line arguments
-     * @return a string with a message for help and version, or null if there is no message
+     * @return a string with a message for help and version, or null if there is no
+     *         message
      * @throws ApexException on command argument errors
      */
     public String parse(final String[] args) throws ApexException {
@@ -191,7 +196,7 @@ public class ApexCommandLineArguments {
 
     /**
      * Print version information for Apex.
-     * 
+     *
      * @return the version string
      */
     public String version() {
@@ -209,7 +214,7 @@ public class ApexCommandLineArguments {
         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
 
         new HelpFormatter().printHelp(stringPrintWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options",
-                        options, 0, 0, "");
+            options, 0, 0, "");
 
         return stringWriter.toString();
     }
@@ -287,7 +292,7 @@ public class ApexCommandLineArguments {
         String relativeFileRootValue = relativeFileRoot;
 
         if (!ParameterValidationUtils.validateStringParameter(relativeFileRoot)) {
-            relativeFileRootValue = System.getProperty(RELATIVE_FILE_ROOT);
+            relativeFileRootValue = System.getProperty(APEX_RELATIVE_FILE_ROOT);
         }
 
         if (!ParameterValidationUtils.validateStringParameter(relativeFileRootValue)) {
@@ -297,7 +302,7 @@ public class ApexCommandLineArguments {
         }
 
         this.relativeFileRoot = relativeFileRootValue;
-        System.setProperty(RELATIVE_FILE_ROOT, relativeFileRootValue);
+        System.setProperty(APEX_RELATIVE_FILE_ROOT, relativeFileRootValue);
     }
 
     /**
@@ -312,7 +317,7 @@ public class ApexCommandLineArguments {
     /**
      * Validate readable file.
      *
-     * @param fileTag the file tag
+     * @param fileTag  the file tag
      * @param fileName the file name
      * @throws ApexException the apex exception
      */
@@ -321,7 +326,8 @@ public class ApexCommandLineArguments {
             throw new ApexException(fileTag + " file was not specified as an argument");
         }
 
-        // The file name can refer to a resource on the local file system or on the class path
+        // The file name can refer to a resource on the local file system or on the
+        // class path
         final URL fileUrl = ResourceUtils.getUrl4Resource(fileName);
         if (fileUrl == null) {
             throw new ApexException(fileTag + FILE_PREAMBLE + fileName + "\" does not exist");
@@ -345,13 +351,15 @@ public class ApexCommandLineArguments {
     private void validateRelativeFileRoot() throws ApexException {
         File relativeFileRootPath = new File(relativeFileRoot);
         if (!relativeFileRootPath.isDirectory()) {
-            throw new ApexException(
-                            "relative file root \"" + relativeFileRoot + "\" does not exist or is not a directory");
+            throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" does not exist or is not a directory");
+        }
+
+        if (!relativeFileRootPath.canRead()) {
+            throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" is not a readable directory");
         }
 
-        if (!relativeFileRootPath.canWrite()) {
-            throw new ApexException(
-                            "relative file root \"" + relativeFileRoot + "\" does not exist or is not a directory");
+        if (!relativeFileRootPath.canExecute()) {
+            throw new ApexException(RELATIVE_FILE_ROOT + relativeFileRoot + "\" is not an executable directory");
         }
     }