Coverage Report - org.owasp.dependencycheck.data.update.StandardUpdate
 
Classes in this File Line Coverage Branch Coverage Complexity
StandardUpdate
0%
0/143
0%
0/52
6.875
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 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.data.update;
 19  
 
 20  
 import java.net.MalformedURLException;
 21  
 import java.util.Calendar;
 22  
 import java.util.Date;
 23  
 import java.util.HashSet;
 24  
 import java.util.Set;
 25  
 import java.util.concurrent.ExecutionException;
 26  
 import java.util.concurrent.ExecutorService;
 27  
 import java.util.concurrent.Executors;
 28  
 import java.util.concurrent.Future;
 29  
 import java.util.logging.Level;
 30  
 import java.util.logging.Logger;
 31  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 32  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 33  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 34  
 import static org.owasp.dependencycheck.data.nvdcve.DatabaseProperties.MODIFIED;
 35  
 import org.owasp.dependencycheck.data.update.exception.InvalidDataException;
 36  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 37  
 import org.owasp.dependencycheck.data.update.task.DownloadTask;
 38  
 import org.owasp.dependencycheck.data.update.task.ProcessTask;
 39  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 40  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 41  
 import org.owasp.dependencycheck.utils.Settings;
 42  
 
 43  
 /**
 44  
  * Class responsible for updating the NVDCVE data store.
 45  
  *
 46  
  * @author Jeremy Long <jeremy.long@owasp.org>
 47  
  */
 48  
 public class StandardUpdate {
 49  
 
 50  
     /**
 51  
      * Static logger.
 52  
      */
 53  0
     private static final Logger LOGGER = Logger.getLogger(StandardUpdate.class.getName());
 54  
     /**
 55  
      * The max thread pool size to use when downloading files.
 56  
      */
 57  0
     public static final int MAX_THREAD_POOL_SIZE = Settings.getInt(Settings.KEYS.MAX_DOWNLOAD_THREAD_POOL_SIZE, 3);
 58  
     /**
 59  
      * Information about the timestamps and URLs for data that needs to be updated.
 60  
      */
 61  
     private DatabaseProperties properties;
 62  
     /**
 63  
      * A collection of updateable NVD CVE items.
 64  
      */
 65  
     private UpdateableNvdCve updateable;
 66  
     /**
 67  
      * Reference to the Cve Database.
 68  
      */
 69  0
     private CveDB cveDB = null;
 70  
 
 71  
     /**
 72  
      * Gets whether or not an update is needed.
 73  
      *
 74  
      * @return true or false depending on whether an update is needed
 75  
      */
 76  
     public boolean isUpdateNeeded() {
 77  0
         return updateable.isUpdateNeeded();
 78  
     }
 79  
 
 80  
     /**
 81  
      * Constructs a new Standard Update Task.
 82  
      *
 83  
      * @throws MalformedURLException thrown if a configured URL is malformed
 84  
      * @throws DownloadFailedException thrown if a timestamp cannot be checked on a configured URL
 85  
      * @throws UpdateException thrown if there is an exception generating the update task
 86  
      */
 87  0
     public StandardUpdate() throws MalformedURLException, DownloadFailedException, UpdateException {
 88  0
         openDataStores();
 89  0
         properties = cveDB.getDatabaseProperties();
 90  0
         updateable = updatesNeeded();
 91  0
     }
 92  
 
 93  
     /**
 94  
      * <p>
 95  
      * Downloads the latest NVD CVE XML file from the web and imports it into the current CVE Database.</p>
 96  
      *
 97  
      * @throws UpdateException is thrown if there is an error updating the database
 98  
      */
 99  
     public void update() throws UpdateException {
 100  0
         int maxUpdates = 0;
 101  
         try {
 102  0
             for (NvdCveInfo cve : updateable) {
 103  0
                 if (cve.getNeedsUpdate()) {
 104  0
                     maxUpdates += 1;
 105  
                 }
 106  0
             }
 107  0
             if (maxUpdates <= 0) {
 108  
                 return;
 109  
             }
 110  0
             if (maxUpdates > 3) {
 111  0
                 LOGGER.log(Level.INFO,
 112  
                         "NVD CVE requires several updates; this could take a couple of minutes.");
 113  
             }
 114  0
             if (maxUpdates > 0) {
 115  0
                 openDataStores();
 116  
             }
 117  
 
 118  0
             final int poolSize = (MAX_THREAD_POOL_SIZE < maxUpdates) ? MAX_THREAD_POOL_SIZE : maxUpdates;
 119  
 
 120  0
             final ExecutorService downloadExecutors = Executors.newFixedThreadPool(poolSize);
 121  0
             final ExecutorService processExecutor = Executors.newSingleThreadExecutor();
 122  0
             final Set<Future<Future<ProcessTask>>> downloadFutures = new HashSet<Future<Future<ProcessTask>>>(maxUpdates);
 123  0
             for (NvdCveInfo cve : updateable) {
 124  0
                 if (cve.getNeedsUpdate()) {
 125  0
                     final DownloadTask call = new DownloadTask(cve, processExecutor, cveDB, Settings.getInstance());
 126  0
                     downloadFutures.add(downloadExecutors.submit(call));
 127  
                 }
 128  0
             }
 129  0
             downloadExecutors.shutdown();
 130  
 
 131  
             //next, move the future future processTasks to just future processTasks
 132  0
             final Set<Future<ProcessTask>> processFutures = new HashSet<Future<ProcessTask>>(maxUpdates);
 133  0
             for (Future<Future<ProcessTask>> future : downloadFutures) {
 134  0
                 Future<ProcessTask> task = null;
 135  
                 try {
 136  0
                     task = future.get();
 137  0
                 } catch (InterruptedException ex) {
 138  0
                     downloadExecutors.shutdownNow();
 139  0
                     processExecutor.shutdownNow();
 140  
 
 141  0
                     LOGGER.log(Level.FINE, "Thread was interrupted during download", ex);
 142  0
                     throw new UpdateException("The download was interrupted", ex);
 143  0
                 } catch (ExecutionException ex) {
 144  0
                     downloadExecutors.shutdownNow();
 145  0
                     processExecutor.shutdownNow();
 146  
 
 147  0
                     LOGGER.log(Level.FINE, "Thread was interrupted during download execution", ex);
 148  0
                     throw new UpdateException("The execution of the download was interrupted", ex);
 149  0
                 }
 150  0
                 if (task == null) {
 151  0
                     downloadExecutors.shutdownNow();
 152  0
                     processExecutor.shutdownNow();
 153  0
                     LOGGER.log(Level.FINE, "Thread was interrupted during download");
 154  0
                     throw new UpdateException("The download was interrupted; unable to complete the update");
 155  
                 } else {
 156  0
                     processFutures.add(task);
 157  
                 }
 158  0
             }
 159  
 
 160  0
             for (Future<ProcessTask> future : processFutures) {
 161  
                 try {
 162  0
                     final ProcessTask task = future.get();
 163  0
                     if (task.getException() != null) {
 164  0
                         throw task.getException();
 165  
                     }
 166  0
                 } catch (InterruptedException ex) {
 167  0
                     processExecutor.shutdownNow();
 168  0
                     LOGGER.log(Level.FINE, "Thread was interrupted during processing", ex);
 169  0
                     throw new UpdateException(ex);
 170  0
                 } catch (ExecutionException ex) {
 171  0
                     processExecutor.shutdownNow();
 172  0
                     LOGGER.log(Level.FINE, "Execution Exception during process", ex);
 173  0
                     throw new UpdateException(ex);
 174  
                 } finally {
 175  0
                     processExecutor.shutdown();
 176  0
                 }
 177  0
             }
 178  
 
 179  0
             if (maxUpdates >= 1) { //ensure the modified file date gets written (we may not have actually updated it)
 180  0
                 properties.save(updateable.get(MODIFIED));
 181  0
                 LOGGER.log(Level.INFO, "Begin database maintenance.");
 182  0
                 cveDB.cleanupDatabase();
 183  0
                 LOGGER.log(Level.INFO, "End database maintenance.");
 184  
             }
 185  
         } finally {
 186  0
             closeDataStores();
 187  0
         }
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Determines if the index needs to be updated. This is done by fetching the NVD CVE meta data and checking the last
 192  
      * update date. If the data needs to be refreshed this method will return the NvdCveUrl for the files that need to
 193  
      * be updated.
 194  
      *
 195  
      * @return the collection of files that need to be updated
 196  
      * @throws MalformedURLException is thrown if the URL for the NVD CVE Meta data is incorrect
 197  
      * @throws DownloadFailedException is thrown if there is an error. downloading the NVD CVE download data file
 198  
      * @throws UpdateException Is thrown if there is an issue with the last updated properties file
 199  
      */
 200  
     protected final UpdateableNvdCve updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException {
 201  0
         UpdateableNvdCve updates = null;
 202  
         try {
 203  0
             updates = retrieveCurrentTimestampsFromWeb();
 204  0
         } catch (InvalidDataException ex) {
 205  0
             final String msg = "Unable to retrieve valid timestamp from nvd cve downloads page";
 206  0
             LOGGER.log(Level.FINE, msg, ex);
 207  0
             throw new DownloadFailedException(msg, ex);
 208  0
         } catch (InvalidSettingException ex) {
 209  0
             LOGGER.log(Level.FINE, "Invalid setting found when retrieving timestamps", ex);
 210  0
             throw new DownloadFailedException("Invalid settings", ex);
 211  0
         }
 212  
 
 213  0
         if (updates == null) {
 214  0
             throw new DownloadFailedException("Unable to retrieve the timestamps of the currently published NVD CVE data");
 215  
         }
 216  0
         if (!properties.isEmpty()) {
 217  
             try {
 218  0
                 final long lastUpdated = Long.parseLong(properties.getProperty(DatabaseProperties.LAST_UPDATED, "0"));
 219  0
                 final Date now = new Date();
 220  0
                 final int days = Settings.getInt(Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS, 7);
 221  0
                 if (lastUpdated == updates.getTimeStamp(MODIFIED)) {
 222  0
                     updates.clear(); //we don't need to update anything.
 223  0
                 } else if (withinRange(lastUpdated, now.getTime(), days)) {
 224  0
                     for (NvdCveInfo entry : updates) {
 225  0
                         if (MODIFIED.equals(entry.getId())) {
 226  0
                             entry.setNeedsUpdate(true);
 227  
                         } else {
 228  0
                             entry.setNeedsUpdate(false);
 229  
                         }
 230  0
                     }
 231  
                 } else { //we figure out which of the several XML files need to be downloaded.
 232  0
                     for (NvdCveInfo entry : updates) {
 233  0
                         if (MODIFIED.equals(entry.getId())) {
 234  0
                             entry.setNeedsUpdate(true);
 235  
                         } else {
 236  0
                             long currentTimestamp = 0;
 237  
                             try {
 238  0
                                 currentTimestamp = Long.parseLong(properties.getProperty(DatabaseProperties.LAST_UPDATED_BASE + entry.getId(), "0"));
 239  0
                             } catch (NumberFormatException ex) {
 240  0
                                 final String msg = String.format("Error parsing '%s' '%s' from nvdcve.lastupdated",
 241  
                                         DatabaseProperties.LAST_UPDATED_BASE, entry.getId());
 242  0
                                 LOGGER.log(Level.FINE, msg, ex);
 243  0
                             }
 244  0
                             if (currentTimestamp == entry.getTimestamp()) {
 245  0
                                 entry.setNeedsUpdate(false);
 246  
                             }
 247  
                         }
 248  0
                     }
 249  
                 }
 250  0
             } catch (NumberFormatException ex) {
 251  0
                 final String msg = "An invalid schema version or timestamp exists in the data.properties file.";
 252  0
                 LOGGER.log(Level.WARNING, msg);
 253  0
                 LOGGER.log(Level.FINE, "", ex);
 254  0
             }
 255  
         }
 256  0
         return updates;
 257  
     }
 258  
 
 259  
     /**
 260  
      * Retrieves the timestamps from the NVD CVE meta data file.
 261  
      *
 262  
      * @return the timestamp from the currently published nvdcve downloads page
 263  
      * @throws MalformedURLException thrown if the URL for the NVD CCE Meta data is incorrect.
 264  
      * @throws DownloadFailedException thrown if there is an error downloading the nvd cve meta data file
 265  
      * @throws InvalidDataException thrown if there is an exception parsing the timestamps
 266  
      * @throws InvalidSettingException thrown if the settings are invalid
 267  
      */
 268  
     private UpdateableNvdCve retrieveCurrentTimestampsFromWeb()
 269  
             throws MalformedURLException, DownloadFailedException, InvalidDataException, InvalidSettingException {
 270  
 
 271  0
         final UpdateableNvdCve updates = new UpdateableNvdCve();
 272  0
         updates.add(MODIFIED, Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL),
 273  
                 Settings.getString(Settings.KEYS.CVE_MODIFIED_12_URL),
 274  
                 false);
 275  
 
 276  0
         final int start = Settings.getInt(Settings.KEYS.CVE_START_YEAR);
 277  0
         final int end = Calendar.getInstance().get(Calendar.YEAR);
 278  0
         final String baseUrl20 = Settings.getString(Settings.KEYS.CVE_SCHEMA_2_0);
 279  0
         final String baseUrl12 = Settings.getString(Settings.KEYS.CVE_SCHEMA_1_2);
 280  0
         for (int i = start; i <= end; i++) {
 281  0
             updates.add(Integer.toString(i), String.format(baseUrl20, i),
 282  
                     String.format(baseUrl12, i),
 283  
                     true);
 284  
         }
 285  
 
 286  0
         return updates;
 287  
     }
 288  
 
 289  
     /**
 290  
      * Closes the CVE and CPE data stores.
 291  
      */
 292  
     protected void closeDataStores() {
 293  0
         if (cveDB != null) {
 294  
             try {
 295  0
                 cveDB.close();
 296  0
             } catch (Throwable ignore) {
 297  0
                 LOGGER.log(Level.FINEST, "Error closing the cveDB", ignore);
 298  0
             }
 299  
         }
 300  0
     }
 301  
 
 302  
     /**
 303  
      * Opens the CVE and CPE data stores.
 304  
      *
 305  
      * @throws UpdateException thrown if a data store cannot be opened
 306  
      */
 307  
     protected final void openDataStores() throws UpdateException {
 308  0
         if (cveDB != null) {
 309  0
             return;
 310  
         }
 311  
         try {
 312  0
             cveDB = new CveDB();
 313  0
             cveDB.open();
 314  0
         } catch (DatabaseException ex) {
 315  0
             closeDataStores();
 316  0
             LOGGER.log(Level.FINE, "Database Exception opening databases", ex);
 317  0
             throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details.");
 318  0
         }
 319  0
     }
 320  
 
 321  
     /**
 322  
      * Determines if the epoch date is within the range specified of the compareTo epoch time. This takes the
 323  
      * (compareTo-date)/1000/60/60/24 to get the number of days. If the calculated days is less then the range the date
 324  
      * is considered valid.
 325  
      *
 326  
      * @param date the date to be checked.
 327  
      * @param compareTo the date to compare to.
 328  
      * @param range the range in days to be considered valid.
 329  
      * @return whether or not the date is within the range.
 330  
      */
 331  
     protected boolean withinRange(long date, long compareTo, int range) {
 332  0
         final double differenceInDays = (compareTo - date) / 1000.0 / 60.0 / 60.0 / 24.0;
 333  0
         return differenceInDays < range;
 334  
     }
 335  
 }