Skip to content

Find, verify, and analyze leaked credentials

feat: propagate file info in log context (#3405)

Richard Gomez34e443adcf15
diff --git a/pkg/sources/filesystem/filesystem.go b/pkg/sources/filesystem/filesystem.go
index 74ae9bcae..98805882d 100644
--- a/pkg/sources/filesystem/filesystem.go
+++ b/pkg/sources/filesystem/filesystem.go
@@ -153,7 +153,7 @@ func (s *Source) scanDir(ctx context.Context, path string, chunksChan chan *sour
var skipSymlinkErr = errors.New("skipping symlink")
func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sources.Chunk) error {
- logger := ctx.Logger().WithValues("path", path)
+ fileCtx := context.WithValues(ctx, "path", path)
fileStat, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("unable to stat file: %w", err)
@@ -168,7 +168,7 @@ func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sou
}
defer inputFile.Close()
- logger.V(3).Info("scanning file")
+ fileCtx.Logger().V(3).Info("scanning file")
chunkSkel := &sources.Chunk{
SourceType: s.Type(),
@@ -185,7 +185,7 @@ func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sou
Verify: s.verify,
}
- return handlers.HandleFile(ctx, inputFile, chunkSkel, sources.ChanReporter{Ch: chunksChan})
+ return handlers.HandleFile(fileCtx, inputFile, chunkSkel, sources.ChanReporter{Ch: chunksChan})
}
// Enumerate implements SourceUnitEnumerator interface. This implementation simply
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go
index ca6f82f96..c6d35c436 100644
--- a/pkg/sources/git/git.go
+++ b/pkg/sources/git/git.go
@@ -657,9 +657,8 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
logger.Error(
err,
"error handling binary file",
- "filename", fileName,
"commit", commitHash,
- "file", diff.PathB,
+ "path", fileName,
)
}
continue
@@ -677,9 +676,8 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
if err != nil {
ctx.Logger().Error(
err, "error creating reader for commits",
- "filename", fileName,
"commit", fullHash,
- "file", diff.PathB,
+ "path", fileName,
)
return nil
}
@@ -687,11 +685,10 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
data := make([]byte, d.Len())
if _, err := io.ReadFull(reader, data); err != nil {
- ctx.Logger().Error(
+ logger.Error(
err, "error reading diff content for commit",
- "filename", fileName,
"commit", fullHash,
- "file", diff.PathB,
+ "path", fileName,
)
return nil
}
@@ -829,7 +826,7 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
)
for diff := range diffChan {
fullHash := diff.Commit.Hash
- logger := ctx.Logger().WithValues("filename", diff.PathB, "commit", fullHash, "file", diff.PathB)
+ logger := ctx.Logger().WithValues("commit", fullHash, "path", diff.PathB)
logger.V(2).Info("scanning staged changes from git")
if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth {
@@ -877,7 +874,7 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
Verify: s.verify,
}
if err := s.handleBinary(ctx, gitDir, reporter, chunkSkel, commitHash, fileName); err != nil {
- logger.Error(err, "error handling binary file", "filename", fileName)
+ logger.Error(err, "error handling binary file")
}
continue
}
@@ -887,24 +884,14 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
reader, err := d.ReadCloser()
if err != nil {
- ctx.Logger().Error(
- err, "error creating reader for staged",
- "filename", fileName,
- "commit", fullHash,
- "file", diff.PathB,
- )
+ logger.Error(err, "error creating reader for staged")
return nil
}
defer reader.Close()
data := make([]byte, d.Len())
if _, err := reader.Read(data); err != nil {
- ctx.Logger().Error(
- err, "error reading diff content for staged",
- "filename", fileName,
- "commit", fullHash,
- "file", diff.PathB,
- )
+ logger.Error(err, "error reading diff content for staged")
return nil
}
chunk := sources.Chunk{
@@ -1255,7 +1242,7 @@ func (s *Git) handleBinary(ctx context.Context, gitDir string, reporter sources.
defer func() { _ = cmd.Wait() }()
- return handlers.HandleFile(ctx, stdout, chunkSkel, reporter, handlers.WithSkipArchives(s.skipArchives))
+ return handlers.HandleFile(fileCtx, stdout, chunkSkel, reporter, handlers.WithSkipArchives(s.skipArchives))
}
func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) error {
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index b5d3ef90e..8c9458347 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -1492,8 +1492,10 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
SourceMetadata: &source_metadatapb.MetaData{
Data: &source_metadatapb.MetaData_Github{Github: meta},
},
- Verify: s.verify}
- return handlers.HandleFile(ctx, readCloser, &chunkSkel, reporter)
+ Verify: s.verify,
+ }
+ fileCtx := context.WithValues(ctx, "path", meta.GetFile())
+ return handlers.HandleFile(fileCtx, readCloser, &chunkSkel, reporter)
}
func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporter sources.ChunkReporter) error {