Class NvdCveClient
java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.NvdCveClient
- All Implemented Interfaces:
PagedDataSource<DefCveItem>,AutoCloseable,Iterator<Collection<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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe header name for the NVD API Key.private final StringThe NVD API key; can be null if a key is not used.private static final StringThe default endpoint for the NVD CVE API.private final StringThe NVD API endpoint used to call the NVD CVE API.private List<org.apache.hc.core5.http.NameValuePair>A list of filters to apply to the request.private booleanFlag indicating if the first call has been made.private final List<Future<RateLimitedCall>>The list of future responses.The map of indexes to retrieve from the NVD and their retry count.private intThe last HTTP Status Code returned by the API.private ZonedDateTimeThe last lastModified timestamp from the NVD data processed.private static final org.slf4j.LoggerReference to the logger.private final intThe maximum number of pages to retrieve from the NVD API.private final com.fasterxml.jackson.databind.ObjectMapperJackson object mapper.private intThe number of results per page.private RateLimitedClientThe rate limited HTTP client for calling the NVD APIs.private intThe total results from the NVD CVE API call.private final StringThe user agent to append to the default open-vulnerability-client's user-agent stringprivate StringThe version of the client. -
Constructor Summary
ConstructorsConstructorDescriptionNvdCveClient(String apiKey, String endpoint, int maxPageCount, int requestsPerThirtySeconds) Constructs a new NVD CVE API client.NvdCveClient(String apiKey, String endpoint, int maxPageCount, int maxRetryCount, int requestsPerThirtySeconds) Constructs a new NVD CVE API client.NvdCveClient(String apiKey, String endpoint, long delay, int maxPageCount, int maxRetryCount, HttpAsyncClientSupplier httpClientSupplier, int requestsPerThirtySeconds) Constructs a new NVD CVE API client.NvdCveClient(String apiKey, String endpoint, long delay, int maxPageCount, int maxRetryCount, HttpAsyncClientSupplier httpClientSupplier, String userAgent, int requestsPerThirtySeconds) Constructs a new NVD CVE API client.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. -
Method Summary
Modifier and TypeMethodDescriptionprivate Collection<DefCveItem>_next(int retryCount) private Future<RateLimitedCall>callApi(int startIndex) Asynchronously calls the NVD CVE API.voidclose()Cleanup allocated resources.private ZonedDateTimefindLastUpdated(ZonedDateTime lastUpdated, List<DefCveItem> list) Retrieve the latest last updated date from the list of vulnerabilities.private RateLimitedCallintReturns the last HTTP Status Code.Returns the latest updated date.intOnly available after the first call to `next()`; returns the total number of records that will be returned.booleanhasNext()Returnstrueif there are more records available; otherwisefalse.next()Retrieves the next entry.protected intAttempts to parse the resultsPerPage error message to determine the maximum number of results per page.private voidprivate void(package private) voidsetFilters(List<org.apache.hc.core5.http.NameValuePair> filters) Set the filter parameters for the NVD CVE API calls.(package private) voidsetResultsPerPage(int resultsPerPage) The number of results per page; the default is 2000.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Field Details
-
LOG
private static final org.slf4j.Logger LOGReference to the logger. -
DEFAULT_ENDPOINT
The default endpoint for the NVD CVE API.- See Also:
-
API_KEY_NAME
The header name for the NVD API Key.- See Also:
-
apiKey
The NVD API key; can be null if a key is not used. -
endpoint
The NVD API endpoint used to call the NVD CVE API. -
objectMapper
private final com.fasterxml.jackson.databind.ObjectMapper objectMapperJackson object mapper. -
userAgent
The user agent to append to the default open-vulnerability-client's user-agent string -
rlClient
The rate limited HTTP client for calling the NVD APIs. -
futures
The list of future responses. -
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 firstCallFlag indicating if the first call has been made. -
resultsPerPage
private int resultsPerPageThe number of results per page. -
totalAvailable
private int totalAvailableThe total results from the NVD CVE API call. -
maxPageCount
private final int maxPageCountThe maximum number of pages to retrieve from the NVD API. -
filters
A list of filters to apply to the request. -
lastStatusCode
private int lastStatusCodeThe last HTTP Status Code returned by the API. -
lastUpdated
The last lastModified timestamp from the NVD data processed. -
version
The version of the client.
-
-
Constructor Details
-
NvdCveClient
Constructs a new NVD CVE API client.- Parameters:
apiKey- the api key; can be nullendpoint- the endpoint for the NVD CVE API; if null the default endpoint is usedmaxPageCount- 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 nullendpoint- the endpoint for the NVD CVE API; if null the default endpoint is usedmaxPageCount- 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 nullendpoint- the endpoint for the NVD CVE API; if null the default endpoint is useddelay- 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; ifnulla default client will be usedrequestsPerThirtySeconds- 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 nullendpoint- the endpoint for the NVD CVE API; if null the default endpoint is useddelay- 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; ifnulla default client will be useduserAgent- the user agent to append to the default open-vulnerability-client's user-agent stringrequestsPerThirtySeconds- 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 nullendpoint- the endpoint for the NVD CVE API; if null the default endpoint is useddelay- 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; ifnulla 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; ifnullthe 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
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:
getLastStatusCodein interfacePagedDataSource<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:
getTotalAvailablein interfacePagedDataSource<DefCveItem>- Returns:
- the total number of records that will be returned
-
callApi
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:PagedDataSourceCleanup allocated resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacePagedDataSource<DefCveItem>
-
hasNext
public boolean hasNext()Description copied from interface:PagedDataSourceReturnstrueif there are more records available; otherwisefalse.- Specified by:
hasNextin interfaceIterator<Collection<DefCveItem>>- Specified by:
hasNextin interfacePagedDataSource<DefCveItem>- Returns:
trueif there are more records available; otherwisefalse.
-
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:
nextin interfaceIterator<Collection<DefCveItem>>- Specified by:
nextin interfacePagedDataSource<DefCveItem>- Returns:
- the next collection of CVE entries
-
_next
-
parseResultsPerPage
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
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
Description copied from interface:PagedDataSourceReturns the latest updated date.- Specified by:
getLastUpdatedin interfacePagedDataSource<DefCveItem>- Returns:
- the latest updated date
-
getCompletedFuture
-
queueUnsuccessful
private void queueUnsuccessful() -
queueCalls
private void queueCalls()
-