| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.update.task; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.net.URL; |
| 23 | |
import java.util.concurrent.Callable; |
| 24 | |
import java.util.concurrent.ExecutorService; |
| 25 | |
import java.util.concurrent.Future; |
| 26 | |
import java.util.logging.Level; |
| 27 | |
import java.util.logging.Logger; |
| 28 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 29 | |
import org.owasp.dependencycheck.data.update.NvdCveInfo; |
| 30 | |
import org.owasp.dependencycheck.data.update.exception.UpdateException; |
| 31 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 32 | |
import org.owasp.dependencycheck.utils.Downloader; |
| 33 | |
import org.owasp.dependencycheck.utils.Settings; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | public class CallableDownloadTask implements Callable<Future<ProcessTask>> { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | private static final Logger LOGGER = Logger.getLogger(CallableDownloadTask.class.getName()); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public CallableDownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException { |
| 58 | 0 | this.nvdCveInfo = nvdCveInfo; |
| 59 | 0 | this.processorService = processor; |
| 60 | 0 | this.cveDB = cveDB; |
| 61 | 0 | this.settings = settings; |
| 62 | |
|
| 63 | |
final File file1; |
| 64 | |
final File file2; |
| 65 | |
|
| 66 | |
try { |
| 67 | 0 | file1 = File.createTempFile("cve" + nvdCveInfo.getId() + "_", ".xml", Settings.getTempDirectory()); |
| 68 | 0 | file2 = File.createTempFile("cve_1_2_" + nvdCveInfo.getId() + "_", ".xml", Settings.getTempDirectory()); |
| 69 | 0 | } catch (IOException ex) { |
| 70 | 0 | throw new UpdateException("Unable to create temporary files", ex); |
| 71 | 0 | } |
| 72 | 0 | this.first = file1; |
| 73 | 0 | this.second = file2; |
| 74 | |
|
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
private CveDB cveDB; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private ExecutorService processorService; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
private NvdCveInfo nvdCveInfo; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private Settings settings; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public NvdCveInfo getNvdCveInfo() { |
| 99 | |
return nvdCveInfo; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public void setNvdCveInfo(NvdCveInfo nvdCveInfo) { |
| 108 | |
this.nvdCveInfo = nvdCveInfo; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
private File first; |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public File getFirst() { |
| 121 | |
return first; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public void setFirst(File first) { |
| 130 | |
this.first = first; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
private File second; |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public File getSecond() { |
| 143 | |
return second; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
public void setSecond(File second) { |
| 152 | |
this.second = second; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | 0 | private Exception exception = null; |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public Exception getException() { |
| 165 | |
return exception; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public boolean hasException() { |
| 174 | 0 | return exception != null; |
| 175 | |
} |
| 176 | |
|
| 177 | |
@Override |
| 178 | |
public Future<ProcessTask> call() throws Exception { |
| 179 | |
try { |
| 180 | 0 | Settings.setInstance(settings); |
| 181 | 0 | final URL url1 = new URL(nvdCveInfo.getUrl()); |
| 182 | 0 | final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl()); |
| 183 | 0 | String msg = String.format("Download Started for NVD CVE - %s", nvdCveInfo.getId()); |
| 184 | 0 | LOGGER.log(Level.INFO, msg); |
| 185 | |
try { |
| 186 | 0 | Downloader.fetchFile(url1, first); |
| 187 | 0 | Downloader.fetchFile(url2, second); |
| 188 | 0 | } catch (DownloadFailedException ex) { |
| 189 | 0 | msg = String.format("Download Failed for NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 190 | 0 | LOGGER.log(Level.WARNING, msg); |
| 191 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 192 | 0 | return null; |
| 193 | 0 | } |
| 194 | |
|
| 195 | 0 | msg = String.format("Download Complete for NVD CVE - %s", nvdCveInfo.getId()); |
| 196 | 0 | LOGGER.log(Level.INFO, msg); |
| 197 | |
|
| 198 | 0 | final ProcessTask task = new ProcessTask(cveDB, this, settings); |
| 199 | 0 | return this.processorService.submit(task); |
| 200 | |
|
| 201 | 0 | } catch (Throwable ex) { |
| 202 | 0 | final String msg = String.format("An exception occurred downloading NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 203 | 0 | LOGGER.log(Level.WARNING, msg); |
| 204 | 0 | LOGGER.log(Level.FINE, "Download Task Failed", ex); |
| 205 | |
} finally { |
| 206 | 0 | Settings.cleanup(false); |
| 207 | 0 | } |
| 208 | 0 | return null; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public void cleanup() { |
| 215 | 0 | boolean deleted = false; |
| 216 | |
try { |
| 217 | 0 | if (first != null && first.exists()) { |
| 218 | 0 | deleted = first.delete(); |
| 219 | |
} |
| 220 | |
} finally { |
| 221 | 0 | if (first != null && (first.exists() || !deleted)) { |
| 222 | 0 | first.deleteOnExit(); |
| 223 | |
} |
| 224 | |
} |
| 225 | |
try { |
| 226 | 0 | deleted = false; |
| 227 | 0 | if (second != null && second.exists()) { |
| 228 | 0 | deleted = second.delete(); |
| 229 | |
} |
| 230 | |
} finally { |
| 231 | 0 | if (second != null && (second.exists() || !deleted)) { |
| 232 | 0 | second.deleteOnExit(); |
| 233 | |
} |
| 234 | |
} |
| 235 | 0 | } |
| 236 | |
} |