Coverage Report - org.owasp.dependencycheck.Engine
 
Classes in this File Line Coverage Branch Coverage Complexity
Engine
58%
108/186
60%
39/64
3.174
 
 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;
 19  
 
 20  
 import org.owasp.dependencycheck.analyzer.AnalysisPhase;
 21  
 import org.owasp.dependencycheck.analyzer.Analyzer;
 22  
 import org.owasp.dependencycheck.analyzer.AnalyzerService;
 23  
 import org.owasp.dependencycheck.analyzer.FileTypeAnalyzer;
 24  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 25  
 import org.owasp.dependencycheck.data.nvdcve.ConnectionFactory;
 26  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 27  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 28  
 import org.owasp.dependencycheck.data.update.CachedWebDataSource;
 29  
 import org.owasp.dependencycheck.data.update.UpdateService;
 30  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 31  
 import org.owasp.dependencycheck.dependency.Dependency;
 32  
 import org.owasp.dependencycheck.exception.NoDataException;
 33  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 import org.slf4j.Logger;
 36  
 import org.slf4j.LoggerFactory;
 37  
 
 38  
 import java.io.File;
 39  
 import java.io.FileFilter;
 40  
 import java.util.ArrayList;
 41  
 import java.util.Collection;
 42  
 import java.util.EnumMap;
 43  
 import java.util.HashSet;
 44  
 import java.util.Iterator;
 45  
 import java.util.List;
 46  
 import java.util.Map;
 47  
 import java.util.Set;
 48  
 
 49  
 /**
 50  
  * Scans files, directories, etc. for Dependencies. Analyzers are loaded and used to process the files found by the scan, if a
 51  
  * file is encountered and an Analyzer is associated with the file type then the file is turned into a dependency.
 52  
  *
 53  
  * @author Jeremy Long
 54  
  */
 55  
 public class Engine implements FileFilter {
 56  
 
 57  
     /**
 58  
      * The list of dependencies.
 59  
      */
 60  8
     private List<Dependency> dependencies = new ArrayList<Dependency>();
 61  
     /**
 62  
      * A Map of analyzers grouped by Analysis phase.
 63  
      */
 64  8
     private Map<AnalysisPhase, List<Analyzer>> analyzers = new EnumMap<AnalysisPhase, List<Analyzer>>(AnalysisPhase.class);
 65  
 
 66  
     /**
 67  
      * A Map of analyzers grouped by Analysis phase.
 68  
      */
 69  8
     private Set<FileTypeAnalyzer> fileTypeAnalyzers = new HashSet<FileTypeAnalyzer>();
 70  
 
 71  
     /**
 72  
      * The ClassLoader to use when dynamically loading Analyzer and Update services.
 73  
      */
 74  8
     private ClassLoader serviceClassLoader = Thread.currentThread().getContextClassLoader();
 75  
     /**
 76  
      * The Logger for use throughout the class.
 77  
      */
 78  2
     private static final Logger LOGGER = LoggerFactory.getLogger(Engine.class);
 79  
 
 80  
     /**
 81  
      * Creates a new Engine.
 82  
      *
 83  
      * @throws DatabaseException thrown if there is an error connecting to the database
 84  
      */
 85  8
     public Engine() throws DatabaseException {
 86  8
         initializeEngine();
 87  8
     }
 88  
 
 89  
     /**
 90  
      * Creates a new Engine.
 91  
      *
 92  
      * @param serviceClassLoader a reference the class loader being used
 93  
      * @throws DatabaseException thrown if there is an error connecting to the database
 94  
      */
 95  0
     public Engine(ClassLoader serviceClassLoader) throws DatabaseException {
 96  0
         this.serviceClassLoader = serviceClassLoader;
 97  0
         initializeEngine();
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Creates a new Engine using the specified classloader to dynamically load Analyzer and Update services.
 102  
      *
 103  
      * @throws DatabaseException thrown if there is an error connecting to the database
 104  
      */
 105  
     protected final void initializeEngine() throws DatabaseException {
 106  8
         ConnectionFactory.initialize();
 107  8
         loadAnalyzers();
 108  8
     }
 109  
 
 110  
     /**
 111  
      * Properly cleans up resources allocated during analysis.
 112  
      */
 113  
     public void cleanup() {
 114  0
         ConnectionFactory.cleanup();
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Loads the analyzers specified in the configuration file (or system properties).
 119  
      */
 120  
     private void loadAnalyzers() {
 121  8
         if (!analyzers.isEmpty()) {
 122  0
             return;
 123  
         }
 124  88
         for (AnalysisPhase phase : AnalysisPhase.values()) {
 125  80
             analyzers.put(phase, new ArrayList<Analyzer>());
 126  
         }
 127  
 
 128  8
         final AnalyzerService service = new AnalyzerService(serviceClassLoader);
 129  8
         final List<Analyzer> iterator = service.getAnalyzers();
 130  8
         for (Analyzer a : iterator) {
 131  192
             analyzers.get(a.getAnalysisPhase()).add(a);
 132  192
             if (a instanceof FileTypeAnalyzer) {
 133  128
                 this.fileTypeAnalyzers.add((FileTypeAnalyzer) a);
 134  
             }
 135  192
         }
 136  8
     }
 137  
 
 138  
     /**
 139  
      * Get the List of the analyzers for a specific phase of analysis.
 140  
      *
 141  
      * @param phase the phase to get the configured analyzers.
 142  
      * @return the analyzers loaded
 143  
      */
 144  
     public List<Analyzer> getAnalyzers(AnalysisPhase phase) {
 145  0
         return analyzers.get(phase);
 146  
     }
 147  
 
 148  
     /**
 149  
      * Get the dependencies identified.
 150  
      *
 151  
      * @return the dependencies identified
 152  
      */
 153  
     public List<Dependency> getDependencies() {
 154  98
         return dependencies;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Sets the dependencies.
 159  
      *
 160  
      * @param dependencies the dependencies
 161  
      */
 162  
     public void setDependencies(List<Dependency> dependencies) {
 163  0
         this.dependencies = dependencies;
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Scans an array of files or directories. If a directory is specified, it will be scanned recursively. Any dependencies
 168  
      * identified are added to the dependency collection.
 169  
      *
 170  
      * @param paths an array of paths to files or directories to be analyzed
 171  
      * @return the list of dependencies scanned
 172  
      * @since v0.3.2.5
 173  
      */
 174  
     public List<Dependency> scan(String[] paths) {
 175  0
         final List<Dependency> deps = new ArrayList<Dependency>();
 176  0
         for (String path : paths) {
 177  0
             final List<Dependency> d = scan(path);
 178  0
             if (d != null) {
 179  0
                 deps.addAll(d);
 180  
             }
 181  
         }
 182  0
         return deps;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Scans a given file or directory. If a directory is specified, it will be scanned recursively. Any dependencies identified
 187  
      * are added to the dependency collection.
 188  
      *
 189  
      * @param path the path to a file or directory to be analyzed
 190  
      * @return the list of dependencies scanned
 191  
      */
 192  
     public List<Dependency> scan(String path) {
 193  0
         final File file = new File(path);
 194  0
         return scan(file);
 195  
     }
 196  
 
 197  
     /**
 198  
      * Scans an array of files or directories. If a directory is specified, it will be scanned recursively. Any dependencies
 199  
      * identified are added to the dependency collection.
 200  
      *
 201  
      * @param files an array of paths to files or directories to be analyzed.
 202  
      * @return the list of dependencies
 203  
      * @since v0.3.2.5
 204  
      */
 205  
     public List<Dependency> scan(File[] files) {
 206  0
         final List<Dependency> deps = new ArrayList<Dependency>();
 207  0
         for (File file : files) {
 208  0
             final List<Dependency> d = scan(file);
 209  0
             if (d != null) {
 210  0
                 deps.addAll(d);
 211  
             }
 212  
         }
 213  0
         return deps;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Scans a collection of files or directories. If a directory is specified, it will be scanned recursively. Any dependencies
 218  
      * identified are added to the dependency collection.
 219  
      *
 220  
      * @param files a set of paths to files or directories to be analyzed
 221  
      * @return the list of dependencies scanned
 222  
      * @since v0.3.2.5
 223  
      */
 224  
     public List<Dependency> scan(Collection<File> files) {
 225  0
         final List<Dependency> deps = new ArrayList<Dependency>();
 226  0
         for (File file : files) {
 227  0
             final List<Dependency> d = scan(file);
 228  0
             if (d != null) {
 229  0
                 deps.addAll(d);
 230  
             }
 231  0
         }
 232  0
         return deps;
 233  
     }
 234  
 
 235  
     /**
 236  
      * Scans a given file or directory. If a directory is specified, it will be scanned recursively. Any dependencies identified
 237  
      * are added to the dependency collection.
 238  
      *
 239  
      * @param file the path to a file or directory to be analyzed
 240  
      * @return the list of dependencies scanned
 241  
      * @since v0.3.2.4
 242  
      */
 243  
     public List<Dependency> scan(File file) {
 244  10
         if (file.exists()) {
 245  10
             if (file.isDirectory()) {
 246  6
                 return scanDirectory(file);
 247  
             } else {
 248  4
                 final Dependency d = scanFile(file);
 249  4
                 if (d != null) {
 250  4
                     final List<Dependency> deps = new ArrayList<Dependency>();
 251  4
                     deps.add(d);
 252  4
                     return deps;
 253  
                 }
 254  
             }
 255  
         }
 256  0
         return null;
 257  
     }
 258  
 
 259  
     /**
 260  
      * Recursively scans files and directories. Any dependencies identified are added to the dependency collection.
 261  
      *
 262  
      * @param dir the directory to scan
 263  
      * @return the list of Dependency objects scanned
 264  
      */
 265  
     protected List<Dependency> scanDirectory(File dir) {
 266  92
         final File[] files = dir.listFiles();
 267  92
         final List<Dependency> deps = new ArrayList<Dependency>();
 268  92
         if (files != null) {
 269  186
             for (File f : files) {
 270  94
                 if (f.isDirectory()) {
 271  86
                     final List<Dependency> d = scanDirectory(f);
 272  86
                     if (d != null) {
 273  86
                         deps.addAll(d);
 274  
                     }
 275  86
                 } else {
 276  8
                     final Dependency d = scanFile(f);
 277  8
                     deps.add(d);
 278  
                 }
 279  
             }
 280  
         }
 281  92
         return deps;
 282  
     }
 283  
 
 284  
     /**
 285  
      * Scans a specified file. If a dependency is identified it is added to the dependency collection.
 286  
      *
 287  
      * @param file The file to scan
 288  
      * @return the scanned dependency
 289  
      */
 290  
     protected Dependency scanFile(File file) {
 291  12
         Dependency dependency = null;
 292  12
         if (file.isFile()) {
 293  12
             if (accept(file)) {
 294  8
                 dependency = new Dependency(file);
 295  8
                 dependencies.add(dependency);
 296  
             }
 297  
         } else {
 298  0
             LOGGER.debug("Path passed to scanFile(File) is not a file: {}. Skipping the file.", file);
 299  
         }
 300  12
         return dependency;
 301  
     }
 302  
 
 303  
     /**
 304  
      * Runs the analyzers against all of the dependencies. Since the mutable dependencies list is exposed via
 305  
      * {@link #getDependencies()}, this method iterates over a copy of the dependencies list. Thus, the potential for
 306  
      * {@link java.util.ConcurrentModificationException}s is avoided, and analyzers may safely add or remove entries from the
 307  
      * dependencies list.
 308  
      */
 309  
     public void analyzeDependencies() {
 310  4
         boolean autoUpdate = true;
 311  
         try {
 312  4
             autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE);
 313  0
         } catch (InvalidSettingException ex) {
 314  0
             LOGGER.debug("Invalid setting for auto-update; using true.");
 315  4
         }
 316  4
         if (autoUpdate) {
 317  0
             doUpdates();
 318  
         }
 319  
 
 320  
         //need to ensure that data exists
 321  
         try {
 322  4
             ensureDataExists();
 323  0
         } catch (NoDataException ex) {
 324  0
             LOGGER.error("{}\n\nUnable to continue dependency-check analysis.", ex.getMessage());
 325  0
             LOGGER.debug("", ex);
 326  0
             return;
 327  0
         } catch (DatabaseException ex) {
 328  0
             LOGGER.error("{}\n\nUnable to continue dependency-check analysis.", ex.getMessage());
 329  0
             LOGGER.debug("", ex);
 330  0
             return;
 331  
 
 332  4
         }
 333  
 
 334  4
         LOGGER.debug("\n----------------------------------------------------\nBEGIN ANALYSIS\n----------------------------------------------------");
 335  4
         LOGGER.info("Analysis Starting");
 336  4
         final long analysisStart = System.currentTimeMillis();
 337  
 
 338  
         // analysis phases
 339  44
         for (AnalysisPhase phase : AnalysisPhase.values()) {
 340  40
             final List<Analyzer> analyzerList = analyzers.get(phase);
 341  
 
 342  40
             for (Analyzer a : analyzerList) {
 343  96
                 a = initializeAnalyzer(a);
 344  
 
 345  
                 /* need to create a copy of the collection because some of the
 346  
                  * analyzers may modify it. This prevents ConcurrentModificationExceptions.
 347  
                  * This is okay for adds/deletes because it happens per analyzer.
 348  
                  */
 349  96
                 LOGGER.debug("Begin Analyzer '{}'", a.getName());
 350  96
                 final Set<Dependency> dependencySet = new HashSet<Dependency>(dependencies);
 351  96
                 for (Dependency d : dependencySet) {
 352  192
                     boolean shouldAnalyze = true;
 353  192
                     if (a instanceof FileTypeAnalyzer) {
 354  128
                         final FileTypeAnalyzer fAnalyzer = (FileTypeAnalyzer) a;
 355  128
                         shouldAnalyze = fAnalyzer.accept(d.getActualFile());
 356  
                     }
 357  192
                     if (shouldAnalyze) {
 358  76
                         LOGGER.debug("Begin Analysis of '{}'", d.getActualFilePath());
 359  
                         try {
 360  76
                             a.analyze(d, this);
 361  0
                         } catch (AnalysisException ex) {
 362  0
                             LOGGER.warn("An error occurred while analyzing '{}'.", d.getActualFilePath());
 363  0
                             LOGGER.debug("", ex);
 364  0
                         } catch (Throwable ex) {
 365  
                             //final AnalysisException ax = new AnalysisException(axMsg, ex);
 366  0
                             LOGGER.warn("An unexpected error occurred during analysis of '{}'", d.getActualFilePath());
 367  0
                             LOGGER.debug("", ex);
 368  76
                         }
 369  
                     }
 370  192
                 }
 371  96
             }
 372  
         }
 373  44
         for (AnalysisPhase phase : AnalysisPhase.values()) {
 374  40
             final List<Analyzer> analyzerList = analyzers.get(phase);
 375  
 
 376  40
             for (Analyzer a : analyzerList) {
 377  96
                 closeAnalyzer(a);
 378  96
             }
 379  
         }
 380  
 
 381  4
         LOGGER.debug("\n----------------------------------------------------\nEND ANALYSIS\n----------------------------------------------------");
 382  4
         LOGGER.info("Analysis Complete ({} ms)", System.currentTimeMillis() - analysisStart);
 383  4
     }
 384  
 
 385  
     /**
 386  
      * Initializes the given analyzer.
 387  
      *
 388  
      * @param analyzer the analyzer to initialize
 389  
      * @return the initialized analyzer
 390  
      */
 391  
     protected Analyzer initializeAnalyzer(Analyzer analyzer) {
 392  
         try {
 393  96
             LOGGER.debug("Initializing {}", analyzer.getName());
 394  96
             analyzer.initialize();
 395  2
         } catch (Throwable ex) {
 396  2
             LOGGER.error("Exception occurred initializing {}.", analyzer.getName());
 397  2
             LOGGER.debug("", ex);
 398  
             try {
 399  2
                 analyzer.close();
 400  0
             } catch (Throwable ex1) {
 401  0
                 LOGGER.trace("", ex1);
 402  2
             }
 403  94
         }
 404  96
         return analyzer;
 405  
     }
 406  
 
 407  
     /**
 408  
      * Closes the given analyzer.
 409  
      *
 410  
      * @param analyzer the analyzer to close
 411  
      */
 412  
     protected void closeAnalyzer(Analyzer analyzer) {
 413  96
         LOGGER.debug("Closing Analyzer '{}'", analyzer.getName());
 414  
         try {
 415  96
             analyzer.close();
 416  0
         } catch (Throwable ex) {
 417  0
             LOGGER.trace("", ex);
 418  96
         }
 419  96
     }
 420  
 
 421  
     /**
 422  
      * Cycles through the cached web data sources and calls update on all of them.
 423  
      */
 424  
     public void doUpdates() {
 425  0
         LOGGER.info("Checking for updates");
 426  0
         final long updateStart = System.currentTimeMillis();
 427  0
         final UpdateService service = new UpdateService(serviceClassLoader);
 428  0
         final Iterator<CachedWebDataSource> iterator = service.getDataSources();
 429  0
         while (iterator.hasNext()) {
 430  0
             final CachedWebDataSource source = iterator.next();
 431  
             try {
 432  0
                 source.update();
 433  0
             } catch (UpdateException ex) {
 434  0
                 LOGGER.warn(
 435  
                         "Unable to update Cached Web DataSource, using local data instead. Results may not include recent vulnerabilities.");
 436  0
                 LOGGER.debug("Unable to update details for {}", source.getClass().getName(), ex);
 437  0
             }
 438  0
         }
 439  0
         LOGGER.info("Check for updates complete ({} ms)", System.currentTimeMillis() - updateStart);
 440  0
     }
 441  
 
 442  
     /**
 443  
      * Returns a full list of all of the analyzers. This is useful for reporting which analyzers where used.
 444  
      *
 445  
      * @return a list of Analyzers
 446  
      */
 447  
     public List<Analyzer> getAnalyzers() {
 448  0
         final List<Analyzer> ret = new ArrayList<Analyzer>();
 449  0
         for (AnalysisPhase phase : AnalysisPhase.values()) {
 450  0
             final List<Analyzer> analyzerList = analyzers.get(phase);
 451  0
             ret.addAll(analyzerList);
 452  
         }
 453  0
         return ret;
 454  
     }
 455  
 
 456  
     /**
 457  
      * Checks all analyzers to see if an extension is supported.
 458  
      *
 459  
      * @param file a file extension
 460  
      * @return true or false depending on whether or not the file extension is supported
 461  
      */
 462  
     @Override
 463  
     public boolean accept(File file) {
 464  1710
         if (file == null) {
 465  0
             return false;
 466  
         }
 467  1710
         boolean scan = false;
 468  1710
         for (FileTypeAnalyzer a : this.fileTypeAnalyzers) {
 469  
             /* note, we can't break early on this loop as the analyzers need to know if
 470  
              they have files to work on prior to initialization */
 471  27360
             scan |= a.accept(file);
 472  27360
         }
 473  1710
         return scan;
 474  
     }
 475  
 
 476  
     /**
 477  
      * Returns the set of file type analyzers.
 478  
      *
 479  
      * @return the set of file type analyzers
 480  
      */
 481  
     public Set<FileTypeAnalyzer> getFileTypeAnalyzers() {
 482  0
         return this.fileTypeAnalyzers;
 483  
     }
 484  
 
 485  
     /**
 486  
      * Checks the CPE Index to ensure documents exists. If none exist a NoDataException is thrown.
 487  
      *
 488  
      * @throws NoDataException thrown if no data exists in the CPE Index
 489  
      * @throws DatabaseException thrown if there is an exception opening the database
 490  
      */
 491  
     private void ensureDataExists() throws NoDataException, DatabaseException {
 492  4
         final CveDB cve = new CveDB();
 493  
         try {
 494  4
             cve.open();
 495  4
             if (!cve.dataExists()) {
 496  0
                 throw new NoDataException("No documents exist");
 497  
             }
 498  0
         } catch (DatabaseException ex) {
 499  0
             throw new NoDataException(ex.getMessage(), ex);
 500  
         } finally {
 501  4
             cve.close();
 502  4
         }
 503  4
     }
 504  
 }