[chore] Remove logrus from engine package (#1085)

This commit is contained in:
Miccah 2023-02-09 16:55:19 -06:00 committed by GitHub
parent 114f4b6989
commit 58e8c1e4ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 36 additions and 37 deletions

View file

@ -182,7 +182,11 @@ func run(state overseer.State) {
}
}()
}
logger, sync := log.New("trufflehog", log.WithConsoleSink(os.Stderr))
logFormat := log.WithConsoleSink
if *jsonOut {
logFormat = log.WithJSONSink
}
logger, sync := log.New("trufflehog", logFormat(os.Stderr))
context.SetDefaultLogger(logger)
defer func() { _ = sync() }()

View file

@ -4,7 +4,6 @@ import (
"runtime"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -25,7 +24,7 @@ func (e *Engine) ScanCircleCI(ctx context.Context, token string) error {
var conn anypb.Any
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
if err != nil {
logrus.WithError(err).Error("failed to marshal Circle CI connection")
ctx.Logger().Error(err, "failed to marshal Circle CI connection")
return err
}
@ -41,7 +40,7 @@ func (e *Engine) ScanCircleCI(ctx context.Context, token string) error {
defer e.sourcesWg.Done()
err := circleSource.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Error("error scanning Circle CI")
ctx.Logger().Error(err, "error scanning Circle CI")
}
}()
return nil

View file

@ -2,13 +2,13 @@ package engine
import (
"bytes"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
@ -88,10 +88,10 @@ func Start(ctx context.Context, options ...EngineOption) *Engine {
if e.concurrency == 0 {
numCPU := runtime.NumCPU()
logrus.Warn("No concurrency specified, defaulting to ", numCPU)
ctx.Logger().Info("No concurrency specified, defaulting to max", "cpu", numCPU)
e.concurrency = numCPU
}
logrus.Debugf("running with up to %d workers", e.concurrency)
ctx.Logger().V(2).Info("engine started", "workers", e.concurrency)
if len(e.decoders) == 0 {
e.decoders = decoders.DefaultDecoders()
@ -103,11 +103,12 @@ func Start(ctx context.Context, options ...EngineOption) *Engine {
e.detectors[false] = []detectors.Detector{}
}
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]))
ctx.Logger().V(2).Info("loaded decoders", "count", len(e.decoders))
ctx.Logger().V(2).Info("loaded detectors",
"total", len(e.detectors[true])+len(e.detectors[false]),
"verification_enabled", len(e.detectors[true]),
"verification_disabled", len(e.detectors[false]),
)
// start the workers
for i := 0; i < e.concurrency; i++ {
@ -159,17 +160,18 @@ func (e *Engine) BytesScanned() uint64 {
}
func (e *Engine) DetectorAvgTime() map[string][]time.Duration {
logger := context.Background().Logger()
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")
logger.Info("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")
logger.Info("expected DetectorAvgTime value to be []time.Duration")
return true
}
avgTime[key] = value
@ -190,7 +192,7 @@ func (e *Engine) detectorWorker(ctx context.Context) {
case *decoders.Base64:
decoderType = detectorspb.DecoderType_BASE64
default:
logrus.Warnf("unknown decoder type: %T", decoder)
ctx.Logger().Info("unknown decoder type", "type", reflect.TypeOf(decoder).String())
decoderType = detectorspb.DecoderType_UNKNOWN
}
decoded := decoder.FromChunk(chunk)
@ -219,10 +221,10 @@ func (e *Engine) detectorWorker(ctx context.Context) {
return detector.FromData(ctx, verify, decoded.Data)
}()
if err != nil {
logrus.WithFields(logrus.Fields{
"source_type": decoded.SourceType.String(),
"metadata": decoded.SourceMetadata,
}).WithError(err).Error("could not scan chunk")
ctx.Logger().Error(err, "could not scan chunk",
"source_type", decoded.SourceType.String(),
"metadata", decoded.SourceMetadata,
)
continue
}

View file

@ -4,7 +4,6 @@ import (
"runtime"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -23,7 +22,7 @@ func (e *Engine) ScanFileSystem(ctx context.Context, c sources.Config) error {
var conn anypb.Any
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
if err != nil {
logrus.WithError(err).Error("failed to marshal filesystem connection")
ctx.Logger().Error(err, "failed to marshal filesystem connection")
return err
}
@ -39,7 +38,7 @@ func (e *Engine) ScanFileSystem(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := fileSystemSource.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Error("error scanning filesystem")
ctx.Logger().Error(err, "error scanning filesystem")
}
}()
return nil

View file

@ -5,7 +5,6 @@ import (
"runtime"
gogit "github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
@ -61,7 +60,7 @@ func (e *Engine) ScanGit(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := gitSource.ScanRepo(ctx, repo, c.RepoPath, scanOptions, e.ChunksChan())
if err != nil {
logrus.WithError(err).Fatal("could not scan repo")
ctx.Logger().Error(err, "could not scan repo")
}
}()
return nil

View file

@ -1,7 +1,6 @@
package engine
import (
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -35,12 +34,12 @@ func (e *Engine) ScanGitHub(ctx context.Context, c sources.Config) error {
var conn anypb.Any
err := anypb.MarshalFrom(&conn, &connection, proto.MarshalOptions{})
if err != nil {
logrus.WithError(err).Error("failed to marshal github connection")
ctx.Logger().Error(err, "failed to marshal github connection")
return err
}
err = source.Init(ctx, "trufflehog - github", 0, 0, false, &conn, c.Concurrency)
if err != nil {
logrus.WithError(err).Error("failed to initialize github source")
ctx.Logger().Error(err, "failed to initialize github source")
return err
}
@ -50,7 +49,7 @@ func (e *Engine) ScanGitHub(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := source.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Fatal("could not scan github")
ctx.Logger().Error(err, "could not scan github")
}
}()
return nil

View file

@ -6,7 +6,6 @@ import (
"github.com/go-errors/errors"
gogit "github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -49,7 +48,7 @@ func (e *Engine) ScanGitLab(ctx context.Context, c sources.Config) error {
var conn anypb.Any
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
if err != nil {
logrus.WithError(err).Error("failed to marshal gitlab connection")
ctx.Logger().Error(err, "failed to marshal gitlab connection")
return err
}
@ -66,7 +65,7 @@ func (e *Engine) ScanGitLab(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := gitlabSource.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Error("error scanning GitLab")
ctx.Logger().Error(err, "error scanning GitLab")
}
}()
return nil

View file

@ -5,7 +5,6 @@ import (
"runtime"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -42,7 +41,7 @@ func (e *Engine) ScanS3(ctx context.Context, c sources.Config) error {
var conn anypb.Any
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
if err != nil {
logrus.WithError(err).Error("failed to marshal S3 connection")
ctx.Logger().Error(err, "failed to marshal S3 connection")
return err
}
@ -58,7 +57,7 @@ func (e *Engine) ScanS3(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := s3Source.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Error("error scanning S3")
ctx.Logger().Error(err, "error scanning S3")
}
}()
return nil

View file

@ -4,7 +4,6 @@ import (
"os"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
@ -46,7 +45,7 @@ func (e *Engine) ScanSyslog(ctx context.Context, c sources.Config) error {
err = source.Init(ctx, "trufflehog - syslog", 0, 0, false, &conn, c.Concurrency)
source.InjectConnection(connection)
if err != nil {
logrus.WithError(err).Error("failed to initialize syslog source")
ctx.Logger().Error(err, "failed to initialize syslog source")
return err
}
@ -56,7 +55,7 @@ func (e *Engine) ScanSyslog(ctx context.Context, c sources.Config) error {
defer e.sourcesWg.Done()
err := source.Chunks(ctx, e.ChunksChan())
if err != nil {
logrus.WithError(err).Fatal("could not scan syslog")
ctx.Logger().Error(err, "could not scan syslog")
}
}()
return nil