2022-01-13 20:02:24 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-03-22 16:27:15 +00:00
|
|
|
"bytes"
|
2022-01-13 20:02:24 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-01-19 06:24:56 +00:00
|
|
|
"sync/atomic"
|
2022-01-13 20:02:24 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
2022-04-04 04:13:39 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Engine struct {
|
2022-02-07 18:29:06 +00:00
|
|
|
concurrency int
|
|
|
|
chunks chan *sources.Chunk
|
|
|
|
results chan detectors.ResultWithMetadata
|
|
|
|
decoders []decoders.Decoder
|
|
|
|
detectors map[bool][]detectors.Detector
|
|
|
|
chunksScanned uint64
|
2022-03-06 06:42:06 +00:00
|
|
|
detectorAvgTime sync.Map
|
2022-05-25 16:35:44 +00:00
|
|
|
sourcesWg sync.WaitGroup
|
|
|
|
workersWg sync.WaitGroup
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EngineOption func(*Engine)
|
|
|
|
|
|
|
|
func WithConcurrency(concurrency int) EngineOption {
|
|
|
|
return func(e *Engine) {
|
|
|
|
e.concurrency = concurrency
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDetectors(verify bool, d ...detectors.Detector) EngineOption {
|
|
|
|
return func(e *Engine) {
|
|
|
|
if e.detectors == nil {
|
|
|
|
e.detectors = make(map[bool][]detectors.Detector)
|
|
|
|
}
|
|
|
|
if e.detectors[verify] == nil {
|
2022-01-19 06:24:56 +00:00
|
|
|
e.detectors[true] = []detectors.Detector{}
|
|
|
|
e.detectors[false] = []detectors.Detector{}
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
e.detectors[verify] = append(e.detectors[verify], d...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDecoders(decoders ...decoders.Decoder) EngineOption {
|
|
|
|
return func(e *Engine) {
|
|
|
|
e.decoders = decoders
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Start(ctx context.Context, options ...EngineOption) *Engine {
|
|
|
|
e := &Engine{
|
2022-02-07 18:29:06 +00:00
|
|
|
chunks: make(chan *sources.Chunk),
|
|
|
|
results: make(chan detectors.ResultWithMetadata),
|
2022-03-06 06:42:06 +00:00
|
|
|
detectorAvgTime: sync.Map{},
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(e)
|
|
|
|
}
|
|
|
|
|
2022-04-01 23:47:27 +00:00
|
|
|
// Set defaults.
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
if e.concurrency == 0 {
|
|
|
|
numCPU := runtime.NumCPU()
|
|
|
|
logrus.Warn("No concurrency specified, defaulting to ", numCPU)
|
|
|
|
e.concurrency = numCPU
|
|
|
|
}
|
2022-04-04 04:13:39 +00:00
|
|
|
logrus.Debugf("running with up to %d workers", e.concurrency)
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
if len(e.decoders) == 0 {
|
|
|
|
e.decoders = decoders.DefaultDecoders()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.detectors) == 0 {
|
|
|
|
e.detectors = map[bool][]detectors.Detector{}
|
|
|
|
e.detectors[true] = DefaultDetectors()
|
2022-01-19 06:24:56 +00:00
|
|
|
e.detectors[false] = []detectors.Detector{}
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 06:24:56 +00:00
|
|
|
logrus.Debugf("loaded %d decoders", len(e.decoders))
|
|
|
|
logrus.Debugf("loaded %d detectors total, %d with verification enabled. %d with verification disabled",
|
|
|
|
len(e.detectors[true])+len(e.detectors[false]),
|
|
|
|
len(e.detectors[true]),
|
|
|
|
len(e.detectors[false]))
|
|
|
|
|
|
|
|
// start the workers
|
2022-05-25 16:35:44 +00:00
|
|
|
for i := 0; i < e.concurrency; i++ {
|
|
|
|
e.workersWg.Add(1)
|
|
|
|
go func() {
|
2022-08-29 18:45:37 +00:00
|
|
|
defer common.Recover(ctx)
|
2022-05-25 16:35:44 +00:00
|
|
|
defer e.workersWg.Done()
|
|
|
|
e.detectorWorker(ctx)
|
|
|
|
}()
|
|
|
|
}
|
2022-01-19 06:24:56 +00:00
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:35:44 +00:00
|
|
|
// Finish waits for running sources to complete and workers to finish scanning
|
|
|
|
// chunks before closing their respective channels. Once Finish is called, no
|
|
|
|
// more sources may be scanned by the engine.
|
2022-08-29 18:45:37 +00:00
|
|
|
func (e *Engine) Finish(ctx context.Context) {
|
|
|
|
defer common.Recover(ctx)
|
2022-05-25 16:35:44 +00:00
|
|
|
// wait for the sources to finish putting chunks onto the chunks channel
|
|
|
|
e.sourcesWg.Wait()
|
|
|
|
close(e.chunks)
|
|
|
|
// wait for the workers to finish processing all of the chunks and putting
|
|
|
|
// results onto the results channel
|
|
|
|
e.workersWg.Wait()
|
|
|
|
|
|
|
|
// TODO: re-evaluate whether this is needed and investigate why if so
|
|
|
|
//
|
|
|
|
// not entirely sure why results don't get processed without this pause
|
|
|
|
// since we've put all results on the channel at this point.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
close(e.results)
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
func (e *Engine) ChunksChan() chan *sources.Chunk {
|
|
|
|
return e.chunks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) ResultsChan() chan detectors.ResultWithMetadata {
|
|
|
|
return e.results
|
|
|
|
}
|
|
|
|
|
2022-01-19 06:24:56 +00:00
|
|
|
func (e *Engine) ChunksScanned() uint64 {
|
|
|
|
return e.chunksScanned
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:29:06 +00:00
|
|
|
func (e *Engine) DetectorAvgTime() map[string][]time.Duration {
|
2022-03-06 06:42:06 +00:00
|
|
|
avgTime := map[string][]time.Duration{}
|
|
|
|
e.detectorAvgTime.Range(func(k, v interface{}) bool {
|
|
|
|
key, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
logrus.Warnf("expected DetectorAvgTime key to be a string")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := v.([]time.Duration)
|
|
|
|
if !ok {
|
|
|
|
logrus.Warnf("expected DetectorAvgTime value to be []time.Duration")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
avgTime[key] = value
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return avgTime
|
2022-02-07 18:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
func (e *Engine) detectorWorker(ctx context.Context) {
|
|
|
|
for chunk := range e.chunks {
|
2022-05-04 21:11:10 +00:00
|
|
|
fragStart, mdLine := fragmentFirstLine(chunk)
|
2022-01-13 20:02:24 +00:00
|
|
|
for _, decoder := range e.decoders {
|
|
|
|
decoded := decoder.FromChunk(chunk)
|
|
|
|
if decoded == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataLower := strings.ToLower(string(decoded.Data))
|
|
|
|
for verify, detectorsSet := range e.detectors {
|
|
|
|
for _, detector := range detectorsSet {
|
2022-02-07 18:29:06 +00:00
|
|
|
start := time.Now()
|
2022-01-13 20:02:24 +00:00
|
|
|
foundKeyword := false
|
|
|
|
for _, kw := range detector.Keywords() {
|
|
|
|
if strings.Contains(dataLower, strings.ToLower(kw)) {
|
|
|
|
foundKeyword = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundKeyword {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-07 19:15:48 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
|
|
|
defer cancel()
|
2022-01-13 20:02:24 +00:00
|
|
|
results, err := detector.FromData(ctx, verify, decoded.Data)
|
|
|
|
if err != nil {
|
2022-02-10 18:54:33 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"source_type": decoded.SourceType.String(),
|
|
|
|
"metadata": decoded.SourceMetadata,
|
|
|
|
}).WithError(err).Error("could not scan chunk")
|
2022-01-13 20:02:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
2022-03-22 16:27:15 +00:00
|
|
|
if isGitSource(chunk.SourceType) {
|
2022-05-04 21:11:10 +00:00
|
|
|
offset := FragmentLineOffset(chunk, &result)
|
|
|
|
*mdLine = fragStart + offset
|
2022-03-22 16:27:15 +00:00
|
|
|
}
|
2022-05-04 21:11:10 +00:00
|
|
|
e.results <- detectors.CopyMetadata(chunk, result)
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
2022-02-07 18:29:06 +00:00
|
|
|
if len(results) > 0 {
|
2022-04-01 23:47:27 +00:00
|
|
|
elapsed := time.Since(start)
|
2022-02-07 18:29:06 +00:00
|
|
|
detectorName := results[0].DetectorType.String()
|
2022-03-06 06:42:06 +00:00
|
|
|
avgTimeI, ok := e.detectorAvgTime.Load(detectorName)
|
2022-04-01 23:47:27 +00:00
|
|
|
var avgTime []time.Duration
|
2022-03-06 06:42:06 +00:00
|
|
|
if ok {
|
|
|
|
avgTime, ok = avgTimeI.([]time.Duration)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 23:47:27 +00:00
|
|
|
avgTime = append(avgTime, elapsed)
|
2022-03-06 06:42:06 +00:00
|
|
|
e.detectorAvgTime.Store(detectorName, avgTime)
|
2022-02-07 18:29:06 +00:00
|
|
|
}
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 06:24:56 +00:00
|
|
|
atomic.AddUint64(&e.chunksScanned, 1)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 16:27:15 +00:00
|
|
|
|
|
|
|
// gitSources is a list of sources that utilize the Git source. It is stored this way because slice consts are not
|
|
|
|
// supported.
|
|
|
|
func gitSources() []sourcespb.SourceType {
|
|
|
|
return []sourcespb.SourceType{
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_GIT,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_GITHUB,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_GITLAB,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_BITBUCKET,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_GERRIT,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_GITHUB_UNAUTHENTICATED_ORG,
|
|
|
|
sourcespb.SourceType_SOURCE_TYPE_PUBLIC_GIT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGitSource(sourceType sourcespb.SourceType) bool {
|
|
|
|
for _, i := range gitSources() {
|
|
|
|
if i == sourceType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:11:10 +00:00
|
|
|
// FragmentLineOffset sets the line number for a provided source chunk with a given detector result.
|
|
|
|
func FragmentLineOffset(chunk *sources.Chunk, result *detectors.Result) int64 {
|
|
|
|
lines := bytes.Split(chunk.Data, []byte("\n"))
|
|
|
|
for i, line := range lines {
|
|
|
|
if bytes.Contains(line, result.Raw) {
|
|
|
|
return int64(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// fragmentFirstLine returns the first line number of a fragment along with a pointer to the value to update in the
|
|
|
|
// chunk metadata.
|
|
|
|
func fragmentFirstLine(chunk *sources.Chunk) (int64, *int64) {
|
|
|
|
var fragmentStart *int64
|
2022-03-22 16:27:15 +00:00
|
|
|
switch metadata := chunk.SourceMetadata.GetData().(type) {
|
|
|
|
case *source_metadatapb.MetaData_Git:
|
2022-05-04 21:11:10 +00:00
|
|
|
fragmentStart = &metadata.Git.Line
|
2022-03-22 16:27:15 +00:00
|
|
|
case *source_metadatapb.MetaData_Github:
|
2022-05-04 21:11:10 +00:00
|
|
|
fragmentStart = &metadata.Github.Line
|
2022-03-22 16:27:15 +00:00
|
|
|
case *source_metadatapb.MetaData_Gitlab:
|
2022-05-04 21:11:10 +00:00
|
|
|
fragmentStart = &metadata.Gitlab.Line
|
2022-03-22 16:27:15 +00:00
|
|
|
case *source_metadatapb.MetaData_Bitbucket:
|
2022-05-04 21:11:10 +00:00
|
|
|
fragmentStart = &metadata.Bitbucket.Line
|
2022-03-22 16:27:15 +00:00
|
|
|
case *source_metadatapb.MetaData_Gerrit:
|
2022-05-04 21:11:10 +00:00
|
|
|
fragmentStart = &metadata.Gerrit.Line
|
2022-05-04 22:45:12 +00:00
|
|
|
default:
|
|
|
|
return 0, nil
|
2022-03-22 16:27:15 +00:00
|
|
|
}
|
2022-05-04 21:11:10 +00:00
|
|
|
return *fragmentStart, fragmentStart
|
2022-03-22 16:27:15 +00:00
|
|
|
}
|