Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Use S3 credentials waterfall (#1823)
Cody Rosee9efed85c266
diff --git a/pkg/sources/s3/s3.go b/pkg/sources/s3/s3.goindex d95746378..f27f54501 100644--- a/pkg/sources/s3/s3.go+++ b/pkg/sources/s3/s3.go@@ -130,20 +130,19 @@ func (s *Source) newClient(region, roleArn string) (*s3.S3, error) {cfg.CredentialsChainVerboseErrors = aws.Bool(true)cfg.Region = aws.String(region)- if roleArn == "" {- switch cred := s.conn.GetCredential().(type) {- case *sourcespb.S3_SessionToken:- cfg.Credentials = credentials.NewStaticCredentials(cred.SessionToken.Key, cred.SessionToken.Secret, cred.SessionToken.SessionToken)- case *sourcespb.S3_AccessKey:- cfg.Credentials = credentials.NewStaticCredentials(cred.AccessKey.Key, cred.AccessKey.Secret, "")- case *sourcespb.S3_Unauthenticated:- cfg.Credentials = credentials.AnonymousCredentials- case *sourcespb.S3_CloudEnvironment:- // Nothing needs to be done!- default:- return nil, errors.Errorf("invalid configuration given for %s source", s.name)- }- } else {+ switch cred := s.conn.GetCredential().(type) {+ case *sourcespb.S3_SessionToken:+ cfg.Credentials = credentials.NewStaticCredentials(cred.SessionToken.Key, cred.SessionToken.Secret, cred.SessionToken.SessionToken)+ case *sourcespb.S3_AccessKey:+ cfg.Credentials = credentials.NewStaticCredentials(cred.AccessKey.Key, cred.AccessKey.Secret, "")+ case *sourcespb.S3_Unauthenticated:+ cfg.Credentials = credentials.AnonymousCredentials+ default:+ // In all other cases, the AWS SDK will follow its normal waterfall logic to pick up credentials (i.e. they can+ // come from the environment or the credentials file or whatever else AWS gets up to).+ }++ if roleArn != "" {sess, err := session.NewSession(cfg)if err != nil {return nil, errdiff --git a/pkg/sources/s3/s3_integration_test.go b/pkg/sources/s3/s3_integration_test.goindex 3764caee5..12d72376c 100644--- a/pkg/sources/s3/s3_integration_test.go+++ b/pkg/sources/s3/s3_integration_test.go@@ -141,14 +141,11 @@ func TestSource_Validate(t *testing.T) {var cancelOnce sync.Oncedefer cancelOnce.Do(cancel)- // These are used by the tests that assume roles- t.Setenv("AWS_ACCESS_KEY_ID", s3key)- t.Setenv("AWS_SECRET_ACCESS_KEY", s3secret)-s := &Source{}+ // As of this writing, credentials set in the environment or the on-disk credentials file also work, but I+ // couldn't figure out how to write automated tests for those cases that weren't ugly as sin.conn, err := anypb.New(&sourcespb.S3{- // These are used by the tests that don't assume rolesCredential: &sourcespb.S3_AccessKey{AccessKey: &credentialspb.KeySecret{Key: s3key,