Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Support sub-unit resumption in filesystem scans (#4325)
Miccahd7987d71ce87
diff --git a/pkg/sources/filesystem/filesystem.go b/pkg/sources/filesystem/filesystem.goindex 58b91bab6..5a5e38c48 100644--- a/pkg/sources/filesystem/filesystem.go+++ b/pkg/sources/filesystem/filesystem.go@@ -120,7 +120,12 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ .func (s *Source) scanDir(ctx context.Context, path string, chunksChan chan *sources.Chunk) error {workerPool := new(errgroup.Group)workerPool.SetLimit(s.concurrency)- defer func() { _ = workerPool.Wait() }()+ defer func() {+ _ = workerPool.Wait()+ s.ClearEncodedResumeInfoFor(path)+ }()+ startState := s.GetEncodedResumeInfoFor(path)+ resuming := startState != ""return fs.WalkDir(os.DirFS(path), ".", func(relativePath string, d fs.DirEntry, err error) error {if err != nil {@@ -147,10 +152,21 @@ func (s *Source) scanDir(ctx context.Context, path string, chunksChan chan *sourreturn nil}+ if resuming {+ // The start state holds the path that last completed+ // scanning. When we find it, we can start scanning+ // again on the next one.+ if fullPath == startState {+ resuming = false+ }+ return nil+ }+workerPool.Go(func() error {if err = s.scanFile(ctx, fullPath, chunksChan); err != nil {ctx.Logger().Error(err, "error scanning file", "path", fullPath, "error", err)}+ s.SetEncodedResumeInfoFor(path, fullPath)return nil})diff --git a/pkg/sources/sources.go b/pkg/sources/sources.goindex ce5eed74c..57aeb27c0 100644--- a/pkg/sources/sources.go+++ b/pkg/sources/sources.go@@ -1,6 +1,7 @@package sourcesimport (+ "encoding/json""errors""runtime""sync"@@ -447,12 +448,14 @@ type StdinConfig struct{}// Progress is used to update job completion progress across sources.type Progress struct {- mut sync.Mutex- PercentComplete int64- Message string- EncodedResumeInfo string- SectionsCompleted int32- SectionsRemaining int32+ mut sync.Mutex+ // encodedResumeInfoByID is used for sub-unit resumption (see below)+ encodedResumeInfoByID map[string]string+ PercentComplete int64+ Message string+ EncodedResumeInfo string+ SectionsCompleted int32+ SectionsRemaining int32}// Validator is an interface for validating a source. Sources can optionally implement this interface to validate@@ -510,3 +513,71 @@ func (p *Progress) GetProgress() *Progress {defer p.mut.Unlock()return p}++// -sub-unit-resumption------------------------------------------------------------+//+// The following collection of methods are intended to provide a thread-safe+// way to access the EncodedResumeInfo for Sources to enable saving and+// resuming progress mid SourceUnit scan.+//+// This level of synchronization is only necessary when multiple concurrent+// invocations of ChunkUnit consume/mutate the same Progress object. The source+// manager executes scans this way under certain circumstances.+//+// Usage:+// - id should be the SourceUnit ID+// - value is opaque data each Source uses+//++// GetEncodedResumeInfoFor gets the encoded resume information for the provided+// ID, usually a unit ID.+func (p *Progress) GetEncodedResumeInfoFor(id string) string {+ p.mut.Lock()+ defer p.mut.Unlock()+ p.ensureEncodedResumeInfoByID()+ return p.encodedResumeInfoByID[id]+}++// SetEncodedResumeInfoFor sets the encoded resume information for the provided+// ID, usually a unit ID.+func (p *Progress) SetEncodedResumeInfoFor(id, value string) {+ p.mut.Lock()+ defer p.mut.Unlock()+ p.ensureEncodedResumeInfoByID()+ p.encodedResumeInfoByID[id] = value+ p.EncodedResumeInfo = marshalEncodedResumeInfo(p.encodedResumeInfoByID)+}++// ClearEncodedResumeInfoFor removes the encoded resume information from being+// tracked.+func (p *Progress) ClearEncodedResumeInfoFor(id string) {+ p.mut.Lock()+ defer p.mut.Unlock()+ p.ensureEncodedResumeInfoByID()+ delete(p.encodedResumeInfoByID, id)+ p.EncodedResumeInfo = marshalEncodedResumeInfo(p.encodedResumeInfoByID)+}++// ensureEncodedResumeInfoByID ensures the encodedResumeInfoByID attribute is a+// non-nil map. The mutex must be held when calling this function.+func (p *Progress) ensureEncodedResumeInfoByID() {+ if p.encodedResumeInfoByID != nil {+ return+ }+ p.encodedResumeInfoByID = unmarshalEncodedResumeInfo(p.EncodedResumeInfo)+}++// marshalEncodedResumeInfo converts a map of values into a serialized string.+func marshalEncodedResumeInfo(values map[string]string) string {+ marshalled, _ := json.Marshal(values)+ return string(marshalled)+}++// unmarshalEncodedResumeInfo converts a serialized string into a map of values.+func unmarshalEncodedResumeInfo(data string) map[string]string {+ resumeInfo := make(map[string]string)+ _ = json.Unmarshal([]byte(data), &resumeInfo)+ return resumeInfo+}++// -/sub-unit-resumption-----------------------------------------------------------