View Javadoc

1   /*
2    * This file is part of dependency-check-cli.
3    *
4    * Dependency-check-cli 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-cli 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-cli. If not, see http://www.gnu.org/licenses/.
16   *
17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
18   */
19  package org.owasp.dependencycheck;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.List;
26  import java.util.logging.Level;
27  import java.util.logging.LogManager;
28  import java.util.logging.Logger;
29  import org.apache.commons.cli.ParseException;
30  import org.owasp.dependencycheck.reporting.ReportGenerator;
31  import org.owasp.dependencycheck.dependency.Dependency;
32  import org.owasp.dependencycheck.cli.CliParser;
33  import org.owasp.dependencycheck.utils.Settings;
34  
35  /*
36   * This file is part of App.
37   *
38   * App is free software: you can redistribute it and/or modify it under the
39   * terms of the GNU General Public License as published by the Free Software
40   * Foundation, either version 3 of the License, or (at your option) any later
41   * version.
42   *
43   * App is distributed in the hope that it will be useful, but WITHOUT ANY
44   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
45   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
46   *
47   * You should have received a copy of the GNU General Public License along with
48   * App. If not, see http://www.gnu.org/licenses/.
49   *
50   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
51   */
52  /**
53   * The command line interface for the DependencyCheck application.
54   *
55   * @author Jeremy Long (jeremy.long@owasp.org)
56   */
57  public class App {
58  
59      /**
60       * The location of the log properties configuration file.
61       */
62      private static final String LOG_PROPERTIES_FILE = "log.properties";
63  
64      /**
65       * The main method for the application.
66       *
67       * @param args the command line arguments
68       */
69      public static void main(String[] args) {
70          prepareLogger();
71          final App app = new App();
72          app.run(args);
73      }
74  
75      /**
76       * Configures the logger for use by the application.
77       */
78      private static void prepareLogger() {
79          InputStream in = null;
80          try {
81              in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
82              LogManager.getLogManager().reset();
83              LogManager.getLogManager().readConfiguration(in);
84          } catch (IOException ex) {
85              Logger.getLogger(App.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex);
86          } catch (SecurityException ex) {
87              Logger.getLogger(App.class.getName()).log(Level.FINE, "Error preparing the logger", ex);
88          } finally {
89              if (in != null) {
90                  try {
91                      in.close();
92                  } catch (Exception ex) {
93                      Logger.getLogger(App.class.getName()).log(Level.FINEST, "Error closing resource stream", ex);
94                  }
95              }
96          }
97      }
98  
99      /**
100      * Main CLI entry-point into the application.
101      *
102      * @param args the command line arguments
103      */
104     public void run(String[] args) {
105 
106         final CliParser cli = new CliParser();
107         try {
108             cli.parse(args);
109         } catch (FileNotFoundException ex) {
110             System.err.println(ex.getMessage());
111             cli.printHelp();
112             return;
113         } catch (ParseException ex) {
114             System.err.println(ex.getMessage());
115             cli.printHelp();
116             return;
117         }
118 
119         if (cli.isGetVersion()) {
120             cli.printVersionInfo();
121         } else if (cli.isRunScan()) {
122             updateSettings(cli.isAutoUpdate(), cli.getConnectionTimeout(), cli.getProxyUrl(), cli.getProxyPort(), cli.getDataDirectory());
123             runScan(cli.getReportDirectory(), cli.getReportFormat(), cli.getApplicationName(), cli.getScanFiles());
124         } else {
125             cli.printHelp();
126         }
127     }
128 
129     /**
130      * Scans the specified directories and writes the dependency reports to the
131      * reportDirectory.
132      *
133      * @param reportDirectory the path to the directory where the reports will
134      * be written
135      * @param outputFormat the output format of the report
136      * @param applicationName the application name for the report
137      * @param files the files/directories to scan
138      */
139     private void runScan(String reportDirectory, String outputFormat, String applicationName, String[] files) {
140         final Engine scanner = new Engine();
141 
142         for (String file : files) {
143             scanner.scan(file);
144         }
145 
146         scanner.analyzeDependencies();
147         final List<Dependency> dependencies = scanner.getDependencies();
148 
149         final ReportGenerator report = new ReportGenerator(applicationName, dependencies, scanner.getAnalyzers());
150         try {
151             report.generateReports(reportDirectory, outputFormat);
152         } catch (IOException ex) {
153             Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an IO error while attempting to generate the report.");
154             Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
155         } catch (Exception ex) {
156             Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an error while attempting to generate the report.");
157             Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
158         }
159     }
160 
161     /**
162      * Updates the global Settings.
163      *
164      * @param autoUpdate whether or not to update cached web data sources
165      * @param connectionTimeout the timeout to use when downloading resources
166      * (null or blank will use default)
167      * @param proxyUrl the proxy url (null or blank means no proxy will be used)
168      * @param proxyPort the proxy port (null or blank means no port will be
169      * used)
170      * @param dataDirectory the directory to store/retrieve persistent data from
171      */
172     private void updateSettings(boolean autoUpdate, String connectionTimeout, String proxyUrl, String proxyPort, String dataDirectory) {
173         if (dataDirectory != null) {
174             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
175         } else if (System.getProperty("basedir") != null) {
176             final File dataDir = new File(System.getProperty("basedir"), "data");
177             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
178         } else {
179             final File jarPath = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath());
180             final File base = jarPath.getParentFile();
181             final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
182             final File dataDir = new File(base, sub);
183             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
184         }
185 
186 
187         Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
188         if (proxyUrl != null && !proxyUrl.isEmpty()) {
189             Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
190         }
191         if (proxyPort != null && !proxyPort.isEmpty()) {
192             Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
193         }
194         if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
195             Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
196         }
197     }
198 }