2022-01-13 20:02:24 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-01-19 00:59:18 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
gogit "github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2022-02-17 01:10:42 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2022-01-13 20:02:24 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
2022-03-01 04:25:24 +00:00
|
|
|
func (e *Engine) ScanGit(ctx context.Context, repoPath, headRef, baseRef string, maxDepth int, filter *common.Filter) error {
|
2022-01-15 00:07:45 +00:00
|
|
|
repo, err := gogit.PlainOpenWithOptions(repoPath, &gogit.PlainOpenOptions{DetectDotGit: true})
|
2022-01-13 20:02:24 +00:00
|
|
|
if err != nil {
|
2022-01-15 00:07:45 +00:00
|
|
|
return fmt.Errorf("could open repo: %s: %w", repoPath, err)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
logOptions := &gogit.LogOptions{
|
2022-01-24 19:59:04 +00:00
|
|
|
All: true,
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
var sinceCommit, headCommit *object.Commit
|
2022-03-01 04:25:24 +00:00
|
|
|
if len(baseRef) > 0 {
|
|
|
|
baseHash := plumbing.NewHash(baseRef)
|
|
|
|
if baseHash.IsZero() {
|
|
|
|
base, err := git.TryAdditionalBaseRefs(repo, baseRef)
|
|
|
|
if err == nil && !base.IsZero() {
|
|
|
|
baseHash = *base
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sinceCommit, err = repo.CommitObject(baseHash)
|
2022-01-13 20:02:24 +00:00
|
|
|
if err != nil {
|
2022-03-01 04:25:24 +00:00
|
|
|
return fmt.Errorf("unable to resolve commit %s: %s", baseRef, err)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
2022-01-22 00:28:41 +00:00
|
|
|
}
|
2022-01-13 20:02:24 +00:00
|
|
|
|
2022-03-01 04:25:24 +00:00
|
|
|
if headRef != "" {
|
|
|
|
headHash, err := git.TryAdditionalBaseRefs(repo, headRef)
|
2022-01-13 20:02:24 +00:00
|
|
|
if err != nil {
|
2022-03-01 04:25:24 +00:00
|
|
|
return fmt.Errorf("could not parse revision: %q: %w", headRef, err)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
headCommit, err = repo.CommitObject(*headHash)
|
2022-01-13 20:02:24 +00:00
|
|
|
if err != nil {
|
2022-03-01 04:25:24 +00:00
|
|
|
return fmt.Errorf("could not find commit: %q: %w", headRef, err)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"commit": headCommit.Hash.String(),
|
|
|
|
}).Debug("resolved head reference")
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
logOptions.From = headCommit.Hash
|
|
|
|
logOptions.All = false
|
|
|
|
}
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
gitSource := git.NewGit(sourcespb.SourceType_SOURCE_TYPE_GIT, 0, 0, "local", true, runtime.NumCPU(),
|
|
|
|
func(file, email, commit, repository string) *source_metadatapb.MetaData {
|
|
|
|
return &source_metadatapb.MetaData{
|
|
|
|
Data: &source_metadatapb.MetaData_Git{
|
|
|
|
Git: &source_metadatapb.Git{
|
|
|
|
Commit: commit,
|
|
|
|
File: file,
|
|
|
|
Email: email,
|
|
|
|
Repository: repository,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-22 00:28:41 +00:00
|
|
|
opts := []git.ScanOption{
|
|
|
|
git.ScanOptionFilter(filter),
|
|
|
|
git.ScanOptionLogOptions(logOptions),
|
|
|
|
}
|
|
|
|
// TODO: Add kingpin type that can differentiate between `not set` and `0` for int.
|
|
|
|
if maxDepth != 0 {
|
|
|
|
opts = append(opts, git.ScanOptionMaxDepth(int64(maxDepth)))
|
|
|
|
}
|
|
|
|
if sinceCommit != nil {
|
2022-02-17 01:10:42 +00:00
|
|
|
opts = append(opts, git.ScanOptionBaseCommit(sinceCommit))
|
|
|
|
}
|
|
|
|
if headCommit != nil {
|
|
|
|
opts = append(opts, git.ScanOptionHeadCommit(headCommit))
|
2022-01-22 00:28:41 +00:00
|
|
|
}
|
|
|
|
scanOptions := git.NewScanOptions(opts...)
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
go func() {
|
2022-01-22 00:28:41 +00:00
|
|
|
err := gitSource.ScanRepo(ctx, repo, scanOptions, e.ChunksChan())
|
2022-01-13 20:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("could not scan repo")
|
|
|
|
}
|
|
|
|
close(e.ChunksChan())
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|