Class GitHubSecurityAdvisoryClient
java.lang.Object
io.github.jeremylong.openvulnerability.client.ghsa.GitHubSecurityAdvisoryClient
- All Implemented Interfaces:
PagedDataSource<SecurityAdvisory>,AutoCloseable,Iterator<Collection<SecurityAdvisory>>
public class GitHubSecurityAdvisoryClient
extends Object
implements PagedDataSource<SecurityAdvisory>
Client for accessing the GitHub Security Advisory API.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe mustache template to retrieve advisories.private final com.samskivert.mustache.TemplateTemplate for security advisories.private StringThe classification of the advisory ("GENERAL", "MALWARE")private static final StringThe mustache template to retrieve advisory CWEs.private final com.samskivert.mustache.TemplateTemplate for security advisory's CWEs - in case we have more than 50.private final StringThe GitHub GraphQL endpoint.private booleanHas the first HTTP call been made?private Future<org.apache.hc.client5.http.async.methods.SimpleHttpResponse>Asynchronous future HTTP Response.static final StringThe GitHub GraphQL endpoint URL.private final StringThe GitHub Access Token.private final GithubRateLimitedAsyncClientHTTP Client to access the API.private intThe last HTTP status code received.private ZonedDateTimeThe last updated date time value retrieved.private static final org.slf4j.LoggerReference to the logger.private final com.fasterxml.jackson.databind.ObjectMapperJackson JSON object mapper.private StringThe publishedSince filter.private intThe number of available entries; only available after the first call to `next()`.private intThe total count of advisories retrieved.private StringThe updatedSince filter.private final StringThe user agent to append to the default open-vulnerability-client's user-agent stringprivate StringThe version of the client.private static final StringThe mustache template to retrieve advisory vulnerabilities.private final com.samskivert.mustache.TemplateTemplate for security advisory's vulnerabilities - in case we have more than 100. -
Constructor Summary
ConstructorsConstructorDescriptionGitHubSecurityAdvisoryClient(String githubToken) Constructs a new client.GitHubSecurityAdvisoryClient(String githubToken, String endpoint) Constructs a new client.GitHubSecurityAdvisoryClient(String githubToken, String endpoint, HttpAsyncClientSupplier httpClientSupplier) Constructs a new client.GitHubSecurityAdvisoryClient(String githubToken, String endpoint, HttpAsyncClientSupplier httpClientSupplier, String userAgent) Constructs a new client. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Cleanup allocated resources.private voidensureSubPages(List<SecurityAdvisory> list) Ensure that the CWE and Vulnerability lists have been completely fetched and requests any missing entries.private SecurityAdvisoryResponseFetches additional data from the GraphQL API.private ZonedDateTimefindLastUpdated(ZonedDateTime lastUpdatedDate, List<SecurityAdvisory> list) Retrieve the latest last updated date from the list of security advisories.intReturns the last HTTP Status Code received.Returns the latest updated date.intThe number of available entries; only available after the first call to `next()`.booleanhasNext()Returnstrueif there are more records available; otherwisefalse.private com.samskivert.mustache.TemplateloadMustacheTemplate(String resourceName) Loads a mustache template from the project's resources by name.next()Returns the next list of security advisories.private Future<org.apache.hc.client5.http.async.methods.SimpleHttpResponse>Internal API to query the endpoint.voidsetClassifications(String classifications) The classification of the advisory ("GENERAL", "MALWARE")voidsetPublishedSinceFilter(ZonedDateTime utcPublishedSinceFilter) Filter the Security Advisories by those published since the give date time.voidsetUpdatedSinceFilter(ZonedDateTime utcUpdatedSinceFilter) Filter the Security Advisories by those updated since the give date time.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
-
GITHUB_GRAPHQL_ENDPOINT
The GitHub GraphQL endpoint URL.- See Also:
-
LOG
private static final org.slf4j.Logger LOGReference to the logger. -
ADVISORIES_TEMPLATE
The mustache template to retrieve advisories.- See Also:
-
VULNERABILITIES_TEMPLATE
The mustache template to retrieve advisory vulnerabilities.- See Also:
-
CWES_TEMPLATE
The mustache template to retrieve advisory CWEs.- See Also:
-
httpClient
HTTP Client to access the API. -
objectMapper
private final com.fasterxml.jackson.databind.ObjectMapper objectMapperJackson JSON object mapper. -
advistoriesTemplate
private final com.samskivert.mustache.Template advistoriesTemplateTemplate for security advisories. -
vulnerabilitiesTemplate
private final com.samskivert.mustache.Template vulnerabilitiesTemplateTemplate for security advisory's vulnerabilities - in case we have more than 100. -
cwesTemplate
private final com.samskivert.mustache.Template cwesTemplateTemplate for security advisory's CWEs - in case we have more than 50. -
firstCall
private boolean firstCallHas the first HTTP call been made? -
lastStatusCode
private int lastStatusCodeThe last HTTP status code received. -
totalCount
private int totalCountThe total count of advisories retrieved. -
totalAvailable
private int totalAvailableThe number of available entries; only available after the first call to `next()`. -
futureResponse
Asynchronous future HTTP Response. -
endpoint
The GitHub GraphQL endpoint. -
githubToken
The GitHub Access Token. -
userAgent
The user agent to append to the default open-vulnerability-client's user-agent string -
classifications
The classification of the advisory ("GENERAL", "MALWARE") -
updatedSinceFilter
The updatedSince filter. -
publishedSinceFilter
The publishedSince filter. -
lastUpdated
The last updated date time value retrieved. -
version
The version of the client.
-
-
Constructor Details
-
GitHubSecurityAdvisoryClient
Constructs a new client.- Parameters:
githubToken- the GitHub API Token.
-
GitHubSecurityAdvisoryClient
Constructs a new client.- Parameters:
endpoint- the GraphQL endpoint of GitHub or GHE.githubToken- the GitHub API Token.
-
GitHubSecurityAdvisoryClient
public GitHubSecurityAdvisoryClient(String githubToken, String endpoint, HttpAsyncClientSupplier httpClientSupplier) Constructs a new client.- Parameters:
githubToken- the GitHub API Token.endpoint- the GraphQL endpoint of GitHub or GHE.httpClientSupplier- supplier for custom HTTP clients; ifnulla default client will be used
-
GitHubSecurityAdvisoryClient
public GitHubSecurityAdvisoryClient(String githubToken, String endpoint, HttpAsyncClientSupplier httpClientSupplier, String userAgent) Constructs a new client.- Parameters:
githubToken- the GitHub API Token.endpoint- the GraphQL endpoint of GitHub or GHE.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 string
-
-
Method Details
-
loadMustacheTemplate
Loads a mustache template from the project's resources by name.- Parameters:
resourceName- the name of the template.- Returns:
- the mustache template.
-
setClassifications
The classification of the advisory ("GENERAL", "MALWARE")- Parameters:
classifications- the classification of the advisory.
-
setUpdatedSinceFilter
Filter the Security Advisories by those updated since the give date time.- Parameters:
utcUpdatedSinceFilter- the date to filter on.
-
setPublishedSinceFilter
Filter the Security Advisories by those published since the give date time.- Parameters:
utcPublishedSinceFilter- the date to filter on.
-
query
Internal API to query the endpoint.- Parameters:
json- the GraphQL query body (i.e. minus the query:).- Returns:
- the Asynchronous HTTP Response.
-
close
Cleanup allocated resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacePagedDataSource<SecurityAdvisory>- Throws:
Exception- thrown if there is a problem.
-
getTotalAvailable
public int getTotalAvailable()The number of available entries; only available after the first call to `next()`.- Specified by:
getTotalAvailablein interfacePagedDataSource<SecurityAdvisory>- Returns:
- the number of available entries
-
getLastStatusCode
public int getLastStatusCode()Returns the last HTTP Status Code received.- Specified by:
getLastStatusCodein interfacePagedDataSource<SecurityAdvisory>- Returns:
- the last HTTP Status Code received.
-
hasNext
public boolean hasNext()Returnstrueif there are more records available; otherwisefalse.- Specified by:
hasNextin interfaceIterator<Collection<SecurityAdvisory>>- Specified by:
hasNextin interfacePagedDataSource<SecurityAdvisory>- Returns:
trueif there are more records available; otherwisefalse.
-
next
Returns the next list of security advisories. 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<SecurityAdvisory>>- Specified by:
nextin interfacePagedDataSource<SecurityAdvisory>- Returns:
- a list of security advisories.
-
buildGraphQLData
-
getLastUpdated
Returns the latest updated date.- Specified by:
getLastUpdatedin interfacePagedDataSource<SecurityAdvisory>- Returns:
- the latest updated date
-
findLastUpdated
Retrieve the latest last updated date from the list of security advisories.- Parameters:
lastUpdatedDate- the last updated date.list- the list of security advisories.- Returns:
- the latest last updated date.
-
ensureSubPages
private void ensureSubPages(List<SecurityAdvisory> list) throws ExecutionException, InterruptedException Ensure that the CWE and Vulnerability lists have been completely fetched and requests any missing entries.- Parameters:
list- the list of security advisories to validate and if necessary add CWE or vulnerability data.- Throws:
ExecutionException- thrown if there is a problem.InterruptedException- thrown if interrupted.
-
fetch
private SecurityAdvisoryResponse fetch(com.samskivert.mustache.Template template, String ghsaId, String after) throws InterruptedException, ExecutionException Fetches additional data from the GraphQL API.- Parameters:
template- the template to use for the request.ghsaId- the advisory id used to filter the request.after- the end cursor from the previous request.- Returns:
- the requested data.
- Throws:
ExecutionException- thrown if there is a problem.InterruptedException- thrown if interrupted.
-