Coverage Report - org.owasp.dependencycheck.agent.DependencyCheckScanAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyCheckScanAgent
0%
0/140
0%
0/114
1.906
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 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) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.agent;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.IOException;
 22  
 import java.util.List;
 23  
 import java.util.logging.Level;
 24  
 import java.util.logging.Logger;
 25  
 import org.owasp.dependencycheck.Engine;
 26  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 27  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 28  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 29  
 import org.owasp.dependencycheck.dependency.Dependency;
 30  
 import org.owasp.dependencycheck.dependency.Identifier;
 31  
 import org.owasp.dependencycheck.dependency.Vulnerability;
 32  
 import org.owasp.dependencycheck.exception.ScanAgentException;
 33  
 import org.owasp.dependencycheck.reporting.ReportGenerator;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 
 36  
 /**
 37  
  * This class provides a way to easily conduct a scan solely based on existing evidence metadata rather than collecting
 38  
  * evidence from the files themselves. This class is based on the Ant task and Maven plugin with the exception that it
 39  
  * takes a list of dependencies that can be programmatically added from data in a spreadsheet, database or some other
 40  
  * datasource and conduct a scan based on this pre-defined evidence.
 41  
  *
 42  
  * <h2>Example:</h2>
 43  
  * <pre>
 44  
  * List<Dependency> dependencies = new ArrayList<Dependency>();
 45  
  * Dependency dependency = new Dependency(new File(FileUtils.getBitBucket()));
 46  
  * dependency.getProductEvidence().addEvidence("my-datasource", "name", "Jetty", Confidence.HIGH);
 47  
  * dependency.getVersionEvidence().addEvidence("my-datasource", "version", "5.1.10", Confidence.HIGH);
 48  
  * dependency.getVendorEvidence().addEvidence("my-datasource", "vendor", "mortbay", Confidence.HIGH);
 49  
  * dependencies.add(dependency);
 50  
  *
 51  
  * DependencyCheckScanAgent scan = new DependencyCheckScanAgent();
 52  
  * scan.setDependencies(dependencies);
 53  
  * scan.setReportFormat(ReportGenerator.Format.ALL);
 54  
  * scan.setReportOutputDirectory(System.getProperty("user.home"));
 55  
  * scan.execute();
 56  
  * </pre>
 57  
  *
 58  
  * @author Steve Springett <steve.springett@owasp.org>
 59  
  */
 60  
 @SuppressWarnings("unused")
 61  0
 public class DependencyCheckScanAgent {
 62  
 
 63  
     /**
 64  
      * System specific new line character.
 65  
      */
 66  0
     private static final String NEW_LINE = System.getProperty("line.separator", "\n").intern();
 67  
     /**
 68  
      * Logger for use throughout the class.
 69  
      */
 70  0
     private static final Logger LOGGER = Logger.getLogger(DependencyCheckScanAgent.class.getName());
 71  
     /**
 72  
      * The application name for the report.
 73  
      */
 74  0
     private String applicationName = "Dependency-Check";
 75  
 
 76  
     /**
 77  
      * Get the value of applicationName.
 78  
      *
 79  
      * @return the value of applicationName
 80  
      */
 81  
     public String getApplicationName() {
 82  
         return applicationName;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Set the value of applicationName.
 87  
      *
 88  
      * @param applicationName new value of applicationName
 89  
      */
 90  
     public void setApplicationName(String applicationName) {
 91  
         this.applicationName = applicationName;
 92  
     }
 93  
 
 94  
     /**
 95  
      * The pre-determined dependencies to scan
 96  
      */
 97  
     private List<Dependency> dependencies;
 98  
 
 99  
     /**
 100  
      * Returns a list of pre-determined dependencies.
 101  
      *
 102  
      * @return returns a list of dependencies
 103  
      */
 104  
     public List<Dependency> getDependencies() {
 105  
         return dependencies;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Sets the list of dependencies to scan.
 110  
      *
 111  
      * @param dependencies new value of dependencies
 112  
      */
 113  
     public void setDependencies(List<Dependency> dependencies) {
 114  
         this.dependencies = dependencies;
 115  
     }
 116  
 
 117  
     /**
 118  
      * The location of the data directory that contains
 119  
      */
 120  0
     private String dataDirectory = null;
 121  
 
 122  
     /**
 123  
      * Get the value of dataDirectory.
 124  
      *
 125  
      * @return the value of dataDirectory
 126  
      */
 127  
     public String getDataDirectory() {
 128  
         return dataDirectory;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Set the value of dataDirectory.
 133  
      *
 134  
      * @param dataDirectory new value of dataDirectory
 135  
      */
 136  
     public void setDataDirectory(String dataDirectory) {
 137  
         this.dataDirectory = dataDirectory;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Specifies the destination directory for the generated Dependency-Check report.
 142  
      */
 143  
     private String reportOutputDirectory;
 144  
 
 145  
     /**
 146  
      * Get the value of reportOutputDirectory.
 147  
      *
 148  
      * @return the value of reportOutputDirectory
 149  
      */
 150  
     public String getReportOutputDirectory() {
 151  
         return reportOutputDirectory;
 152  
     }
 153  
 
 154  
     /**
 155  
      * Set the value of reportOutputDirectory.
 156  
      *
 157  
      * @param reportOutputDirectory new value of reportOutputDirectory
 158  
      */
 159  
     public void setReportOutputDirectory(String reportOutputDirectory) {
 160  
         this.reportOutputDirectory = reportOutputDirectory;
 161  
     }
 162  
 
 163  
     /**
 164  
      * Specifies if the build should be failed if a CVSS score above a specified level is identified. The default is 11
 165  
      * which means since the CVSS scores are 0-10, by default the build will never fail and the CVSS score is set to 11.
 166  
      * The valid range for the fail build on CVSS is 0 to 11, where anything above 10 will not cause the build to fail.
 167  
      */
 168  0
     private float failBuildOnCVSS = 11;
 169  
 
 170  
     /**
 171  
      * Get the value of failBuildOnCVSS.
 172  
      *
 173  
      * @return the value of failBuildOnCVSS
 174  
      */
 175  
     public float getFailBuildOnCVSS() {
 176  
         return failBuildOnCVSS;
 177  
     }
 178  
 
 179  
     /**
 180  
      * Set the value of failBuildOnCVSS.
 181  
      *
 182  
      * @param failBuildOnCVSS new value of failBuildOnCVSS
 183  
      */
 184  
     public void setFailBuildOnCVSS(float failBuildOnCVSS) {
 185  
         this.failBuildOnCVSS = failBuildOnCVSS;
 186  
     }
 187  
 
 188  
     /**
 189  
      * Sets whether auto-updating of the NVD CVE/CPE data is enabled. It is not recommended that this be turned to
 190  
      * false. Default is true.
 191  
      */
 192  0
     private boolean autoUpdate = true;
 193  
 
 194  
     /**
 195  
      * Get the value of autoUpdate.
 196  
      *
 197  
      * @return the value of autoUpdate
 198  
      */
 199  
     public boolean isAutoUpdate() {
 200  
         return autoUpdate;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Set the value of autoUpdate.
 205  
      *
 206  
      * @param autoUpdate new value of autoUpdate
 207  
      */
 208  
     public void setAutoUpdate(boolean autoUpdate) {
 209  
         this.autoUpdate = autoUpdate;
 210  
     }
 211  
 
 212  
     /**
 213  
      * The report format to be generated (HTML, XML, VULN, ALL). This configuration option has no affect if using this
 214  
      * within the Site plugin unless the externalReport is set to true. Default is HTML.
 215  
      */
 216  0
     private ReportGenerator.Format reportFormat = ReportGenerator.Format.HTML;
 217  
 
 218  
     /**
 219  
      * Get the value of reportFormat.
 220  
      *
 221  
      * @return the value of reportFormat
 222  
      */
 223  
     public ReportGenerator.Format getReportFormat() {
 224  
         return reportFormat;
 225  
     }
 226  
 
 227  
     /**
 228  
      * Set the value of reportFormat.
 229  
      *
 230  
      * @param reportFormat new value of reportFormat
 231  
      */
 232  
     public void setReportFormat(ReportGenerator.Format reportFormat) {
 233  
         this.reportFormat = reportFormat;
 234  
     }
 235  
 
 236  
     /**
 237  
      * The Proxy URL.
 238  
      */
 239  
     private String proxyUrl;
 240  
 
 241  
     /**
 242  
      * Get the value of proxyUrl.
 243  
      *
 244  
      * @return the value of proxyUrl
 245  
      */
 246  
     public String getProxyUrl() {
 247  
         return proxyUrl;
 248  
     }
 249  
 
 250  
     /**
 251  
      * Set the value of proxyUrl.
 252  
      *
 253  
      * @param proxyUrl new value of proxyUrl
 254  
      */
 255  
     public void setProxyUrl(String proxyUrl) {
 256  
         this.proxyUrl = proxyUrl;
 257  
     }
 258  
 
 259  
     /**
 260  
      * The Proxy Port.
 261  
      */
 262  
     private String proxyPort;
 263  
 
 264  
     /**
 265  
      * Get the value of proxyPort.
 266  
      *
 267  
      * @return the value of proxyPort
 268  
      */
 269  
     public String getProxyPort() {
 270  
         return proxyPort;
 271  
     }
 272  
 
 273  
     /**
 274  
      * Set the value of proxyPort.
 275  
      *
 276  
      * @param proxyPort new value of proxyPort
 277  
      */
 278  
     public void setProxyPort(String proxyPort) {
 279  
         this.proxyPort = proxyPort;
 280  
     }
 281  
 
 282  
     /**
 283  
      * The Proxy username.
 284  
      */
 285  
     private String proxyUsername;
 286  
 
 287  
     /**
 288  
      * Get the value of proxyUsername.
 289  
      *
 290  
      * @return the value of proxyUsername
 291  
      */
 292  
     public String getProxyUsername() {
 293  
         return proxyUsername;
 294  
     }
 295  
 
 296  
     /**
 297  
      * Set the value of proxyUsername.
 298  
      *
 299  
      * @param proxyUsername new value of proxyUsername
 300  
      */
 301  
     public void setProxyUsername(String proxyUsername) {
 302  
         this.proxyUsername = proxyUsername;
 303  
     }
 304  
 
 305  
     /**
 306  
      * The Proxy password.
 307  
      */
 308  
     private String proxyPassword;
 309  
 
 310  
     /**
 311  
      * Get the value of proxyPassword.
 312  
      *
 313  
      * @return the value of proxyPassword
 314  
      */
 315  
     public String getProxyPassword() {
 316  
         return proxyPassword;
 317  
     }
 318  
 
 319  
     /**
 320  
      * Set the value of proxyPassword.
 321  
      *
 322  
      * @param proxyPassword new value of proxyPassword
 323  
      */
 324  
     public void setProxyPassword(String proxyPassword) {
 325  
         this.proxyPassword = proxyPassword;
 326  
     }
 327  
 
 328  
     /**
 329  
      * The Connection Timeout.
 330  
      */
 331  
     private String connectionTimeout;
 332  
 
 333  
     /**
 334  
      * Get the value of connectionTimeout.
 335  
      *
 336  
      * @return the value of connectionTimeout
 337  
      */
 338  
     public String getConnectionTimeout() {
 339  
         return connectionTimeout;
 340  
     }
 341  
 
 342  
     /**
 343  
      * Set the value of connectionTimeout.
 344  
      *
 345  
      * @param connectionTimeout new value of connectionTimeout
 346  
      */
 347  
     public void setConnectionTimeout(String connectionTimeout) {
 348  
         this.connectionTimeout = connectionTimeout;
 349  
     }
 350  
 
 351  
     /**
 352  
      * The file path used for verbose logging.
 353  
      */
 354  0
     private String logFile = null;
 355  
 
 356  
     /**
 357  
      * Get the value of logFile.
 358  
      *
 359  
      * @return the value of logFile
 360  
      */
 361  
     public String getLogFile() {
 362  
         return logFile;
 363  
     }
 364  
 
 365  
     /**
 366  
      * Set the value of logFile.
 367  
      *
 368  
      * @param logFile new value of logFile
 369  
      */
 370  
     public void setLogFile(String logFile) {
 371  
         this.logFile = logFile;
 372  
     }
 373  
 
 374  
     /**
 375  
      * The path to the suppression file.
 376  
      */
 377  
     private String suppressionFile;
 378  
 
 379  
     /**
 380  
      * Get the value of suppressionFile.
 381  
      *
 382  
      * @return the value of suppressionFile
 383  
      */
 384  
     public String getSuppressionFile() {
 385  
         return suppressionFile;
 386  
     }
 387  
 
 388  
     /**
 389  
      * Set the value of suppressionFile.
 390  
      *
 391  
      * @param suppressionFile new value of suppressionFile
 392  
      */
 393  
     public void setSuppressionFile(String suppressionFile) {
 394  
         this.suppressionFile = suppressionFile;
 395  
     }
 396  
 
 397  
     /**
 398  
      * flag indicating whether or not to show a summary of findings.
 399  
      */
 400  0
     private boolean showSummary = true;
 401  
 
 402  
     /**
 403  
      * Get the value of showSummary.
 404  
      *
 405  
      * @return the value of showSummary
 406  
      */
 407  
     public boolean isShowSummary() {
 408  
         return showSummary;
 409  
     }
 410  
 
 411  
     /**
 412  
      * Set the value of showSummary.
 413  
      *
 414  
      * @param showSummary new value of showSummary
 415  
      */
 416  
     public void setShowSummary(boolean showSummary) {
 417  
         this.showSummary = showSummary;
 418  
     }
 419  
 
 420  
     /**
 421  
      * Whether or not the nexus analyzer is enabled.
 422  
      */
 423  0
     private boolean nexusAnalyzerEnabled = true;
 424  
 
 425  
     /**
 426  
      * Get the value of nexusAnalyzerEnabled.
 427  
      *
 428  
      * @return the value of nexusAnalyzerEnabled
 429  
      */
 430  
     public boolean isNexusAnalyzerEnabled() {
 431  
         return nexusAnalyzerEnabled;
 432  
     }
 433  
 
 434  
     /**
 435  
      * Set the value of nexusAnalyzerEnabled.
 436  
      *
 437  
      * @param nexusAnalyzerEnabled new value of nexusAnalyzerEnabled
 438  
      */
 439  
     public void setNexusAnalyzerEnabled(boolean nexusAnalyzerEnabled) {
 440  
         this.nexusAnalyzerEnabled = nexusAnalyzerEnabled;
 441  
     }
 442  
 
 443  
     /**
 444  
      * The URL of the Nexus server.
 445  
      */
 446  
     private String nexusUrl;
 447  
 
 448  
     /**
 449  
      * Get the value of nexusUrl.
 450  
      *
 451  
      * @return the value of nexusUrl
 452  
      */
 453  
     public String getNexusUrl() {
 454  
         return nexusUrl;
 455  
     }
 456  
 
 457  
     /**
 458  
      * Set the value of nexusUrl.
 459  
      *
 460  
      * @param nexusUrl new value of nexusUrl
 461  
      */
 462  
     public void setNexusUrl(String nexusUrl) {
 463  
         this.nexusUrl = nexusUrl;
 464  
     }
 465  
 
 466  
     /**
 467  
      * Whether or not the defined proxy should be used when connecting to Nexus.
 468  
      */
 469  0
     private boolean nexusUsesProxy = true;
 470  
 
 471  
     /**
 472  
      * Get the value of nexusUsesProxy.
 473  
      *
 474  
      * @return the value of nexusUsesProxy
 475  
      */
 476  
     public boolean isNexusUsesProxy() {
 477  
         return nexusUsesProxy;
 478  
     }
 479  
 
 480  
     /**
 481  
      * Set the value of nexusUsesProxy.
 482  
      *
 483  
      * @param nexusUsesProxy new value of nexusUsesProxy
 484  
      */
 485  
     public void setNexusUsesProxy(boolean nexusUsesProxy) {
 486  
         this.nexusUsesProxy = nexusUsesProxy;
 487  
     }
 488  
 
 489  
     /**
 490  
      * The database driver name; such as org.h2.Driver.
 491  
      */
 492  
     private String databaseDriverName;
 493  
 
 494  
     /**
 495  
      * Get the value of databaseDriverName.
 496  
      *
 497  
      * @return the value of databaseDriverName
 498  
      */
 499  
     public String getDatabaseDriverName() {
 500  
         return databaseDriverName;
 501  
     }
 502  
 
 503  
     /**
 504  
      * Set the value of databaseDriverName.
 505  
      *
 506  
      * @param databaseDriverName new value of databaseDriverName
 507  
      */
 508  
     public void setDatabaseDriverName(String databaseDriverName) {
 509  
         this.databaseDriverName = databaseDriverName;
 510  
     }
 511  
 
 512  
     /**
 513  
      * The path to the database driver JAR file if it is not on the class path.
 514  
      */
 515  
     private String databaseDriverPath;
 516  
 
 517  
     /**
 518  
      * Get the value of databaseDriverPath.
 519  
      *
 520  
      * @return the value of databaseDriverPath
 521  
      */
 522  
     public String getDatabaseDriverPath() {
 523  
         return databaseDriverPath;
 524  
     }
 525  
 
 526  
     /**
 527  
      * Set the value of databaseDriverPath.
 528  
      *
 529  
      * @param databaseDriverPath new value of databaseDriverPath
 530  
      */
 531  
     public void setDatabaseDriverPath(String databaseDriverPath) {
 532  
         this.databaseDriverPath = databaseDriverPath;
 533  
     }
 534  
 
 535  
     /**
 536  
      * The database connection string.
 537  
      */
 538  
     private String connectionString;
 539  
 
 540  
     /**
 541  
      * Get the value of connectionString.
 542  
      *
 543  
      * @return the value of connectionString
 544  
      */
 545  
     public String getConnectionString() {
 546  
         return connectionString;
 547  
     }
 548  
 
 549  
     /**
 550  
      * Set the value of connectionString.
 551  
      *
 552  
      * @param connectionString new value of connectionString
 553  
      */
 554  
     public void setConnectionString(String connectionString) {
 555  
         this.connectionString = connectionString;
 556  
     }
 557  
 
 558  
     /**
 559  
      * The user name for connecting to the database.
 560  
      */
 561  
     private String databaseUser;
 562  
 
 563  
     /**
 564  
      * Get the value of databaseUser.
 565  
      *
 566  
      * @return the value of databaseUser
 567  
      */
 568  
     public String getDatabaseUser() {
 569  
         return databaseUser;
 570  
     }
 571  
 
 572  
     /**
 573  
      * Set the value of databaseUser.
 574  
      *
 575  
      * @param databaseUser new value of databaseUser
 576  
      */
 577  
     public void setDatabaseUser(String databaseUser) {
 578  
         this.databaseUser = databaseUser;
 579  
     }
 580  
 
 581  
     /**
 582  
      * The password to use when connecting to the database.
 583  
      */
 584  
     private String databasePassword;
 585  
 
 586  
     /**
 587  
      * Get the value of databasePassword.
 588  
      *
 589  
      * @return the value of databasePassword
 590  
      */
 591  
     public String getDatabasePassword() {
 592  
         return databasePassword;
 593  
     }
 594  
 
 595  
     /**
 596  
      * Set the value of databasePassword.
 597  
      *
 598  
      * @param databasePassword new value of databasePassword
 599  
      */
 600  
     public void setDatabasePassword(String databasePassword) {
 601  
         this.databasePassword = databasePassword;
 602  
     }
 603  
 
 604  
     /**
 605  
      * Additional ZIP File extensions to add analyze. This should be a comma-separated list of file extensions to treat
 606  
      * like ZIP files.
 607  
      */
 608  
     private String zipExtensions;
 609  
 
 610  
     /**
 611  
      * Get the value of zipExtensions.
 612  
      *
 613  
      * @return the value of zipExtensions
 614  
      */
 615  
     public String getZipExtensions() {
 616  
         return zipExtensions;
 617  
     }
 618  
 
 619  
     /**
 620  
      * Set the value of zipExtensions.
 621  
      *
 622  
      * @param zipExtensions new value of zipExtensions
 623  
      */
 624  
     public void setZipExtensions(String zipExtensions) {
 625  
         this.zipExtensions = zipExtensions;
 626  
     }
 627  
 
 628  
     /**
 629  
      * The url for the modified NVD CVE (1.2 schema).
 630  
      */
 631  
     private String cveUrl12Modified;
 632  
 
 633  
     /**
 634  
      * Get the value of cveUrl12Modified.
 635  
      *
 636  
      * @return the value of cveUrl12Modified
 637  
      */
 638  
     public String getCveUrl12Modified() {
 639  
         return cveUrl12Modified;
 640  
     }
 641  
 
 642  
     /**
 643  
      * Set the value of cveUrl12Modified.
 644  
      *
 645  
      * @param cveUrl12Modified new value of cveUrl12Modified
 646  
      */
 647  
     public void setCveUrl12Modified(String cveUrl12Modified) {
 648  
         this.cveUrl12Modified = cveUrl12Modified;
 649  
     }
 650  
 
 651  
     /**
 652  
      * The url for the modified NVD CVE (2.0 schema).
 653  
      */
 654  
     private String cveUrl20Modified;
 655  
 
 656  
     /**
 657  
      * Get the value of cveUrl20Modified.
 658  
      *
 659  
      * @return the value of cveUrl20Modified
 660  
      */
 661  
     public String getCveUrl20Modified() {
 662  
         return cveUrl20Modified;
 663  
     }
 664  
 
 665  
     /**
 666  
      * Set the value of cveUrl20Modified.
 667  
      *
 668  
      * @param cveUrl20Modified new value of cveUrl20Modified
 669  
      */
 670  
     public void setCveUrl20Modified(String cveUrl20Modified) {
 671  
         this.cveUrl20Modified = cveUrl20Modified;
 672  
     }
 673  
 
 674  
     /**
 675  
      * Base Data Mirror URL for CVE 1.2.
 676  
      */
 677  
     private String cveUrl12Base;
 678  
 
 679  
     /**
 680  
      * Get the value of cveUrl12Base.
 681  
      *
 682  
      * @return the value of cveUrl12Base
 683  
      */
 684  
     public String getCveUrl12Base() {
 685  
         return cveUrl12Base;
 686  
     }
 687  
 
 688  
     /**
 689  
      * Set the value of cveUrl12Base.
 690  
      *
 691  
      * @param cveUrl12Base new value of cveUrl12Base
 692  
      */
 693  
     public void setCveUrl12Base(String cveUrl12Base) {
 694  
         this.cveUrl12Base = cveUrl12Base;
 695  
     }
 696  
 
 697  
     /**
 698  
      * Data Mirror URL for CVE 2.0.
 699  
      */
 700  
     private String cveUrl20Base;
 701  
 
 702  
     /**
 703  
      * Get the value of cveUrl20Base.
 704  
      *
 705  
      * @return the value of cveUrl20Base
 706  
      */
 707  
     public String getCveUrl20Base() {
 708  
         return cveUrl20Base;
 709  
     }
 710  
 
 711  
     /**
 712  
      * Set the value of cveUrl20Base.
 713  
      *
 714  
      * @param cveUrl20Base new value of cveUrl20Base
 715  
      */
 716  
     public void setCveUrl20Base(String cveUrl20Base) {
 717  
         this.cveUrl20Base = cveUrl20Base;
 718  
     }
 719  
 
 720  
     /**
 721  
      * The path to Mono for .NET assembly analysis on non-windows systems.
 722  
      */
 723  
     private String pathToMono;
 724  
 
 725  
     /**
 726  
      * Get the value of pathToMono.
 727  
      *
 728  
      * @return the value of pathToMono
 729  
      */
 730  
     public String getPathToMono() {
 731  
         return pathToMono;
 732  
     }
 733  
 
 734  
     /**
 735  
      * Set the value of pathToMono.
 736  
      *
 737  
      * @param pathToMono new value of pathToMono
 738  
      */
 739  
     public void setPathToMono(String pathToMono) {
 740  
         this.pathToMono = pathToMono;
 741  
     }
 742  
 
 743  
     /**
 744  
      * Executes the Dependency-Check on the dependent libraries.
 745  
      *
 746  
      * @return the Engine used to scan the dependencies.
 747  
      * @throws org.owasp.dependencycheck.data.nvdcve.DatabaseException thrown if there is an exception connecting to the
 748  
      * database
 749  
      */
 750  
     private Engine executeDependencyCheck() throws DatabaseException {
 751  0
         populateSettings();
 752  0
         Engine engine = null;
 753  0
         engine = new Engine();
 754  0
         engine.setDependencies(this.dependencies);
 755  0
         engine.analyzeDependencies();
 756  0
         return engine;
 757  
     }
 758  
 
 759  
     /**
 760  
      * Generates the reports for a given dependency-check engine.
 761  
      *
 762  
      * @param engine a dependency-check engine
 763  
      * @param outDirectory the directory to write the reports to
 764  
      */
 765  
     private void generateExternalReports(Engine engine, File outDirectory) {
 766  0
         DatabaseProperties prop = null;
 767  0
         CveDB cve = null;
 768  
         try {
 769  0
             cve = new CveDB();
 770  0
             cve.open();
 771  0
             prop = cve.getDatabaseProperties();
 772  0
         } catch (DatabaseException ex) {
 773  0
             LOGGER.log(Level.FINE, "Unable to retrieve DB Properties", ex);
 774  
         } finally {
 775  0
             if (cve != null) {
 776  0
                 cve.close();
 777  
             }
 778  
         }
 779  0
         final ReportGenerator r = new ReportGenerator(this.applicationName, engine.getDependencies(), engine.getAnalyzers(), prop);
 780  
         try {
 781  0
             r.generateReports(outDirectory.getCanonicalPath(), this.reportFormat.name());
 782  0
         } catch (IOException ex) {
 783  0
             LOGGER.log(Level.SEVERE,
 784  
                     "Unexpected exception occurred during analysis; please see the verbose error log for more details.");
 785  0
             LOGGER.log(Level.FINE, null, ex);
 786  0
         } catch (Throwable ex) {
 787  0
             LOGGER.log(Level.SEVERE,
 788  
                     "Unexpected exception occurred during analysis; please see the verbose error log for more details.");
 789  0
             LOGGER.log(Level.FINE, null, ex);
 790  0
         }
 791  0
     }
 792  
 
 793  
     /**
 794  
      * Takes the properties supplied and updates the dependency-check settings. Additionally, this sets the system
 795  
      * properties required to change the proxy url, port, and connection timeout.
 796  
      */
 797  
     private void populateSettings() {
 798  0
         Settings.initialize();
 799  0
         if (dataDirectory != null) {
 800  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
 801  
         } else {
 802  0
             final File jarPath = new File(DependencyCheckScanAgent.class.getProtectionDomain().getCodeSource().getLocation().getPath());
 803  0
             final File base = jarPath.getParentFile();
 804  0
             final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
 805  0
             final File dataDir = new File(base, sub);
 806  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
 807  
         }
 808  
 
 809  0
         Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
 810  
 
 811  0
         if (proxyUrl != null && !proxyUrl.isEmpty()) {
 812  0
             Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
 813  
         }
 814  0
         if (proxyPort != null && !proxyPort.isEmpty()) {
 815  0
             Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
 816  
         }
 817  0
         if (proxyUsername != null && !proxyUsername.isEmpty()) {
 818  0
             Settings.setString(Settings.KEYS.PROXY_USERNAME, proxyUsername);
 819  
         }
 820  0
         if (proxyPassword != null && !proxyPassword.isEmpty()) {
 821  0
             Settings.setString(Settings.KEYS.PROXY_PASSWORD, proxyPassword);
 822  
         }
 823  0
         if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
 824  0
             Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
 825  
         }
 826  0
         if (suppressionFile != null && !suppressionFile.isEmpty()) {
 827  0
             Settings.setString(Settings.KEYS.SUPPRESSION_FILE, suppressionFile);
 828  
         }
 829  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED, nexusAnalyzerEnabled);
 830  0
         if (nexusUrl != null && !nexusUrl.isEmpty()) {
 831  0
             Settings.setString(Settings.KEYS.ANALYZER_NEXUS_URL, nexusUrl);
 832  
         }
 833  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY, nexusUsesProxy);
 834  0
         if (databaseDriverName != null && !databaseDriverName.isEmpty()) {
 835  0
             Settings.setString(Settings.KEYS.DB_DRIVER_NAME, databaseDriverName);
 836  
         }
 837  0
         if (databaseDriverPath != null && !databaseDriverPath.isEmpty()) {
 838  0
             Settings.setString(Settings.KEYS.DB_DRIVER_PATH, databaseDriverPath);
 839  
         }
 840  0
         if (connectionString != null && !connectionString.isEmpty()) {
 841  0
             Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString);
 842  
         }
 843  0
         if (databaseUser != null && !databaseUser.isEmpty()) {
 844  0
             Settings.setString(Settings.KEYS.DB_USER, databaseUser);
 845  
         }
 846  0
         if (databasePassword != null && !databasePassword.isEmpty()) {
 847  0
             Settings.setString(Settings.KEYS.DB_PASSWORD, databasePassword);
 848  
         }
 849  0
         if (zipExtensions != null && !zipExtensions.isEmpty()) {
 850  0
             Settings.setString(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS, zipExtensions);
 851  
         }
 852  0
         if (cveUrl12Modified != null && !cveUrl12Modified.isEmpty()) {
 853  0
             Settings.setString(Settings.KEYS.CVE_MODIFIED_12_URL, cveUrl12Modified);
 854  
         }
 855  0
         if (cveUrl20Modified != null && !cveUrl20Modified.isEmpty()) {
 856  0
             Settings.setString(Settings.KEYS.CVE_MODIFIED_20_URL, cveUrl20Modified);
 857  
         }
 858  0
         if (cveUrl12Base != null && !cveUrl12Base.isEmpty()) {
 859  0
             Settings.setString(Settings.KEYS.CVE_SCHEMA_1_2, cveUrl12Base);
 860  
         }
 861  0
         if (cveUrl20Base != null && !cveUrl20Base.isEmpty()) {
 862  0
             Settings.setString(Settings.KEYS.CVE_SCHEMA_2_0, cveUrl20Base);
 863  
         }
 864  0
         if (pathToMono != null && !pathToMono.isEmpty()) {
 865  0
             Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, pathToMono);
 866  
         }
 867  0
     }
 868  
 
 869  
     /**
 870  
      * Executes the dependency-check and generates the report.
 871  
      *
 872  
      * @throws org.owasp.dependencycheck.exception.ScanAgentException thrown if there is an exception executing the
 873  
      * scan.
 874  
      */
 875  
     public void execute() throws ScanAgentException {
 876  0
         Engine engine = null;
 877  
         try {
 878  0
             engine = executeDependencyCheck();
 879  0
             generateExternalReports(engine, new File(this.reportOutputDirectory));
 880  0
             if (this.showSummary) {
 881  0
                 showSummary(engine.getDependencies());
 882  
             }
 883  0
             if (this.failBuildOnCVSS <= 10) {
 884  0
                 checkForFailure(engine.getDependencies());
 885  
             }
 886  0
         } catch (DatabaseException ex) {
 887  0
             LOGGER.log(Level.SEVERE,
 888  
                     "Unable to connect to the dependency-check database; analysis has stopped");
 889  0
             LOGGER.log(Level.FINE, "", ex);
 890  
         } finally {
 891  0
             Settings.cleanup();
 892  0
             if (engine != null) {
 893  0
                 engine.cleanup();
 894  
             }
 895  
         }
 896  0
     }
 897  
 
 898  
     /**
 899  
      * Checks to see if a vulnerability has been identified with a CVSS score that is above the threshold set in the
 900  
      * configuration.
 901  
      *
 902  
      * @param dependencies the list of dependency objects
 903  
      * @throws org.owasp.dependencycheck.exception.ScanAgentException thrown if there is an exception executing the
 904  
      * scan.
 905  
      */
 906  
     private void checkForFailure(List<Dependency> dependencies) throws ScanAgentException {
 907  0
         final StringBuilder ids = new StringBuilder();
 908  0
         for (Dependency d : dependencies) {
 909  0
             boolean addName = true;
 910  0
             for (Vulnerability v : d.getVulnerabilities()) {
 911  0
                 if (v.getCvssScore() >= failBuildOnCVSS) {
 912  0
                     if (addName) {
 913  0
                         addName = false;
 914  0
                         ids.append(NEW_LINE).append(d.getFileName()).append(": ");
 915  0
                         ids.append(v.getName());
 916  
                     } else {
 917  0
                         ids.append(", ").append(v.getName());
 918  
                     }
 919  
                 }
 920  0
             }
 921  0
         }
 922  0
         if (ids.length() > 0) {
 923  0
             final String msg = String.format("%n%nDependency-Check Failure:%n"
 924  
                     + "One or more dependencies were identified with vulnerabilities that have a CVSS score greater then '%.1f': %s%n"
 925  
                     + "See the dependency-check report for more details.%n%n", failBuildOnCVSS, ids.toString());
 926  
 
 927  0
             throw new ScanAgentException(msg);
 928  
         }
 929  0
     }
 930  
 
 931  
     /**
 932  
      * Generates a warning message listing a summary of dependencies and their associated CPE and CVE entries.
 933  
      *
 934  
      * @param dependencies a list of dependency objects
 935  
      */
 936  
     private void showSummary(List<Dependency> dependencies) {
 937  0
         final StringBuilder summary = new StringBuilder();
 938  0
         for (Dependency d : dependencies) {
 939  0
             boolean firstEntry = true;
 940  0
             final StringBuilder ids = new StringBuilder();
 941  0
             for (Vulnerability v : d.getVulnerabilities()) {
 942  0
                 if (firstEntry) {
 943  0
                     firstEntry = false;
 944  
                 } else {
 945  0
                     ids.append(", ");
 946  
                 }
 947  0
                 ids.append(v.getName());
 948  0
             }
 949  0
             if (ids.length() > 0) {
 950  0
                 summary.append(d.getFileName()).append(" (");
 951  0
                 firstEntry = true;
 952  0
                 for (Identifier id : d.getIdentifiers()) {
 953  0
                     if (firstEntry) {
 954  0
                         firstEntry = false;
 955  
                     } else {
 956  0
                         summary.append(", ");
 957  
                     }
 958  0
                     summary.append(id.getValue());
 959  0
                 }
 960  0
                 summary.append(") : ").append(ids).append(NEW_LINE);
 961  
             }
 962  0
         }
 963  0
         if (summary.length() > 0) {
 964  0
             final String msg = String.format("%n%n"
 965  
                     + "One or more dependencies were identified with known vulnerabilities:%n%n%s"
 966  
                     + "%n%nSee the dependency-check report for more details.%n%n", summary.toString());
 967  0
             LOGGER.log(Level.WARNING, msg);
 968  
         }
 969  0
     }
 970  
 
 971  
 }