| 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.IOException; |
| 24 | |
import java.net.MalformedURLException; |
| 25 | |
import java.sql.SQLException; |
| 26 | |
import java.util.Calendar; |
| 27 | |
import java.util.Date; |
| 28 | |
import java.util.HashSet; |
| 29 | |
import java.util.Iterator; |
| 30 | |
import java.util.Set; |
| 31 | |
import java.util.concurrent.ExecutionException; |
| 32 | |
import java.util.concurrent.ExecutorService; |
| 33 | |
import java.util.concurrent.Executors; |
| 34 | |
import java.util.concurrent.Future; |
| 35 | |
import java.util.logging.Level; |
| 36 | |
import java.util.logging.Logger; |
| 37 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 38 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 39 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 40 | |
import org.owasp.dependencycheck.utils.Settings; |
| 41 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 42 | |
import org.owasp.dependencycheck.utils.InvalidSettingException; |
| 43 | |
import static org.owasp.dependencycheck.data.update.DataStoreMetaInfo.MODIFIED; |
| 44 | |
import org.owasp.dependencycheck.utils.FileUtils; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public class StandardUpdate { |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | public static final int MAX_THREAD_POOL_SIZE = Settings.getInt(Settings.KEYS.MAX_DOWNLOAD_THREAD_POOL_SIZE, 3); |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private DataStoreMetaInfo properties; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private Updateable updateable; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 0 | private boolean deleteAndRecreate = false; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 0 | private CveDB cveDB = null; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public boolean isUpdateNeeded() { |
| 82 | 0 | return updateable.isUpdateNeeded(); |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
protected void setDeleteAndRecreate(boolean deleteAndRecreate) { |
| 91 | 0 | this.deleteAndRecreate = deleteAndRecreate; |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public boolean shouldDeleteAndRecreate() { |
| 100 | 0 | return deleteAndRecreate; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 0 | public StandardUpdate() throws MalformedURLException, DownloadFailedException, UpdateException { |
| 113 | 0 | properties = new DataStoreMetaInfo(); |
| 114 | 0 | updateable = updatesNeeded(); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public void update() throws UpdateException { |
| 125 | 0 | int maxUpdates = 0; |
| 126 | |
try { |
| 127 | 0 | for (NvdCveInfo cve : updateable) { |
| 128 | 0 | if (cve.getNeedsUpdate()) { |
| 129 | 0 | maxUpdates += 1; |
| 130 | |
} |
| 131 | |
} |
| 132 | 0 | if (maxUpdates <= 0) { |
| 133 | |
return; |
| 134 | |
} |
| 135 | 0 | if (maxUpdates > 3) { |
| 136 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, |
| 137 | |
"NVD CVE requires several updates; this could take a couple of minutes."); |
| 138 | |
} |
| 139 | 0 | if (maxUpdates > 0) { |
| 140 | 0 | openDataStores(); |
| 141 | |
} |
| 142 | |
|
| 143 | 0 | final int poolSize = (MAX_THREAD_POOL_SIZE > maxUpdates) ? MAX_THREAD_POOL_SIZE : maxUpdates; |
| 144 | 0 | final ExecutorService downloadExecutor = Executors.newFixedThreadPool(poolSize); |
| 145 | 0 | final ExecutorService processExecutor = Executors.newSingleThreadExecutor(); |
| 146 | 0 | final Set<Future<CallableDownloadTask>> downloadFutures = new HashSet<Future<CallableDownloadTask>>(maxUpdates); |
| 147 | 0 | final Set<Future<ProcessTask>> processFutures = new HashSet<Future<ProcessTask>>(maxUpdates); |
| 148 | 0 | int ctr = 0; |
| 149 | 0 | for (NvdCveInfo cve : updateable) { |
| 150 | 0 | if (cve.getNeedsUpdate()) { |
| 151 | 0 | ctr += 1; |
| 152 | |
final File file1; |
| 153 | |
final File file2; |
| 154 | |
try { |
| 155 | 0 | file1 = File.createTempFile("cve" + cve.getId() + "_", ".xml"); |
| 156 | 0 | file2 = File.createTempFile("cve_1_2_" + cve.getId() + "_", ".xml"); |
| 157 | 0 | } catch (IOException ex) { |
| 158 | 0 | throw new UpdateException(ex); |
| 159 | 0 | } |
| 160 | 0 | final CallableDownloadTask call = new CallableDownloadTask(cve, file1, file2); |
| 161 | 0 | downloadFutures.add(downloadExecutor.submit(call)); |
| 162 | |
|
| 163 | 0 | boolean waitForFuture = ctr % 2 == 0; |
| 164 | |
|
| 165 | 0 | final Iterator<Future<CallableDownloadTask>> itr = downloadFutures.iterator(); |
| 166 | 0 | while (itr.hasNext()) { |
| 167 | 0 | final Future<CallableDownloadTask> future = itr.next(); |
| 168 | 0 | if (waitForFuture) { |
| 169 | 0 | spinWaitForFuture(future); |
| 170 | |
} |
| 171 | 0 | if (future.isDone()) { |
| 172 | |
try { |
| 173 | 0 | final CallableDownloadTask filePair = future.get(); |
| 174 | 0 | itr.remove(); |
| 175 | 0 | final ProcessTask task = new ProcessTask(cveDB, properties, filePair); |
| 176 | 0 | processFutures.add(processExecutor.submit(task)); |
| 177 | 0 | } catch (InterruptedException ex) { |
| 178 | 0 | downloadExecutor.shutdownNow(); |
| 179 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Thread was interupted", ex); |
| 180 | 0 | throw new UpdateException(ex); |
| 181 | 0 | } catch (ExecutionException ex) { |
| 182 | 0 | downloadExecutor.shutdownNow(); |
| 183 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.SEVERE, null, ex); |
| 184 | 0 | throw new UpdateException(ex); |
| 185 | 0 | } |
| 186 | |
} |
| 187 | 0 | } |
| 188 | |
|
| 189 | 0 | } |
| 190 | |
} |
| 191 | |
|
| 192 | |
try { |
| 193 | 0 | final Iterator<Future<CallableDownloadTask>> itr = downloadFutures.iterator(); |
| 194 | 0 | while (itr.hasNext()) { |
| 195 | 0 | final Future<CallableDownloadTask> future = itr.next(); |
| 196 | 0 | final CallableDownloadTask filePair = future.get(); |
| 197 | 0 | final ProcessTask task = new ProcessTask(cveDB, properties, filePair); |
| 198 | 0 | processFutures.add(processExecutor.submit(task)); |
| 199 | 0 | } |
| 200 | 0 | } catch (InterruptedException ex) { |
| 201 | 0 | downloadExecutor.shutdownNow(); |
| 202 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Thread was interupted during download", ex); |
| 203 | 0 | throw new UpdateException(ex); |
| 204 | 0 | } catch (ExecutionException ex) { |
| 205 | 0 | downloadExecutor.shutdownNow(); |
| 206 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Execution Exception during download", ex); |
| 207 | 0 | throw new UpdateException(ex); |
| 208 | |
} finally { |
| 209 | 0 | downloadExecutor.shutdown(); |
| 210 | 0 | } |
| 211 | |
|
| 212 | 0 | for (Future<ProcessTask> future : processFutures) { |
| 213 | |
try { |
| 214 | 0 | final ProcessTask task = future.get(); |
| 215 | 0 | if (task.getException() != null) { |
| 216 | 0 | throw task.getException(); |
| 217 | |
} |
| 218 | 0 | } catch (InterruptedException ex) { |
| 219 | 0 | processExecutor.shutdownNow(); |
| 220 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Thread was interupted during processing", ex); |
| 221 | 0 | throw new UpdateException(ex); |
| 222 | 0 | } catch (ExecutionException ex) { |
| 223 | 0 | processExecutor.shutdownNow(); |
| 224 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Execution Exception during process", ex); |
| 225 | 0 | throw new UpdateException(ex); |
| 226 | |
} finally { |
| 227 | 0 | processExecutor.shutdown(); |
| 228 | 0 | } |
| 229 | |
} |
| 230 | |
|
| 231 | 0 | if (maxUpdates >= 1) { |
| 232 | 0 | properties.save(updateable.get(MODIFIED)); |
| 233 | 0 | cveDB.cleanupDatabase(); |
| 234 | |
} |
| 235 | |
} finally { |
| 236 | 0 | closeDataStores(); |
| 237 | 0 | } |
| 238 | 0 | } |
| 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 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
protected final Updateable updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException { |
| 353 | 0 | Updateable updates = null; |
| 354 | |
try { |
| 355 | 0 | updates = retrieveCurrentTimestampsFromWeb(); |
| 356 | 0 | } catch (InvalidDataException ex) { |
| 357 | 0 | final String msg = "Unable to retrieve valid timestamp from nvd cve downloads page"; |
| 358 | 0 | Logger |
| 359 | |
.getLogger(StandardUpdate.class |
| 360 | |
.getName()).log(Level.FINE, msg, ex); |
| 361 | 0 | throw new DownloadFailedException(msg, ex); |
| 362 | 0 | } catch (InvalidSettingException ex) { |
| 363 | 0 | Logger.getLogger(StandardUpdate.class |
| 364 | |
.getName()).log(Level.FINE, "Invalid setting found when retrieving timestamps", ex); |
| 365 | 0 | throw new DownloadFailedException( |
| 366 | |
"Invalid settings", ex); |
| 367 | 0 | } |
| 368 | |
|
| 369 | 0 | if (updates == null) { |
| 370 | 0 | throw new DownloadFailedException("Unable to retrieve the timestamps of the currently published NVD CVE data"); |
| 371 | |
} |
| 372 | 0 | if (!properties.isEmpty()) { |
| 373 | |
try { |
| 374 | |
float version; |
| 375 | |
|
| 376 | 0 | if (properties.getProperty("version") == null) { |
| 377 | 0 | deleteAndRecreate = true; |
| 378 | |
} else { |
| 379 | |
try { |
| 380 | 0 | version = Float.parseFloat(properties.getProperty("version")); |
| 381 | 0 | final float currentVersion = Float.parseFloat(CveDB.DB_SCHEMA_VERSION); |
| 382 | 0 | if (currentVersion > version) { |
| 383 | 0 | deleteAndRecreate = true; |
| 384 | |
} |
| 385 | 0 | } catch (NumberFormatException ex) { |
| 386 | 0 | deleteAndRecreate = true; |
| 387 | 0 | } |
| 388 | |
} |
| 389 | |
|
| 390 | 0 | if (deleteAndRecreate) { |
| 391 | 0 | return updates; |
| 392 | |
} |
| 393 | |
|
| 394 | 0 | final long lastUpdated = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED, "0")); |
| 395 | 0 | final Date now = new Date(); |
| 396 | 0 | final int days = Settings.getInt(Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS, 7); |
| 397 | 0 | if (lastUpdated == updates.getTimeStamp(MODIFIED)) { |
| 398 | 0 | updates.clear(); |
| 399 | 0 | } else if (withinRange(lastUpdated, now.getTime(), days)) { |
| 400 | 0 | for (NvdCveInfo entry : updates) { |
| 401 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 402 | 0 | entry.setNeedsUpdate(true); |
| 403 | |
} else { |
| 404 | 0 | entry.setNeedsUpdate(false); |
| 405 | |
} |
| 406 | |
} |
| 407 | |
} else { |
| 408 | 0 | for (NvdCveInfo entry : updates) { |
| 409 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 410 | 0 | entry.setNeedsUpdate(true); |
| 411 | |
} else { |
| 412 | 0 | long currentTimestamp = 0; |
| 413 | |
try { |
| 414 | 0 | currentTimestamp = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED_BASE + entry.getId(), "0")); |
| 415 | 0 | } catch (NumberFormatException ex) { |
| 416 | 0 | final String msg = String.format("Error parsing '%s' '%s' from nvdcve.lastupdated", |
| 417 | |
DataStoreMetaInfo.LAST_UPDATED_BASE, entry.getId()); |
| 418 | 0 | Logger |
| 419 | |
.getLogger(StandardUpdate.class |
| 420 | |
.getName()).log(Level.FINE, msg, ex); |
| 421 | 0 | } |
| 422 | 0 | if (currentTimestamp == entry.getTimestamp()) { |
| 423 | 0 | entry.setNeedsUpdate(false); |
| 424 | |
} |
| 425 | 0 | } |
| 426 | |
} |
| 427 | |
} |
| 428 | 0 | } catch (NumberFormatException ex) { |
| 429 | 0 | final String msg = "An invalid schema version or timestamp exists in the data.properties file."; |
| 430 | 0 | Logger |
| 431 | |
.getLogger(StandardUpdate.class |
| 432 | |
.getName()).log(Level.WARNING, msg); |
| 433 | 0 | Logger.getLogger(StandardUpdate.class |
| 434 | |
.getName()).log(Level.FINE, null, ex); |
| 435 | 0 | } |
| 436 | |
} |
| 437 | 0 | return updates; |
| 438 | |
} |
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
private Updateable retrieveCurrentTimestampsFromWeb() |
| 453 | |
throws MalformedURLException, DownloadFailedException, InvalidDataException, InvalidSettingException { |
| 454 | |
|
| 455 | 0 | final Updateable updates = new Updateable(); |
| 456 | 0 | updates.add(MODIFIED, Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL), |
| 457 | |
Settings.getString(Settings.KEYS.CVE_MODIFIED_12_URL), |
| 458 | |
false); |
| 459 | |
|
| 460 | 0 | final int start = Settings.getInt(Settings.KEYS.CVE_START_YEAR); |
| 461 | 0 | final int end = Calendar.getInstance().get(Calendar.YEAR); |
| 462 | 0 | final String baseUrl20 = Settings.getString(Settings.KEYS.CVE_SCHEMA_2_0); |
| 463 | 0 | final String baseUrl12 = Settings.getString(Settings.KEYS.CVE_SCHEMA_1_2); |
| 464 | 0 | for (int i = start; i <= end; i++) { |
| 465 | 0 | updates.add(Integer.toString(i), String.format(baseUrl20, i), |
| 466 | |
String.format(baseUrl12, i), |
| 467 | |
true); |
| 468 | |
} |
| 469 | |
|
| 470 | 0 | return updates; |
| 471 | |
} |
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
protected void deleteExistingData() throws IOException { |
| 479 | 0 | File data = Settings.getDataFile(Settings.KEYS.CVE_DATA_DIRECTORY); |
| 480 | 0 | if (data.exists()) { |
| 481 | 0 | FileUtils.delete(data); |
| 482 | |
} |
| 483 | 0 | data = DataStoreMetaInfo.getPropertiesFile(); |
| 484 | 0 | if (data.exists()) { |
| 485 | 0 | FileUtils.delete(data); |
| 486 | |
} |
| 487 | 0 | } |
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
protected void closeDataStores() { |
| 493 | 0 | if (cveDB != null) { |
| 494 | |
try { |
| 495 | 0 | cveDB.close(); |
| 496 | 0 | } catch (Exception ignore) { |
| 497 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINEST, "Error closing the cveDB", ignore); |
| 498 | 0 | } |
| 499 | |
} |
| 500 | 0 | } |
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
protected void openDataStores() throws UpdateException { |
| 508 | |
|
| 509 | |
try { |
| 510 | 0 | cveDB = new CveDB(); |
| 511 | 0 | cveDB.open(); |
| 512 | 0 | } catch (IOException ex) { |
| 513 | 0 | closeDataStores(); |
| 514 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "IO Error opening databases", ex); |
| 515 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 516 | 0 | } catch (SQLException ex) { |
| 517 | 0 | closeDataStores(); |
| 518 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "SQL Exception opening databases", ex); |
| 519 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 520 | 0 | } catch (DatabaseException ex) { |
| 521 | 0 | closeDataStores(); |
| 522 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Database Exception opening databases", ex); |
| 523 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 524 | 0 | } catch (ClassNotFoundException ex) { |
| 525 | 0 | closeDataStores(); |
| 526 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, "Class not found exception opening databases", ex); |
| 527 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 528 | 0 | } |
| 529 | 0 | } |
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | |
protected boolean withinRange(long date, long compareTo, int range) { |
| 543 | 0 | final double differenceInDays = (compareTo - date) / 1000.0 / 60.0 / 60.0 / 24.0; |
| 544 | 0 | return differenceInDays < range; |
| 545 | |
} |
| 546 | |
|
| 547 | |
private void spinWaitForFuture(final Future<CallableDownloadTask> future) { |
| 548 | |
|
| 549 | 0 | while (!future.isDone()) { |
| 550 | |
try { |
| 551 | 0 | Thread.sleep(1000); |
| 552 | 0 | } catch (InterruptedException ex) { |
| 553 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.FINE, null, ex); |
| 554 | 0 | } |
| 555 | |
} |
| 556 | 0 | } |
| 557 | |
} |