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 Details

    • GITHUB_GRAPHQL_ENDPOINT

      public static final String GITHUB_GRAPHQL_ENDPOINT
      The GitHub GraphQL endpoint URL.
      See Also:
    • LOG

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

      private static final String ADVISORIES_TEMPLATE
      The mustache template to retrieve advisories.
      See Also:
    • VULNERABILITIES_TEMPLATE

      private static final String VULNERABILITIES_TEMPLATE
      The mustache template to retrieve advisory vulnerabilities.
      See Also:
    • CWES_TEMPLATE

      private static final String CWES_TEMPLATE
      The mustache template to retrieve advisory CWEs.
      See Also:
    • httpClient

      private final GithubRateLimitedAsyncClient httpClient
      HTTP Client to access the API.
    • objectMapper

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

      private final com.samskivert.mustache.Template advistoriesTemplate
      Template for security advisories.
    • vulnerabilitiesTemplate

      private final com.samskivert.mustache.Template vulnerabilitiesTemplate
      Template for security advisory's vulnerabilities - in case we have more than 100.
    • cwesTemplate

      private final com.samskivert.mustache.Template cwesTemplate
      Template for security advisory's CWEs - in case we have more than 50.
    • firstCall

      private boolean firstCall
      Has the first HTTP call been made?
    • lastStatusCode

      private int lastStatusCode
      The last HTTP status code received.
    • totalCount

      private int totalCount
      The total count of advisories retrieved.
    • totalAvailable

      private int totalAvailable
      The number of available entries; only available after the first call to `next()`.
    • futureResponse

      private Future<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> futureResponse
      Asynchronous future HTTP Response.
    • endpoint

      private final String endpoint
      The GitHub GraphQL endpoint.
    • githubToken

      private final String githubToken
      The GitHub Access Token.
    • userAgent

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

      private String classifications
      The classification of the advisory ("GENERAL", "MALWARE")
    • updatedSinceFilter

      private String updatedSinceFilter
      The updatedSince filter.
    • publishedSinceFilter

      private String publishedSinceFilter
      The publishedSince filter.
    • lastUpdated

      private ZonedDateTime lastUpdated
      The last updated date time value retrieved.
    • version

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

    • GitHubSecurityAdvisoryClient

      public GitHubSecurityAdvisoryClient(String githubToken)
      Constructs a new client.
      Parameters:
      githubToken - the GitHub API Token.
    • GitHubSecurityAdvisoryClient

      public GitHubSecurityAdvisoryClient(String githubToken, String endpoint)
      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; if null a 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; if null a default client will be used
      userAgent - the user agent to append to the default open-vulnerability-client's user-agent string
  • Method Details

    • loadMustacheTemplate

      private com.samskivert.mustache.Template loadMustacheTemplate(String resourceName)
      Loads a mustache template from the project's resources by name.
      Parameters:
      resourceName - the name of the template.
      Returns:
      the mustache template.
    • setClassifications

      public void setClassifications(String classifications)
      The classification of the advisory ("GENERAL", "MALWARE")
      Parameters:
      classifications - the classification of the advisory.
    • setUpdatedSinceFilter

      public void setUpdatedSinceFilter(ZonedDateTime utcUpdatedSinceFilter)
      Filter the Security Advisories by those updated since the give date time.
      Parameters:
      utcUpdatedSinceFilter - the date to filter on.
    • setPublishedSinceFilter

      public void setPublishedSinceFilter(ZonedDateTime utcPublishedSinceFilter)
      Filter the Security Advisories by those published since the give date time.
      Parameters:
      utcPublishedSinceFilter - the date to filter on.
    • query

      private Future<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> query(String json)
      Internal API to query the endpoint.
      Parameters:
      json - the GraphQL query body (i.e. minus the query:).
      Returns:
      the Asynchronous HTTP Response.
    • close

      public void close() throws Exception
      Cleanup allocated resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface PagedDataSource<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:
      getTotalAvailable in interface PagedDataSource<SecurityAdvisory>
      Returns:
      the number of available entries
    • getLastStatusCode

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

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

      public Collection<SecurityAdvisory> 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:
      next in interface Iterator<Collection<SecurityAdvisory>>
      Specified by:
      next in interface PagedDataSource<SecurityAdvisory>
      Returns:
      a list of security advisories.
    • buildGraphQLData

      private Map<String,String> buildGraphQLData()
    • getLastUpdated

      public ZonedDateTime getLastUpdated()
      Returns the latest updated date.
      Specified by:
      getLastUpdated in interface PagedDataSource<SecurityAdvisory>
      Returns:
      the latest updated date
    • findLastUpdated

      private ZonedDateTime findLastUpdated(ZonedDateTime lastUpdatedDate, List<SecurityAdvisory> list)
      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.