Skip to content

Commit 94fdf01

Browse files
authored
Stop using detectableChunk in processResult (#4691)
The processResult function took a detectableChunk as a parameter, but that was purely out of convenience, because callers originally happened to have that type available. We later added more callers that didn't have a detectableChunk available, so they had to synthesize one. This was tolerable, but we're exploring more refactors that will introduce processResult callers that cannot synthesize a detectableChunk (because they don't have the right information available). This PR therefore modifies the signature of processResult to require only the necessary information so that we're not sandbagged by this unrelated structure down the road.
1 parent 073c922 commit 94fdf01

File tree

2 files changed

+51
-69
lines changed

2 files changed

+51
-69
lines changed

pkg/engine/engine.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,13 +1000,10 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
10001000
res.SetVerificationError(errOverlap)
10011001
e.processResult(
10021002
ctx,
1003-
detectableChunk{
1004-
chunk: chunk.chunk,
1005-
detector: detector,
1006-
decoder: chunk.decoder,
1007-
wgDoneFn: wgDetect.Done,
1008-
},
10091003
res,
1004+
chunk.chunk,
1005+
chunk.decoder,
1006+
detector.Detector.Description(),
10101007
isFalsePositive,
10111008
)
10121009

@@ -1133,7 +1130,7 @@ func (e *Engine) detectChunk(ctx context.Context, data detectableChunk) {
11331130
}
11341131

11351132
for _, res := range results {
1136-
e.processResult(ctx, data, res, isFalsePositive)
1133+
e.processResult(ctx, res, data.chunk, data.decoder, data.detector.Detector.Description(), isFalsePositive)
11371134
}
11381135
}
11391136

@@ -1172,20 +1169,18 @@ func (e *Engine) filterResults(
11721169

11731170
// processResult generates a detectors.ResultWithMetadata from the provided chunk and result and puts it on the results
11741171
// channel, unless the result exists on a line with an ignore tag, in which case no result is generated.
1175-
//
1176-
// CMR: The provided chunk is wrapped in a detectableChunk, but I'm pretty sure that's purely out of convenience
1177-
// (because that's what this function's callers are using when they call this function). We're past detection at this
1178-
// point in the engine, so we should probably refactor that parameter into a less confusing data type.
11791172
func (e *Engine) processResult(
11801173
ctx context.Context,
1181-
data detectableChunk,
11821174
res detectors.Result,
1175+
chunk sources.Chunk,
1176+
decoderType detectorspb.DecoderType,
1177+
detectorDescription string,
11831178
isFalsePositive func(detectors.Result) (bool, string),
11841179
) {
11851180
ignoreLinePresent := false
1186-
if SupportsLineNumbers(data.chunk.SourceType) {
1187-
copyChunk := data.chunk
1188-
copyMetaDataClone := proto.Clone(data.chunk.SourceMetadata)
1181+
if SupportsLineNumbers(chunk.SourceType) {
1182+
copyChunk := chunk
1183+
copyMetaDataClone := proto.Clone(chunk.SourceMetadata)
11891184
if copyMetaData, ok := copyMetaDataClone.(*source_metadatapb.MetaData); ok {
11901185
copyChunk.SourceMetadata = copyMetaData
11911186
}
@@ -1195,15 +1190,15 @@ func (e *Engine) processResult(
11951190
ctx.Logger().Error(err, "error setting link")
11961191
return
11971192
}
1198-
data.chunk = copyChunk
1193+
chunk = copyChunk
11991194
}
12001195
if ignoreLinePresent {
12011196
return
12021197
}
12031198

1204-
secret := detectors.CopyMetadata(&data.chunk, res)
1205-
secret.DecoderType = data.decoder
1206-
secret.DetectorDescription = data.detector.Detector.Description()
1199+
secret := detectors.CopyMetadata(&chunk, res)
1200+
secret.DecoderType = decoderType
1201+
secret.DetectorDescription = detectorDescription
12071202

12081203
if !res.Verified && res.Raw != nil {
12091204
isFp, _ := isFalsePositive(res)

pkg/engine/engine_test.go

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -559,21 +559,18 @@ func TestProcessResult_SourceSupportsLineNumbers_LinkUpdated(t *testing.T) {
559559
// Arrange: Create an engine
560560
e := Engine{results: make(chan detectors.ResultWithMetadata, 1)}
561561

562-
// Arrange: Create a detectableChunk
563-
data := detectableChunk{
564-
chunk: sources.Chunk{
565-
Data: []byte("abcde\nswordfish"),
566-
SourceMetadata: &source_metadatapb.MetaData{
567-
Data: &source_metadatapb.MetaData_Github{
568-
Github: &source_metadatapb.Github{
569-
Line: 1,
570-
Link: "https://github.com/org/repo/blob/abcdef/file.txt#L1",
571-
},
562+
// Arrange: Create a Chunk
563+
chunk := sources.Chunk{
564+
Data: []byte("abcde\nswordfish"),
565+
SourceMetadata: &source_metadatapb.MetaData{
566+
Data: &source_metadatapb.MetaData_Github{
567+
Github: &source_metadatapb.Github{
568+
Line: 1,
569+
Link: "https://github.com/org/repo/blob/abcdef/file.txt#L1",
572570
},
573571
},
574-
SourceType: sourcespb.SourceType_SOURCE_TYPE_GIT,
575572
},
576-
detector: &ahocorasick.DetectorMatch{Detector: fakeDetectorV1{}},
573+
SourceType: sourcespb.SourceType_SOURCE_TYPE_GIT,
577574
}
578575

579576
// Arrange: Create a Result
@@ -583,7 +580,7 @@ func TestProcessResult_SourceSupportsLineNumbers_LinkUpdated(t *testing.T) {
583580
}
584581

585582
// Act
586-
e.processResult(context.AddLogger(t.Context()), data, result, nil)
583+
e.processResult(context.AddLogger(t.Context()), result, chunk, 0, "", nil)
587584

588585
// Assert that the link has been correctly updated
589586
require.Len(t, e.results, 1)
@@ -595,20 +592,17 @@ func TestProcessResult_IgnoreLinePresent_NothingGenerated(t *testing.T) {
595592
// Arrange: Create an engine
596593
e := Engine{results: make(chan detectors.ResultWithMetadata, 1)}
597594

598-
// Arrange: Create a detectableChunk
599-
data := detectableChunk{
600-
chunk: sources.Chunk{
601-
Data: []byte("swordfish trufflehog:ignore"),
602-
SourceMetadata: &source_metadatapb.MetaData{
603-
Data: &source_metadatapb.MetaData_Git{
604-
Git: &source_metadatapb.Git{
605-
Line: 1,
606-
},
595+
// Arrange: Create a Chunk
596+
chunk := sources.Chunk{
597+
Data: []byte("swordfish trufflehog:ignore"),
598+
SourceMetadata: &source_metadatapb.MetaData{
599+
Data: &source_metadatapb.MetaData_Git{
600+
Git: &source_metadatapb.Git{
601+
Line: 1,
607602
},
608603
},
609-
SourceType: sourcespb.SourceType_SOURCE_TYPE_GIT,
610604
},
611-
detector: &ahocorasick.DetectorMatch{Detector: fakeDetectorV1{}},
605+
SourceType: sourcespb.SourceType_SOURCE_TYPE_GIT,
612606
}
613607

614608
// Arrange: Create a Result
@@ -618,7 +612,7 @@ func TestProcessResult_IgnoreLinePresent_NothingGenerated(t *testing.T) {
618612
}
619613

620614
// Act
621-
e.processResult(context.AddLogger(t.Context()), data, result, nil)
615+
e.processResult(context.AddLogger(t.Context()), result, chunk, 0, "", nil)
622616

623617
// Assert that no results were generated
624618
assert.Empty(t, e.results)
@@ -628,27 +622,23 @@ func TestProcessResult_AllFieldsCopied(t *testing.T) {
628622
// Arrange: Create an engine
629623
e := Engine{results: make(chan detectors.ResultWithMetadata, 1)}
630624

631-
// Arrange: Create a detectableChunk
632-
data := detectableChunk{
633-
chunk: sources.Chunk{
634-
SourceName: "test source",
635-
SourceID: 1,
636-
JobID: 2,
637-
SecretID: 3,
638-
SourceMetadata: &source_metadatapb.MetaData{
639-
Data: &source_metadatapb.MetaData_Docker{
640-
Docker: &source_metadatapb.Docker{
641-
File: "file",
642-
Image: "image",
643-
Layer: "layer",
644-
Tag: "tag",
645-
},
625+
// Arrange: Create a Chunk
626+
chunk := sources.Chunk{
627+
SourceName: "test source",
628+
SourceID: 1,
629+
JobID: 2,
630+
SecretID: 3,
631+
SourceMetadata: &source_metadatapb.MetaData{
632+
Data: &source_metadatapb.MetaData_Docker{
633+
Docker: &source_metadatapb.Docker{
634+
File: "file",
635+
Image: "image",
636+
Layer: "layer",
637+
Tag: "tag",
646638
},
647639
},
648-
SourceType: sourcespb.SourceType_SOURCE_TYPE_DOCKER,
649640
},
650-
decoder: detectorspb.DecoderType_PLAIN,
651-
detector: &ahocorasick.DetectorMatch{Detector: fakeDetectorV1{}},
641+
SourceType: sourcespb.SourceType_SOURCE_TYPE_DOCKER,
652642
}
653643

654644
// Arrange: Create a Result
@@ -662,12 +652,12 @@ func TestProcessResult_AllFieldsCopied(t *testing.T) {
662652
}
663653

664654
// Act
665-
e.processResult(context.AddLogger(t.Context()), data, result, nil)
655+
e.processResult(context.AddLogger(t.Context()), result, chunk, detectorspb.DecoderType_PLAIN, "a detector that detects", nil)
666656

667657
// Assert that the single generated result has the correct fields
668658
require.Len(t, e.results, 1)
669659
r := <-e.results
670-
if diff := cmp.Diff(data.chunk.SourceMetadata, r.SourceMetadata, protocmp.Transform()); diff != "" {
660+
if diff := cmp.Diff(chunk.SourceMetadata, r.SourceMetadata, protocmp.Transform()); diff != "" {
671661
t.Errorf("metadata mismatch (-want +got):\n%s", diff)
672662
}
673663
assert.Equal(t, map[string]string{"key": "value"}, r.ExtraData)
@@ -682,6 +672,7 @@ func TestProcessResult_AllFieldsCopied(t *testing.T) {
682672
assert.Equal(t, "test source", r.SourceName)
683673
assert.Equal(t, sourcespb.SourceType_SOURCE_TYPE_DOCKER, r.SourceType)
684674
assert.Equal(t, detectorspb.DecoderType_PLAIN, r.DecoderType)
675+
assert.Equal(t, "a detector that detects", r.DetectorDescription)
685676
}
686677

687678
func TestProcessResult_FalsePositiveFlagSetCorrectly(t *testing.T) {
@@ -722,10 +713,6 @@ func TestProcessResult_FalsePositiveFlagSetCorrectly(t *testing.T) {
722713
// Arrange: Create an Engine
723714
e := Engine{results: make(chan detectors.ResultWithMetadata, 1)}
724715

725-
// Arrange: Create a detectableChunk
726-
// (It needs a DetectorMatch to avoid a panic.)
727-
data := detectableChunk{detector: &ahocorasick.DetectorMatch{Detector: fakeDetectorV1{}}}
728-
729716
// Arrange: Create a Result
730717
res := detectors.Result{
731718
Raw: []byte("something not nil"), // The false positive check is not run when Raw is nil
@@ -736,7 +723,7 @@ func TestProcessResult_FalsePositiveFlagSetCorrectly(t *testing.T) {
736723
isFalsePositive := func(_ detectors.Result) (bool, string) { return tt.isFalsePositive, "" }
737724

738725
// Act
739-
e.processResult(context.AddLogger(t.Context()), data, res, isFalsePositive)
726+
e.processResult(context.AddLogger(t.Context()), res, sources.Chunk{}, 0, "", isFalsePositive)
740727

741728
// Assert that the single generated result has the correct false positive flag
742729
require.Len(t, e.results, 1)

0 commit comments

Comments
 (0)