Skip to content

Commit e8b1127

Browse files
enhanced the code
1 parent a11c1ea commit e8b1127

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,27 +709,31 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
709709
}
710710
eng.Start(ctx)
711711

712-
tmpFile := filepath.Join(os.TempDir(), "truffle-temp-repo")
712+
tmpDir := filepath.Join(os.TempDir(), "trufflehog_"+strconv.Itoa(os.Getpid()))
713+
// We need to persist the repo(s) if we're using legacy JSON output
714+
// because it requires commit SHAs in the output.
715+
persistRepo := *gitNoCleanup || *githubNoCleanup || *gitlabNoCleanup
716+
if *jsonLegacy && !persistRepo {
717+
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
718+
return scanMetrics, fmt.Errorf("failed to create temporary directory: %v", err)
719+
}
720+
*gitNoCleanup, *githubNoCleanup, *gitlabNoCleanup = true, true, true
721+
*gitClonePath, *githubClonePath, *gitlabClonePath = tmpDir, tmpDir, tmpDir
722+
}
723+
713724
defer func() {
714725
// Clean up temporary artifacts.
715726
if err := cleantemp.CleanTempArtifacts(ctx); err != nil {
716727
ctx.Logger().Error(err, "error cleaning temp artifacts")
717728
}
718729

719-
if err := os.RemoveAll(tmpFile); err != nil {
720-
ctx.Logger().Error(err, "error removing temporary directory")
730+
if !persistRepo {
731+
if err := os.RemoveAll(tmpDir); err != nil {
732+
ctx.Logger().Error(err, "error removing temporary directory")
733+
}
721734
}
722735
}()
723736

724-
persistRepo := *gitNoCleanup || *githubNoCleanup || *gitlabNoCleanup
725-
if *jsonLegacy && !persistRepo {
726-
if err := os.MkdirAll(tmpFile, os.ModePerm); err != nil {
727-
return scanMetrics, fmt.Errorf("failed to create temporary directory: %v", err)
728-
}
729-
*gitNoCleanup, *githubNoCleanup, *gitlabNoCleanup = true, true, true
730-
*gitClonePath, *githubClonePath, *gitlabClonePath = tmpFile, tmpFile, tmpFile
731-
}
732-
733737
var refs []sources.JobProgressRef
734738
switch cmd {
735739
case gitScan.FullCommand():

pkg/output/legacy_json.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,24 @@ import (
1818
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
1919
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
2020
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
21-
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
2221
)
2322

2423
// LegacyJSONPrinter is a printer that prints results in legacy JSON format for backwards compatibility.
2524
type LegacyJSONPrinter struct{ mu sync.Mutex }
2625

2726
func (p *LegacyJSONPrinter) Print(ctx context.Context, r *detectors.ResultWithMetadata) error {
28-
var repo, repoPath string
27+
var repoPath string
2928
switch r.SourceType {
3029
case sourcespb.SourceType_SOURCE_TYPE_GIT:
31-
repo = r.SourceMetadata.GetGit().Repository
3230
repoPath = r.SourceMetadata.GetGit().RepositoryLocalPath
3331
case sourcespb.SourceType_SOURCE_TYPE_GITHUB:
34-
repo = r.SourceMetadata.GetGithub().Repository
3532
repoPath = r.SourceMetadata.GetGithub().RepositoryLocalPath
3633
case sourcespb.SourceType_SOURCE_TYPE_GITLAB:
37-
repo = r.SourceMetadata.GetGitlab().Repository
3834
repoPath = r.SourceMetadata.GetGitlab().RepositoryLocalPath
3935
default:
4036
return fmt.Errorf("unsupported source type for legacy json output: %s", r.SourceType)
4137
}
4238

43-
// cloning the repo again here is not great and only works with unauthed repos
44-
var err error
45-
46-
if repoPath == "" {
47-
repoPath, _, err = git.PrepareRepo(ctx, repo, "")
48-
if err != nil || repoPath == "" {
49-
return fmt.Errorf("error preparing git repo for scanning: %w", err)
50-
}
51-
}
52-
5339
legacy, err := convertToLegacyJSON(r, repoPath)
5440
if err != nil {
5541
return fmt.Errorf("could not convert to legacy JSON: %w", err)

pkg/sources/git/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (s *Source) scanDir(ctx context.Context, gitDir string, reporter sources.Ch
354354

355355
err = func() error {
356356
// remove the directory only if it was created as a temporary path, or if it is a clone path and --no-cleanup is not set.
357-
if strings.HasPrefix(gitDir, filepath.Join(os.TempDir(), "trufflehog")) || (!s.conn.GetNoCleanup() && s.conn.GetClonePath() != "") {
357+
if strings.HasPrefix(gitDir, filepath.Join(os.TempDir(), "trufflehog-")) || (!s.conn.GetNoCleanup() && s.conn.GetClonePath() != "") {
358358
defer os.RemoveAll(gitDir)
359359
}
360360

pkg/sources/github/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func (s *Source) cloneAndScanRepo(ctx context.Context, repoURL string, repoInfo
743743
return duration, err
744744
}
745745
// remove the path only if it was created as a temporary path, or if it is a clone path and --no-cleanup is not set.
746-
if strings.HasPrefix(path, filepath.Join(os.TempDir(), "trufflehog")) || (!s.conn.NoCleanup && s.conn.GetClonePath() != "") {
746+
if strings.HasPrefix(path, filepath.Join(os.TempDir(), "trufflehog-")) || (!s.conn.NoCleanup && s.conn.GetClonePath() != "") {
747747
defer os.RemoveAll(path)
748748
}
749749

pkg/sources/gitlab/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporte
10951095
}
10961096

10971097
// remove the path only if it was created as a temporary path, or if it is a clone path and --no-cleanup is not set.
1098-
if strings.HasPrefix(path, filepath.Join(os.TempDir(), "trufflehog")) || (!s.noCleanup && s.clonePath != "") {
1098+
if strings.HasPrefix(path, filepath.Join(os.TempDir(), "trufflehog-")) || (!s.noCleanup && s.clonePath != "") {
10991099
defer os.RemoveAll(path)
11001100
}
11011101

0 commit comments

Comments
 (0)