feat: Add support to skip using GitHub dependency graph API

This commit is contained in:
abhisek 2024-10-19 10:38:42 +05:30
parent 78a728b87a
commit d2290cdfe7
No known key found for this signature in database
GPG Key ID: CB92A4990C02A88F
5 changed files with 42 additions and 23 deletions

View File

@ -16,9 +16,10 @@ const (
) )
type GithubOrgReaderConfig struct { type GithubOrgReaderConfig struct {
OrganizationURL string OrganizationURL string
IncludeArchived bool IncludeArchived bool
MaxRepositories int MaxRepositories int
SkipDependencyGraphAPI bool
} }
type githubOrgReader struct { type githubOrgReader struct {
@ -129,7 +130,11 @@ func (p *githubOrgReader) handleRepositoryBatch(repositories []*github.Repositor
return nil return nil
} }
githubReader, err := NewGithubReader(p.client, repoUrls, "") githubReader, err := NewGithubReader(p.client, GitHubReaderConfig{
Urls: repoUrls,
SkipGitHubDependencyGraphAPI: p.config.SkipDependencyGraphAPI,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,10 +14,15 @@ import (
"github.com/safedep/vet/pkg/parser" "github.com/safedep/vet/pkg/parser"
) )
type GitHubReaderConfig struct {
Urls []string
LockfileAs string
SkipGitHubDependencyGraphAPI bool
}
type githubReader struct { type githubReader struct {
client *github.Client client *github.Client
github_urls []string config GitHubReaderConfig
lockfileAs string
} }
// NewGithubReader creates a [PackageManifestReader] that can be used to read // NewGithubReader creates a [PackageManifestReader] that can be used to read
@ -25,13 +30,10 @@ type githubReader struct {
// the parser auto-detects the format based on file name. This reader fails and // the parser auto-detects the format based on file name. This reader fails and
// returns an error on first error encountered while parsing github_urls // returns an error on first error encountered while parsing github_urls
func NewGithubReader(client *github.Client, func NewGithubReader(client *github.Client,
github_urls []string, config GitHubReaderConfig) (PackageManifestReader, error) {
lockfileAs string) (PackageManifestReader, error) {
return &githubReader{ return &githubReader{
client: client, client: client,
github_urls: github_urls, config: config,
lockfileAs: lockfileAs, // This is unused currently
}, nil }, nil
} }
@ -50,7 +52,7 @@ func (p *githubReader) EnumManifests(handler func(*models.PackageManifest,
// We will not fail fast! This is because when we are scanning multiple // We will not fail fast! This is because when we are scanning multiple
// github urls, which we may while scanning an entire org, we want to make // github urls, which we may while scanning an entire org, we want to make
// as much progress as possible while logging errors // as much progress as possible while logging errors
for _, github_url := range p.github_urls { for _, github_url := range p.config.Urls {
logger.Debugf("Processing Github URL: %s", github_url) logger.Debugf("Processing Github URL: %s", github_url)
gitURL, err := giturl.NewGitURL(github_url) gitURL, err := giturl.NewGitURL(github_url)
@ -153,6 +155,9 @@ func (p *githubReader) processTopLevelLockfiles(ctx context.Context, client *git
func (p *githubReader) processRemoteDependencyGraph(ctx context.Context, client *github.Client, func (p *githubReader) processRemoteDependencyGraph(ctx context.Context, client *github.Client,
gitUrl giturl.IGitURL, handler func(*models.PackageManifest, gitUrl giturl.IGitURL, handler func(*models.PackageManifest,
PackageReader) error) error { PackageReader) error) error {
if p.config.SkipGitHubDependencyGraphAPI {
return errors.New("dependency graph API is disabled in the configuration")
}
logger.Infof("Fetching dependency graph from %s", gitUrl.GetURL().String()) logger.Infof("Fetching dependency graph from %s", gitUrl.GetURL().String())

16
scan.go
View File

@ -30,6 +30,7 @@ var (
githubRepoUrls []string githubRepoUrls []string
githubOrgUrl string githubOrgUrl string
githubOrgMaxRepositories int githubOrgMaxRepositories int
githubSkipDependencyGraphAPI bool
scanExclude []string scanExclude []string
transitiveAnalysis bool transitiveAnalysis bool
transitiveDepth int transitiveDepth int
@ -97,6 +98,8 @@ func newScanCommand() *cobra.Command {
"Github organization URL (Example: https://github.com/safedep)") "Github organization URL (Example: https://github.com/safedep)")
cmd.Flags().IntVarP(&githubOrgMaxRepositories, "github-org-max-repo", "", 1000, cmd.Flags().IntVarP(&githubOrgMaxRepositories, "github-org-max-repo", "", 1000,
"Maximum number of repositories to process for the Github Org") "Maximum number of repositories to process for the Github Org")
cmd.Flags().BoolVarP(&githubSkipDependencyGraphAPI, "skip-github-dependency-graph-api", "", false,
"Do not use GitHub Dependency Graph API to fetch dependencies")
cmd.Flags().StringVarP(&lockfileAs, "lockfile-as", "", "", cmd.Flags().StringVarP(&lockfileAs, "lockfile-as", "", "",
"Parser to use for the lockfile (vet scan parsers to list)") "Parser to use for the lockfile (vet scan parsers to list)")
cmd.Flags().StringVarP(&manifestType, "type", "", "", cmd.Flags().StringVarP(&manifestType, "type", "", "",
@ -234,15 +237,20 @@ func internalStartScan() error {
githubClient := githubClientBuilder() githubClient := githubClientBuilder()
// nolint:ineffassign,staticcheck // nolint:ineffassign,staticcheck
reader, err = readers.NewGithubReader(githubClient, githubRepoUrls, lockfileAs) reader, err = readers.NewGithubReader(githubClient, readers.GitHubReaderConfig{
Urls: githubRepoUrls,
LockfileAs: lockfileAs,
SkipGitHubDependencyGraphAPI: githubSkipDependencyGraphAPI,
})
} else if len(githubOrgUrl) > 0 { } else if len(githubOrgUrl) > 0 {
githubClient := githubClientBuilder() githubClient := githubClientBuilder()
// nolint:ineffassign,staticcheck // nolint:ineffassign,staticcheck
reader, err = readers.NewGithubOrgReader(githubClient, &readers.GithubOrgReaderConfig{ reader, err = readers.NewGithubOrgReader(githubClient, &readers.GithubOrgReaderConfig{
OrganizationURL: githubOrgUrl, OrganizationURL: githubOrgUrl,
IncludeArchived: false, IncludeArchived: false,
MaxRepositories: githubOrgMaxRepositories, MaxRepositories: githubOrgMaxRepositories,
SkipDependencyGraphAPI: githubSkipDependencyGraphAPI,
}) })
} else if len(purlSpec) > 0 { } else if len(purlSpec) > 0 {
// nolint:ineffassign,staticcheck // nolint:ineffassign,staticcheck

View File

@ -27,10 +27,11 @@ func TestGithubReaderWithVetPublicRepository(t *testing.T) {
assert.Nil(t, err, "github client creation error") assert.Nil(t, err, "github client creation error")
githubReader, err := readers.NewGithubReader(githubClient, []string{ githubReader, err := readers.NewGithubReader(githubClient, readers.GitHubReaderConfig{
"https://github.com/safedep/vet", Urls: []string{
"https://github.com/safedep/demo-client-java", "https://github.com/safedep/vet",
}, "") "https://github.com/safedep/demo-client-java",
}, LockfileAs: "", SkipGitHubDependencyGraphAPI: false})
assert.Nil(t, err, "github reader builder error") assert.Nil(t, err, "github reader builder error")

View File

@ -3,5 +3,5 @@
set -ex set -ex
echo $( \ echo $( \
$E2E_VET_BINARY scan -s --no-banner --github https://github.com/safedep/demo-client-java.git --report-summary=false --filter 'vulns.critical.exists(p, p.id == "GHSA-4wrc-f8pq-fpqp")' \ $E2E_VET_BINARY scan -s --no-banner --github https://github.com/safedep/demo-client-java.git --report-summary=false --filter 'vulns.critical.exists(p, p.id == "GHSA-4wrc-f8pq-fpqp")' --skip-github-dependency-graph-api \
) | grep "https://github.com/spring-projects/spring-framework" ) | grep "https://github.com/spring-projects/spring-framework"