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