2022-03-15 00:07:07 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-25 16:35:44 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-03-15 00:07:07 +00:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
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/common"
|
|
|
|
"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.
|
2022-08-12 16:56:24 +00:00
|
|
|
func (e *Engine) ScanS3(ctx context.Context, c sources.Config) 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 {
|
|
|
|
if len(c.Key) > 0 || len(c.Secret) > 0 {
|
2022-03-15 00:07:07 +00:00
|
|
|
return fmt.Errorf("cannot use cloud credentials and basic auth together")
|
|
|
|
}
|
|
|
|
connection.Credential = &sourcespb.S3_CloudEnvironment{}
|
|
|
|
}
|
2022-08-10 17:11:13 +00:00
|
|
|
if len(c.Key) > 0 && len(c.Secret) > 0 {
|
2022-03-15 00:07:07 +00:00
|
|
|
connection.Credential = &sourcespb.S3_AccessKey{
|
|
|
|
AccessKey: &credentialspb.KeySecret{
|
2022-08-10 17:11:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
var conn anypb.Any
|
|
|
|
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
|
|
|
|
if err != nil {
|
2023-01-06 05:44:37 +00:00
|
|
|
logrus.WithError(err).Error("failed to marshal S3 connection")
|
2022-03-15 00:07:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Source := s3.Source{}
|
2022-05-13 21:35:06 +00:00
|
|
|
err = s3Source.Init(ctx, "trufflehog - s3", 0, int64(sourcespb.SourceType_SOURCE_TYPE_S3), true, &conn, runtime.NumCPU())
|
2022-03-15 00:07:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WrapPrefix(err, "failed to init S3 source", 0)
|
|
|
|
}
|
2022-05-25 16:35:44 +00:00
|
|
|
|
|
|
|
e.sourcesWg.Add(1)
|
2022-03-15 00:07:07 +00:00
|
|
|
go func() {
|
2022-09-22 14:01:10 +00:00
|
|
|
defer common.RecoverWithExit(ctx)
|
2022-05-25 16:35:44 +00:00
|
|
|
defer e.sourcesWg.Done()
|
2022-03-15 00:07:07 +00:00
|
|
|
err := s3Source.Chunks(ctx, e.ChunksChan())
|
|
|
|
if err != nil {
|
2023-01-06 05:44:37 +00:00
|
|
|
logrus.WithError(err).Error("error scanning S3")
|
2022-03-15 00:07:07 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|