Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
GitHub concurrency (#25)
Dustin Decker7e38e699f62f
diff --git a/pkg/output/plain.go b/pkg/output/plain.goindex 85fe7b593..7cd42c8f8 100644--- a/pkg/output/plain.go+++ b/pkg/output/plain.go@@ -22,6 +22,7 @@ func PrintPlainOutput(r *detectors.ResultWithMetadata) {DetectorType: r.Result.DetectorType.String(),Verified: r.Result.Verified,MetaData: r.SourceMetadata,+ Raw: strings.TrimSpace(string(r.Result.Raw)),}meta, err := structToMap(out.MetaData.Data)@@ -38,6 +39,7 @@ func PrintPlainOutput(r *detectors.ResultWithMetadata) {whitePrinter.Print("Found unverified result π·πβ\n")}printer.Printf("Detector Type: %s\n", out.DetectorType)+ printer.Printf("Raw result: %s\n", whitePrinter.Sprint(out.Raw))for _, data := range meta {for k, v := range data {printer.Printf("%s: %s\n", strings.Title(k), v)@@ -58,5 +60,6 @@ func structToMap(obj interface{}) (m map[string]map[string]interface{}, err errotype outputFormat struct {DetectorType stringVerified bool+ Raw string*source_metadatapb.MetaData}diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.goindex 5e39652a1..d2d996360 100644--- a/pkg/sources/github/github.go+++ b/pkg/sources/github/github.go@@ -10,6 +10,7 @@ import ("runtime""strconv""strings"+ "sync""time""github.com/bradleyfalzon/ghinstallation"@@ -18,6 +19,7 @@ import ("github.com/google/go-github/v41/github"log "github.com/sirupsen/logrus""golang.org/x/oauth2"+ "golang.org/x/sync/semaphore""google.golang.org/protobuf/proto""google.golang.org/protobuf/types/known/anypb"@@ -43,9 +45,10 @@ type Source struct {httpClient *http.ClientaCtx context.Contextsources.Progress- log *log.Entry- token string- conn *sourcespb.GitHub+ log *log.Entry+ token string+ conn *sourcespb.GitHub+ jobSem *semaphore.Weighted}// Ensure the Source satisfies the interface at compile time@@ -97,6 +100,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64,s.sourceId = sourceIds.jobId = jobIds.verify = verify+ s.jobSem = semaphore.NewWeighted(int64(concurrency))s.httpClient = common.SaneHttpClient()@@ -296,43 +300,60 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) errrand.Shuffle(len(s.repos), func(i, j int) { s.repos[i], s.repos[j] = s.repos[j], s.repos[i] })}- log.Infof("Found %v total repos to scan", len(s.repos))+ log.Debugf("Found %v total repos to scan", len(s.repos))+ wg := sync.WaitGroup{}+ errChan := make(chan error)for i, repoURL := range s.repos {- s.SetProgressComplete(i, len(s.repos), fmt.Sprintf("Repo: %s", repoURL))-- if !strings.HasSuffix(repoURL, ".git") {- continue- }- if strings.Contains(repoURL, "DefinitelyTyped") {- continue- }- s.log.WithField("repo", repoURL).Debug("attempting to clone repo")- var path string- var repo *gogit.Repository- var err error+ s.jobSem.Acquire(ctx, 1)+ wg.Add(1)+ go func(ctx context.Context, errCh chan error, repoURL string, i int) {+ defer s.jobSem.Release(1)+ defer wg.Done()+ s.SetProgressComplete(i, len(s.repos), fmt.Sprintf("Repo: %s", repoURL))++ if !strings.HasSuffix(repoURL, ".git") {+ return+ }+ if strings.Contains(repoURL, "DefinitelyTyped") {+ return+ }+ s.log.WithField("repo", repoURL).Debug("attempting to clone repo")+ var path string+ var repo *gogit.Repository+ var err error++ switch s.conn.GetCredential().(type) {+ case *sourcespb.GitHub_Unauthenticated:+ path, repo, err = git.CloneRepoUsingUnauthenticated(repoURL)+ default:+ var token string+ token, err = s.Token(ctx, installationClient)+ if err != nil {+ errCh <- err+ return+ }+ path, repo, err = git.CloneRepoUsingToken(token, repoURL, "clone")+ }- switch s.conn.GetCredential().(type) {- case *sourcespb.GitHub_Unauthenticated:- path, repo, err = git.CloneRepoUsingUnauthenticated(repoURL)- default:- var token string- token, err = s.Token(ctx, installationClient)+ defer os.RemoveAll(path)if err != nil {- return err+ log.WithError(err).Errorf("unable to clone repo, continuing")+ return}- path, repo, err = git.CloneRepoUsingToken(token, repoURL, "clone")- }+ err = s.git.ScanRepo(ctx, repo, git.NewScanOptions(), chunksChan)+ if err != nil {+ log.WithError(err).Errorf("unable to scan repo, continuing")+ }+ }(ctx, errChan, repoURL, i)+ }- defer os.RemoveAll(path)+ wg.Wait()+ close(errChan)+ // This only returns first error which is what we did prior to concurrency+ for err := range errChan {if err != nil {- log.WithError(err).Warnf("unable to clone repo, continuing")- continue- }- err = s.git.ScanRepo(ctx, repo, git.NewScanOptions(), chunksChan)- if err != nil {- log.WithError(err).Warnf("unable to scan repo")+ return err}-}return nildiff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.goindex 3751468d2..10f81f611 100644--- a/pkg/sources/github/github_test.go+++ b/pkg/sources/github/github_test.go@@ -185,7 +185,7 @@ func TestSource_Scan(t *testing.T) {SourceMetadata: &source_metadatapb.MetaData{Data: &source_metadatapb.MetaData_Github{Github: &source_metadatapb.Github{- Repository: "https://github.com/trufflesecurity/driftwood.git",+ Repository: "https://github.com/trufflesecurity/TruffleHog-Enterprise-Github-Action.git",},},},