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