| 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 java.io.File; |
| 22 | |
import java.io.FileNotFoundException; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.sql.SQLException; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.Map; |
| 27 | |
import java.util.concurrent.Callable; |
| 28 | |
import java.util.logging.Level; |
| 29 | |
import java.util.logging.Logger; |
| 30 | |
import javax.xml.parsers.ParserConfigurationException; |
| 31 | |
import javax.xml.parsers.SAXParser; |
| 32 | |
import javax.xml.parsers.SAXParserFactory; |
| 33 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 34 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 35 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 36 | |
import org.owasp.dependencycheck.data.nvdcve.NvdCve12Handler; |
| 37 | |
import org.owasp.dependencycheck.data.nvdcve.NvdCve20Handler; |
| 38 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 39 | |
import org.xml.sax.SAXException; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public class ProcessTask implements Callable<ProcessTask> { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | private UpdateException exception = null; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public UpdateException getException() { |
| 60 | 0 | return exception; |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public void setException(UpdateException exception) { |
| 69 | 0 | this.exception = exception; |
| 70 | 0 | } |
| 71 | |
private final CveDB cveDB; |
| 72 | |
private final CallableDownloadTask filePair; |
| 73 | |
private final DataStoreMetaInfo properties; |
| 74 | |
|
| 75 | 0 | public ProcessTask(final CveDB cveDB, final DataStoreMetaInfo properties, final CallableDownloadTask filePair) { |
| 76 | 0 | this.cveDB = cveDB; |
| 77 | 0 | this.filePair = filePair; |
| 78 | 0 | this.properties = properties; |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public ProcessTask call() throws Exception { |
| 83 | |
try { |
| 84 | 0 | processFiles(); |
| 85 | 0 | } catch (UpdateException ex) { |
| 86 | 0 | this.exception = ex; |
| 87 | 0 | } |
| 88 | 0 | return this; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
protected void importXML(File file, File oldVersion) throws ParserConfigurationException, |
| 106 | |
SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException { |
| 107 | |
|
| 108 | 0 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 109 | 0 | final SAXParser saxParser = factory.newSAXParser(); |
| 110 | |
|
| 111 | 0 | final NvdCve12Handler cve12Handler = new NvdCve12Handler(); |
| 112 | 0 | saxParser.parse(oldVersion, cve12Handler); |
| 113 | 0 | final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities(); |
| 114 | |
|
| 115 | 0 | final NvdCve20Handler cve20Handler = new NvdCve20Handler(); |
| 116 | 0 | cve20Handler.setCveDB(cveDB); |
| 117 | 0 | cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap); |
| 118 | 0 | saxParser.parse(file, cve20Handler); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
private void processFiles() throws UpdateException { |
| 122 | 0 | String msg = String.format("Processing Started for NVD CVE - %s", filePair.getNvdCveInfo().getId()); |
| 123 | 0 | Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, msg); |
| 124 | |
try { |
| 125 | 0 | importXML(filePair.getFirst(), filePair.getSecond()); |
| 126 | 0 | cveDB.commit(); |
| 127 | 0 | properties.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(StandardUpdate.class.getName()).log(Level.INFO, msg); |
| 147 | 0 | } |
| 148 | |
} |