Coverage Report - org.owasp.dependencycheck.maven.AggregateMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AggregateMojo
0%
0/115
0%
0/86
7.125
 
 1  
 /*
 2  
  * This file is part of dependency-check-maven.
 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) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.maven;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.IOException;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collections;
 24  
 import java.util.HashSet;
 25  
 import java.util.List;
 26  
 import java.util.Locale;
 27  
 import java.util.Set;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.MojoFailureException;
 30  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 31  
 import org.apache.maven.plugins.annotations.Mojo;
 32  
 import org.apache.maven.plugins.annotations.Parameter;
 33  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 34  
 import org.apache.maven.project.MavenProject;
 35  
 import org.owasp.dependencycheck.analyzer.DependencyBundlingAnalyzer;
 36  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 37  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 38  
 import org.owasp.dependencycheck.dependency.Dependency;
 39  
 import org.owasp.dependencycheck.utils.Settings;
 40  
 
 41  
 /**
 42  
  * Maven Plugin that checks project dependencies and the dependencies of all child modules to see if they have any known published
 43  
  * vulnerabilities.
 44  
  *
 45  
  * @author Jeremy Long
 46  
  */
 47  
 @Mojo(
 48  
         name = "aggregate",
 49  
         defaultPhase = LifecyclePhase.VERIFY,
 50  
         /*aggregator = true,*/
 51  
         threadSafe = true,
 52  
         requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
 53  
         requiresOnline = true
 54  
 )
 55  0
 public class AggregateMojo extends BaseDependencyCheckMojo {
 56  
 
 57  
     /**
 58  
      * Executes the aggregate dependency-check goal. This runs dependency-check and generates the subsequent reports.
 59  
      *
 60  
      * @throws MojoExecutionException thrown if there is ane exception running the mojo
 61  
      * @throws MojoFailureException thrown if dependency-check is configured to fail the build
 62  
      */
 63  
     @Override
 64  
     public void runCheck() throws MojoExecutionException, MojoFailureException {
 65  0
         final Engine engine = generateDataFile();
 66  
 
 67  0
         if (getProject() == getReactorProjects().get(getReactorProjects().size() - 1)) {
 68  
 
 69  
             //ensure that the .ser file was created for each.
 70  0
             for (MavenProject current : getReactorProjects()) {
 71  0
                 final File dataFile = getDataFile(current);
 72  0
                 if (dataFile == null) { //dc was never run on this project. write the ser to the target.
 73  0
                     getLog().error(String.format("Module '%s' did not execute dependency-check; an attempt will be made to perform "
 74  
                             + "the check but dependencies may be missed resulting in false negatives.", current.getName()));
 75  0
                     generateDataFile(engine, current);
 76  
                 }
 77  0
             }
 78  
 
 79  0
             for (MavenProject current : getReactorProjects()) {
 80  0
                 List<Dependency> dependencies = readDataFile(current);
 81  0
                 if (dependencies == null) {
 82  0
                     dependencies = new ArrayList<Dependency>();
 83  
                 }
 84  0
                 final Set<MavenProject> childProjects = getDescendants(current);
 85  0
                 for (MavenProject reportOn : childProjects) {
 86  0
                     final List<Dependency> childDeps = readDataFile(reportOn);
 87  0
                     if (childDeps != null && !childDeps.isEmpty()) {
 88  0
                         if (getLog().isDebugEnabled()) {
 89  0
                             getLog().debug(String.format("Adding %d dependencies from %s", childDeps.size(), reportOn.getName()));
 90  
                         }
 91  0
                         dependencies.addAll(childDeps);
 92  
                     } else {
 93  0
                         if (getLog().isDebugEnabled()) {
 94  0
                             getLog().debug(String.format("No dependencies read for %s", reportOn.getName()));
 95  
                         }
 96  
                     }
 97  0
                 }
 98  0
                 engine.getDependencies().clear();
 99  0
                 engine.getDependencies().addAll(dependencies);
 100  0
                 final DependencyBundlingAnalyzer bundler = new DependencyBundlingAnalyzer();
 101  
                 try {
 102  0
                     if (getLog().isDebugEnabled()) {
 103  0
                         getLog().debug(String.format("Dependency count pre-bundler: %s", engine.getDependencies().size()));
 104  
                     }
 105  0
                     bundler.analyze(null, engine);
 106  0
                     if (getLog().isDebugEnabled()) {
 107  0
                         getLog().debug(String.format("Dependency count post-bundler: %s", engine.getDependencies().size()));
 108  
                     }
 109  0
                 } catch (AnalysisException ex) {
 110  0
                     getLog().warn("An error occured grouping the dependencies; duplicate entries may exist in the report", ex);
 111  0
                     getLog().debug("Bundling Exception", ex);
 112  0
                 }
 113  
 
 114  0
                 File outputDir = getCorrectOutputDirectory(current);
 115  0
                 if (outputDir == null) {
 116  
                     //in some regards we shouldn't be writting this, but we are anyway.
 117  
                     //we shouldn't write this because nothing is configured to generate this report.
 118  0
                     outputDir = new File(current.getBuild().getDirectory());
 119  
                 }
 120  0
                 writeReports(engine, current, outputDir);
 121  0
             }
 122  
         }
 123  0
         engine.cleanup();
 124  0
         Settings.cleanup();
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Returns a set containing all the descendant projects of the given project.
 129  
      *
 130  
      * @param project the project for which all descendants will be returned
 131  
      * @return the set of descendant projects
 132  
      */
 133  
     protected Set<MavenProject> getDescendants(MavenProject project) {
 134  0
         if (project == null) {
 135  0
             return Collections.emptySet();
 136  
         }
 137  0
         final Set<MavenProject> descendants = new HashSet<MavenProject>();
 138  0
         int size = 0;
 139  0
         if (getLog().isDebugEnabled()) {
 140  0
             getLog().debug(String.format("Collecting descendants of %s", project.getName()));
 141  
         }
 142  0
         for (String m : project.getModules()) {
 143  0
             for (MavenProject mod : getReactorProjects()) {
 144  
                 try {
 145  0
                     File mpp = new File(project.getBasedir(), m);
 146  0
                     mpp = mpp.getCanonicalFile();
 147  0
                     if (mpp.compareTo(mod.getBasedir()) == 0 && descendants.add(mod)
 148  
                             && getLog().isDebugEnabled()) {
 149  0
                         getLog().debug(String.format("Decendent module %s added", mod.getName()));
 150  
 
 151  
                     }
 152  0
                 } catch (IOException ex) {
 153  0
                     if (getLog().isDebugEnabled()) {
 154  0
                         getLog().debug("Unable to determine module path", ex);
 155  
                     }
 156  0
                 }
 157  0
             }
 158  0
         }
 159  
         do {
 160  0
             size = descendants.size();
 161  0
             for (MavenProject p : getReactorProjects()) {
 162  0
                 if (project.equals(p.getParent()) || descendants.contains(p.getParent())) {
 163  0
                     if (descendants.add(p) && getLog().isDebugEnabled()) {
 164  0
                         getLog().debug(String.format("Decendent %s added", p.getName()));
 165  
 
 166  
                     }
 167  0
                     for (MavenProject modTest : getReactorProjects()) {
 168  0
                         if (p.getModules() != null && p.getModules().contains(modTest.getName())
 169  
                                 && descendants.add(modTest)
 170  
                                 && getLog().isDebugEnabled()) {
 171  0
                             getLog().debug(String.format("Decendent %s added", modTest.getName()));
 172  
                         }
 173  0
                     }
 174  
                 }
 175  0
                 final Set<MavenProject> addedDescendants = new HashSet<MavenProject>();
 176  0
                 for (MavenProject dec : descendants) {
 177  0
                     for (String mod : dec.getModules()) {
 178  
                         try {
 179  0
                             File mpp = new File(dec.getBasedir(), mod);
 180  0
                             mpp = mpp.getCanonicalFile();
 181  0
                             if (mpp.compareTo(p.getBasedir()) == 0) {
 182  0
                                 addedDescendants.add(p);
 183  
                             }
 184  0
                         } catch (IOException ex) {
 185  0
                             if (getLog().isDebugEnabled()) {
 186  0
                                 getLog().debug("Unable to determine module path", ex);
 187  
                             }
 188  0
                         }
 189  0
                     }
 190  0
                 }
 191  0
                 for (MavenProject addedDescendant : addedDescendants) {
 192  0
                     if (descendants.add(addedDescendant) && getLog().isDebugEnabled()) {
 193  0
                         getLog().debug(String.format("Decendent module %s added", addedDescendant.getName()));
 194  
                     }
 195  0
                 }
 196  0
             }
 197  0
         } while (size != 0 && size != descendants.size());
 198  0
         if (getLog().isDebugEnabled()) {
 199  0
             getLog().debug(String.format("%s has %d children", project, descendants.size()));
 200  
         }
 201  0
         return descendants;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Test if the project has pom packaging
 206  
      *
 207  
      * @param mavenProject Project to test
 208  
      * @return <code>true</code> if it has a pom packaging; otherwise <code>false</code>
 209  
      */
 210  
     protected boolean isMultiModule(MavenProject mavenProject) {
 211  0
         return "pom".equals(mavenProject.getPackaging());
 212  
     }
 213  
 
 214  
     /**
 215  
      * Initilizes the engine, runs a scan, and writes the serialized dependencies to disk.
 216  
      *
 217  
      * @return the Engine used to execute dependency-check
 218  
      * @throws MojoExecutionException thrown if there is an exception running the mojo
 219  
      * @throws MojoFailureException thrown if dependency-check is configured to fail the build if severe CVEs are identified.
 220  
      */
 221  
     protected Engine generateDataFile() throws MojoExecutionException, MojoFailureException {
 222  
         final Engine engine;
 223  
         try {
 224  0
             engine = initializeEngine();
 225  0
         } catch (DatabaseException ex) {
 226  0
             if (getLog().isDebugEnabled()) {
 227  0
                 getLog().debug("Database connection error", ex);
 228  
             }
 229  0
             throw new MojoExecutionException("An exception occured connecting to the local database. Please see the log file for more details.", ex);
 230  0
         }
 231  0
         return generateDataFile(engine, getProject());
 232  
     }
 233  
 
 234  
     /**
 235  
      * Runs dependency-check's Engine and writes the serialized dependencies to disk.
 236  
      *
 237  
      * @param engine the Engine to use when scanning.
 238  
      * @param project the project to scan and generate the data file for
 239  
      * @return the Engine used to execute dependency-check
 240  
      * @throws MojoExecutionException thrown if there is an exception running the mojo
 241  
      * @throws MojoFailureException thrown if dependency-check is configured to fail the build if severe CVEs are identified.
 242  
      */
 243  
     protected Engine generateDataFile(Engine engine, MavenProject project) throws MojoExecutionException, MojoFailureException {
 244  0
         if (getLog().isDebugEnabled()) {
 245  0
             getLog().debug(String.format("Begin Scanning: %s", project.getName()));
 246  
         }
 247  0
         engine.getDependencies().clear();
 248  0
         engine.resetFileTypeAnalyzers();
 249  0
         scanArtifacts(project, engine);
 250  0
         engine.analyzeDependencies();
 251  0
         final File target = new File(project.getBuild().getDirectory());
 252  0
         writeDataFile(project, target, engine.getDependencies());
 253  0
         showSummary(project, engine.getDependencies());
 254  0
         checkForFailure(engine.getDependencies());
 255  0
         return engine;
 256  
     }
 257  
 
 258  
     @Override
 259  
     public boolean canGenerateReport() {
 260  0
         return true; //aggregate always returns true for now - we can look at a more complicated/acurate solution later
 261  
     }
 262  
 
 263  
     /**
 264  
      * The name of the report in the site.
 265  
      */
 266  0
     @SuppressWarnings("CanBeFinal")
 267  
     @Parameter(property = "name", defaultValue = "dependency-check:aggregate", required = true)
 268  
     private String name = "dependency-check:aggregate";
 269  
 
 270  
     /**
 271  
      * Returns the report name.
 272  
      *
 273  
      * @param locale the location
 274  
      * @return the report name
 275  
      */
 276  
     @Override
 277  
     public String getName(Locale locale) {
 278  0
         return name;
 279  
     }
 280  
 
 281  
     /**
 282  
      * Gets the description of the Dependency-Check report to be displayed in the Maven Generated Reports page.
 283  
      *
 284  
      * @param locale The Locale to get the description for
 285  
      * @return the description
 286  
      */
 287  
     @Override
 288  
     public String getDescription(Locale locale) {
 289  0
         return "Generates an aggregate report of all child Maven projects providing details on any "
 290  
                 + "published vulnerabilities within project dependencies. This report is a best "
 291  
                 + "effort and may contain false positives and false negatives.";
 292  
     }
 293  
 }