| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public class App { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private static final String LOG_PROPERTIES_FILE = "log.properties"; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public static void main(String[] args) { |
| 70 | 0 | prepareLogger(); |
| 71 | 0 | final App app = new App(); |
| 72 | 0 | app.run(args); |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private static void prepareLogger() { |
| 79 | 0 | InputStream in = null; |
| 80 | |
try { |
| 81 | 0 | in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE); |
| 82 | 0 | LogManager.getLogManager().reset(); |
| 83 | 0 | LogManager.getLogManager().readConfiguration(in); |
| 84 | 0 | } catch (IOException ex) { |
| 85 | 0 | Logger.getLogger(App.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex); |
| 86 | 0 | } catch (SecurityException ex) { |
| 87 | 0 | Logger.getLogger(App.class.getName()).log(Level.FINE, "Error preparing the logger", ex); |
| 88 | |
} finally { |
| 89 | 0 | if (in != null) { |
| 90 | |
try { |
| 91 | 0 | in.close(); |
| 92 | 0 | } catch (Exception ex) { |
| 93 | 0 | Logger.getLogger(App.class.getName()).log(Level.FINEST, "Error closing resource stream", ex); |
| 94 | 0 | } |
| 95 | |
} |
| 96 | |
} |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void run(String[] args) { |
| 105 | |
|
| 106 | 0 | final CliParser cli = new CliParser(); |
| 107 | |
try { |
| 108 | 0 | cli.parse(args); |
| 109 | 0 | } catch (FileNotFoundException ex) { |
| 110 | 0 | System.err.println(ex.getMessage()); |
| 111 | 0 | cli.printHelp(); |
| 112 | 0 | return; |
| 113 | 0 | } catch (ParseException ex) { |
| 114 | 0 | System.err.println(ex.getMessage()); |
| 115 | 0 | cli.printHelp(); |
| 116 | 0 | return; |
| 117 | 0 | } |
| 118 | |
|
| 119 | 0 | if (cli.isGetVersion()) { |
| 120 | 0 | cli.printVersionInfo(); |
| 121 | 0 | } else if (cli.isRunScan()) { |
| 122 | 0 | updateSettings(cli.isAutoUpdate(), cli.getConnectionTimeout(), cli.getProxyUrl(), cli.getProxyPort(), cli.getDataDirectory()); |
| 123 | 0 | runScan(cli.getReportDirectory(), cli.getReportFormat(), cli.getApplicationName(), cli.getScanFiles()); |
| 124 | |
} else { |
| 125 | 0 | cli.printHelp(); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private void runScan(String reportDirectory, String outputFormat, String applicationName, String[] files) { |
| 140 | 0 | final Engine scanner = new Engine(); |
| 141 | |
|
| 142 | 0 | for (String file : files) { |
| 143 | 0 | scanner.scan(file); |
| 144 | |
} |
| 145 | |
|
| 146 | 0 | scanner.analyzeDependencies(); |
| 147 | 0 | final List<Dependency> dependencies = scanner.getDependencies(); |
| 148 | |
|
| 149 | 0 | final ReportGenerator report = new ReportGenerator(applicationName, dependencies, scanner.getAnalyzers()); |
| 150 | |
try { |
| 151 | 0 | report.generateReports(reportDirectory, outputFormat); |
| 152 | 0 | } catch (IOException ex) { |
| 153 | 0 | Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an IO error while attempting to generate the report."); |
| 154 | 0 | Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex); |
| 155 | 0 | } catch (Exception ex) { |
| 156 | 0 | Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an error while attempting to generate the report."); |
| 157 | 0 | Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex); |
| 158 | 0 | } |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
private void updateSettings(boolean autoUpdate, String connectionTimeout, String proxyUrl, String proxyPort, String dataDirectory) { |
| 173 | 0 | if (dataDirectory != null) { |
| 174 | 0 | Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory); |
| 175 | 0 | } else if (System.getProperty("basedir") != null) { |
| 176 | 0 | final File dataDir = new File(System.getProperty("basedir"), "data"); |
| 177 | 0 | Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath()); |
| 178 | 0 | } else { |
| 179 | 0 | final File jarPath = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath()); |
| 180 | 0 | final File base = jarPath.getParentFile(); |
| 181 | 0 | final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY); |
| 182 | 0 | final File dataDir = new File(base, sub); |
| 183 | 0 | Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath()); |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | 0 | Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate); |
| 188 | 0 | if (proxyUrl != null && !proxyUrl.isEmpty()) { |
| 189 | 0 | Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl); |
| 190 | |
} |
| 191 | 0 | if (proxyPort != null && !proxyPort.isEmpty()) { |
| 192 | 0 | Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort); |
| 193 | |
} |
| 194 | 0 | if (connectionTimeout != null && !connectionTimeout.isEmpty()) { |
| 195 | 0 | Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout); |
| 196 | |
} |
| 197 | 0 | } |
| 198 | |
} |