Coverage Report - org.owasp.dependencycheck.App
 
Classes in this File Line Coverage Branch Coverage Complexity
App
0%
0/138
0%
0/90
12.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-cli.
 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) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.util.List;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 import org.apache.commons.cli.ParseException;
 28  
 import org.owasp.dependencycheck.cli.CliParser;
 29  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 30  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 31  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 32  
 import org.owasp.dependencycheck.dependency.Dependency;
 33  
 import org.owasp.dependencycheck.reporting.ReportGenerator;
 34  
 import org.owasp.dependencycheck.utils.LogUtils;
 35  
 import org.owasp.dependencycheck.utils.Settings;
 36  
 
 37  
 /**
 38  
  * The command line interface for the DependencyCheck application.
 39  
  *
 40  
  * @author Jeremy Long <jeremy.long@owasp.org>
 41  
  */
 42  
 public class App {
 43  
 
 44  
     /**
 45  
      * The location of the log properties configuration file.
 46  
      */
 47  
     private static final String LOG_PROPERTIES_FILE = "log.properties";
 48  
 
 49  
     /**
 50  
      * The logger.
 51  
      */
 52  0
     private static final Logger LOGGER = Logger.getLogger(App.class.getName());
 53  
 
 54  
     /**
 55  
      * The main method for the application.
 56  
      *
 57  
      * @param args the command line arguments
 58  
      */
 59  
     public static void main(String[] args) {
 60  
         try {
 61  0
             Settings.initialize();
 62  0
             final App app = new App();
 63  0
             app.run(args);
 64  
         } finally {
 65  0
             Settings.cleanup(true);
 66  0
         }
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Main CLI entry-point into the application.
 71  
      *
 72  
      * @param args the command line arguments
 73  
      */
 74  
     public void run(String[] args) {
 75  0
         final CliParser cli = new CliParser();
 76  
 
 77  
         try {
 78  0
             cli.parse(args);
 79  0
         } catch (FileNotFoundException ex) {
 80  0
             System.err.println(ex.getMessage());
 81  0
             cli.printHelp();
 82  0
             return;
 83  0
         } catch (ParseException ex) {
 84  0
             System.err.println(ex.getMessage());
 85  0
             cli.printHelp();
 86  0
             return;
 87  0
         }
 88  
 
 89  0
         final InputStream in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
 90  0
         LogUtils.prepareLogger(in, cli.getVerboseLog());
 91  
 
 92  0
         if (cli.isGetVersion()) {
 93  0
             cli.printVersionInfo();
 94  0
         } else if (cli.isRunScan()) {
 95  0
             populateSettings(cli);
 96  0
             runScan(cli.getReportDirectory(), cli.getReportFormat(), cli.getApplicationName(), cli.getScanFiles());
 97  
         } else {
 98  0
             cli.printHelp();
 99  
         }
 100  0
     }
 101  
 
 102  
     /**
 103  
      * Scans the specified directories and writes the dependency reports to the reportDirectory.
 104  
      *
 105  
      * @param reportDirectory the path to the directory where the reports will be written
 106  
      * @param outputFormat the output format of the report
 107  
      * @param applicationName the application name for the report
 108  
      * @param files the files/directories to scan
 109  
      */
 110  
     private void runScan(String reportDirectory, String outputFormat, String applicationName, String[] files) {
 111  0
         Engine scanner = null;
 112  
         try {
 113  0
             scanner = new Engine();
 114  
 
 115  0
             for (String file : files) {
 116  0
                 scanner.scan(file);
 117  
             }
 118  
 
 119  0
             scanner.analyzeDependencies();
 120  0
             final List<Dependency> dependencies = scanner.getDependencies();
 121  0
             DatabaseProperties prop = null;
 122  0
             CveDB cve = null;
 123  
             try {
 124  0
                 cve = new CveDB();
 125  0
                 cve.open();
 126  0
                 prop = cve.getDatabaseProperties();
 127  0
             } catch (DatabaseException ex) {
 128  0
                 LOGGER.log(Level.FINE, "Unable to retrieve DB Properties", ex);
 129  
             } finally {
 130  0
                 if (cve != null) {
 131  0
                     cve.close();
 132  
                 }
 133  
             }
 134  0
             final ReportGenerator report = new ReportGenerator(applicationName, dependencies, scanner.getAnalyzers(), prop);
 135  
             try {
 136  0
                 report.generateReports(reportDirectory, outputFormat);
 137  0
             } catch (IOException ex) {
 138  0
                 LOGGER.log(Level.SEVERE, "There was an IO error while attempting to generate the report.");
 139  0
                 LOGGER.log(Level.FINE, null, ex);
 140  0
             } catch (Throwable ex) {
 141  0
                 LOGGER.log(Level.SEVERE, "There was an error while attempting to generate the report.");
 142  0
                 LOGGER.log(Level.FINE, null, ex);
 143  0
             }
 144  0
         } catch (DatabaseException ex) {
 145  0
             LOGGER.log(Level.SEVERE, "Unable to connect to the dependency-check database; analysis has stopped");
 146  0
             LOGGER.log(Level.FINE, "", ex);
 147  
         } finally {
 148  0
             if (scanner != null) {
 149  0
                 scanner.cleanup();
 150  
             }
 151  
         }
 152  0
     }
 153  
 
 154  
     /**
 155  
      * Updates the global Settings.
 156  
      *
 157  
      * @param cli a reference to the CLI Parser that contains the command line arguments used to set the corresponding
 158  
      * settings in the core engine.
 159  
      */
 160  
     private void populateSettings(CliParser cli) {
 161  
 
 162  0
         final boolean autoUpdate = cli.isAutoUpdate();
 163  0
         final String connectionTimeout = cli.getConnectionTimeout();
 164  0
         final String proxyServer = cli.getProxyServer();
 165  0
         final String proxyPort = cli.getProxyPort();
 166  0
         final String proxyUser = cli.getProxyUsername();
 167  0
         final String proxyPass = cli.getProxyPassword();
 168  0
         final String dataDirectory = cli.getDataDirectory();
 169  0
         final File propertiesFile = cli.getPropertiesFile();
 170  0
         final String suppressionFile = cli.getSuppressionFile();
 171  0
         final boolean jarDisabled = cli.isJarDisabled();
 172  0
         final boolean archiveDisabled = cli.isArchiveDisabled();
 173  0
         final boolean assemblyDisabled = cli.isAssemblyDisabled();
 174  0
         final boolean nuspecDisabled = cli.isNuspecDisabled();
 175  0
         final boolean nexusDisabled = cli.isNexusDisabled();
 176  0
         final String nexusUrl = cli.getNexusUrl();
 177  0
         final String databaseDriverName = cli.getDatabaseDriverName();
 178  0
         final String databaseDriverPath = cli.getDatabaseDriverPath();
 179  0
         final String connectionString = cli.getConnectionString();
 180  0
         final String databaseUser = cli.getDatabaseUser();
 181  0
         final String databasePassword = cli.getDatabasePassword();
 182  0
         final String additionalZipExtensions = cli.getAdditionalZipExtensions();
 183  0
         final String pathToMono = cli.getPathToMono();
 184  
 
 185  0
         if (propertiesFile != null) {
 186  
             try {
 187  0
                 Settings.mergeProperties(propertiesFile);
 188  0
             } catch (FileNotFoundException ex) {
 189  0
                 final String msg = String.format("Unable to load properties file '%s'", propertiesFile.getPath());
 190  0
                 LOGGER.log(Level.SEVERE, msg);
 191  0
                 LOGGER.log(Level.FINE, null, ex);
 192  0
             } catch (IOException ex) {
 193  0
                 final String msg = String.format("Unable to find properties file '%s'", propertiesFile.getPath());
 194  0
                 LOGGER.log(Level.SEVERE, msg);
 195  0
                 LOGGER.log(Level.FINE, null, ex);
 196  0
             }
 197  
         }
 198  
         // We have to wait until we've merged the properties before attempting to set whether we use
 199  
         // the proxy for Nexus since it could be disabled in the properties, but not explicitly stated
 200  
         // on the command line
 201  0
         final boolean nexusUsesProxy = cli.isNexusUsesProxy();
 202  0
         if (dataDirectory != null) {
 203  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
 204  0
         } else if (System.getProperty("basedir") != null) {
 205  0
             final File dataDir = new File(System.getProperty("basedir"), "data");
 206  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
 207  0
         } else {
 208  0
             final File jarPath = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath());
 209  0
             final File base = jarPath.getParentFile();
 210  0
             final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
 211  0
             final File dataDir = new File(base, sub);
 212  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
 213  
         }
 214  0
         Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
 215  0
         if (proxyServer != null && !proxyServer.isEmpty()) {
 216  0
             Settings.setString(Settings.KEYS.PROXY_SERVER, proxyServer);
 217  
         }
 218  0
         if (proxyPort != null && !proxyPort.isEmpty()) {
 219  0
             Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
 220  
         }
 221  0
         if (proxyUser != null && !proxyUser.isEmpty()) {
 222  0
             Settings.setString(Settings.KEYS.PROXY_USERNAME, proxyUser);
 223  
         }
 224  0
         if (proxyPass != null && !proxyPass.isEmpty()) {
 225  0
             Settings.setString(Settings.KEYS.PROXY_PASSWORD, proxyPass);
 226  
         }
 227  0
         if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
 228  0
             Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
 229  
         }
 230  0
         if (suppressionFile != null && !suppressionFile.isEmpty()) {
 231  0
             Settings.setString(Settings.KEYS.SUPPRESSION_FILE, suppressionFile);
 232  
         }
 233  
 
 234  
         //File Type Analyzer Settings
 235  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_JAR_ENABLED, !jarDisabled);
 236  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_ARCHIVE_ENABLED, !archiveDisabled);
 237  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_NUSPEC_ENABLED, !nuspecDisabled);
 238  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_ASSEMBLY_ENABLED, !assemblyDisabled);
 239  
 
 240  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED, !nexusDisabled);
 241  0
         if (nexusUrl != null && !nexusUrl.isEmpty()) {
 242  0
             Settings.setString(Settings.KEYS.ANALYZER_NEXUS_URL, nexusUrl);
 243  
         }
 244  0
         Settings.setBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY, nexusUsesProxy);
 245  0
         if (databaseDriverName != null && !databaseDriverName.isEmpty()) {
 246  0
             Settings.setString(Settings.KEYS.DB_DRIVER_NAME, databaseDriverName);
 247  
         }
 248  0
         if (databaseDriverPath != null && !databaseDriverPath.isEmpty()) {
 249  0
             Settings.setString(Settings.KEYS.DB_DRIVER_PATH, databaseDriverPath);
 250  
         }
 251  0
         if (connectionString != null && !connectionString.isEmpty()) {
 252  0
             Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString);
 253  
         }
 254  0
         if (databaseUser != null && !databaseUser.isEmpty()) {
 255  0
             Settings.setString(Settings.KEYS.DB_USER, databaseUser);
 256  
         }
 257  0
         if (databasePassword != null && !databasePassword.isEmpty()) {
 258  0
             Settings.setString(Settings.KEYS.DB_PASSWORD, databasePassword);
 259  
         }
 260  0
         if (additionalZipExtensions != null && !additionalZipExtensions.isEmpty()) {
 261  0
             Settings.setString(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS, additionalZipExtensions);
 262  
         }
 263  0
         if (pathToMono != null && !pathToMono.isEmpty()) {
 264  0
             Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, pathToMono);
 265  
         }
 266  0
     }
 267  
 }