View Javadoc
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.Objects;
26  import java.util.Optional;
27  import java.util.Set;
28  
29  import org.apache.maven.model.ConfigurationContainer;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.xml.Xpp3Dom;
37  import org.owasp.dependencycheck.Engine;
38  import org.owasp.dependencycheck.exception.ExceptionCollection;
39  
40  /**
41   * Maven Plugin that checks project dependencies and the dependencies of all
42   * child modules to see if they have any known published vulnerabilities.
43   *
44   * @author Jeremy Long
45   */
46  @Mojo(
47          name = "aggregate",
48          defaultPhase = LifecyclePhase.VERIFY,
49          aggregator = true,
50          threadSafe = true,
51          requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
52          requiresOnline = true
53  )
54  public class AggregateMojo extends BaseDependencyCheckMojo {
55  
56      /**
57       * The name of the report in the site.
58       */
59      @SuppressWarnings("CanBeFinal")
60      @Parameter(property = "name", defaultValue = "dependency-check:aggregate", required = true)
61      private String name = "dependency-check:aggregate";
62  
63      /**
64       * Scans the dependencies of the projects in aggregate.
65       *
66       * @param engine the engine used to perform the scanning
67       * @return a collection of exceptions
68       * @throws MojoExecutionException thrown if a fatal exception occurs
69       */
70      @Override
71      protected ExceptionCollection scanDependencies(final Engine engine) throws MojoExecutionException {
72          ExceptionCollection exCol = scanArtifacts(getProject(), engine, true);
73          for (MavenProject childProject : getDescendants(this.getProject())) {
74              //TODO consider the following as to whether a child should be skipped per #2152
75              //childProject.getBuildPlugins().get(0).getExecutions().get(0).getConfiguration()
76              final ExceptionCollection ex = scanArtifacts(childProject, engine, true);
77              if (ex != null) {
78                  if (exCol == null) {
79                      exCol = ex;
80                  } else {
81                      exCol.getExceptions().addAll(ex.getExceptions());
82                  }
83                  if (ex.isFatal()) {
84                      exCol.setFatal(true);
85                      final String msg = String.format("Fatal exception(s) analyzing %s", childProject.getName());
86                      if (this.isFailOnError()) {
87                          throw new MojoExecutionException(msg, exCol);
88                      }
89                      getLog().error(msg);
90                      if (getLog().isDebugEnabled()) {
91                          getLog().debug(exCol);
92                      }
93                  }
94              }
95          }
96          return exCol;
97      }
98  
99      /**
100      * Scans the plugins of the project.
101      *
102      * @param engine the engine used to perform the scanning
103      * @param exCollection the collection of exceptions that might have occurred
104      * previously
105      * @return a collection of exceptions
106      * @throws MojoExecutionException thrown if a fatal exception occurs
107      */
108     @Override
109     protected ExceptionCollection scanPlugins(final Engine engine, final ExceptionCollection exCollection) throws MojoExecutionException {
110         ExceptionCollection exCol = scanPlugins(getProject(), engine, null);
111         for (MavenProject childProject : getDescendants(this.getProject())) {
112             exCol = scanPlugins(childProject, engine, exCol);
113         }
114         return exCol;
115     }
116 
117     /**
118      * Returns a set containing all the descendant projects of the given
119      * project.
120      *
121      * @param project the project for which all descendants will be returned
122      * @return the set of descendant projects
123      */
124     protected Set<MavenProject> getDescendants(MavenProject project) {
125         if (project == null) {
126             return Collections.emptySet();
127         }
128         final Set<MavenProject> descendants = new HashSet<>();
129         int size;
130         if (getLog().isDebugEnabled()) {
131             getLog().debug(String.format("Collecting descendants of %s", project.getName()));
132         }
133         for (String m : project.getModules()) {
134             for (MavenProject mod : getReactorProjects()) {
135                 if (!isConfiguredToSkip(mod)) {
136                     try {
137                         File mpp = new File(project.getBasedir(), m);
138                         mpp = mpp.getCanonicalFile();
139                         if (mpp.compareTo(mod.getBasedir()) == 0 && descendants.add(mod)
140                                 && getLog().isDebugEnabled()) {
141                             getLog().debug(String.format("Descendant module %s added", mod.getName()));
142 
143                         }
144                     } catch (IOException ex) {
145                         if (getLog().isDebugEnabled()) {
146                             getLog().debug("Unable to determine module path", ex);
147                         }
148                     }
149                 }
150             }
151         }
152         do {
153             size = descendants.size();
154             for (MavenProject p : getReactorProjects()) {
155                 if (!isConfiguredToSkip(p)) {
156                     if (project.equals(p.getParent()) || descendants.contains(p.getParent())) {
157                         if (descendants.add(p) && getLog().isDebugEnabled()) {
158                             getLog().debug(String.format("Descendant %s added", p.getName()));
159 
160                         }
161                         for (MavenProject modTest : getReactorProjects()) {
162                             if (!isConfiguredToSkip(modTest)) {
163                                 if (p.getModules() != null && p.getModules().contains(modTest.getName())
164                                         && descendants.add(modTest)
165                                         && getLog().isDebugEnabled()) {
166                                     getLog().debug(String.format("Descendant %s added", modTest.getName()));
167                                 }
168                             }
169                         }
170                     }
171                     final Set<MavenProject> addedDescendants = new HashSet<>();
172                     for (MavenProject dec : descendants) {
173                         if (!isConfiguredToSkip(dec)) {
174                             for (String mod : dec.getModules()) {
175                                 try {
176                                     File mpp = new File(dec.getBasedir(), mod);
177                                     mpp = mpp.getCanonicalFile();
178                                     if (mpp.compareTo(p.getBasedir()) == 0) {
179                                         addedDescendants.add(p);
180                                     }
181                                 } catch (IOException ex) {
182                                     if (getLog().isDebugEnabled()) {
183                                         getLog().debug("Unable to determine module path", ex);
184                                     }
185                                 }
186                             }
187                         }
188                     }
189                     for (MavenProject addedDescendant : addedDescendants) {
190                         if (!isConfiguredToSkip(addedDescendant)) {
191                             if (descendants.add(addedDescendant) && getLog().isDebugEnabled()) {
192                                 getLog().debug(String.format("Descendant module %s added", addedDescendant.getName()));
193                             }
194                         }
195                     }
196                 }
197             }
198         } while (size != 0 && size != descendants.size());
199         if (getLog().isDebugEnabled()) {
200             getLog().debug(String.format("%s has %d children", project, descendants.size()));
201         }
202         return descendants;
203     }
204 
205     /**
206      * Checks the ODC configuration in the child project to see if should be
207      * skipped.
208      *
209      * @param mavenProject the maven project to check
210      * @return <code>true</code> if the project is configured to skip ODC;
211      * otherwise <code>false</code>
212      */
213     protected boolean isConfiguredToSkip(MavenProject mavenProject) {
214         final Optional<String> value = mavenProject.getBuildPlugins().stream()
215                 .filter(f -> "org.owasp:dependency-check-maven".equals(f.getKey()))
216                 .map(ConfigurationContainer::getConfiguration)
217                 .filter(c -> c != null && c instanceof Xpp3Dom)
218                 .map(c -> (Xpp3Dom) c)
219                 .map(c -> c.getChild("skip"))
220                 .filter(Objects::nonNull)
221                 .map(Xpp3Dom::getValue)
222                 .findFirst();
223 
224         final String property = mavenProject.getProperties().getProperty("dependency-check.skip");
225 
226         final boolean skip = (value.isPresent() && "true".equalsIgnoreCase(value.get())) || "true".equalsIgnoreCase(property);
227         if (skip) {
228             getLog().debug("Aggregation skipping " + mavenProject.getId());
229         }
230         return skip;
231     }
232 
233     /**
234      * Test if the project has pom packaging
235      *
236      * @param mavenProject Project to test
237      * @return <code>true</code> if it has a pom packaging; otherwise
238      * <code>false</code>
239      */
240     protected boolean isMultiModule(MavenProject mavenProject) {
241         return "pom".equals(mavenProject.getPackaging());
242     }
243 
244     @Override
245     public boolean canGenerateReport() {
246         return true; //aggregate always returns true for now - we can look at a more complicated/accurate solution later
247     }
248 
249     /**
250      * Returns the report name.
251      *
252      * @param locale the location
253      * @return the report name
254      */
255     @Override
256     public String getName(Locale locale) {
257         return name;
258     }
259 
260     /**
261      * Gets the description of the Dependency-Check report to be displayed in
262      * the Maven Generated Reports page.
263      *
264      * @param locale The Locale to get the description for
265      * @return the description
266      */
267     @Override
268     public String getDescription(Locale locale) {
269         return "Generates an aggregate report of all child Maven projects providing details on any "
270                 + "published vulnerabilities within project dependencies. This report is a best "
271                 + "effort and may contain false positives and false negatives.";
272     }
273 }