|
| 1 | +package org.dependencytrack.vulndb.source.euvd; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 6 | +import io.github.resilience4j.retry.Retry; |
| 7 | +import io.github.resilience4j.retry.RetryConfig; |
| 8 | +import io.github.resilience4j.retry.RetryRegistry; |
| 9 | +import org.dependencytrack.vulndb.api.Database; |
| 10 | +import org.dependencytrack.vulndb.api.Importer; |
| 11 | +import org.dependencytrack.vulndb.api.Rating; |
| 12 | +import org.dependencytrack.vulndb.api.Reference; |
| 13 | +import org.dependencytrack.vulndb.api.Source; |
| 14 | +import org.dependencytrack.vulndb.api.Vulnerability; |
| 15 | +import org.metaeffekt.core.security.cvss.CvssVector; |
| 16 | +import org.metaeffekt.core.security.cvss.v2.Cvss2; |
| 17 | +import org.metaeffekt.core.security.cvss.v3.Cvss3; |
| 18 | +import org.metaeffekt.core.security.cvss.v4P0.Cvss4P0; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.slf4j.MDC; |
| 22 | + |
| 23 | +import java.net.URI; |
| 24 | +import java.net.http.HttpClient; |
| 25 | +import java.net.http.HttpRequest; |
| 26 | +import java.net.http.HttpResponse; |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static io.github.resilience4j.core.IntervalFunction.ofExponentialRandomBackoff; |
| 31 | + |
| 32 | +public final class EuvdImporter implements Importer { |
| 33 | + |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(EuvdImporter.class); |
| 35 | + |
| 36 | + private Database database; |
| 37 | + private HttpClient httpClient; |
| 38 | + private ObjectMapper objectMapper; |
| 39 | + private Retry retry; |
| 40 | + |
| 41 | + @Override |
| 42 | + public Source source() { |
| 43 | + return new Source("euvd", "European Union Vulnerability Database", null, "https://euvd.enisa.europa.eu/"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void init(final Database database) { |
| 48 | + this.database = database; |
| 49 | + this.httpClient = HttpClient.newHttpClient(); |
| 50 | + this.objectMapper = new ObjectMapper() |
| 51 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 52 | + .registerModule(new JavaTimeModule()); |
| 53 | + this.retry = RetryRegistry.of( |
| 54 | + RetryConfig.<HttpResponse<?>>custom() |
| 55 | + .retryOnResult(response -> response.statusCode() == 403) |
| 56 | + .intervalFunction(ofExponentialRandomBackoff( |
| 57 | + Duration.ofSeconds(15), |
| 58 | + /* multiplier */ 2.0, |
| 59 | + /* randomizationFactor */ 0.5, |
| 60 | + Duration.ofMinutes(3))) |
| 61 | + .maxAttempts(12) |
| 62 | + .build()) |
| 63 | + .retry("euvd-api"); |
| 64 | + this.retry.getEventPublisher().onRetry( |
| 65 | + event -> LOGGER.warn( |
| 66 | + "Performing retry attempt {} in {}", |
| 67 | + event.getNumberOfRetryAttempts(), |
| 68 | + event.getWaitInterval())); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void runImport() throws Exception { |
| 73 | + boolean hasMore; |
| 74 | + int pageNumber = 0; |
| 75 | + int vulnsImported = 0; |
| 76 | + do { |
| 77 | + final HttpResponse<byte[]> response = retry.executeCallable( |
| 78 | + () -> httpClient.send( |
| 79 | + HttpRequest.newBuilder(URI.create( |
| 80 | + "https://euvdservices.enisa.europa.eu/api/search?size=100&page=%d".formatted(pageNumber))) |
| 81 | + .GET() |
| 82 | + .header("User-Agent", "github.com/DependencyTrack/vuln-db") |
| 83 | + .build(), |
| 84 | + HttpResponse.BodyHandlers.ofByteArray())); |
| 85 | + if (response.statusCode() != 200) { |
| 86 | + throw new Exception("Unexpected response code: " + response.statusCode()); |
| 87 | + } |
| 88 | + |
| 89 | + final var vulnsPage = objectMapper.readValue(response.body(), EuvdVulnerabilitiesPage.class); |
| 90 | + final List<Vulnerability> vulns = vulnsPage.items().stream() |
| 91 | + .map(euvdVuln -> { |
| 92 | + try (var ignored = MDC.putCloseable("vulnId", euvdVuln.id())) { |
| 93 | + return convert(euvdVuln); |
| 94 | + } |
| 95 | + }) |
| 96 | + .toList(); |
| 97 | + |
| 98 | + database.storeVulnerabilities(vulns); |
| 99 | + |
| 100 | + vulnsImported += vulns.size(); |
| 101 | + hasMore = vulnsImported < vulnsPage.total(); |
| 102 | + LOGGER.info("Imported {}/{} vulnerabilities", vulnsImported, vulnsPage.total()); |
| 103 | + } while (hasMore); |
| 104 | + } |
| 105 | + |
| 106 | + private Vulnerability convert(final EuvdVulnerability euvdVuln) { |
| 107 | + return new Vulnerability( |
| 108 | + euvdVuln.id(), |
| 109 | + euvdVuln.aliases(), |
| 110 | + /* related */ null, |
| 111 | + euvdVuln.description(), |
| 112 | + /* cwes */ null, |
| 113 | + getRatings(euvdVuln), |
| 114 | + euvdVuln.references() != null |
| 115 | + ? euvdVuln.references().stream().map(referenceUrl -> new Reference(referenceUrl, null)).toList() |
| 116 | + : null, |
| 117 | + /* matchingCriteria */ null, |
| 118 | + /* createdAt */ null, |
| 119 | + euvdVuln.datePublished() != null ? euvdVuln.datePublished().toInstant() : null, |
| 120 | + euvdVuln.dateUpdated() != null ? euvdVuln.dateUpdated().toInstant() : null, |
| 121 | + /* rejectedAt */ null); |
| 122 | + } |
| 123 | + |
| 124 | + private List<Rating> getRatings(final EuvdVulnerability euvdVuln) { |
| 125 | + if (euvdVuln.baseScoreVector() == null) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + final CvssVector vector = CvssVector.parseVector(euvdVuln.baseScoreVector()); |
| 130 | + if (vector != null) { |
| 131 | + final Rating.Method method = switch (vector) { |
| 132 | + case Cvss2 ignored -> Rating.Method.CVSSv2; |
| 133 | + case Cvss3 ignored -> Rating.Method.CVSSv3; |
| 134 | + case Cvss4P0 ignored -> Rating.Method.CVSSv4; |
| 135 | + default -> null; |
| 136 | + }; |
| 137 | + if (method == null) { |
| 138 | + LOGGER.warn("Unexpected CVSS type {}", vector.getClass().getName()); |
| 139 | + } |
| 140 | + |
| 141 | + return List.of( |
| 142 | + new Rating( |
| 143 | + method, |
| 144 | + Rating.Severity.ofCvss(vector), |
| 145 | + vector.toString(), |
| 146 | + vector.getBaseScore())); |
| 147 | + } else { |
| 148 | + LOGGER.warn("Failed to parse CVSS vector {}", euvdVuln.baseScoreVector()); |
| 149 | + } |
| 150 | + |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments