| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
@Mojo( |
| 48 | |
name = "aggregate", |
| 49 | |
defaultPhase = LifecyclePhase.VERIFY, |
| 50 | |
|
| 51 | |
threadSafe = false, |
| 52 | |
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, |
| 53 | |
requiresOnline = true |
| 54 | |
) |
| 55 | 0 | public class AggregateMojo extends BaseDependencyCheckMojo { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public void runCheck() throws MojoExecutionException, MojoFailureException { |
| 65 | 0 | final Engine engine = generateDataFile(); |
| 66 | |
|
| 67 | |
|
| 68 | 0 | if (getProject() == getLastProject()) { |
| 69 | |
|
| 70 | |
|
| 71 | 0 | for (MavenProject current : getReactorProjects()) { |
| 72 | 0 | final File dataFile = getDataFile(current); |
| 73 | 0 | if (dataFile == null && !skipProject(current)) { |
| 74 | 0 | getLog().error(String.format("Module '%s' did not execute dependency-check; an attempt will be made to perform " |
| 75 | 0 | + "the check but dependencies may be missed resulting in false negatives.", current.getName())); |
| 76 | 0 | generateDataFile(engine, current); |
| 77 | |
} |
| 78 | 0 | } |
| 79 | |
|
| 80 | 0 | for (MavenProject current : getReactorProjects()) { |
| 81 | 0 | List<Dependency> dependencies = readDataFile(current); |
| 82 | 0 | if (dependencies == null) { |
| 83 | 0 | dependencies = new ArrayList<Dependency>(); |
| 84 | |
} |
| 85 | 0 | final Set<MavenProject> childProjects = getDescendants(current); |
| 86 | 0 | for (MavenProject reportOn : childProjects) { |
| 87 | 0 | final List<Dependency> childDeps = readDataFile(reportOn); |
| 88 | 0 | if (childDeps != null && !childDeps.isEmpty()) { |
| 89 | 0 | if (getLog().isDebugEnabled()) { |
| 90 | 0 | getLog().debug(String.format("Adding %d dependencies from %s", childDeps.size(), reportOn.getName())); |
| 91 | |
} |
| 92 | 0 | dependencies.addAll(childDeps); |
| 93 | |
} else { |
| 94 | 0 | if (getLog().isDebugEnabled()) { |
| 95 | 0 | getLog().debug(String.format("No dependencies read for %s", reportOn.getName())); |
| 96 | |
} |
| 97 | |
} |
| 98 | 0 | } |
| 99 | 0 | engine.getDependencies().clear(); |
| 100 | 0 | engine.getDependencies().addAll(dependencies); |
| 101 | 0 | final DependencyBundlingAnalyzer bundler = new DependencyBundlingAnalyzer(); |
| 102 | |
try { |
| 103 | 0 | if (getLog().isDebugEnabled()) { |
| 104 | 0 | getLog().debug(String.format("Dependency count pre-bundler: %s", engine.getDependencies().size())); |
| 105 | |
} |
| 106 | 0 | bundler.analyze(null, engine); |
| 107 | 0 | if (getLog().isDebugEnabled()) { |
| 108 | 0 | getLog().debug(String.format("Dependency count post-bundler: %s", engine.getDependencies().size())); |
| 109 | |
} |
| 110 | 0 | } catch (AnalysisException ex) { |
| 111 | 0 | getLog().warn("An error occurred grouping the dependencies; duplicate entries may exist in the report", ex); |
| 112 | 0 | getLog().debug("Bundling Exception", ex); |
| 113 | 0 | } |
| 114 | |
|
| 115 | 0 | File outputDir = getCorrectOutputDirectory(current); |
| 116 | 0 | if (outputDir == null) { |
| 117 | |
|
| 118 | |
|
| 119 | 0 | outputDir = new File(current.getBuild().getDirectory()); |
| 120 | |
} |
| 121 | 0 | writeReports(engine, current, outputDir); |
| 122 | 0 | } |
| 123 | |
} |
| 124 | 0 | engine.cleanup(); |
| 125 | 0 | Settings.cleanup(); |
| 126 | 0 | } |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
private MavenProject getLastProject() { |
| 134 | 0 | for (int x = getReactorProjects().size() - 1; x >= 0; x--) { |
| 135 | 0 | final MavenProject p = getReactorProjects().get(x); |
| 136 | 0 | if (!skipProject(p)) { |
| 137 | 0 | return p; |
| 138 | |
} |
| 139 | |
|
| 140 | |
} |
| 141 | 0 | return null; |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
private boolean skipProject(MavenProject project) { |
| 151 | 0 | final String skip = (String) project.getProperties().get("maven.site.skip"); |
| 152 | 0 | return "true".equalsIgnoreCase(skip) && isGeneratingSite(); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
protected Set<MavenProject> getDescendants(MavenProject project) { |
| 162 | 0 | if (project == null) { |
| 163 | 0 | return Collections.emptySet(); |
| 164 | |
} |
| 165 | 0 | final Set<MavenProject> descendants = new HashSet<MavenProject>(); |
| 166 | 0 | int size = 0; |
| 167 | 0 | if (getLog().isDebugEnabled()) { |
| 168 | 0 | getLog().debug(String.format("Collecting descendants of %s", project.getName())); |
| 169 | |
} |
| 170 | 0 | for (String m : project.getModules()) { |
| 171 | 0 | for (MavenProject mod : getReactorProjects()) { |
| 172 | |
try { |
| 173 | 0 | File mpp = new File(project.getBasedir(), m); |
| 174 | 0 | mpp = mpp.getCanonicalFile(); |
| 175 | 0 | if (mpp.compareTo(mod.getBasedir()) == 0 && descendants.add(mod) |
| 176 | 0 | && getLog().isDebugEnabled()) { |
| 177 | 0 | getLog().debug(String.format("Decendent module %s added", mod.getName())); |
| 178 | |
|
| 179 | |
} |
| 180 | 0 | } catch (IOException ex) { |
| 181 | 0 | if (getLog().isDebugEnabled()) { |
| 182 | 0 | getLog().debug("Unable to determine module path", ex); |
| 183 | |
} |
| 184 | 0 | } |
| 185 | 0 | } |
| 186 | 0 | } |
| 187 | |
do { |
| 188 | 0 | size = descendants.size(); |
| 189 | 0 | for (MavenProject p : getReactorProjects()) { |
| 190 | 0 | if (project.equals(p.getParent()) || descendants.contains(p.getParent())) { |
| 191 | 0 | if (descendants.add(p) && getLog().isDebugEnabled()) { |
| 192 | 0 | getLog().debug(String.format("Decendent %s added", p.getName())); |
| 193 | |
|
| 194 | |
} |
| 195 | 0 | for (MavenProject modTest : getReactorProjects()) { |
| 196 | 0 | if (p.getModules() != null && p.getModules().contains(modTest.getName()) |
| 197 | 0 | && descendants.add(modTest) |
| 198 | 0 | && getLog().isDebugEnabled()) { |
| 199 | 0 | getLog().debug(String.format("Decendent %s added", modTest.getName())); |
| 200 | |
} |
| 201 | 0 | } |
| 202 | |
} |
| 203 | 0 | final Set<MavenProject> addedDescendants = new HashSet<MavenProject>(); |
| 204 | 0 | for (MavenProject dec : descendants) { |
| 205 | 0 | for (String mod : dec.getModules()) { |
| 206 | |
try { |
| 207 | 0 | File mpp = new File(dec.getBasedir(), mod); |
| 208 | 0 | mpp = mpp.getCanonicalFile(); |
| 209 | 0 | if (mpp.compareTo(p.getBasedir()) == 0) { |
| 210 | 0 | addedDescendants.add(p); |
| 211 | |
} |
| 212 | 0 | } catch (IOException ex) { |
| 213 | 0 | if (getLog().isDebugEnabled()) { |
| 214 | 0 | getLog().debug("Unable to determine module path", ex); |
| 215 | |
} |
| 216 | 0 | } |
| 217 | 0 | } |
| 218 | 0 | } |
| 219 | 0 | for (MavenProject addedDescendant : addedDescendants) { |
| 220 | 0 | if (descendants.add(addedDescendant) && getLog().isDebugEnabled()) { |
| 221 | 0 | getLog().debug(String.format("Decendent module %s added", addedDescendant.getName())); |
| 222 | |
} |
| 223 | 0 | } |
| 224 | 0 | } |
| 225 | 0 | } while (size != 0 && size != descendants.size()); |
| 226 | 0 | if (getLog().isDebugEnabled()) { |
| 227 | 0 | getLog().debug(String.format("%s has %d children", project, descendants.size())); |
| 228 | |
} |
| 229 | 0 | return descendants; |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
protected boolean isMultiModule(MavenProject mavenProject) { |
| 239 | 0 | return "pom".equals(mavenProject.getPackaging()); |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
protected Engine generateDataFile() throws MojoExecutionException, MojoFailureException { |
| 250 | |
final Engine engine; |
| 251 | |
try { |
| 252 | 0 | engine = initializeEngine(); |
| 253 | 0 | } catch (DatabaseException ex) { |
| 254 | 0 | if (getLog().isDebugEnabled()) { |
| 255 | 0 | getLog().debug("Database connection error", ex); |
| 256 | |
} |
| 257 | 0 | throw new MojoExecutionException("An exception occured connecting to the local database. Please see the log file for more details.", ex); |
| 258 | 0 | } |
| 259 | 0 | return generateDataFile(engine, getProject()); |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
protected Engine generateDataFile(Engine engine, MavenProject project) throws MojoExecutionException, MojoFailureException { |
| 272 | 0 | if (getLog().isDebugEnabled()) { |
| 273 | 0 | getLog().debug(String.format("Begin Scanning: %s", project.getName())); |
| 274 | |
} |
| 275 | 0 | engine.getDependencies().clear(); |
| 276 | 0 | engine.resetFileTypeAnalyzers(); |
| 277 | 0 | scanArtifacts(project, engine); |
| 278 | 0 | engine.analyzeDependencies(); |
| 279 | 0 | final File target = new File(project.getBuild().getDirectory()); |
| 280 | 0 | writeDataFile(project, target, engine.getDependencies()); |
| 281 | 0 | showSummary(project, engine.getDependencies()); |
| 282 | 0 | checkForFailure(engine.getDependencies()); |
| 283 | 0 | return engine; |
| 284 | |
} |
| 285 | |
|
| 286 | |
@Override |
| 287 | |
public boolean canGenerateReport() { |
| 288 | 0 | return true; |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | 0 | @SuppressWarnings("CanBeFinal") |
| 295 | |
@Parameter(property = "name", defaultValue = "dependency-check:aggregate", required = true) |
| 296 | |
private String name = "dependency-check:aggregate"; |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
@Override |
| 305 | |
public String getName(Locale locale) { |
| 306 | 0 | return name; |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
@Override |
| 316 | |
public String getDescription(Locale locale) { |
| 317 | 0 | return "Generates an aggregate report of all child Maven projects providing details on any " |
| 318 | |
+ "published vulnerabilities within project dependencies. This report is a best " |
| 319 | |
+ "effort and may contain false positives and false negatives."; |
| 320 | |
} |
| 321 | |
} |