Coverage Report - org.owasp.dependencycheck.data.update.task.ProcessTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessTask
0%
0/50
N/A
3.5
 
 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) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.update.task;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import java.sql.SQLException;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import java.util.concurrent.Callable;
 27  
 import java.util.logging.Level;
 28  
 import java.util.logging.Logger;
 29  
 import javax.xml.parsers.ParserConfigurationException;
 30  
 import javax.xml.parsers.SAXParser;
 31  
 import javax.xml.parsers.SAXParserFactory;
 32  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 33  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 34  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 35  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 36  
 import org.owasp.dependencycheck.data.update.xml.NvdCve12Handler;
 37  
 import org.owasp.dependencycheck.data.update.xml.NvdCve20Handler;
 38  
 import org.owasp.dependencycheck.dependency.VulnerableSoftware;
 39  
 import org.owasp.dependencycheck.utils.Settings;
 40  
 import org.xml.sax.SAXException;
 41  
 
 42  
 /**
 43  
  * A callable task that will process a given set of NVD CVE xml files and update the Cve Database accordingly.
 44  
  *
 45  
  * @author Jeremy Long <jeremy.long@owasp.org>
 46  
  */
 47  0
 public class ProcessTask implements Callable<ProcessTask> {
 48  
 
 49  
     /**
 50  
      * The logger.
 51  
      */
 52  0
     private static final Logger LOGGER = Logger.getLogger(ProcessTask.class.getName());
 53  
     /**
 54  
      * A field to store any update exceptions that occur during the "call".
 55  
      */
 56  0
     private UpdateException exception = null;
 57  
 
 58  
     /**
 59  
      * Get the value of exception.
 60  
      *
 61  
      * @return the value of exception
 62  
      */
 63  
     public UpdateException getException() {
 64  
         return exception;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Set the value of exception.
 69  
      *
 70  
      * @param exception new value of exception
 71  
      */
 72  
     public void setException(UpdateException exception) {
 73  
         this.exception = exception;
 74  
     }
 75  
     /**
 76  
      * A reference to the CveDB.
 77  
      */
 78  
     private final CveDB cveDB;
 79  
     /**
 80  
      * A reference to the callable download task.
 81  
      */
 82  
     private final DownloadTask filePair;
 83  
     /**
 84  
      * A reference to the properties.
 85  
      */
 86  
     private final DatabaseProperties properties;
 87  
     /**
 88  
      * A reference to the global settings object.
 89  
      */
 90  
     private Settings settings;
 91  
 
 92  
     /**
 93  
      * Constructs a new ProcessTask used to process an NVD CVE update.
 94  
      *
 95  
      * @param cveDB the data store object
 96  
      * @param filePair the download task that contains the URL references to download
 97  
      * @param settings a reference to the global settings object; this is necessary so that when the thread is started
 98  
      * the dependencies have a correct reference to the global settings.
 99  
      */
 100  0
     public ProcessTask(final CveDB cveDB, final DownloadTask filePair, Settings settings) {
 101  0
         this.cveDB = cveDB;
 102  0
         this.filePair = filePair;
 103  0
         this.properties = cveDB.getDatabaseProperties();
 104  0
         this.settings = settings;
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Implements the callable interface.
 109  
      *
 110  
      * @return this object
 111  
      * @throws Exception thrown if there is an exception; note that any UpdateExceptions are simply added to the tasks
 112  
      * exception collection
 113  
      */
 114  
     @Override
 115  
     public ProcessTask call() throws Exception {
 116  
         try {
 117  0
             Settings.setInstance(settings);
 118  0
             processFiles();
 119  0
         } catch (UpdateException ex) {
 120  0
             this.exception = ex;
 121  
         } finally {
 122  0
             Settings.cleanup(false);
 123  0
         }
 124  0
         return this;
 125  
     }
 126  
 
 127  
     /**
 128  
      * Imports the NVD CVE XML File into the Lucene Index.
 129  
      *
 130  
      * @param file the file containing the NVD CVE XML
 131  
      * @param oldVersion contains the file containing the NVD CVE XML 1.2
 132  
      * @throws ParserConfigurationException is thrown if there is a parser configuration exception
 133  
      * @throws SAXException is thrown if there is a SAXException
 134  
      * @throws IOException is thrown if there is a IO Exception
 135  
      * @throws SQLException is thrown if there is a SQL exception
 136  
      * @throws DatabaseException is thrown if there is a database exception
 137  
      * @throws ClassNotFoundException thrown if the h2 database driver cannot be loaded
 138  
      */
 139  
     protected void importXML(File file, File oldVersion) throws ParserConfigurationException,
 140  
             SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException {
 141  
 
 142  0
         final SAXParserFactory factory = SAXParserFactory.newInstance();
 143  0
         final SAXParser saxParser = factory.newSAXParser();
 144  
 
 145  0
         final NvdCve12Handler cve12Handler = new NvdCve12Handler();
 146  0
         saxParser.parse(oldVersion, cve12Handler);
 147  0
         final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities();
 148  
 
 149  0
         final NvdCve20Handler cve20Handler = new NvdCve20Handler();
 150  0
         cve20Handler.setCveDB(cveDB);
 151  0
         cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap);
 152  0
         saxParser.parse(file, cve20Handler);
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Processes the NVD CVE XML file and imports the data into the DB.
 157  
      *
 158  
      * @throws UpdateException thrown if there is an error loading the data into the database
 159  
      */
 160  
     private void processFiles() throws UpdateException {
 161  0
         String msg = String.format("Processing Started for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 162  0
         LOGGER.log(Level.INFO, msg);
 163  
         try {
 164  0
             importXML(filePair.getFirst(), filePair.getSecond());
 165  0
             cveDB.commit();
 166  0
             properties.save(filePair.getNvdCveInfo());
 167  0
         } catch (FileNotFoundException ex) {
 168  0
             throw new UpdateException(ex);
 169  0
         } catch (ParserConfigurationException ex) {
 170  0
             throw new UpdateException(ex);
 171  0
         } catch (SAXException ex) {
 172  0
             throw new UpdateException(ex);
 173  0
         } catch (IOException ex) {
 174  0
             throw new UpdateException(ex);
 175  0
         } catch (SQLException ex) {
 176  0
             throw new UpdateException(ex);
 177  0
         } catch (DatabaseException ex) {
 178  0
             throw new UpdateException(ex);
 179  0
         } catch (ClassNotFoundException ex) {
 180  0
             throw new UpdateException(ex);
 181  
         } finally {
 182  0
             filePair.cleanup();
 183  0
         }
 184  0
         msg = String.format("Processing Complete for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 185  0
         LOGGER.log(Level.INFO, msg);
 186  0
     }
 187  
 }