| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.data.update; |
| 20 | |
|
| 21 | |
import org.owasp.dependencycheck.data.nvdcve.InvalidDataException; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileNotFoundException; |
| 24 | |
import java.io.IOException; |
| 25 | |
import javax.xml.parsers.ParserConfigurationException; |
| 26 | |
import org.xml.sax.SAXException; |
| 27 | |
import java.net.MalformedURLException; |
| 28 | |
import java.sql.SQLException; |
| 29 | |
import java.util.Calendar; |
| 30 | |
import java.util.Date; |
| 31 | |
import java.util.HashSet; |
| 32 | |
import java.util.Set; |
| 33 | |
import java.util.concurrent.ExecutionException; |
| 34 | |
import java.util.concurrent.ExecutorService; |
| 35 | |
import java.util.concurrent.Executors; |
| 36 | |
import java.util.concurrent.Future; |
| 37 | |
import java.util.logging.Level; |
| 38 | |
import java.util.logging.Logger; |
| 39 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 40 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 41 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 42 | |
import org.owasp.dependencycheck.utils.Settings; |
| 43 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 44 | |
import org.owasp.dependencycheck.utils.InvalidSettingException; |
| 45 | |
import static org.owasp.dependencycheck.data.update.DataStoreMetaInfo.MODIFIED; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public class StandardUpdateTask extends AbstractUpdateTask { |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public static final int MAX_THREAD_POOL_SIZE = Settings.getInt(Settings.KEYS.MAX_DOWNLOAD_THREAD_POOL_SIZE, 3); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public StandardUpdateTask(DataStoreMetaInfo properties) throws MalformedURLException, DownloadFailedException, UpdateException { |
| 70 | 0 | super(properties); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
@Override |
| 81 | |
public void update() throws UpdateException { |
| 82 | 0 | int maxUpdates = 0; |
| 83 | |
try { |
| 84 | 0 | for (NvdCveInfo cve : getUpdateable()) { |
| 85 | 0 | if (cve.getNeedsUpdate()) { |
| 86 | 0 | maxUpdates += 1; |
| 87 | |
} |
| 88 | |
} |
| 89 | 0 | if (maxUpdates <= 0) { |
| 90 | |
return; |
| 91 | |
} |
| 92 | 0 | if (maxUpdates > 3) { |
| 93 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 94 | |
"NVD CVE requires several updates; this could take a couple of minutes."); |
| 95 | |
} |
| 96 | 0 | if (maxUpdates > 0) { |
| 97 | 0 | openDataStores(); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | final int poolSize = (MAX_THREAD_POOL_SIZE > maxUpdates) ? MAX_THREAD_POOL_SIZE : maxUpdates; |
| 101 | 0 | final ExecutorService executorService = Executors.newFixedThreadPool(poolSize); |
| 102 | 0 | final Set<Future<CallableDownloadTask>> futures = new HashSet<Future<CallableDownloadTask>>(maxUpdates); |
| 103 | |
|
| 104 | 0 | for (NvdCveInfo cve : getUpdateable()) { |
| 105 | 0 | if (cve.getNeedsUpdate()) { |
| 106 | |
final File file1; |
| 107 | |
final File file2; |
| 108 | |
try { |
| 109 | 0 | file1 = File.createTempFile("cve" + cve.getId() + "_", ".xml"); |
| 110 | 0 | file2 = File.createTempFile("cve_1_2_" + cve.getId() + "_", ".xml"); |
| 111 | 0 | } catch (IOException ex) { |
| 112 | 0 | throw new UpdateException(ex); |
| 113 | 0 | } |
| 114 | 0 | final CallableDownloadTask call = new CallableDownloadTask(cve, file1, file2); |
| 115 | 0 | futures.add(executorService.submit(call)); |
| 116 | 0 | } |
| 117 | |
} |
| 118 | |
|
| 119 | |
try { |
| 120 | 0 | for (Future<CallableDownloadTask> future : futures) { |
| 121 | 0 | final CallableDownloadTask filePair = future.get(); |
| 122 | 0 | String msg = String.format("Processing Started for NVD CVE - %s", filePair.getNvdCveInfo().getId()); |
| 123 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, msg); |
| 124 | |
try { |
| 125 | 0 | importXML(filePair.getFirst(), filePair.getSecond()); |
| 126 | 0 | getCveDB().commit(); |
| 127 | 0 | getProperties().save(filePair.getNvdCveInfo()); |
| 128 | 0 | } catch (FileNotFoundException ex) { |
| 129 | 0 | throw new UpdateException(ex); |
| 130 | 0 | } catch (ParserConfigurationException ex) { |
| 131 | 0 | throw new UpdateException(ex); |
| 132 | 0 | } catch (SAXException ex) { |
| 133 | 0 | throw new UpdateException(ex); |
| 134 | 0 | } catch (IOException ex) { |
| 135 | 0 | throw new UpdateException(ex); |
| 136 | 0 | } catch (SQLException ex) { |
| 137 | 0 | throw new UpdateException(ex); |
| 138 | 0 | } catch (DatabaseException ex) { |
| 139 | 0 | throw new UpdateException(ex); |
| 140 | 0 | } catch (ClassNotFoundException ex) { |
| 141 | 0 | throw new UpdateException(ex); |
| 142 | |
} finally { |
| 143 | 0 | filePair.cleanup(); |
| 144 | 0 | } |
| 145 | 0 | msg = String.format("Processing Complete for NVD CVE - %s", filePair.getNvdCveInfo().getId()); |
| 146 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, msg); |
| 147 | 0 | } |
| 148 | 0 | } catch (InterruptedException ex) { |
| 149 | 0 | executorService.shutdownNow(); |
| 150 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.FINE, "Thread was interupted", ex); |
| 151 | 0 | throw new UpdateException(ex); |
| 152 | 0 | } catch (ExecutionException ex) { |
| 153 | 0 | executorService.shutdownNow(); |
| 154 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.SEVERE, null, ex); |
| 155 | 0 | throw new UpdateException(ex); |
| 156 | |
} finally { |
| 157 | |
|
| 158 | 0 | executorService.shutdown(); |
| 159 | 0 | } |
| 160 | |
|
| 161 | 0 | if (maxUpdates >= 1) { |
| 162 | 0 | getProperties().save(getUpdateable().get(MODIFIED)); |
| 163 | 0 | getCveDB().cleanupDatabase(); |
| 164 | |
} |
| 165 | |
} finally { |
| 166 | 0 | closeDataStores(); |
| 167 | 0 | } |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
@Override |
| 283 | |
protected Updateable updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException { |
| 284 | 0 | Updateable updates = null; |
| 285 | |
try { |
| 286 | 0 | updates = retrieveCurrentTimestampsFromWeb(); |
| 287 | 0 | } catch (InvalidDataException ex) { |
| 288 | 0 | final String msg = "Unable to retrieve valid timestamp from nvd cve downloads page"; |
| 289 | 0 | Logger |
| 290 | |
.getLogger(StandardUpdateTask.class |
| 291 | |
.getName()).log(Level.FINE, msg, ex); |
| 292 | 0 | throw new DownloadFailedException(msg, ex); |
| 293 | 0 | } catch (InvalidSettingException ex) { |
| 294 | 0 | Logger.getLogger(StandardUpdateTask.class |
| 295 | |
.getName()).log(Level.FINE, "Invalid setting found when retrieving timestamps", ex); |
| 296 | 0 | throw new DownloadFailedException( |
| 297 | |
"Invalid settings", ex); |
| 298 | 0 | } |
| 299 | |
|
| 300 | 0 | if (updates == null) { |
| 301 | 0 | throw new DownloadFailedException("Unable to retrieve the timestamps of the currently published NVD CVE data"); |
| 302 | |
} |
| 303 | 0 | final DataStoreMetaInfo properties = getProperties(); |
| 304 | 0 | if (!properties.isEmpty()) { |
| 305 | |
try { |
| 306 | |
float version; |
| 307 | |
|
| 308 | 0 | if (properties.getProperty("version") == null) { |
| 309 | 0 | setDeleteAndRecreate(true); |
| 310 | |
} else { |
| 311 | |
try { |
| 312 | 0 | version = Float.parseFloat(properties.getProperty("version")); |
| 313 | 0 | final float currentVersion = Float.parseFloat(CveDB.DB_SCHEMA_VERSION); |
| 314 | 0 | if (currentVersion > version) { |
| 315 | 0 | setDeleteAndRecreate(true); |
| 316 | |
} |
| 317 | 0 | } catch (NumberFormatException ex) { |
| 318 | 0 | setDeleteAndRecreate(true); |
| 319 | 0 | } |
| 320 | |
} |
| 321 | |
|
| 322 | 0 | if (shouldDeleteAndRecreate()) { |
| 323 | 0 | return updates; |
| 324 | |
} |
| 325 | |
|
| 326 | 0 | final long lastUpdated = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED, "0")); |
| 327 | 0 | final Date now = new Date(); |
| 328 | 0 | final int days = Settings.getInt(Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS, 7); |
| 329 | 0 | if (lastUpdated == updates.getTimeStamp(MODIFIED)) { |
| 330 | 0 | updates.clear(); |
| 331 | 0 | } else if (withinRange(lastUpdated, now.getTime(), days)) { |
| 332 | 0 | for (NvdCveInfo entry : updates) { |
| 333 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 334 | 0 | entry.setNeedsUpdate(true); |
| 335 | |
} else { |
| 336 | 0 | entry.setNeedsUpdate(false); |
| 337 | |
} |
| 338 | |
} |
| 339 | |
} else { |
| 340 | 0 | for (NvdCveInfo entry : updates) { |
| 341 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 342 | 0 | entry.setNeedsUpdate(true); |
| 343 | |
} else { |
| 344 | 0 | long currentTimestamp = 0; |
| 345 | |
try { |
| 346 | 0 | currentTimestamp = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED_BASE + entry.getId(), "0")); |
| 347 | 0 | } catch (NumberFormatException ex) { |
| 348 | 0 | final String msg = String.format("Error parsing '%s' '%s' from nvdcve.lastupdated", |
| 349 | |
DataStoreMetaInfo.LAST_UPDATED_BASE, entry.getId()); |
| 350 | 0 | Logger |
| 351 | |
.getLogger(StandardUpdateTask.class |
| 352 | |
.getName()).log(Level.FINE, msg, ex); |
| 353 | 0 | } |
| 354 | 0 | if (currentTimestamp == entry.getTimestamp()) { |
| 355 | 0 | entry.setNeedsUpdate(false); |
| 356 | |
} |
| 357 | 0 | } |
| 358 | |
} |
| 359 | |
} |
| 360 | 0 | } catch (NumberFormatException ex) { |
| 361 | 0 | final String msg = "An invalid schema version or timestamp exists in the data.properties file."; |
| 362 | 0 | Logger |
| 363 | |
.getLogger(StandardUpdateTask.class |
| 364 | |
.getName()).log(Level.WARNING, msg); |
| 365 | 0 | Logger.getLogger(StandardUpdateTask.class |
| 366 | |
.getName()).log(Level.FINE, null, ex); |
| 367 | 0 | } |
| 368 | |
} |
| 369 | 0 | return updates; |
| 370 | |
} |
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
private Updateable retrieveCurrentTimestampsFromWeb() |
| 385 | |
throws MalformedURLException, DownloadFailedException, InvalidDataException, InvalidSettingException { |
| 386 | |
|
| 387 | 0 | final Updateable updates = new Updateable(); |
| 388 | 0 | updates.add(MODIFIED, Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL), |
| 389 | |
Settings.getString(Settings.KEYS.CVE_MODIFIED_12_URL), |
| 390 | |
false); |
| 391 | |
|
| 392 | 0 | final int start = Settings.getInt(Settings.KEYS.CVE_START_YEAR); |
| 393 | 0 | final int end = Calendar.getInstance().get(Calendar.YEAR); |
| 394 | 0 | final String baseUrl20 = Settings.getString(Settings.KEYS.CVE_SCHEMA_2_0); |
| 395 | 0 | final String baseUrl12 = Settings.getString(Settings.KEYS.CVE_SCHEMA_1_2); |
| 396 | 0 | for (int i = start; i <= end; i++) { |
| 397 | 0 | updates.add(Integer.toString(i), String.format(baseUrl20, i), |
| 398 | |
String.format(baseUrl12, i), |
| 399 | |
true); |
| 400 | |
} |
| 401 | |
|
| 402 | 0 | return updates; |
| 403 | |
} |
| 404 | |
} |