2022-03-15 00:07:07 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-25 16:35:44 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-03-15 00:07:07 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
2022-08-10 17:11:13 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
2022-03-15 00:07:07 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/s3"
|
|
|
|
)
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
// ScanS3 scans S3 buckets.
|
2023-02-10 20:43:00 +00:00
|
|
|
func (e *Engine) ScanS3(ctx context.Context, c sources.S3Config) error {
|
2022-03-15 00:07:07 +00:00
|
|
|
connection := &sourcespb.S3{
|
|
|
|
Credential: &sourcespb.S3_Unauthenticated{},
|
|
|
|
}
|
2022-08-10 17:11:13 +00:00
|
|
|
if c.CloudCred {
|
2023-04-03 21:56:43 +00:00
|
|
|
if len(c.Key) > 0 || len(c.Secret) > 0 || len(c.SessionToken) > 0 {
|
|
|
|
return fmt.Errorf("cannot use cloud environment and static credentials together")
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|
|
|
|
connection.Credential = &sourcespb.S3_CloudEnvironment{}
|
|
|
|
}
|
2022-08-10 17:11:13 +00:00
|
|
|
if len(c.Key) > 0 && len(c.Secret) > 0 {
|
2023-04-03 21:56:43 +00:00
|
|
|
if len(c.SessionToken) > 0 {
|
|
|
|
connection.Credential = &sourcespb.S3_SessionToken{
|
|
|
|
SessionToken: &credentialspb.AWSSessionTokenSecret{
|
|
|
|
Key: c.Key,
|
|
|
|
Secret: c.Secret,
|
|
|
|
SessionToken: c.SessionToken,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connection.Credential = &sourcespb.S3_AccessKey{
|
|
|
|
AccessKey: &credentialspb.KeySecret{
|
|
|
|
Key: c.Key,
|
|
|
|
Secret: c.Secret,
|
|
|
|
},
|
|
|
|
}
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-10 17:11:13 +00:00
|
|
|
if len(c.Buckets) > 0 {
|
|
|
|
connection.Buckets = c.Buckets
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|
2023-08-18 00:30:20 +00:00
|
|
|
|
|
|
|
if len(c.Roles) > 0 {
|
|
|
|
connection.Roles = c.Roles
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:07:07 +00:00
|
|
|
var conn anypb.Any
|
|
|
|
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
|
|
|
|
if err != nil {
|
2023-02-09 22:55:19 +00:00
|
|
|
ctx.Logger().Error(err, "failed to marshal S3 connection")
|
2022-03-15 00:07:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:58:38 +00:00
|
|
|
sourceName := "trufflehog - s3"
|
2023-09-13 00:23:25 +00:00
|
|
|
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, s3.SourceType)
|
2023-09-12 23:58:38 +00:00
|
|
|
|
|
|
|
s3Source := &s3.Source{}
|
2023-09-14 18:28:24 +00:00
|
|
|
if err := s3Source.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|
2023-09-12 23:58:38 +00:00
|
|
|
_, err = e.sourceManager.Run(ctx, sourceName, s3Source)
|
2023-08-03 18:36:30 +00:00
|
|
|
return err
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|