Skip to content

Commit

Permalink
address linter (trufflesecurity#2783)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav authored May 8, 2024
1 parent 8ef15e9 commit c7b72b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ func run(state overseer.State) {
// Verify that all the user-provided detectors support the optional
// detector features.
{
if err, id := verifyDetectorsAreVersioner(includeDetectorSet); err != nil {
if id, err := verifyDetectorsAreVersioner(includeDetectorSet); err != nil {
logFatal(err, "invalid include list detector configuration", "detector", id)
}
if err, id := verifyDetectorsAreVersioner(excludeDetectorSet); err != nil {
if id, err := verifyDetectorsAreVersioner(excludeDetectorSet); err != nil {
logFatal(err, "invalid exclude list detector configuration", "detector", id)
}
if err, id := verifyDetectorsAreVersioner(detectorsWithCustomVerifierEndpoints); err != nil {
if id, err := verifyDetectorsAreVersioner(detectorsWithCustomVerifierEndpoints); err != nil {
logFatal(err, "invalid verifier detector configuration", "detector", id)
}
// Extra check for endpoint customization.
Expand Down Expand Up @@ -608,6 +608,8 @@ func run(state overseer.State) {
if err := e.ScanPostman(ctx, cfg); err != nil {
logFatal(err, "Failed to scan Postman.")
}
default:
logFatal(fmt.Errorf("invalid command"), "Command not recognized.")
}

// Wait for all workers to finish.
Expand Down Expand Up @@ -691,7 +693,10 @@ func commaSeparatedToSlice(s []string) []string {
}

func printAverageDetectorTime(e *engine.Engine) {
fmt.Fprintln(os.Stderr, "Average detector time is the measurement of average time spent on each detector when results are returned.")
fmt.Fprintln(
os.Stderr,
"Average detector time is the measurement of average time spent on each detector when results are returned.",
)
for detectorName, duration := range e.GetDetectorsMetrics() {
fmt.Fprintf(os.Stderr, "%s: %s\n", detectorName, duration)
}
Expand Down Expand Up @@ -724,7 +729,7 @@ func getWithDetectorID[T any](d detectors.Detector, data map[config.DetectorID]T

// verifyDetectorsAreVersioner checks all keys in a provided map to verify the
// provided type is actually a Versioner.
func verifyDetectorsAreVersioner[T any](data map[config.DetectorID]T) (error, config.DetectorID) {
func verifyDetectorsAreVersioner[T any](data map[config.DetectorID]T) (config.DetectorID, error) {
isVersioner := engine.DefaultDetectorTypesImplementing[detectors.Versioner]()
for id := range data {
if id.Version == 0 {
Expand All @@ -736,7 +741,7 @@ func verifyDetectorsAreVersioner[T any](data map[config.DetectorID]T) (error, co
continue
}
// Version provided on a non-Versioner detector.
return fmt.Errorf("version provided but detector does not have a version"), id
return id, fmt.Errorf("version provided but detector does not have a version")
}
return nil, config.DetectorID{}
return config.DetectorID{}, nil
}
17 changes: 11 additions & 6 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)

var overlapError = errors.New("More than one detector has found this result. For your safety, verification has been disabled. You can override this behavior by using the --allow-verification-overlap flag.")
var errOverlap = errors.New(
"More than one detector has found this result. For your safety, verification has been disabled." +
"You can override this behavior by using the --allow-verification-overlap flag.",
)

// Metrics for the scan engine for external consumption.
type Metrics struct {
Expand Down Expand Up @@ -383,7 +386,9 @@ func (e *Engine) initialize(ctx context.Context, options ...Option) error {
e.notifyVerifiedResults = true
e.notifyUnknownResults = true
e.notifyUnverifiedResults = true
e.verificationOverlapChunksChan = make(chan verificationOverlapChunk, defaultChannelBuffer*verificationOverlapChunksChanMultiplier)
e.verificationOverlapChunksChan = make(
chan verificationOverlapChunk, defaultChannelBuffer*verificationOverlapChunksChanMultiplier,
)
e.results = make(chan detectors.ResultWithMetadata, defaultChannelBuffer)
e.dedupeCache = cache
e.printer = new(output.PlainPrinter)
Expand All @@ -405,8 +410,8 @@ func (e *Engine) initSourceManager(ctx context.Context) {
const defaultOutputBufferSize = 64

opts := []func(*sources.SourceManager){
sources.WithConcurrentSources(int(e.concurrency)),
sources.WithConcurrentUnits(int(e.concurrency)),
sources.WithConcurrentSources(e.concurrency),
sources.WithConcurrentUnits(e.concurrency),
sources.WithSourceUnits(),
sources.WithBufferedOutput(defaultOutputBufferSize),
}
Expand Down Expand Up @@ -528,7 +533,7 @@ func (e *Engine) startWorkers(ctx context.Context) {
const notifierWorkerRatio = 4
maxNotifierWorkers := 1
if numWorkers := e.concurrency / notifierWorkerRatio; numWorkers > 0 {
maxNotifierWorkers = int(numWorkers)
maxNotifierWorkers = numWorkers
}
ctx.Logger().V(2).Info("starting notifier workers", "count", maxNotifierWorkers)
for worker := 0; worker < maxNotifierWorkers; worker++ {
Expand Down Expand Up @@ -745,7 +750,7 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
if e.verificationOverlapTracker != nil {
e.verificationOverlapTracker.increment()
}
res.SetVerificationError(overlapError)
res.SetVerificationError(errOverlap)
e.processResult(ctx, detectableChunk{
chunk: chunk.chunk,
detector: detector,
Expand Down

0 comments on commit c7b72b9

Please sign in to comment.