Skip to content

Commit

Permalink
Add context to handler (#877)
Browse files Browse the repository at this point in the history
* Add context to handler

* Return rather than break out of select
  • Loading branch information
bill-rich committed Oct 28, 2022
1 parent 034ca4f commit ab71b93
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
19 changes: 14 additions & 5 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"context"
"io"

"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
Expand All @@ -18,7 +19,7 @@ type Handler interface {
New()
}

func HandleFile(file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {
func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {
for _, handler := range DefaultHandlers() {
handler.New()
var isType bool
Expand All @@ -27,10 +28,18 @@ func HandleFile(file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sour
continue
}
handlerChan := handler.FromFile(file)
for data := range handlerChan {
chunk := *chunkSkel
chunk.Data = data
chunksChan <- &chunk
for {
select {
case data := <-handlerChan:
chunk := *chunkSkel
chunk.Data = data
chunksChan <- &chunk
case <-ctx.Done():
return false
}
if handlerChan == nil {
break
}
}
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sources/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err
},
Verify: s.verify,
}
if handlers.HandleFile(reReader, chunkSkel, chunksChan) {
if handlers.HandleFile(ctx, reReader, chunkSkel, chunksChan) {
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
SourceMetadata: metadata,
Verify: s.verify,
}
if err := handleBinary(repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {
if err := handleBinary(ctx, repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {
log.WithError(err).WithField("file", fileName).Debug("Error handling binary file")
}
continue
Expand Down Expand Up @@ -504,7 +504,7 @@ func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path strin
SourceMetadata: metadata,
Verify: s.verify,
}
if err := handleBinary(repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {
if err := handleBinary(ctx, repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {
log.WithError(err).WithField("file", fileName).Debug("Error handling binary file")
}
continue
Expand Down Expand Up @@ -782,7 +782,7 @@ func getSafeRemoteURL(repo *git.Repository, preferred string) string {
return safeURL
}

func handleBinary(repo *git.Repository, chunksChan chan *sources.Chunk, chunkSkel *sources.Chunk, commitHash plumbing.Hash, path string) error {
func handleBinary(ctx context.Context, repo *git.Repository, chunksChan chan *sources.Chunk, chunkSkel *sources.Chunk, commitHash plumbing.Hash, path string) error {
log.WithField("path", path).Trace("Binary file found in repository.")
commit, err := repo.CommitObject(commitHash)
if err != nil {
Expand All @@ -805,7 +805,7 @@ func handleBinary(repo *git.Repository, chunksChan chan *sources.Chunk, chunkSke
return err
}

if handlers.HandleFile(reader, chunkSkel, chunksChan) {
if handlers.HandleFile(ctx, reader, chunkSkel, chunksChan) {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sources/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (s *Source) pageChunker(ctx context.Context, client *s3.S3, chunksChan chan
},
Verify: s.verify,
}
if handlers.HandleFile(reader, chunkSkel, chunksChan) {
if handlers.HandleFile(ctx, reader, chunkSkel, chunksChan) {
return
}

Expand Down

0 comments on commit ab71b93

Please sign in to comment.