1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.taskdefs;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.owasp.dependencycheck.Engine;
23 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
24 import org.owasp.dependencycheck.data.update.exception.UpdateException;
25 import org.owasp.dependencycheck.utils.Downloader;
26 import org.owasp.dependencycheck.utils.InvalidSettingException;
27 import org.owasp.dependencycheck.utils.Settings;
28 import org.slf4j.impl.StaticLoggerBinder;
29
30
31
32
33
34
35
36
37
38
39 @SuppressWarnings("common-java:DuplicatedBlocks")
40 public class Update extends Purge {
41
42
43
44
45 private String nvdApiEndpoint;
46
47
48
49 private String nvdApiKey;
50
51
52
53 private Integer nvdMaxRetryCount;
54
55
56
57 private Integer nvdValidForHours;
58
59
60
61 private String nvdDatafeedUrl;
62
63
64
65 private String nvdUser;
66
67
68
69 private String nvdPassword;
70
71
72
73 private int nvdApiDelay = 0;
74
75
76
77
78 private Integer nvdApiResultsPerPage;
79
80
81
82
83 private String proxyServer;
84
85
86
87 private String proxyPort;
88
89
90
91 private String proxyUsername;
92
93
94
95 private String proxyPassword;
96
97
98
99 private String nonProxyHosts;
100
101
102
103 private String connectionTimeout;
104
105
106
107 private String readTimeout;
108
109
110
111 private String databaseDriverName;
112
113
114
115 private String databaseDriverPath;
116
117
118
119 private String connectionString;
120
121
122
123 private String databaseUser;
124
125
126
127 private String databasePassword;
128
129
130
131
132 private Integer hostedSuppressionsValidForHours;
133
134
135
136
137 private Boolean hostedSuppressionsForceUpdate;
138
139
140
141 private Boolean hostedSuppressionsEnabled;
142
143
144
145
146 public Update() {
147 super();
148
149
150 StaticLoggerBinder.getSingleton().setTask(this);
151 }
152
153
154
155
156
157
158 public String getNvdApiEndpoint() {
159 return nvdApiEndpoint;
160 }
161
162
163
164
165
166
167 public void setNvdApiEndpoint(String nvdApiEndpoint) {
168 this.nvdApiEndpoint = nvdApiEndpoint;
169 }
170
171
172
173
174
175
176 public String getNvdApiKey() {
177 return nvdApiKey;
178 }
179
180
181
182
183
184
185 public void setNvdApiKey(String nvdApiKey) {
186 this.nvdApiKey = nvdApiKey;
187 }
188
189
190
191
192
193
194 public int getNvdMaxRetryCounts() {
195 return nvdMaxRetryCount;
196 }
197
198
199
200
201
202
203 public void setNvdMaxRetryCount(int nvdMaxRetryCount) {
204 this.nvdMaxRetryCount = nvdMaxRetryCount;
205 }
206
207
208
209
210
211
212 public int getNvdValidForHours() {
213 return nvdValidForHours;
214 }
215
216
217
218
219
220
221 public void setNvdValidForHours(int nvdValidForHours) {
222 this.nvdValidForHours = nvdValidForHours;
223 }
224
225
226
227
228
229
230 public String getNvdDatafeedUrl() {
231 return nvdDatafeedUrl;
232 }
233
234
235
236
237
238
239 public void setNvdDatafeedUrl(String nvdDatafeedUrl) {
240 this.nvdDatafeedUrl = nvdDatafeedUrl;
241 }
242
243
244
245
246
247
248 public String getNvdUser() {
249 return nvdUser;
250 }
251
252
253
254
255
256
257 public void setNvdUser(String nvdUser) {
258 this.nvdUser = nvdUser;
259 }
260
261
262
263
264
265
266 public String getNvdPassword() {
267 return nvdPassword;
268 }
269
270
271
272
273
274
275 public void setNvdPassword(String nvdPassword) {
276 this.nvdPassword = nvdPassword;
277 }
278
279
280
281
282
283
284 public int getNvdApiDelay() {
285 return nvdApiDelay;
286 }
287
288
289
290
291
292
293 public void setNvdApiDelay(int nvdApiDelay) {
294 this.nvdApiDelay = nvdApiDelay;
295 }
296
297
298
299
300
301
302 public int getNvdApiResultsPerPage() {
303 return nvdApiResultsPerPage;
304 }
305
306
307
308
309
310
311 public void setApiResultsPerPage(int nvdApiResultsPerPage) {
312 this.nvdApiResultsPerPage = nvdApiResultsPerPage;
313 }
314
315
316
317
318
319
320 public String getProxyServer() {
321 return proxyServer;
322 }
323
324
325
326
327
328
329 public void setProxyServer(String server) {
330 this.proxyServer = server;
331 }
332
333
334
335
336
337
338 public String getProxyPort() {
339 return proxyPort;
340 }
341
342
343
344
345
346
347 public void setProxyPort(String proxyPort) {
348 this.proxyPort = proxyPort;
349 }
350
351
352
353
354
355
356 public String getProxyUsername() {
357 return proxyUsername;
358 }
359
360
361
362
363
364
365 public void setProxyUsername(String proxyUsername) {
366 this.proxyUsername = proxyUsername;
367 }
368
369
370
371
372
373
374 public String getProxyPassword() {
375 return proxyPassword;
376 }
377
378
379
380
381
382
383 public void setProxyPassword(String proxyPassword) {
384 this.proxyPassword = proxyPassword;
385 }
386
387
388
389
390
391
392 public String getNonProxyHosts() {
393 return nonProxyHosts;
394 }
395
396
397
398
399
400
401 public void setNonProxyHosts(String nonProxyHosts) {
402 this.nonProxyHosts = nonProxyHosts;
403 }
404
405
406
407
408
409
410 public String getConnectionTimeout() {
411 return connectionTimeout;
412 }
413
414
415
416
417
418
419 public void setConnectionTimeout(String connectionTimeout) {
420 this.connectionTimeout = connectionTimeout;
421 }
422
423
424
425
426
427
428 public String getReadTimeout() {
429 return readTimeout;
430 }
431
432
433
434
435
436
437 public void setReadTimeout(String readTimeout) {
438 this.readTimeout = readTimeout;
439 }
440
441
442
443
444
445
446 public String getDatabaseDriverName() {
447 return databaseDriverName;
448 }
449
450
451
452
453
454
455 public void setDatabaseDriverName(String databaseDriverName) {
456 this.databaseDriverName = databaseDriverName;
457 }
458
459
460
461
462
463
464 public String getDatabaseDriverPath() {
465 return databaseDriverPath;
466 }
467
468
469
470
471
472
473 public void setDatabaseDriverPath(String databaseDriverPath) {
474 this.databaseDriverPath = databaseDriverPath;
475 }
476
477
478
479
480
481
482 public String getConnectionString() {
483 return connectionString;
484 }
485
486
487
488
489
490
491 public void setConnectionString(String connectionString) {
492 this.connectionString = connectionString;
493 }
494
495
496
497
498
499
500 public String getDatabaseUser() {
501 return databaseUser;
502 }
503
504
505
506
507
508
509 public void setDatabaseUser(String databaseUser) {
510 this.databaseUser = databaseUser;
511 }
512
513
514
515
516
517
518 public String getDatabasePassword() {
519 return databasePassword;
520 }
521
522
523
524
525
526
527 public void setDatabasePassword(String databasePassword) {
528 this.databasePassword = databasePassword;
529 }
530
531
532
533
534
535
536 public Integer getHostedSuppressionsValidForHours() {
537 return hostedSuppressionsValidForHours;
538 }
539
540
541
542
543
544
545
546 public void setHostedSuppressionsValidForHours(final Integer hostedSuppressionsValidForHours) {
547 this.hostedSuppressionsValidForHours = hostedSuppressionsValidForHours;
548 }
549
550
551
552
553
554
555 public Boolean isHostedSuppressionsForceUpdate() {
556 return hostedSuppressionsForceUpdate;
557 }
558
559
560
561
562
563
564
565 public void setHostedSuppressionsForceUpdate(final Boolean hostedSuppressionsForceUpdate) {
566 this.hostedSuppressionsForceUpdate = hostedSuppressionsForceUpdate;
567 }
568
569
570
571
572
573
574 public Boolean isHostedSuppressionsEnabled() {
575 return hostedSuppressionsEnabled;
576 }
577
578
579
580
581
582
583 public void setHostedSuppressionsEnabled(Boolean hostedSuppressionsEnabled) {
584 this.hostedSuppressionsEnabled = hostedSuppressionsEnabled;
585 }
586
587
588
589
590
591
592
593
594
595 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
596 @Override
597 protected void executeWithContextClassloader() throws BuildException {
598 populateSettings();
599 try {
600 Downloader.getInstance().configure(getSettings());
601 } catch (InvalidSettingException e) {
602 throw new BuildException(e);
603 }
604 try (Engine engine = new Engine(Update.class.getClassLoader(), getSettings())) {
605 engine.doUpdates();
606 } catch (UpdateException ex) {
607 if (this.isFailOnError()) {
608 throw new BuildException(ex);
609 }
610 log(ex.getMessage(), Project.MSG_ERR);
611 } catch (DatabaseException ex) {
612 final String msg = "Unable to connect to the dependency-check database; unable to update the NVD data";
613 if (this.isFailOnError()) {
614 throw new BuildException(msg, ex);
615 }
616 log(msg, Project.MSG_ERR);
617 } finally {
618 getSettings().cleanup();
619 }
620 }
621
622
623
624
625
626
627
628
629
630 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
631 @Override
632 protected void populateSettings() throws BuildException {
633 super.populateSettings();
634 getSettings().setStringIfNotEmpty(Settings.KEYS.PROXY_SERVER, proxyServer);
635 getSettings().setStringIfNotEmpty(Settings.KEYS.PROXY_PORT, proxyPort);
636 getSettings().setStringIfNotEmpty(Settings.KEYS.PROXY_USERNAME, proxyUsername);
637 getSettings().setStringIfNotEmpty(Settings.KEYS.PROXY_PASSWORD, proxyPassword);
638 getSettings().setStringIfNotEmpty(Settings.KEYS.PROXY_NON_PROXY_HOSTS, nonProxyHosts);
639 getSettings().setStringIfNotEmpty(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
640 getSettings().setStringIfNotEmpty(Settings.KEYS.CONNECTION_READ_TIMEOUT, readTimeout);
641 getSettings().setStringIfNotEmpty(Settings.KEYS.DB_DRIVER_NAME, databaseDriverName);
642 getSettings().setStringIfNotEmpty(Settings.KEYS.DB_DRIVER_PATH, databaseDriverPath);
643 getSettings().setStringIfNotEmpty(Settings.KEYS.DB_CONNECTION_STRING, connectionString);
644 getSettings().setStringIfNotEmpty(Settings.KEYS.DB_USER, databaseUser);
645 getSettings().setStringIfNotEmpty(Settings.KEYS.DB_PASSWORD, databasePassword);
646 getSettings().setIntIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_VALID_FOR_HOURS, hostedSuppressionsValidForHours);
647 getSettings().setBooleanIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_FORCEUPDATE, hostedSuppressionsForceUpdate);
648 getSettings().setBooleanIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_ENABLED, hostedSuppressionsEnabled);
649
650 getSettings().setStringIfNotEmpty(Settings.KEYS.NVD_API_KEY, nvdApiKey);
651 getSettings().setStringIfNotEmpty(Settings.KEYS.NVD_API_ENDPOINT, nvdApiEndpoint);
652 getSettings().setIntIfNotNull(Settings.KEYS.NVD_API_DELAY, nvdApiDelay);
653 getSettings().setIntIfNotNull(Settings.KEYS.NVD_API_RESULTS_PER_PAGE, nvdApiResultsPerPage);
654 getSettings().setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_URL, nvdDatafeedUrl);
655 getSettings().setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_USER, nvdUser);
656 getSettings().setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_PASSWORD, nvdPassword);
657 if (nvdMaxRetryCount != null) {
658 if (nvdMaxRetryCount > 0) {
659 getSettings().setInt(Settings.KEYS.NVD_API_MAX_RETRY_COUNT, nvdMaxRetryCount);
660 } else {
661 throw new BuildException("Invalid setting: `nvdMaxRetryCount` must be greater than zero");
662 }
663 }
664 if (nvdValidForHours != null) {
665 if (nvdValidForHours >= 0) {
666 getSettings().setInt(Settings.KEYS.NVD_API_VALID_FOR_HOURS, nvdValidForHours);
667 } else {
668 throw new BuildException("Invalid setting: `nvdValidForHours` must be 0 or greater");
669 }
670 }
671 }
672 }