Coverage Report - org.owasp.dependencycheck.maven.DependencyCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyCheckMojo
0%
0/326
0%
0/116
3.429
 
 1  
 /*
 2  
  * This file is part of dependency-check-maven.
 3  
  *
 4  
  * Dependency-check-maven is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-maven is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-maven. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.maven;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.UnsupportedEncodingException;
 25  
 import java.net.URLEncoder;
 26  
 import java.text.DateFormat;
 27  
 import java.util.Date;
 28  
 import java.util.List;
 29  
 import java.util.Locale;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 import org.apache.maven.doxia.sink.SinkFactory;
 33  
 import org.apache.maven.plugin.AbstractMojo;
 34  
 import org.apache.maven.plugin.MojoExecutionException;
 35  
 import org.apache.maven.project.MavenProject;
 36  
 import java.util.Set;
 37  
 import java.util.logging.LogManager;
 38  
 import org.apache.maven.artifact.Artifact;
 39  
 import org.apache.maven.plugins.annotations.Component;
 40  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 41  
 import org.apache.maven.plugins.annotations.Mojo;
 42  
 import org.apache.maven.plugins.annotations.Parameter;
 43  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 44  
 import org.apache.maven.reporting.MavenMultiPageReport;
 45  
 import org.apache.maven.reporting.MavenReport;
 46  
 import org.apache.maven.reporting.MavenReportException;
 47  
 import org.apache.maven.doxia.sink.Sink;
 48  
 import org.apache.maven.plugin.MojoFailureException;
 49  
 import org.owasp.dependencycheck.Engine;
 50  
 import org.owasp.dependencycheck.dependency.Dependency;
 51  
 import org.owasp.dependencycheck.dependency.Evidence;
 52  
 import org.owasp.dependencycheck.dependency.Identifier;
 53  
 import org.owasp.dependencycheck.dependency.Reference;
 54  
 import org.owasp.dependencycheck.dependency.Vulnerability;
 55  
 import org.owasp.dependencycheck.dependency.VulnerableSoftware;
 56  
 import org.owasp.dependencycheck.reporting.ReportGenerator;
 57  
 import org.owasp.dependencycheck.utils.Settings;
 58  
 
 59  
 /**
 60  
  * Maven Plugin that checks project dependencies to see if they have any known
 61  
  * published vulnerabilities.
 62  
  *
 63  
  * @author Jeremy Long (jeremy.long@owasp.org)
 64  
  */
 65  
 @Mojo(name = "check", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true,
 66  
         requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM,
 67  
         requiresOnline = true)
 68  0
 public class DependencyCheckMojo extends AbstractMojo implements MavenMultiPageReport {
 69  
 
 70  
     /**
 71  
      * The properties file location.
 72  
      */
 73  
     private static final String PROPERTIES_FILE = "mojo.properties";
 74  
     /**
 75  
      * Name of the logging properties file.
 76  
      */
 77  
     private static final String LOG_PROPERTIES_FILE = "log.properties";
 78  
     /**
 79  
      * The name of the test scope.
 80  
      */
 81  
     public static final String TEST_SCOPE = "test";
 82  
     // <editor-fold defaultstate="collapsed" desc="Maven bound parameters and components">
 83  
     /**
 84  
      * The Maven Project Object.
 85  
      */
 86  
     @Component
 87  
     private MavenProject project;
 88  
     /**
 89  
      * The name of the site report destination.
 90  
      */
 91  
     @Parameter(property = "report-name", defaultValue = "dependency-check-report")
 92  
     private String reportName;
 93  
     /**
 94  
      * The name of the report to be displayed in the Maven Generated Reports
 95  
      * page
 96  
      */
 97  
     @Parameter(property = "name", defaultValue = "Dependency-Check")
 98  
     private String name;
 99  
     /**
 100  
      * The description of the Dependency-Check report to be displayed in the
 101  
      * Maven Generated Reports page
 102  
      */
 103  
     @Parameter(property = "description", defaultValue = "A report providing details on any published "
 104  
             + "vulnerabilities within project dependencies. This report is a best effort but may contain "
 105  
             + "false positives and false negatives.")
 106  
     private String description;
 107  
     /**
 108  
      * Specifies the destination directory for the generated Dependency-Check
 109  
      * report.
 110  
      */
 111  
     @Parameter(property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}", required = true)
 112  
     private File reportOutputDirectory;
 113  
     /**
 114  
      * Specifies if the build should be failed if a CVSS score above a specified
 115  
      * level is identified. The default is 11 which means since the CVSS scores
 116  
      * are 0-10, by default the build will never fail.
 117  
      */
 118  0
     @Parameter(property = "failBuildOnCVSS", defaultValue = "11", required = true)
 119  
     private float failBuildOnCVSS = 11;
 120  
     /**
 121  
      * The output directory.
 122  
      */
 123  
     @Parameter(defaultValue = "${project.build.directory}", required = true)
 124  
     private File outputDirectory;
 125  
     /**
 126  
      * Sets whether auto-updating of the NVD CVE/CPE data is enabled. It is not
 127  
      * recommended that this be turned to false. Default is true.
 128  
      */
 129  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 130  
     @Parameter(property = "autoupdate", defaultValue = "true", required = true)
 131  
     private boolean autoUpdate = true;
 132  
     /**
 133  
      * The report format to be generated (HTML, XML, VULN, ALL). This
 134  
      * configuration option has no affect if using this within the Site plugin
 135  
      * unless the externalReport is set to true. Default is HTML.
 136  
      */
 137  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 138  
     @Parameter(property = "format", defaultValue = "HTML", required = true)
 139  
     private String format = "HTML";
 140  
     /**
 141  
      * Sets whether or not the external report format should be used.
 142  
      */
 143  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 144  
     @Parameter(property = "externalReport", defaultValue = "false", required = true)
 145  
     private boolean externalReport = false;
 146  
     /**
 147  
      * The Proxy URL.
 148  
      */
 149  0
     @SuppressWarnings("CanBeFinal")
 150  
     @Parameter(property = "proxyUrl", defaultValue = "", required = false)
 151  
     private String proxyUrl = null;
 152  
     /**
 153  
      * The Proxy Port.
 154  
      */
 155  0
     @SuppressWarnings("CanBeFinal")
 156  
     @Parameter(property = "proxyPort", defaultValue = "", required = false)
 157  
     private String proxyPort = null;
 158  
     /**
 159  
      * The Connection Timeout.
 160  
      */
 161  0
     @SuppressWarnings("CanBeFinal")
 162  
     @Parameter(property = "connectionTimeout", defaultValue = "", required = false)
 163  
     private String connectionTimeout = null;
 164  
 
 165  
     // </editor-fold>
 166  
     /**
 167  
      * Configures the logger for use by the application.
 168  
      */
 169  
     private static void prepareLogger() {
 170  0
         InputStream in = null;
 171  
         try {
 172  0
             in = DependencyCheckMojo.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
 173  0
             LogManager.getLogManager().reset();
 174  0
             LogManager.getLogManager().readConfiguration(in);
 175  
             //TODO add code to disable fine grained log file.
 176  
 //            Logger logger = LogManager.getLogManager().getLogger("");
 177  
 //            for (Handler h : logger.getHandlers()) {
 178  
 //                if (h.getFormatter(). h.toString());
 179  
 //            }
 180  0
         } catch (IOException ex) {
 181  0
             System.err.println(ex.toString());
 182  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 183  0
         } catch (SecurityException ex) {
 184  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 185  
         } finally {
 186  0
             if (in != null) {
 187  
                 try {
 188  0
                     in.close();
 189  0
                 } catch (Exception ex) {
 190  
                     //noinspection UnusedAssignment
 191  0
                     in = null;
 192  0
                 }
 193  
             }
 194  
         }
 195  0
     }
 196  
 
 197  
     /**
 198  
      * Executes the Dependency-Check on the dependent libraries.
 199  
      *
 200  
      * @return the Engine used to scan the dependencies.
 201  
      */
 202  
     private Engine executeDependencyCheck() {
 203  0
         prepareLogger();
 204  0
         populateSettings();
 205  0
         final Engine engine = new Engine();
 206  0
         final Set<Artifact> artifacts = project.getArtifacts();
 207  0
         for (Artifact a : artifacts) {
 208  0
             if (!TEST_SCOPE.equals(a.getScope())) {
 209  0
                 engine.scan(a.getFile().getAbsolutePath());
 210  
             }
 211  
         }
 212  0
         engine.analyzeDependencies();
 213  0
         return engine;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Generates the reports for a given dependency-check engine.
 218  
      *
 219  
      * @param engine a dependency-check engine
 220  
      */
 221  
     private void generateExternalReports(Engine engine) {
 222  0
         final ReportGenerator r = new ReportGenerator(project.getName(), engine.getDependencies(), engine.getAnalyzers());
 223  
         try {
 224  0
             r.generateReports(outputDirectory.getCanonicalPath(), format);
 225  0
         } catch (IOException ex) {
 226  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 227  0
         } catch (Exception ex) {
 228  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 229  0
         }
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Generates a dependency-check report using the Maven Site format.
 234  
      *
 235  
      * @param engine the engine used to scan the dependencies
 236  
      * @param sink the sink to write the data to
 237  
      */
 238  
     private void generateMavenSiteReport(final Engine engine, Sink sink) {
 239  0
         final List<Dependency> dependencies = engine.getDependencies();
 240  
 
 241  0
         writeSiteReportHeader(sink, project.getName());
 242  0
         writeSiteReportTOC(sink, dependencies);
 243  
 
 244  0
         int cnt = 0;
 245  0
         for (Dependency d : dependencies) {
 246  0
             writeSiteReportDependencyHeader(sink, d);
 247  0
             cnt = writeSiteReportDependencyAnalysisExceptions(d, cnt, sink);
 248  0
             cnt = writeSiteReportDependencyEvidenceUsed(d, cnt, sink);
 249  0
             cnt = writeSiteReportDependencyRelatedDependencies(d, cnt, sink);
 250  0
             writeSiteReportDependencyIdentifiers(d, sink);
 251  0
             writeSiteReportDependencyVulnerabilities(d, sink, cnt);
 252  
         }
 253  0
         sink.body_();
 254  0
     }
 255  
 
 256  
     // <editor-fold defaultstate="collapsed" desc="various writeXXXXX methods to generate the Site Report">
 257  
     /**
 258  
      * Writes the vulnerabilities to the site report.
 259  
      *
 260  
      * @param d the dependency
 261  
      * @param sink the sink to write the data to
 262  
      * @param collapsibleHeaderCount the collapsible header count
 263  
      */
 264  
     private void writeSiteReportDependencyVulnerabilities(Dependency d, Sink sink, int collapsibleHeaderCount) {
 265  0
         int cnt = collapsibleHeaderCount;
 266  0
         if (d.getVulnerabilities() != null && !d.getVulnerabilities().isEmpty()) {
 267  0
             for (Vulnerability v : d.getVulnerabilities()) {
 268  
 
 269  0
                 sink.paragraph();
 270  0
                 sink.bold();
 271  
                 try {
 272  0
                     sink.link("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + URLEncoder.encode(v.getName(), "US-ASCII"));
 273  0
                     sink.text(v.getName());
 274  0
                     sink.link_();
 275  0
                     sink.bold_();
 276  0
                 } catch (UnsupportedEncodingException ex) {
 277  0
                     sink.text(v.getName());
 278  0
                     sink.bold_();
 279  0
                     sink.lineBreak();
 280  0
                     sink.text("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + v.getName());
 281  0
                 }
 282  0
                 sink.paragraph_();
 283  0
                 sink.paragraph();
 284  0
                 sink.text("Severity: ");
 285  0
                 if (v.getCvssScore() < 4.0) {
 286  0
                     sink.text("Low");
 287  
                 } else {
 288  0
                     if (v.getCvssScore() >= 7.0) {
 289  0
                         sink.text("High");
 290  
                     } else {
 291  0
                         sink.text("Medium");
 292  
                     }
 293  
                 }
 294  0
                 sink.lineBreak();
 295  0
                 sink.text("CVSS Score: " + v.getCvssScore());
 296  0
                 if (v.getCwe() != null && !v.getCwe().isEmpty()) {
 297  0
                     sink.lineBreak();
 298  0
                     sink.text("CWE: ");
 299  0
                     sink.text(v.getCwe());
 300  
                 }
 301  0
                 sink.paragraph_();
 302  0
                 sink.paragraph();
 303  0
                 sink.text(v.getDescription());
 304  0
                 if (v.getReferences() != null && !v.getReferences().isEmpty()) {
 305  0
                     sink.list();
 306  0
                     for (Reference ref : v.getReferences()) {
 307  0
                         sink.listItem();
 308  0
                         sink.text(ref.getSource());
 309  0
                         sink.text(" - ");
 310  0
                         sink.link(ref.getUrl());
 311  0
                         sink.text(ref.getName());
 312  0
                         sink.link_();
 313  0
                         sink.listItem_();
 314  
                     }
 315  0
                     sink.list_();
 316  
                 }
 317  0
                 sink.paragraph_();
 318  0
                 if (v.getVulnerableSoftware() != null && !v.getVulnerableSoftware().isEmpty()) {
 319  0
                     sink.paragraph();
 320  
 
 321  0
                     cnt += 1;
 322  0
                     sink.rawText("Vulnerable Software <a href=\"javascript:toggleElement(this, 'vulnSoft" + cnt + "')\">[-]</a>");
 323  0
                     sink.rawText("<div id=\"vulnSoft" + cnt + "\" style=\"display:block\">");
 324  0
                     sink.list();
 325  0
                     for (VulnerableSoftware vs : v.getVulnerableSoftware()) {
 326  0
                         sink.listItem();
 327  
                         try {
 328  0
                             sink.link("http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + URLEncoder.encode(vs.getName(), "US-ASCII"));
 329  0
                             sink.text(vs.getName());
 330  0
                             sink.link_();
 331  0
                             if (vs.hasPreviousVersion()) {
 332  0
                                 sink.text(" and all previous versions.");
 333  
                             }
 334  0
                         } catch (UnsupportedEncodingException ex) {
 335  0
                             sink.text(vs.getName());
 336  0
                             if (vs.hasPreviousVersion()) {
 337  0
                                 sink.text(" and all previous versions.");
 338  
                             }
 339  0
                             sink.text(" (http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + vs.getName() + ")");
 340  0
                         }
 341  
 
 342  0
                         sink.listItem_();
 343  
                     }
 344  0
                     sink.list_();
 345  0
                     sink.rawText("</div>");
 346  0
                     sink.paragraph_();
 347  
                 }
 348  
             }
 349  
         }
 350  0
     }
 351  
 
 352  
     /**
 353  
      * Writes the identifiers to the site report.
 354  
      *
 355  
      * @param d the dependency
 356  
      * @param sink the sink to write the data to
 357  
      */
 358  
     private void writeSiteReportDependencyIdentifiers(Dependency d, Sink sink) {
 359  0
         if (d.getIdentifiers() != null && !d.getIdentifiers().isEmpty()) {
 360  0
             sink.sectionTitle4();
 361  0
             sink.text("Identifiers");
 362  0
             sink.sectionTitle4_();
 363  0
             sink.list();
 364  0
             for (Identifier i : d.getIdentifiers()) {
 365  0
                 sink.listItem();
 366  0
                 sink.text(i.getType());
 367  0
                 sink.text(": ");
 368  0
                 if (i.getUrl() != null && i.getUrl().length() > 0) {
 369  0
                     sink.link(i.getUrl());
 370  0
                     sink.text(i.getValue());
 371  0
                     sink.link_();
 372  
                 } else {
 373  0
                     sink.text(i.getValue());
 374  
                 }
 375  0
                 if (i.getDescription() != null && i.getDescription().length() > 0) {
 376  0
                     sink.lineBreak();
 377  0
                     sink.text(i.getDescription());
 378  
                 }
 379  0
                 sink.listItem_();
 380  
             }
 381  0
             sink.list_();
 382  
         }
 383  0
     }
 384  
 
 385  
     /**
 386  
      * Writes the related dependencies to the site report.
 387  
      *
 388  
      * @param d the dependency
 389  
      * @param sink the sink to write the data to
 390  
      * @param collapsibleHeaderCount the collapsible header count
 391  
      * @return the collapsible header count
 392  
      */
 393  
     private int writeSiteReportDependencyRelatedDependencies(Dependency d, int collapsibleHeaderCount, Sink sink) {
 394  0
         int cnt = collapsibleHeaderCount;
 395  0
         if (d.getRelatedDependencies() != null && !d.getRelatedDependencies().isEmpty()) {
 396  0
             cnt += 1;
 397  0
             sink.sectionTitle4();
 398  0
             sink.rawText("Related Dependencies <a href=\"javascript:toggleElement(this, 'related" + cnt + "')\">[+]</a>");
 399  0
             sink.sectionTitle4_();
 400  0
             sink.rawText("<div id=\"related" + cnt + "\" style=\"display:none\">");
 401  0
             sink.list();
 402  0
             for (Dependency r : d.getRelatedDependencies()) {
 403  0
                 sink.listItem();
 404  0
                 sink.text(r.getFileName());
 405  0
                 sink.list();
 406  0
                 writeListItem(sink, "File Path: " + r.getFilePath());
 407  0
                 writeListItem(sink, "SHA1: " + r.getSha1sum());
 408  0
                 writeListItem(sink, "MD5: " + r.getMd5sum());
 409  0
                 sink.list_();
 410  0
                 sink.listItem_();
 411  
             }
 412  0
             sink.list_();
 413  0
             sink.rawText("</div>");
 414  
         }
 415  0
         return cnt;
 416  
     }
 417  
 
 418  
     /**
 419  
      * Writes the evidence used to the site report.
 420  
      *
 421  
      * @param d the dependency
 422  
      * @param sink the sink to write the data to
 423  
      * @param collapsibleHeaderCount the collapsible header count
 424  
      * @return the collapsible header count
 425  
      */
 426  
     private int writeSiteReportDependencyEvidenceUsed(Dependency d, int collapsibleHeaderCount, Sink sink) {
 427  0
         int cnt = collapsibleHeaderCount;
 428  0
         if (d.getEvidenceUsed() != null && d.getEvidenceUsed().size() > 0) {
 429  0
             cnt += 1;
 430  0
             sink.sectionTitle4();
 431  0
             sink.rawText("Evidence Collected <a href=\"javascript:toggleElement(this, 'evidence" + cnt + "')\">[+]</a>");
 432  0
             sink.sectionTitle4_();
 433  0
             sink.rawText("<div id=\"evidence" + cnt + "\" style=\"display:none\">");
 434  0
             sink.table();
 435  0
             sink.tableRow();
 436  0
             writeTableHeaderCell(sink, "Source");
 437  0
             writeTableHeaderCell(sink, "Name");
 438  0
             writeTableHeaderCell(sink, "Value");
 439  0
             sink.tableRow_();
 440  0
             for (Evidence e : d.getEvidenceUsed()) {
 441  0
                 sink.tableRow();
 442  0
                 writeTableCell(sink, e.getSource());
 443  0
                 writeTableCell(sink, e.getName());
 444  0
                 writeTableCell(sink, e.getValue());
 445  0
                 sink.tableRow_();
 446  
             }
 447  0
             sink.table_();
 448  0
             sink.rawText("</div>");
 449  
         }
 450  0
         return cnt;
 451  
     }
 452  
 
 453  
     /**
 454  
      * Writes the analysis exceptions generated during analysis to the site
 455  
      * report.
 456  
      *
 457  
      * @param d the dependency
 458  
      * @param sink the sink to write the data to
 459  
      * @param collapsibleHeaderCount the collapsible header count
 460  
      * @return the collapsible header count
 461  
      */
 462  
     private int writeSiteReportDependencyAnalysisExceptions(Dependency d, int collapsibleHeaderCount, Sink sink) {
 463  0
         int cnt = collapsibleHeaderCount;
 464  0
         if (d.getAnalysisExceptions() != null && !d.getAnalysisExceptions().isEmpty()) {
 465  0
             cnt += 1;
 466  0
             sink.sectionTitle4();
 467  0
             sink.rawText("<font style=\"color:red\">Errors occurred during analysis:</font> <a href=\"javascript:toggleElement(this, 'errors"
 468  
                     + cnt + "')\">[+]</a>");
 469  0
             sink.sectionTitle4_();
 470  0
             sink.rawText("<div id=\"errors" + cnt + "\">");
 471  0
             sink.list();
 472  0
             for (Exception e : d.getAnalysisExceptions()) {
 473  0
                 sink.listItem();
 474  0
                 sink.text(e.getMessage());
 475  0
                 sink.listItem_();
 476  
             }
 477  0
             sink.list_();
 478  0
             sink.rawText("</div>");
 479  
         }
 480  0
         return cnt;
 481  
     }
 482  
 
 483  
     /**
 484  
      * Writes the dependency header to the site report.
 485  
      *
 486  
      * @param d the dependency
 487  
      * @param sink the sink to write the data to
 488  
      */
 489  
     private void writeSiteReportDependencyHeader(Sink sink, Dependency d) {
 490  0
         sink.sectionTitle2();
 491  0
         sink.anchor("sha1" + d.getSha1sum());
 492  0
         sink.text(d.getFileName());
 493  0
         sink.anchor_();
 494  0
         sink.sectionTitle2_();
 495  0
         if (d.getDescription() != null && d.getDescription().length() > 0) {
 496  0
             sink.paragraph();
 497  0
             sink.bold();
 498  0
             sink.text("Description: ");
 499  0
             sink.bold_();
 500  0
             sink.text(d.getDescription());
 501  0
             sink.paragraph_();
 502  
         }
 503  0
         if (d.getLicense() != null && d.getLicense().length() > 0) {
 504  0
             sink.paragraph();
 505  0
             sink.bold();
 506  0
             sink.text("License: ");
 507  0
             sink.bold_();
 508  0
             if (d.getLicense().startsWith("http://") && !d.getLicense().contains(" ")) {
 509  0
                 sink.link(d.getLicense());
 510  0
                 sink.text(d.getLicense());
 511  0
                 sink.link_();
 512  
             } else {
 513  0
                 sink.text(d.getLicense());
 514  
             }
 515  0
             sink.paragraph_();
 516  
         }
 517  0
     }
 518  
 
 519  
     /**
 520  
      * Adds a list item to the site report.
 521  
      *
 522  
      * @param sink the sink to write the data to
 523  
      * @param text the text to write
 524  
      */
 525  
     private void writeListItem(Sink sink, String text) {
 526  0
         sink.listItem();
 527  0
         sink.text(text);
 528  0
         sink.listItem_();
 529  0
     }
 530  
 
 531  
     /**
 532  
      * Adds a table cell to the site report.
 533  
      *
 534  
      * @param sink the sink to write the data to
 535  
      * @param text the text to write
 536  
      */
 537  
     private void writeTableCell(Sink sink, String text) {
 538  0
         sink.tableCell();
 539  0
         sink.text(text);
 540  0
         sink.tableCell_();
 541  0
     }
 542  
 
 543  
     /**
 544  
      * Adds a table header cell to the site report.
 545  
      *
 546  
      * @param sink the sink to write the data to
 547  
      * @param text the text to write
 548  
      */
 549  
     private void writeTableHeaderCell(Sink sink, String text) {
 550  0
         sink.tableHeaderCell();
 551  0
         sink.text(text);
 552  0
         sink.tableHeaderCell_();
 553  0
     }
 554  
 
 555  
     /**
 556  
      * Writes the TOC for the site report.
 557  
      *
 558  
      * @param sink the sink to write the data to
 559  
      * @param dependencies the dependencies that are being reported on
 560  
      */
 561  
     private void writeSiteReportTOC(Sink sink, final List<Dependency> dependencies) {
 562  0
         sink.list();
 563  0
         for (Dependency d : dependencies) {
 564  0
             sink.listItem();
 565  0
             sink.link("#sha1" + d.getSha1sum());
 566  0
             sink.text(d.getFileName());
 567  0
             sink.link_();
 568  0
             if (!d.getVulnerabilities().isEmpty()) {
 569  0
                 sink.rawText(" <font style=\"color:red\">•</font>");
 570  
             }
 571  0
             if (!d.getRelatedDependencies().isEmpty()) {
 572  0
                 sink.list();
 573  0
                 for (Dependency r : d.getRelatedDependencies()) {
 574  0
                     writeListItem(sink, r.getFileName());
 575  
                 }
 576  0
                 sink.list_();
 577  
             }
 578  0
             sink.listItem_();
 579  
         }
 580  0
         sink.list_();
 581  0
     }
 582  
 
 583  
     /**
 584  
      * Writes the site report header.
 585  
      *
 586  
      * @param sink the sink to write the data to
 587  
      * @param projectName the name of the project
 588  
      */
 589  
     private void writeSiteReportHeader(Sink sink, String projectName) {
 590  0
         sink.head();
 591  0
         sink.title();
 592  0
         sink.text("Dependency-Check Report: " + projectName);
 593  0
         sink.title_();
 594  0
         sink.head_();
 595  0
         sink.body();
 596  0
         sink.rawText("<script type=\"text/javascript\">");
 597  0
         sink.rawText("function toggleElement(el, targetId) {");
 598  0
         sink.rawText("if (el.innerText == '[+]') {");
 599  0
         sink.rawText("    el.innerText = '[-]';");
 600  0
         sink.rawText("    document.getElementById(targetId).style.display='block';");
 601  0
         sink.rawText("} else {");
 602  0
         sink.rawText("    el.innerText = '[+]';");
 603  0
         sink.rawText("    document.getElementById(targetId).style.display='none';");
 604  0
         sink.rawText("}");
 605  
 
 606  0
         sink.rawText("}");
 607  0
         sink.rawText("</script>");
 608  0
         sink.section1();
 609  0
         sink.sectionTitle1();
 610  0
         sink.text("Project: " + projectName);
 611  0
         sink.sectionTitle1_();
 612  0
         sink.date();
 613  0
         final Date now = new Date();
 614  0
         sink.text(DateFormat.getDateTimeInstance().format(now));
 615  0
         sink.date_();
 616  0
         sink.section1_();
 617  0
     }
 618  
     // </editor-fold>
 619  
 
 620  
     /**
 621  
      * Takes the properties supplied and updates the dependency-check settings.
 622  
      * Additionally, this sets the system properties required to change the
 623  
      * proxy url, port, and connection timeout.
 624  
      */
 625  
     private void populateSettings() {
 626  0
         InputStream mojoProperties = null;
 627  
         try {
 628  0
             mojoProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 629  0
             Settings.mergeProperties(mojoProperties);
 630  0
         } catch (IOException ex) {
 631  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.WARNING, "Unable to load the dependency-check ant task.properties file.");
 632  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, null, ex);
 633  
         } finally {
 634  0
             if (mojoProperties != null) {
 635  
                 try {
 636  0
                     mojoProperties.close();
 637  0
                 } catch (IOException ex) {
 638  0
                     Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINEST, null, ex);
 639  0
                 }
 640  
             }
 641  
         }
 642  
 
 643  0
         Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
 644  
 
 645  0
         if (proxyUrl != null && !proxyUrl.isEmpty()) {
 646  0
             Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
 647  
         }
 648  0
         if (proxyPort != null && !proxyPort.isEmpty()) {
 649  0
             Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
 650  
         }
 651  0
         if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
 652  0
             Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
 653  
         }
 654  0
     }
 655  
 
 656  
     /**
 657  
      * Executes the dependency-check and generates the report.
 658  
      *
 659  
      * @throws MojoExecutionException if a maven exception occurs
 660  
      * @throws MojoFailureException thrown if a CVSS score is found that is
 661  
      * higher then the configured level
 662  
      */
 663  
     public void execute() throws MojoExecutionException, MojoFailureException {
 664  0
         final Engine engine = executeDependencyCheck();
 665  0
         generateExternalReports(engine);
 666  0
         if (this.failBuildOnCVSS <= 10) {
 667  0
             checkForFailure(engine.getDependencies());
 668  
         }
 669  0
     }
 670  
 
 671  
     /**
 672  
      * Generates the Dependency-Check Site Report.
 673  
      *
 674  
      * @param sink the sink to write the report to
 675  
      * @param locale the locale to use when generating the report
 676  
      * @throws MavenReportException if a Maven report exception occurs
 677  
      */
 678  
     public void generate(@SuppressWarnings("deprecation") org.codehaus.doxia.sink.Sink sink,
 679  
             Locale locale) throws MavenReportException {
 680  0
         generate((Sink) sink, null, locale);
 681  0
     }
 682  
 
 683  
     /**
 684  
      * Generates the Dependency-Check Site Report.
 685  
      *
 686  
      * @param sink the sink to write the report to
 687  
      * @param sinkFactory the sink factory
 688  
      * @param locale the locale to use when generating the report
 689  
      * @throws MavenReportException if a maven report exception occurs
 690  
      */
 691  
     public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
 692  0
         final Engine engine = executeDependencyCheck();
 693  0
         generateMavenSiteReport(engine, sink);
 694  0
     }
 695  
 
 696  
     // <editor-fold defaultstate="collapsed" desc="required setter/getter methods">
 697  
     /**
 698  
      * Returns the output name.
 699  
      *
 700  
      * @return the output name
 701  
      */
 702  
     public String getOutputName() {
 703  0
         return reportName;
 704  
     }
 705  
 
 706  
     /**
 707  
      * Returns the category name.
 708  
      *
 709  
      * @return the category name
 710  
      */
 711  
     public String getCategoryName() {
 712  0
         return MavenReport.CATEGORY_PROJECT_REPORTS;
 713  
     }
 714  
 
 715  
     /**
 716  
      * Returns the report name.
 717  
      *
 718  
      * @param locale the location
 719  
      * @return the report name
 720  
      */
 721  
     public String getName(Locale locale) {
 722  0
         return name;
 723  
     }
 724  
 
 725  
     /**
 726  
      * Sets the Reporting output directory.
 727  
      *
 728  
      * @param directory the output directory
 729  
      */
 730  
     public void setReportOutputDirectory(File directory) {
 731  0
         reportOutputDirectory = directory;
 732  0
     }
 733  
 
 734  
     /**
 735  
      * Returns the output directory.
 736  
      *
 737  
      * @return the output directory
 738  
      */
 739  
     public File getReportOutputDirectory() {
 740  0
         return reportOutputDirectory;
 741  
     }
 742  
 
 743  
     /**
 744  
      * Gets the description of the Dependency-Check report to be displayed in
 745  
      * the Maven Generated Reports page.
 746  
      *
 747  
      * @param locale The Locale to get the description for
 748  
      * @return the description
 749  
      */
 750  
     public String getDescription(Locale locale) {
 751  0
         return description;
 752  
     }
 753  
 
 754  
     /**
 755  
      * Returns whether this is an external report.
 756  
      *
 757  
      * @return true or false;
 758  
      */
 759  
     public boolean isExternalReport() {
 760  0
         return externalReport;
 761  
     }
 762  
 
 763  
     /**
 764  
      * Returns whether or not the plugin can generate a report.
 765  
      *
 766  
      * @return true
 767  
      */
 768  
     public boolean canGenerateReport() {
 769  0
         return true;
 770  
     }
 771  
     // </editor-fold>
 772  
 
 773  
     /**
 774  
      * Checks to see if a vulnerability has been identified with a CVSS score
 775  
      * that is above the threshold set in the configuration.
 776  
      *
 777  
      * @param dependencies the list of dependency objects
 778  
      * @throws MojoFailureException thrown if a CVSS score is found that is
 779  
      * higher then the threshold set
 780  
      */
 781  
     private void checkForFailure(List<Dependency> dependencies) throws MojoFailureException {
 782  0
         final StringBuilder ids = new StringBuilder();
 783  0
         for (Dependency d : dependencies) {
 784  0
             for (Vulnerability v : d.getVulnerabilities()) {
 785  0
                 if (v.getCvssScore() >= failBuildOnCVSS) {
 786  0
                     if (ids.length() == 0) {
 787  0
                         ids.append(v.getName());
 788  
                     } else {
 789  0
                         ids.append(", ").append(v.getName());
 790  
                     }
 791  
                 }
 792  
             }
 793  
         }
 794  0
         if (ids.length() > 0) {
 795  0
             final String msg = String.format("%n%nDependency-Check Failure:%n"
 796  
                     + "One or more dependencies were identified with vulnerabilities that have a CVSS score greater then '%.1f': %s%n"
 797  
                     + "See the dependency-check report for more details.%n%n", failBuildOnCVSS, ids.toString());
 798  0
             throw new MojoFailureException(msg);
 799  
         }
 800  0
     }
 801  
 }