Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throws error if dir not found #3419

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ func (s *Source) scanDirs(ctx context.Context, reporter sources.ChunkReporter) e
continue
}
if err := s.scanDir(ctx, gitDir, reporter); err != nil {
ctx.Logger().Info("error scanning repository", "repo", gitDir, "error", err)
continue
ctx.Logger().Error(err, "error scanning repository", "repo", gitDir, "error", err)
return err // Return the error instead of continuing
}
}
return nil
Expand All @@ -327,10 +327,16 @@ func (s *Source) scanDir(ctx context.Context, gitDir string, reporter sources.Ch
// TODO: Figure out why we skip directories ending in "git".
return nil
}

// Check if the directory exists
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
return fmt.Errorf("directory does not exist: %s", gitDir)
}
Comment on lines +331 to +334
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this change is the only one that's needed. With just it applied, this is the output I get:

trufflehog (remotes/origin/HEAD *) % ./trufflehog git file:///oops
🐷🔑🐷  TruffleHog. Unearth your secrets. 🐷🔑🐷

2024-10-16T16:14:08-04:00	info-0	trufflehog	running source	{"source_manager_worker_id": "hN9fy", "with_units": true}
2024-10-16T16:14:08-04:00	error	trufflehog	error running scan	{"error": "engine failed to finish execution: fatal: directory does not exist: /oops"}
trufflehog (remotes/origin/HEAD *) % echo $?
1

Are you seeing something different?


// try paths instead of url
repo, err := RepoFromPath(gitDir, s.scanOptions.Bare)
if err != nil {
return reporter.ChunkErr(ctx, err)
return fmt.Errorf("error opening repo from path: %w", err)
}

err = func() error {
Expand All @@ -341,7 +347,7 @@ func (s *Source) scanDir(ctx context.Context, gitDir string, reporter sources.Ch
return s.git.ScanRepo(ctx, repo, gitDir, s.scanOptions, reporter)
}()
if err != nil {
return reporter.ChunkErr(ctx, err)
return err
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sources/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ func (s *SourceManager) runWithoutUnits(ctx context.Context, source Source, repo
defer wg.Wait()
defer close(ch)
if err := source.Chunks(ctx, ch, targets...); err != nil {
// Log the error
ctx.Logger().Error(err, "Error scanning chunks", "error", err)
report.ReportError(Fatal{err})
return Fatal{err}
}
Expand Down