2023-03-08 01:32:04 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-17 00:53:42 +00:00
|
|
|
"strings"
|
2023-03-08 01:32:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-07-31 18:12:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-03-08 01:32:04 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2023-06-26 16:39:57 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
|
2023-03-08 01:32:04 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScanGCS(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
gcsConfig sources.GCSConfig
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "scanned GCS",
|
|
|
|
gcsConfig: sources.GCSConfig{
|
|
|
|
ApiKey: "abc123",
|
|
|
|
ProjectID: "test-project",
|
|
|
|
CloudCred: false,
|
|
|
|
WithoutAuth: false,
|
|
|
|
ServiceAccount: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-03-17 00:53:42 +00:00
|
|
|
name: "missing project ID, with auth",
|
|
|
|
gcsConfig: sources.GCSConfig{ApiKey: "abc123"},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing project ID, without auth, public scan",
|
|
|
|
gcsConfig: sources.GCSConfig{WithoutAuth: true},
|
2023-03-08 01:32:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple selected auth methods",
|
|
|
|
gcsConfig: sources.GCSConfig{
|
|
|
|
ApiKey: "abc123",
|
|
|
|
ProjectID: "test-project",
|
|
|
|
CloudCred: true,
|
|
|
|
WithoutAuth: false,
|
|
|
|
ServiceAccount: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no auth method selected",
|
|
|
|
gcsConfig: sources.GCSConfig{
|
2023-03-17 00:53:42 +00:00
|
|
|
ProjectID: "test-project",
|
|
|
|
MaxObjectSize: 10 * 1024 * 1024,
|
2023-03-08 01:32:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-06-26 16:39:57 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-06-13 20:47:09 +00:00
|
|
|
const defaultOutputBufferSize = 64
|
|
|
|
opts := []func(*sources.SourceManager){
|
|
|
|
sources.WithSourceUnits(),
|
|
|
|
sources.WithBufferedOutput(defaultOutputBufferSize),
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceManager := sources.NewManager(opts...)
|
|
|
|
|
|
|
|
conf := Config{
|
|
|
|
Concurrency: 1,
|
|
|
|
Decoders: decoders.DefaultDecoders(),
|
|
|
|
Detectors: DefaultDetectors(),
|
|
|
|
Verify: false,
|
|
|
|
SourceManager: sourceManager,
|
|
|
|
Dispatcher: NewPrinterDispatcher(new(discardPrinter)),
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := NewEngine(ctx, &conf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
e.Start(ctx)
|
2023-07-31 18:12:08 +00:00
|
|
|
|
2023-06-26 16:39:57 +00:00
|
|
|
go func() {
|
|
|
|
resultCount := 0
|
|
|
|
for range e.ResultsChan() {
|
|
|
|
resultCount++
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-07-31 18:12:08 +00:00
|
|
|
err = e.ScanGCS(ctx, test.gcsConfig)
|
2023-03-17 00:53:42 +00:00
|
|
|
if err != nil && !test.wantErr && !strings.Contains(err.Error(), "googleapi: Error 400: Bad Request") {
|
2023-03-08 01:32:04 +00:00
|
|
|
t.Errorf("ScanGCS() got: %v, want: %v", err, nil)
|
|
|
|
return
|
|
|
|
}
|
2023-08-03 18:36:30 +00:00
|
|
|
if err := e.Finish(ctx); err != nil && !test.wantErr && !strings.Contains(err.Error(), "googleapi: Error 400: Bad Request") {
|
|
|
|
t.Errorf("Finish() got: %v, want: %v", err, nil)
|
|
|
|
return
|
|
|
|
}
|
2023-03-08 01:32:04 +00:00
|
|
|
|
|
|
|
if err == nil && test.wantErr {
|
|
|
|
t.Errorf("ScanGCS() got: %v, want: %v", err, "error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|