Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
0%
0/118
0%
0/28
2.55
Settings$KEYS
N/A
N/A
2.55
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.PrintWriter;
 26  
 import java.io.StringWriter;
 27  
 import java.io.UnsupportedEncodingException;
 28  
 import java.net.URLDecoder;
 29  
 import java.util.Enumeration;
 30  
 import java.util.Properties;
 31  
 import java.util.logging.Level;
 32  
 import java.util.logging.Logger;
 33  
 
 34  
 /**
 35  
  * A simple settings container that wraps the dependencycheck.properties file.
 36  
  *
 37  
  * @author Jeremy Long <jeremy.long@owasp.org>
 38  
  */
 39  
 public final class Settings {
 40  
 
 41  
     /**
 42  
      * The logger.
 43  
      */
 44  0
     private static final Logger LOGGER = Logger.getLogger(Settings.class.getName());
 45  
 
 46  
     /**
 47  
      * The collection of keys used within the properties file.
 48  
      */
 49  
     public static final class KEYS {
 50  
 
 51  
         /**
 52  
          * private constructor because this is a "utility" class containing constants
 53  
          */
 54  
         private KEYS() {
 55  
             //do nothing
 56  
         }
 57  
         /**
 58  
          * The properties key indicating whether or not the cached data sources should be updated.
 59  
          */
 60  
         public static final String AUTO_UPDATE = "autoupdate";
 61  
         /**
 62  
          * The database driver class name. If this is not in the properties file the embedded database is used.
 63  
          */
 64  
         public static final String DB_DRIVER_NAME = "data.driver_name";
 65  
         /**
 66  
          * The database driver class name. If this is not in the properties file the embedded database is used.
 67  
          */
 68  
         public static final String DB_DRIVER_PATH = "data.driver_path";
 69  
         /**
 70  
          * The database connection string. If this is not in the properties file the embedded database is used.
 71  
          */
 72  
         public static final String DB_CONNECTION_STRING = "data.connection_string";
 73  
         /**
 74  
          * The username to use when connecting to the database.
 75  
          */
 76  
         public static final String DB_USER = "data.user";
 77  
         /**
 78  
          * The password to authenticate to the database.
 79  
          */
 80  
         public static final String DB_PASSWORD = "data.password";
 81  
         /**
 82  
          * The base path to use for the data directory (for embedded db).
 83  
          */
 84  
         public static final String DATA_DIRECTORY = "data.directory";
 85  
         /**
 86  
          * The properties key for the URL to retrieve the "meta" data from about the CVE entries.
 87  
          */
 88  
         public static final String CVE_META_URL = "cve.url.meta";
 89  
         /**
 90  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days) using
 91  
          * the 2.0 schema.
 92  
          */
 93  
         public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified";
 94  
         /**
 95  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days) using
 96  
          * the 1.2 schema.
 97  
          */
 98  
         public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified";
 99  
         /**
 100  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days).
 101  
          */
 102  
         public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays";
 103  
         /**
 104  
          * The properties key for the telling us how many cvr.url.* URLs exists. This is used in combination with
 105  
          * CVE_BASE_URL to be able to retrieve the URLs for all of the files that make up the NVD CVE listing.
 106  
          */
 107  
         public static final String CVE_START_YEAR = "cve.startyear";
 108  
         /**
 109  
          * The properties key for the CVE schema version 1.2.
 110  
          */
 111  
         public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base";
 112  
         /**
 113  
          * The properties key for the CVE schema version 2.0.
 114  
          */
 115  
         public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base";
 116  
         /**
 117  
          * The properties key for the proxy url.
 118  
          */
 119  
         public static final String PROXY_URL = "proxy.url";
 120  
         /**
 121  
          * The properties key for the proxy port - this must be an integer value.
 122  
          */
 123  
         public static final String PROXY_PORT = "proxy.port";
 124  
         /**
 125  
          * The properties key for the proxy username.
 126  
          */
 127  
         public static final String PROXY_USERNAME = "proxy.username";
 128  
         /**
 129  
          * The properties key for the proxy password.
 130  
          */
 131  
         public static final String PROXY_PASSWORD = "proxy.password";
 132  
         /**
 133  
          * The properties key for the connection timeout.
 134  
          */
 135  
         public static final String CONNECTION_TIMEOUT = "connection.timeout";
 136  
         /**
 137  
          * The location of the temporary directory.
 138  
          */
 139  
         public static final String TEMP_DIRECTORY = "temp.directory";
 140  
         /**
 141  
          * The maximum number of threads to allocate when downloading files.
 142  
          */
 143  
         public static final String MAX_DOWNLOAD_THREAD_POOL_SIZE = "max.download.threads";
 144  
         /**
 145  
          * The key for a list of suppression files.
 146  
          */
 147  
         public static final String SUPPRESSION_FILE = "suppression.file";
 148  
         /**
 149  
          * The properties key for whether the Jar Analyzer is enabled.
 150  
          */
 151  
         public static final String ANALYZER_JAR_ENABLED = "analyzer.jar.enabled";
 152  
         /**
 153  
          * The properties key for whether the Archive analyzer is enabled.
 154  
          */
 155  
         public static final String ANALYZER_ARCHIVE_ENABLED = "analyzer.archive.enabled";
 156  
         /**
 157  
          * The properties key for whether the .NET Assembly analyzer is enabled.
 158  
          */
 159  
         public static final String ANALYZER_ASSEMBLY_ENABLED = "analyzer.assembly.enabled";
 160  
         /**
 161  
          * The properties key for whether the .NET Nuspec analyzer is enabled.
 162  
          */
 163  
         public static final String ANALYZER_NUSPEC_ENABLED = "analyzer.nuspec.enabled";
 164  
         /**
 165  
          * The properties key for whether the JavaScript analyzer is enabled.
 166  
          */
 167  
         public static final String ANALYZER_JAVASCRIPT_ENABLED = "analyzer.javascript.enabled";
 168  
         /**
 169  
          * The properties key for whether the Nexus analyzer is enabled.
 170  
          */
 171  
         public static final String ANALYZER_NEXUS_ENABLED = "analyzer.nexus.enabled";
 172  
         /**
 173  
          * The properties key for the Nexus search URL.
 174  
          */
 175  
         public static final String ANALYZER_NEXUS_URL = "analyzer.nexus.url";
 176  
         /**
 177  
          * The properties key for using the proxy to reach Nexus.
 178  
          */
 179  
         public static final String ANALYZER_NEXUS_PROXY = "analyzer.nexus.proxy";
 180  
         /**
 181  
          * The path to mono, if available.
 182  
          */
 183  
         public static final String ANALYZER_ASSEMBLY_MONO_PATH = "analyzer.assembly.mono.path";
 184  
         /**
 185  
          * The additional configured zip file extensions, if available.
 186  
          */
 187  
         public static final String ADDITIONAL_ZIP_EXTENSIONS = "extensions.zip";
 188  
         /**
 189  
          * The properties key for whether Test Scope dependencies should be skipped.
 190  
          */
 191  
         public static final String SKIP_TEST_SCOPE = "skip.test.scope";
 192  
         /**
 193  
          * The properties key for whether Runtime Scope dependencies should be skipped.
 194  
          */
 195  
         public static final String SKIP_RUNTIME_SCOPE = "skip.runtime.scope";
 196  
         /**
 197  
          * The properties key for whether Provided Scope dependencies should be skipped.
 198  
          */
 199  
         public static final String SKIP_PROVIDED_SCOPE = "skip.provided.scope";
 200  
     }
 201  
     /**
 202  
      * The properties file location.
 203  
      */
 204  
     private static final String PROPERTIES_FILE = "dependencycheck.properties";
 205  
     /**
 206  
      * The singleton instance variable.
 207  
      */
 208  0
     private static final Settings INSTANCE = new Settings();
 209  
     /**
 210  
      * The properties.
 211  
      */
 212  0
     private Properties props = null;
 213  
 
 214  
     /**
 215  
      * Private constructor for the Settings class. This class loads the properties files.
 216  
      */
 217  0
     private Settings() {
 218  0
         InputStream in = null;
 219  0
         props = new Properties();
 220  
         try {
 221  0
             in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 222  0
             props.load(in);
 223  0
         } catch (IOException ex) {
 224  0
             Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "Unable to load default settings.");
 225  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, null, ex);
 226  
         } finally {
 227  0
             if (in != null) {
 228  
                 try {
 229  0
                     in.close();
 230  0
                 } catch (IOException ex) {
 231  0
                     Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 232  0
                 }
 233  
             }
 234  
         }
 235  0
         logProperties("Properties loaded", props);
 236  0
     }
 237  
 
 238  
     /**
 239  
      * Logs the properties. This will not log any properties that contain 'password' in the key.
 240  
      *
 241  
      * @param header the header to print with the log message
 242  
      * @param properties the properties to log
 243  
      */
 244  
     private static void logProperties(String header, Properties properties) {
 245  0
         if (LOGGER.isLoggable(Level.FINE)) {
 246  0
             final StringWriter sw = new StringWriter();
 247  0
             PrintWriter pw = null;
 248  
             try {
 249  0
                 pw = new PrintWriter(sw);
 250  0
                 pw.format("%s:%n%n", header);
 251  0
                 final Enumeration e = properties.propertyNames();
 252  0
                 while (e.hasMoreElements()) {
 253  0
                     final String key = (String) e.nextElement();
 254  0
                     if (key.contains("password")) {
 255  0
                         pw.format("%s='*****'%n", key);
 256  
                     } else {
 257  0
                         final String value = properties.getProperty(key);
 258  0
                         if (value != null) {
 259  0
                             pw.format("%s='%s'%n", key, value);
 260  
                         }
 261  
                     }
 262  0
                 }
 263  0
                 pw.flush();
 264  0
                 LOGGER.fine(sw.toString());
 265  
             } finally {
 266  0
                 if (pw != null) {
 267  0
                     pw.close();
 268  
                 }
 269  
             }
 270  
 
 271  
         }
 272  0
     }
 273  
 
 274  
     /**
 275  
      * Sets a property value.
 276  
      *
 277  
      * @param key the key for the property
 278  
      * @param value the value for the property
 279  
      */
 280  
     public static void setString(String key, String value) {
 281  0
         INSTANCE.props.setProperty(key, value);
 282  0
         if (LOGGER.isLoggable(Level.FINE)) {
 283  0
             LOGGER.fine(String.format("Setting: %s='%s'", key, value));
 284  
         }
 285  0
     }
 286  
 
 287  
     /**
 288  
      * Sets a property value.
 289  
      *
 290  
      * @param key the key for the property
 291  
      * @param value the value for the property
 292  
      */
 293  
     public static void setBoolean(String key, boolean value) {
 294  0
         if (value) {
 295  0
             INSTANCE.props.setProperty(key, Boolean.TRUE.toString());
 296  
         } else {
 297  0
             INSTANCE.props.setProperty(key, Boolean.FALSE.toString());
 298  
         }
 299  0
         if (LOGGER.isLoggable(Level.FINE)) {
 300  0
             LOGGER.fine(String.format("Setting: %s='%b'", key, value));
 301  
         }
 302  0
     }
 303  
 
 304  
     /**
 305  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided
 306  
      * properties file.<br/><br/>
 307  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 308  
      *
 309  
      * @param filePath the path to the properties file to merge.
 310  
      * @throws FileNotFoundException is thrown when the filePath points to a non-existent file
 311  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 312  
      */
 313  
     public static void mergeProperties(File filePath) throws FileNotFoundException, IOException {
 314  0
         final FileInputStream fis = new FileInputStream(filePath);
 315  0
         mergeProperties(fis);
 316  0
     }
 317  
 
 318  
     /**
 319  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided
 320  
      * properties file.<br/><br/>
 321  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 322  
      *
 323  
      * @param filePath the path to the properties file to merge.
 324  
      * @throws FileNotFoundException is thrown when the filePath points to a non-existent file
 325  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 326  
      */
 327  
     public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
 328  0
         final FileInputStream fis = new FileInputStream(filePath);
 329  0
         mergeProperties(fis);
 330  0
     }
 331  
 
 332  
     /**
 333  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided
 334  
      * properties file.<br/><br/>
 335  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 336  
      *
 337  
      * @param stream an Input Stream pointing at a properties file to merge
 338  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 339  
      */
 340  
     public static void mergeProperties(InputStream stream) throws IOException {
 341  0
         INSTANCE.props.load(stream);
 342  0
         logProperties("Properties updated via merge", INSTANCE.props);
 343  0
     }
 344  
 
 345  
     /**
 346  
      * Returns a value from the properties file as a File object. If the value was specified as a system property or
 347  
      * passed in via the -Dprop=value argument - this method will return the value from the system properties before the
 348  
      * values in the contained configuration file.
 349  
      *
 350  
      * @param key the key to lookup within the properties file
 351  
      * @return the property from the properties file converted to a File object
 352  
      */
 353  
     public static File getFile(String key) {
 354  0
         final String file = getString(key);
 355  0
         if (file == null) {
 356  0
             return null;
 357  
         }
 358  0
         return new File(file);
 359  
     }
 360  
 
 361  
     /**
 362  
      * Returns a value from the properties file as a File object. If the value was specified as a system property or
 363  
      * passed in via the -Dprop=value argument - this method will return the value from the system properties before the
 364  
      * values in the contained configuration file.
 365  
      *
 366  
      * This method will check the configured base directory and will use this as the base of the file path.
 367  
      * Additionally, if the base directory begins with a leading "[JAR]\" sequence with the path to the folder
 368  
      * containing the JAR file containing this class.
 369  
      *
 370  
      * @param key the key to lookup within the properties file
 371  
      * @return the property from the properties file converted to a File object
 372  
      */
 373  
     public static File getDataFile(String key) {
 374  0
         final String file = getString(key);
 375  0
         Logger.getLogger(Settings.class.getName()).log(Level.FINE, String.format("Settings.getDataFile() - file: '%s'", file));
 376  0
         if (file == null) {
 377  0
             return null;
 378  
         }
 379  0
         if (file.startsWith("[JAR]")) {
 380  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, "Settings.getDataFile() - transforming filename");
 381  0
             final File jarPath = getJarPath();
 382  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, String.format("Settings.getDataFile() - jar file: '%s'", jarPath.toString()));
 383  0
             final File retVal = new File(jarPath, file.substring(6));
 384  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, String.format("Settings.getDataFile() - returning: '%s'", retVal.toString()));
 385  0
             return retVal;
 386  
         }
 387  0
         return new File(file);
 388  
     }
 389  
 
 390  
     /**
 391  
      * Attempts to retrieve the folder containing the Jar file containing the Settings class.
 392  
      *
 393  
      * @return a File object
 394  
      */
 395  
     private static File getJarPath() {
 396  0
         final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 397  0
         String decodedPath = ".";
 398  
         try {
 399  0
             decodedPath = URLDecoder.decode(jarPath, "UTF-8");
 400  0
         } catch (UnsupportedEncodingException ex) {
 401  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 402  0
         }
 403  
 
 404  0
         final File path = new File(decodedPath);
 405  0
         if (path.getName().toLowerCase().endsWith(".jar")) {
 406  0
             return path.getParentFile();
 407  
         } else {
 408  0
             return new File(".");
 409  
         }
 410  
     }
 411  
 
 412  
     /**
 413  
      * Returns a value from the properties file. If the value was specified as a system property or passed in via the
 414  
      * -Dprop=value argument - this method will return the value from the system properties before the values in the
 415  
      * contained configuration file.
 416  
      *
 417  
      * @param key the key to lookup within the properties file
 418  
      * @param defaultValue the default value for the requested property
 419  
      * @return the property from the properties file
 420  
      */
 421  
     public static String getString(String key, String defaultValue) {
 422  0
         final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue));
 423  0
         return str;
 424  
     }
 425  
 
 426  
     /**
 427  
      * Returns the temporary directory.
 428  
      *
 429  
      * @return the temporary directory
 430  
      */
 431  
     public static File getTempDirectory() {
 432  0
         return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
 433  
     }
 434  
 
 435  
     /**
 436  
      * Returns a value from the properties file. If the value was specified as a system property or passed in via the
 437  
      * -Dprop=value argument - this method will return the value from the system properties before the values in the
 438  
      * contained configuration file.
 439  
      *
 440  
      * @param key the key to lookup within the properties file
 441  
      * @return the property from the properties file
 442  
      */
 443  
     public static String getString(String key) {
 444  0
         return System.getProperty(key, INSTANCE.props.getProperty(key));
 445  
     }
 446  
 
 447  
     /**
 448  
      * Removes a property from the local properties collection. This is mainly used in test cases.
 449  
      *
 450  
      * @param key the property key to remove
 451  
      */
 452  
     public static void removeProperty(String key) {
 453  0
         INSTANCE.props.remove(key);
 454  0
     }
 455  
 
 456  
     /**
 457  
      * Returns an int value from the properties file. If the value was specified as a system property or passed in via
 458  
      * the -Dprop=value argument - this method will return the value from the system properties before the values in the
 459  
      * contained configuration file.
 460  
      *
 461  
      * @param key the key to lookup within the properties file
 462  
      * @return the property from the properties file
 463  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 464  
      */
 465  
     public static int getInt(String key) throws InvalidSettingException {
 466  
         int value;
 467  
         try {
 468  0
             value = Integer.parseInt(Settings.getString(key));
 469  0
         } catch (NumberFormatException ex) {
 470  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 471  0
         }
 472  0
         return value;
 473  
     }
 474  
 
 475  
     /**
 476  
      * Returns an int value from the properties file. If the value was specified as a system property or passed in via
 477  
      * the -Dprop=value argument - this method will return the value from the system properties before the values in the
 478  
      * contained configuration file.
 479  
      *
 480  
      * @param key the key to lookup within the properties file
 481  
      * @param defaultValue the default value to return
 482  
      * @return the property from the properties file or the defaultValue if the property does not exist or cannot be
 483  
      * converted to an integer
 484  
      */
 485  
     public static int getInt(String key, int defaultValue) {
 486  
         int value;
 487  
         try {
 488  0
             value = Integer.parseInt(Settings.getString(key));
 489  0
         } catch (NumberFormatException ex) {
 490  0
             final String msg = String.format("Could not convert property '%s' to an int.", key);
 491  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex);
 492  0
             value = defaultValue;
 493  0
         }
 494  0
         return value;
 495  
     }
 496  
 
 497  
     /**
 498  
      * Returns a long value from the properties file. If the value was specified as a system property or passed in via
 499  
      * the -Dprop=value argument - this method will return the value from the system properties before the values in the
 500  
      * contained configuration file.
 501  
      *
 502  
      * @param key the key to lookup within the properties file
 503  
      * @return the property from the properties file
 504  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 505  
      */
 506  
     public static long getLong(String key) throws InvalidSettingException {
 507  
         long value;
 508  
         try {
 509  0
             value = Long.parseLong(Settings.getString(key));
 510  0
         } catch (NumberFormatException ex) {
 511  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 512  0
         }
 513  0
         return value;
 514  
     }
 515  
 
 516  
     /**
 517  
      * Returns a boolean value from the properties file. If the value was specified as a system property or passed in
 518  
      * via the <code>-Dprop=value</code> argument this method will return the value from the system properties before
 519  
      * the values in the contained configuration file.
 520  
      *
 521  
      * @param key the key to lookup within the properties file
 522  
      * @return the property from the properties file
 523  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 524  
      */
 525  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 526  
         boolean value;
 527  
         try {
 528  0
             value = Boolean.parseBoolean(Settings.getString(key));
 529  0
         } catch (NumberFormatException ex) {
 530  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 531  0
         }
 532  0
         return value;
 533  
     }
 534  
 
 535  
     /**
 536  
      * Returns a boolean value from the properties file. If the value was specified as a system property or passed in
 537  
      * via the <code>-Dprop=value</code> argument this method will return the value from the system properties before
 538  
      * the values in the contained configuration file.
 539  
      *
 540  
      * @param key the key to lookup within the properties file
 541  
      * @param defaultValue the default value to return if the setting does not exist
 542  
      * @return the property from the properties file
 543  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 544  
      */
 545  
     public static boolean getBoolean(String key, boolean defaultValue) throws InvalidSettingException {
 546  
         boolean value;
 547  
         try {
 548  0
             final String strValue = Settings.getString(key);
 549  0
             if (strValue == null) {
 550  0
                 return defaultValue;
 551  
             }
 552  0
             value = Boolean.parseBoolean(strValue);
 553  0
         } catch (NumberFormatException ex) {
 554  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 555  0
         }
 556  0
         return value;
 557  
     }
 558  
 }