2023-03-08 01:32:04 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/gcs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ScanGCS with the provided options.
|
|
|
|
func (e *Engine) ScanGCS(ctx context.Context, c sources.GCSConfig) error {
|
2023-03-17 00:53:42 +00:00
|
|
|
// Project ID is required if using any authenticated access.
|
|
|
|
if c.ProjectID == "" && !c.WithoutAuth {
|
2023-03-08 01:32:04 +00:00
|
|
|
return fmt.Errorf("project ID is required")
|
|
|
|
}
|
|
|
|
|
2023-03-17 00:53:42 +00:00
|
|
|
// If using unauthenticated access, the project ID is not used.
|
|
|
|
if c.ProjectID != "" && c.WithoutAuth {
|
|
|
|
c.ProjectID = ""
|
|
|
|
ctx.Logger().Info("project ID is not used when using unauthenticated access, ignoring provided project ID")
|
|
|
|
}
|
|
|
|
|
2023-03-08 01:32:04 +00:00
|
|
|
connection := &sourcespb.GCS{
|
|
|
|
ProjectId: c.ProjectID,
|
|
|
|
IncludeBuckets: c.IncludeBuckets,
|
|
|
|
ExcludeBuckets: c.ExcludeBuckets,
|
|
|
|
IncludeObjects: c.IncludeObjects,
|
|
|
|
ExcludeObjects: c.ExcludeObjects,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure only one auth method is selected.
|
|
|
|
if !isAuthValid(ctx, c, connection) {
|
|
|
|
return fmt.Errorf("multiple auth methods selected, please select only one")
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn anypb.Any
|
|
|
|
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal GCS connection: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:58:38 +00:00
|
|
|
sourceName := "trufflehog - gcs"
|
2023-09-13 00:23:25 +00:00
|
|
|
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gcs.SourceType)
|
2023-09-12 23:58:38 +00:00
|
|
|
|
|
|
|
gcsSource := &gcs.Source{}
|
2023-09-14 18:28:24 +00:00
|
|
|
if err := gcsSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, int(c.Concurrency)); err != nil {
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2023-03-08 01:32:04 +00:00
|
|
|
}
|
2023-09-12 23:58:38 +00:00
|
|
|
_, err = e.sourceManager.Run(ctx, sourceName, gcsSource)
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2023-03-08 01:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isAuthValid(ctx context.Context, c sources.GCSConfig, connection *sourcespb.GCS) bool {
|
|
|
|
var isAuthSelected bool
|
|
|
|
|
|
|
|
if c.WithoutAuth {
|
|
|
|
isAuthSelected = true
|
|
|
|
connection.Credential = &sourcespb.GCS_Unauthenticated{}
|
|
|
|
}
|
|
|
|
if c.CloudCred {
|
|
|
|
if isAuthSelected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
isAuthSelected = true
|
|
|
|
connection.Credential = &sourcespb.GCS_Adc{}
|
|
|
|
}
|
|
|
|
if c.ServiceAccount != "" {
|
|
|
|
if isAuthSelected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
isAuthSelected = true
|
2023-03-16 20:04:36 +00:00
|
|
|
connection.Credential = &sourcespb.GCS_ServiceAccountFile{
|
|
|
|
ServiceAccountFile: c.ServiceAccount,
|
2023-03-08 01:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.ApiKey != "" {
|
|
|
|
if isAuthSelected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
isAuthSelected = true
|
|
|
|
connection.Credential = &sourcespb.GCS_ApiKey{
|
|
|
|
ApiKey: c.ApiKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isAuthSelected {
|
|
|
|
ctx.Logger().Info("no auth method selected, using unauthenticated")
|
|
|
|
connection.Credential = &sourcespb.GCS_Unauthenticated{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|