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