2022-03-15 00:05:15 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-25 16:35:44 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-11-15 21:02:37 +00:00
|
|
|
gogit "github.com/go-git/go-git/v5"
|
2022-03-15 00:05:15 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2022-08-10 17:11:13 +00:00
|
|
|
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-08-10 17:11:13 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
2022-11-15 21:02:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
|
2022-08-10 17:11:13 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/gitlab"
|
2022-03-15 00:05:15 +00:00
|
|
|
)
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
// ScanGitLab scans GitLab with the provided configuration.
|
2023-02-10 20:43:00 +00:00
|
|
|
func (e *Engine) ScanGitLab(ctx context.Context, c sources.GitlabConfig) error {
|
2022-11-15 21:02:37 +00:00
|
|
|
logOptions := &gogit.LogOptions{}
|
|
|
|
opts := []git.ScanOption{
|
|
|
|
git.ScanOptionFilter(c.Filter),
|
|
|
|
git.ScanOptionLogOptions(logOptions),
|
|
|
|
}
|
|
|
|
scanOptions := git.NewScanOptions(opts...)
|
|
|
|
|
2023-12-15 19:46:27 +00:00
|
|
|
connection := &sourcespb.GitLab{SkipBinaries: c.SkipBinaries}
|
2022-03-15 00:05:15 +00:00
|
|
|
|
|
|
|
switch {
|
2022-08-10 17:11:13 +00:00
|
|
|
case len(c.Token) > 0:
|
2022-03-15 00:05:15 +00:00
|
|
|
connection.Credential = &sourcespb.GitLab_Token{
|
2022-08-10 17:11:13 +00:00
|
|
|
Token: c.Token,
|
2022-03-15 00:05:15 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("must provide token")
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
if len(c.Endpoint) > 0 {
|
|
|
|
connection.Endpoint = c.Endpoint
|
2022-03-15 00:05:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
if len(c.Repos) > 0 {
|
|
|
|
connection.Repositories = c.Repos
|
2022-03-15 00:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var conn anypb.Any
|
|
|
|
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
|
|
|
|
if err != nil {
|
2023-02-09 22:55:19 +00:00
|
|
|
ctx.Logger().Error(err, "failed to marshal gitlab connection")
|
2022-03-15 00:05:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:58:38 +00:00
|
|
|
sourceName := "trufflehog - gitlab"
|
2023-09-13 00:23:25 +00:00
|
|
|
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gitlab.SourceType)
|
2023-09-12 23:58:38 +00:00
|
|
|
|
|
|
|
gitlabSource := &gitlab.Source{}
|
2023-09-14 18:28:24 +00:00
|
|
|
if err := gitlabSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2022-03-15 00:05:15 +00:00
|
|
|
}
|
2023-09-12 23:58:38 +00:00
|
|
|
gitlabSource.WithScanOptions(scanOptions)
|
|
|
|
_, err = e.sourceManager.Run(ctx, sourceName, gitlabSource)
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2022-03-15 00:05:15 +00:00
|
|
|
}
|