java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.NvdCveClient
All Implemented Interfaces:
PagedDataSource<DefCveItem>, AutoCloseable, Iterator<Collection<DefCveItem>>

public class NvdCveClient extends Object implements PagedDataSource<DefCveItem>
A simple client for the NVD CVE API. Use the NvdCveClientBuilder with the desired filters to build the client and then iterate over the results:
 try (NvdCveClient api = NvdCveClientBuilder.aNvdCveApi().build()) {
     while (api.hasNext()) {
         Collection<DefCveItem> items = api.next();
     }
 }
 
See Also:
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
      Reference to the logger.
    • DEFAULT_ENDPOINT

      private static final String DEFAULT_ENDPOINT
      The default endpoint for the NVD CVE API.
      See Also:
    • API_KEY_NAME

      private static final String API_KEY_NAME
      The header name for the NVD API Key.
      See Also:
    • apiKey

      private final String apiKey
      The NVD API key; can be null if a key is not used.
    • endpoint

      private final String endpoint
      The NVD API endpoint used to call the NVD CVE API.
    • objectMapper

      private final com.fasterxml.jackson.databind.ObjectMapper objectMapper
      Jackson object mapper.
    • userAgent

      private final String userAgent
      The user agent to append to the default open-vulnerability-client's user-agent string
    • rlClient

      private RateLimitedClient rlClient
      The rate limited HTTP client for calling the NVD APIs.
    • futures

      private final List<Future<RateLimitedCall>> futures
      The list of future responses.
    • indexesToRetrieve

      private final Map<Integer,Integer> indexesToRetrieve
      The map of indexes to retrieve from the NVD and their retry count. This is used to retry when failures have occurred on a single index.
    • firstCall

      private boolean firstCall
      Flag indicating if the first call has been made.
    • resultsPerPage

      private int resultsPerPage
      The number of results per page.
    • totalAvailable

      private int totalAvailable
      The total results from the NVD CVE API call.
    • maxPageCount

      private final int maxPageCount
      The maximum number of pages to retrieve from the NVD API.
    • filters

      private List<org.apache.hc.core5.http.NameValuePair> filters
      A list of filters to apply to the request.
    • lastStatusCode

      private int lastStatusCode
      The last HTTP Status Code returned by the API.
    • lastUpdated

      private ZonedDateTime lastUpdated
      The last lastModified timestamp from the NVD data processed.
    • version

      private String version
      The version of the client.
  • Constructor Details

    • NvdCveClient

      NvdCveClient(String apiKey, String endpoint, int maxPageCount, int requestsPerThirtySeconds)
      Constructs a new NVD CVE API client.
      Parameters:
      apiKey - the api key; can be null
      endpoint - the endpoint for the NVD CVE API; if null the default endpoint is used
      maxPageCount - the maximum number of pages to retrieve from the NVD API.
      requestsPerThirtySeconds - the number of requests per 30-seconds allowed. The NVD's published rate limits are 5 requests per 30-seconds without an API Key and 50 requests per 30-seconds with an API Key.
    • NvdCveClient

      NvdCveClient(String apiKey, String endpoint, int maxPageCount, int maxRetryCount, int requestsPerThirtySeconds)
      Constructs a new NVD CVE API client.
      Parameters:
      apiKey - the api key; can be null
      endpoint - the endpoint for the NVD CVE API; if null the default endpoint is used
      maxPageCount - the maximum number of pages to retrieve from the NVD API.
      maxRetryCount - the maximum number of retries for 503 and 429 status code responses.
      requestsPerThirtySeconds - the number of requests per 30-seconds allowed. The NVD's published rate limits are 5 requests per 30-seconds without an API Key and 50 requests per 30-seconds with an API Key.
    • NvdCveClient

      NvdCveClient(String apiKey, String endpoint, long delay, int maxPageCount, int maxRetryCount, HttpAsyncClientSupplier httpClientSupplier, int requestsPerThirtySeconds)
      Constructs a new NVD CVE API client.
      Parameters:
      apiKey - the api key; can be null
      endpoint - the endpoint for the NVD CVE API; if null the default endpoint is used
      delay - the delay in milliseconds between API calls on a single thread.
      maxPageCount - the maximum number of pages to retrieve from the NVD API.
      maxRetryCount - the maximum number of retries for 503 and 429 status code responses.
      httpClientSupplier - supplier for custom HTTP clients; if null a default client will be used
      requestsPerThirtySeconds - the number of requests per 30-seconds allowed. The NVD's published rate limits are 5 requests per 30-seconds without an API Key and 50 requests per 30-seconds with an API Key.
    • NvdCveClient

      NvdCveClient(String apiKey, String endpoint, long delay, int maxPageCount, int maxRetryCount, HttpAsyncClientSupplier httpClientSupplier, String userAgent, int requestsPerThirtySeconds)
      Constructs a new NVD CVE API client.
      Parameters:
      apiKey - the api key; can be null
      endpoint - the endpoint for the NVD CVE API; if null the default endpoint is used
      delay - the delay in milliseconds between API calls on a single thread.
      maxPageCount - the maximum number of pages to retrieve from the NVD API.
      maxRetryCount - the maximum number of retries for 503 and 429 status code responses.
      httpClientSupplier - supplier for custom HTTP clients; if null a default client will be used
      userAgent - the user agent to append to the default open-vulnerability-client's user-agent string
      requestsPerThirtySeconds - the number of requests per 30-seconds allowed. The NVD's published rate limits are 5 requests per 30-seconds without an API Key and 50 requests per 30-seconds with an API Key.
    • NvdCveClient

      NvdCveClient(String apiKey, String endpoint, long delay, int maxPageCount, int maxRetryCount, HttpAsyncClientSupplier httpClientSupplier, String userAgent, Path cacheDirectory, int requestsPerThirtySeconds)
      Constructs a new NVD CVE API client.
      Parameters:
      apiKey - the api key; can be null
      endpoint - the endpoint for the NVD CVE API; if null the default endpoint is used
      delay - the delay in milliseconds between API calls on a single thread.
      maxPageCount - the maximum number of pages to retrieve from the NVD API.
      maxRetryCount - the maximum number of retries for 503 and 429 status code responses.
      httpClientSupplier - supplier for custom HTTP clients; if null a default client will be used.
      userAgent - the user agent to append to the default open-vulnerability-client's user-agent string.
      cacheDirectory - the directory to store the NVD API HTTP response cache; if null the requests will not be cached.
      requestsPerThirtySeconds - the number of requests per 30-seconds allowed. The NVD's published rate limits are 5 requests per 30-seconds without an API Key and 50 requests per 30-seconds with an API Key.
  • Method Details

    • setFilters

      void setFilters(List<org.apache.hc.core5.http.NameValuePair> filters)
      Set the filter parameters for the NVD CVE API calls.
      Parameters:
      filters - the list of parameters used to filter the results in the API call
    • setResultsPerPage

      void setResultsPerPage(int resultsPerPage)
      The number of results per page; the default is 2000.
      Parameters:
      resultsPerPage - the number of results per page
    • getLastStatusCode

      public int getLastStatusCode()
      Returns the last HTTP Status Code.
      Specified by:
      getLastStatusCode in interface PagedDataSource<DefCveItem>
      Returns:
      the last HTTP Status Code
    • getTotalAvailable

      public int getTotalAvailable()
      Only available after the first call to `next()`; returns the total number of records that will be returned.
      Specified by:
      getTotalAvailable in interface PagedDataSource<DefCveItem>
      Returns:
      the total number of records that will be returned
    • callApi

      private Future<RateLimitedCall> callApi(int startIndex) throws NvdApiException
      Asynchronously calls the NVD CVE API.
      Parameters:
      startIndex - the start index to request
      Returns:
      the future
      Throws:
      NvdApiException - thrown if there is a problem calling the API
    • close

      public void close()
      Description copied from interface: PagedDataSource
      Cleanup allocated resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface PagedDataSource<DefCveItem>
    • hasNext

      public boolean hasNext()
      Description copied from interface: PagedDataSource
      Returns true if there are more records available; otherwise false.
      Specified by:
      hasNext in interface Iterator<Collection<DefCveItem>>
      Specified by:
      hasNext in interface PagedDataSource<DefCveItem>
      Returns:
      true if there are more records available; otherwise false.
    • next

      public Collection<DefCveItem> next()

      Retrieves the next entry. Note that even if `hasNext()` returns true it is possible that `next()` will return null. This will generally only occur on the very first call.

      Specified by:
      next in interface Iterator<Collection<DefCveItem>>
      Specified by:
      next in interface PagedDataSource<DefCveItem>
      Returns:
      the next collection of CVE entries
    • _next

      private Collection<DefCveItem> _next(int retryCount)
    • parseResultsPerPage

      protected int parseResultsPerPage(String msg)
      Attempts to parse the resultsPerPage error message to determine the maximum number of results per page.
      Parameters:
      msg - the error message from the NVD
      Returns:
      the parsed results per page if succesful; otherwise the previously confiuged results per page
    • findLastUpdated

      private ZonedDateTime findLastUpdated(ZonedDateTime lastUpdated, List<DefCveItem> list)
      Retrieve the latest last updated date from the list of vulnerabilities.
      Parameters:
      lastUpdated - the last updated date.
      list - the list of vulnerabilities.
      Returns:
      the latest last modified date.
    • getLastUpdated

      public ZonedDateTime getLastUpdated()
      Description copied from interface: PagedDataSource
      Returns the latest updated date.
      Specified by:
      getLastUpdated in interface PagedDataSource<DefCveItem>
      Returns:
      the latest updated date
    • getCompletedFuture

      private RateLimitedCall getCompletedFuture() throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException
    • queueUnsuccessful

      private void queueUnsuccessful()
    • queueCalls

      private void queueCalls()