| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Settings |
|
| 2.3333333333333335;2.333 | ||||
| Settings$KEYS |
|
| 2.3333333333333335;2.333 |
| 1 | /* | |
| 2 | * This file is part of dependency-check-core. | |
| 3 | * | |
| 4 | * Dependency-check-core is free software: you can redistribute it and/or modify it | |
| 5 | * under the terms of the GNU General Public License as published by the Free | |
| 6 | * Software Foundation, either version 3 of the License, or (at your option) any | |
| 7 | * later version. | |
| 8 | * | |
| 9 | * Dependency-check-core is distributed in the hope that it will be useful, but | |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
| 12 | * details. | |
| 13 | * | |
| 14 | * You should have received a copy of the GNU General Public License along with | |
| 15 | * dependency-check-core. If not, see http://www.gnu.org/licenses/. | |
| 16 | * | |
| 17 | * Copyright (c) 2012 Jeremy Long. All Rights Reserved. | |
| 18 | */ | |
| 19 | package org.owasp.dependencycheck.utils; | |
| 20 | ||
| 21 | import java.io.File; | |
| 22 | import java.io.FileInputStream; | |
| 23 | import java.io.FileNotFoundException; | |
| 24 | import java.io.IOException; | |
| 25 | import java.io.InputStream; | |
| 26 | import java.io.UnsupportedEncodingException; | |
| 27 | import java.net.URLDecoder; | |
| 28 | import java.util.Properties; | |
| 29 | import java.util.logging.Level; | |
| 30 | import java.util.logging.Logger; | |
| 31 | ||
| 32 | /** | |
| 33 | * A simple settings container that wraps the dependencycheck.properties file. | |
| 34 | * | |
| 35 | * @author Jeremy Long (jeremy.long@owasp.org) | |
| 36 | */ | |
| 37 | public final class Settings { | |
| 38 | ||
| 39 | /** | |
| 40 | * The collection of keys used within the properties file. | |
| 41 | */ | |
| 42 | public static final class KEYS { | |
| 43 | ||
| 44 | /** | |
| 45 | * private constructor because this is a "utility" class containing | |
| 46 | * constants | |
| 47 | */ | |
| 48 | 0 | private KEYS() { |
| 49 | //do nothing | |
| 50 | 0 | } |
| 51 | /** | |
| 52 | * The properties key indicating whether or not the cached data sources | |
| 53 | * should be updated. | |
| 54 | */ | |
| 55 | public static final String AUTO_UPDATE = "autoupdate"; | |
| 56 | /** | |
| 57 | * The database driver class name. If this is not in the properties file | |
| 58 | * the embedded database is used. | |
| 59 | */ | |
| 60 | public static final String DB_DRIVER = "database.drive"; | |
| 61 | /** | |
| 62 | * The database connection string. If this is not in the properties file | |
| 63 | * the embedded database is used. | |
| 64 | */ | |
| 65 | public static final String DB_CONNECTION_STRING = "database.connectionstring"; | |
| 66 | /** | |
| 67 | * The base path to use for the data directory. | |
| 68 | */ | |
| 69 | public static final String DATA_DIRECTORY = "data.directory"; | |
| 70 | /** | |
| 71 | * The location of the batch update URL. This is a zip file that | |
| 72 | * contains the contents of the data directory. | |
| 73 | */ | |
| 74 | public static final String BATCH_UPDATE_URL = "batch.update.url"; | |
| 75 | /** | |
| 76 | * The properties key for the path where the CVE H2 database will be | |
| 77 | * stored. | |
| 78 | */ | |
| 79 | public static final String CVE_DATA_DIRECTORY = "data.cve"; | |
| 80 | /** | |
| 81 | * The properties key for the URL to retrieve the "meta" data from about | |
| 82 | * the CVE entries. | |
| 83 | */ | |
| 84 | public static final String CVE_META_URL = "cve.url.meta"; | |
| 85 | /** | |
| 86 | * The properties key for the URL to retrieve the recently modified and | |
| 87 | * added CVE entries (last 8 days) using the 2.0 schema. | |
| 88 | */ | |
| 89 | public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified"; | |
| 90 | /** | |
| 91 | * The properties key for the URL to retrieve the recently modified and | |
| 92 | * added CVE entries (last 8 days) using the 1.2 schema. | |
| 93 | */ | |
| 94 | public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified"; | |
| 95 | /** | |
| 96 | * The properties key for the URL to retrieve the recently modified and | |
| 97 | * added CVE entries (last 8 days). | |
| 98 | */ | |
| 99 | public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays"; | |
| 100 | /** | |
| 101 | * The properties key for the telling us how many cvr.url.* URLs exists. | |
| 102 | * This is used in combination with CVE_BASE_URL to be able to retrieve | |
| 103 | * the URLs for all of the files that make up the NVD CVE listing. | |
| 104 | */ | |
| 105 | public static final String CVE_START_YEAR = "cve.startyear"; | |
| 106 | /** | |
| 107 | * The properties key for the CVE schema version 1.2. | |
| 108 | */ | |
| 109 | public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base"; | |
| 110 | /** | |
| 111 | * The properties key for the CVE schema version 2.0. | |
| 112 | */ | |
| 113 | public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base"; | |
| 114 | /** | |
| 115 | * The properties key for the proxy url. | |
| 116 | */ | |
| 117 | public static final String PROXY_URL = "proxy.url"; | |
| 118 | /** | |
| 119 | * The properties key for the proxy port - this must be an integer | |
| 120 | * value. | |
| 121 | */ | |
| 122 | public static final String PROXY_PORT = "proxy.port"; | |
| 123 | /** | |
| 124 | * The properties key for the proxy username. | |
| 125 | */ | |
| 126 | public static final String PROXY_USERNAME = "proxy.username"; | |
| 127 | /** | |
| 128 | * The properties key for the proxy password. | |
| 129 | */ | |
| 130 | public static final String PROXY_PASSWORD = "proxy.password"; | |
| 131 | /** | |
| 132 | * The properties key for the connection timeout. | |
| 133 | */ | |
| 134 | public static final String CONNECTION_TIMEOUT = "connection.timeout"; | |
| 135 | /** | |
| 136 | * The location of the temporary directory. | |
| 137 | */ | |
| 138 | public static final String TEMP_DIRECTORY = "temp.directory"; | |
| 139 | /** | |
| 140 | * The maximum number of threads to allocate when downloading files. | |
| 141 | */ | |
| 142 | public static final String MAX_DOWNLOAD_THREAD_POOL_SIZE = "max.download.threads"; | |
| 143 | /** | |
| 144 | * The key for a list of suppression files. | |
| 145 | */ | |
| 146 | public static final String SUPPRESSION_FILE = "suppression.file"; | |
| 147 | } | |
| 148 | /** | |
| 149 | * The properties file location. | |
| 150 | */ | |
| 151 | private static final String PROPERTIES_FILE = "dependencycheck.properties"; | |
| 152 | /** | |
| 153 | * The singleton instance variable. | |
| 154 | */ | |
| 155 | 1 | private static final Settings INSTANCE = new Settings(); |
| 156 | /** | |
| 157 | * The properties. | |
| 158 | */ | |
| 159 | 1 | private Properties props = null; |
| 160 | ||
| 161 | /** | |
| 162 | * Private constructor for the Settings class. This class loads the | |
| 163 | * properties files. | |
| 164 | */ | |
| 165 | 1 | private Settings() { |
| 166 | 1 | InputStream in = null; |
| 167 | 1 | props = new Properties(); |
| 168 | try { | |
| 169 | 1 | in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
| 170 | 1 | props.load(in); |
| 171 | 0 | } catch (IOException ex) { |
| 172 | 0 | Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "Unable to load default settings."); |
| 173 | 0 | Logger.getLogger(Settings.class.getName()).log(Level.FINE, null, ex); |
| 174 | } finally { | |
| 175 | 1 | if (in != null) { |
| 176 | try { | |
| 177 | 1 | in.close(); |
| 178 | 0 | } catch (IOException ex) { |
| 179 | 0 | Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex); |
| 180 | 1 | } |
| 181 | } | |
| 182 | } | |
| 183 | 1 | } |
| 184 | ||
| 185 | /** | |
| 186 | * Sets a property value. | |
| 187 | * | |
| 188 | * @param key the key for the property | |
| 189 | * @param value the value for the property | |
| 190 | */ | |
| 191 | public static void setString(String key, String value) { | |
| 192 | 17 | INSTANCE.props.setProperty(key, value); |
| 193 | 17 | } |
| 194 | ||
| 195 | /** | |
| 196 | * Sets a property value. | |
| 197 | * | |
| 198 | * @param key the key for the property | |
| 199 | * @param value the value for the property | |
| 200 | */ | |
| 201 | public static void setBoolean(String key, boolean value) { | |
| 202 | 5 | if (value) { |
| 203 | 0 | INSTANCE.props.setProperty(key, Boolean.TRUE.toString()); |
| 204 | } else { | |
| 205 | 5 | INSTANCE.props.setProperty(key, Boolean.FALSE.toString()); |
| 206 | } | |
| 207 | 5 | } |
| 208 | ||
| 209 | /** | |
| 210 | * Merges a new properties file into the current properties. This method | |
| 211 | * allows for the loading of a user provided properties file.<br/><br/> | |
| 212 | * Note: even if using this method - system properties will be loaded before | |
| 213 | * properties loaded from files. | |
| 214 | * | |
| 215 | * @param filePath the path to the properties file to merge. | |
| 216 | * @throws FileNotFoundException is thrown when the filePath points to a | |
| 217 | * non-existent file | |
| 218 | * @throws IOException is thrown when there is an exception loading/merging | |
| 219 | * the properties | |
| 220 | */ | |
| 221 | public static void mergeProperties(File filePath) throws FileNotFoundException, IOException { | |
| 222 | 0 | final FileInputStream fis = new FileInputStream(filePath); |
| 223 | 0 | mergeProperties(fis); |
| 224 | 0 | } |
| 225 | ||
| 226 | /** | |
| 227 | * Merges a new properties file into the current properties. This method | |
| 228 | * allows for the loading of a user provided properties file.<br/><br/> | |
| 229 | * Note: even if using this method - system properties will be loaded before | |
| 230 | * properties loaded from files. | |
| 231 | * | |
| 232 | * @param filePath the path to the properties file to merge. | |
| 233 | * @throws FileNotFoundException is thrown when the filePath points to a | |
| 234 | * non-existent file | |
| 235 | * @throws IOException is thrown when there is an exception loading/merging | |
| 236 | * the properties | |
| 237 | */ | |
| 238 | public static void mergeProperties(String filePath) throws FileNotFoundException, IOException { | |
| 239 | 1 | final FileInputStream fis = new FileInputStream(filePath); |
| 240 | 1 | mergeProperties(fis); |
| 241 | 1 | } |
| 242 | ||
| 243 | /** | |
| 244 | * Merges a new properties file into the current properties. This method | |
| 245 | * allows for the loading of a user provided properties file.<br/><br/> | |
| 246 | * Note: even if using this method - system properties will be loaded before | |
| 247 | * properties loaded from files. | |
| 248 | * | |
| 249 | * @param stream an Input Stream pointing at a properties file to merge | |
| 250 | * @throws IOException is thrown when there is an exception loading/merging | |
| 251 | * the properties | |
| 252 | */ | |
| 253 | public static void mergeProperties(InputStream stream) throws IOException { | |
| 254 | 1 | INSTANCE.props.load(stream); |
| 255 | 1 | } |
| 256 | ||
| 257 | /** | |
| 258 | * Returns a value from the properties file as a File object. If the value | |
| 259 | * was specified as a system property or passed in via the -Dprop=value | |
| 260 | * argument - this method will return the value from the system properties | |
| 261 | * before the values in the contained configuration file. | |
| 262 | * | |
| 263 | * @param key the key to lookup within the properties file | |
| 264 | * @return the property from the properties file converted to a File object | |
| 265 | */ | |
| 266 | public static File getFile(String key) { | |
| 267 | 6 | final String file = getString(key); |
| 268 | 6 | if (file == null) { |
| 269 | 6 | return null; |
| 270 | } | |
| 271 | 0 | return new File(file); |
| 272 | } | |
| 273 | ||
| 274 | /** | |
| 275 | * Returns a value from the properties file as a File object. If the value | |
| 276 | * was specified as a system property or passed in via the -Dprop=value | |
| 277 | * argument - this method will return the value from the system properties | |
| 278 | * before the values in the contained configuration file. | |
| 279 | * | |
| 280 | * This method will check the configured base directory and will use this as | |
| 281 | * the base of the file path. Additionally, if the base directory begins | |
| 282 | * with a leading "[JAR]\" sequence with the path to the folder containing | |
| 283 | * the JAR file containing this class. | |
| 284 | * | |
| 285 | * @param key the key to lookup within the properties file | |
| 286 | * @return the property from the properties file converted to a File object | |
| 287 | */ | |
| 288 | public static File getDataFile(String key) { | |
| 289 | 60 | final String file = getString(key); |
| 290 | 60 | final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY); |
| 291 | 60 | if (baseDir != null) { |
| 292 | 60 | if (baseDir.startsWith("[JAR]/")) { |
| 293 | 0 | final File jarPath = getJarPath(); |
| 294 | 0 | final File newBase = new File(jarPath, baseDir.substring(6)); |
| 295 | 0 | if (Settings.KEYS.DATA_DIRECTORY.equals(key)) { |
| 296 | 0 | return newBase; |
| 297 | } | |
| 298 | 0 | return new File(newBase, file); |
| 299 | } | |
| 300 | 60 | if (Settings.KEYS.DATA_DIRECTORY.equals(key)) { |
| 301 | 34 | return new File(baseDir); |
| 302 | } | |
| 303 | 26 | return new File(baseDir, file); |
| 304 | } | |
| 305 | 0 | return new File(file); |
| 306 | } | |
| 307 | ||
| 308 | /** | |
| 309 | * Attempts to retrieve the folder containing the Jar file containing the | |
| 310 | * Settings class. | |
| 311 | * | |
| 312 | * @return a File object | |
| 313 | */ | |
| 314 | private static File getJarPath() { | |
| 315 | 0 | final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
| 316 | 0 | String decodedPath = "."; |
| 317 | try { | |
| 318 | 0 | decodedPath = URLDecoder.decode(jarPath, "UTF-8"); |
| 319 | 0 | } catch (UnsupportedEncodingException ex) { |
| 320 | 0 | Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex); |
| 321 | 0 | } |
| 322 | ||
| 323 | 0 | final File path = new File(decodedPath); |
| 324 | 0 | if (path.getName().toLowerCase().endsWith(".jar")) { |
| 325 | 0 | return path.getParentFile(); |
| 326 | } else { | |
| 327 | 0 | return new File("."); |
| 328 | } | |
| 329 | } | |
| 330 | ||
| 331 | /** | |
| 332 | * Returns a value from the properties file. If the value was specified as a | |
| 333 | * system property or passed in via the -Dprop=value argument - this method | |
| 334 | * will return the value from the system properties before the values in the | |
| 335 | * contained configuration file. | |
| 336 | * | |
| 337 | * @param key the key to lookup within the properties file | |
| 338 | * @param defaultValue the default value for the requested property | |
| 339 | * @return the property from the properties file | |
| 340 | */ | |
| 341 | public static String getString(String key, String defaultValue) { | |
| 342 | 26 | final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue)); |
| 343 | 26 | return str; |
| 344 | } | |
| 345 | ||
| 346 | /** | |
| 347 | * Returns the temporary directory. | |
| 348 | * | |
| 349 | * @return the temporary directory | |
| 350 | */ | |
| 351 | public static File getTempDirectory() { | |
| 352 | 11 | return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir"))); |
| 353 | } | |
| 354 | ||
| 355 | /** | |
| 356 | * Returns a value from the properties file. If the value was specified as a | |
| 357 | * system property or passed in via the -Dprop=value argument - this method | |
| 358 | * will return the value from the system properties before the values in the | |
| 359 | * contained configuration file. | |
| 360 | * | |
| 361 | * @param key the key to lookup within the properties file | |
| 362 | * @return the property from the properties file | |
| 363 | */ | |
| 364 | public static String getString(String key) { | |
| 365 | 152 | return System.getProperty(key, INSTANCE.props.getProperty(key)); |
| 366 | } | |
| 367 | ||
| 368 | /** | |
| 369 | * Removes a property from the local properties collection. This is mainly | |
| 370 | * used in test cases. | |
| 371 | * | |
| 372 | * @param key the property key to remove | |
| 373 | */ | |
| 374 | public static void removeProperty(String key) { | |
| 375 | 1 | INSTANCE.props.remove(key); |
| 376 | 1 | } |
| 377 | ||
| 378 | /** | |
| 379 | * Returns an int value from the properties file. If the value was specified | |
| 380 | * as a system property or passed in via the -Dprop=value argument - this | |
| 381 | * method will return the value from the system properties before the values | |
| 382 | * in the contained configuration file. | |
| 383 | * | |
| 384 | * @param key the key to lookup within the properties file | |
| 385 | * @return the property from the properties file | |
| 386 | * @throws InvalidSettingException is thrown if there is an error retrieving | |
| 387 | * the setting | |
| 388 | */ | |
| 389 | public static int getInt(String key) throws InvalidSettingException { | |
| 390 | int value; | |
| 391 | try { | |
| 392 | 1 | value = Integer.parseInt(Settings.getString(key)); |
| 393 | 0 | } catch (NumberFormatException ex) { |
| 394 | 0 | throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex); |
| 395 | 1 | } |
| 396 | 1 | return value; |
| 397 | } | |
| 398 | ||
| 399 | /** | |
| 400 | * Returns an int value from the properties file. If the value was specified | |
| 401 | * as a system property or passed in via the -Dprop=value argument - this | |
| 402 | * method will return the value from the system properties before the values | |
| 403 | * in the contained configuration file. | |
| 404 | * | |
| 405 | * @param key the key to lookup within the properties file | |
| 406 | * @param defaultValue the default value to return | |
| 407 | * @return the property from the properties file or the defaultValue if the | |
| 408 | * property does not exist or cannot be converted to an integer | |
| 409 | */ | |
| 410 | public static int getInt(String key, int defaultValue) { | |
| 411 | int value; | |
| 412 | try { | |
| 413 | 1 | value = Integer.parseInt(Settings.getString(key)); |
| 414 | 1 | } catch (NumberFormatException ex) { |
| 415 | 1 | final String msg = String.format("Could not convert property '%s' to an int.", key); |
| 416 | 1 | Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex); |
| 417 | 1 | value = defaultValue; |
| 418 | 0 | } |
| 419 | 1 | return value; |
| 420 | } | |
| 421 | ||
| 422 | /** | |
| 423 | * Returns a long value from the properties file. If the value was specified | |
| 424 | * as a system property or passed in via the -Dprop=value argument - this | |
| 425 | * method will return the value from the system properties before the values | |
| 426 | * in the contained configuration file. | |
| 427 | * | |
| 428 | * @param key the key to lookup within the properties file | |
| 429 | * @return the property from the properties file | |
| 430 | * @throws InvalidSettingException is thrown if there is an error retrieving | |
| 431 | * the setting | |
| 432 | */ | |
| 433 | public static long getLong(String key) throws InvalidSettingException { | |
| 434 | long value; | |
| 435 | try { | |
| 436 | 1 | value = Long.parseLong(Settings.getString(key)); |
| 437 | 0 | } catch (NumberFormatException ex) { |
| 438 | 0 | throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex); |
| 439 | 1 | } |
| 440 | 1 | return value; |
| 441 | } | |
| 442 | ||
| 443 | /** | |
| 444 | * Returns a boolean value from the properties file. If the value was | |
| 445 | * specified as a system property or passed in via the | |
| 446 | * <code>-Dprop=value</code> argument this method will return the value from | |
| 447 | * the system properties before the values in the contained configuration | |
| 448 | * file. | |
| 449 | * | |
| 450 | * @param key the key to lookup within the properties file | |
| 451 | * @return the property from the properties file | |
| 452 | * @throws InvalidSettingException is thrown if there is an error retrieving | |
| 453 | * the setting | |
| 454 | */ | |
| 455 | public static boolean getBoolean(String key) throws InvalidSettingException { | |
| 456 | boolean value; | |
| 457 | try { | |
| 458 | 7 | value = Boolean.parseBoolean(Settings.getString(key)); |
| 459 | 0 | } catch (NumberFormatException ex) { |
| 460 | 0 | throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex); |
| 461 | 7 | } |
| 462 | 7 | return value; |
| 463 | } | |
| 464 | } |