diff --git a/pkg/iac/terraform/state/enumerator/s3.go b/pkg/iac/terraform/state/enumerator/s3.go index 38850f3aa..ff372453a 100644 --- a/pkg/iac/terraform/state/enumerator/s3.go +++ b/pkg/iac/terraform/state/enumerator/s3.go @@ -3,6 +3,7 @@ package enumerator import ( "strings" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3iface" @@ -45,7 +46,9 @@ func (s *S3Enumerator) Enumerate() ([]string, error) { } err := s.client.ListObjectsV2Pages(input, func(output *s3.ListObjectsV2Output, lastPage bool) bool { for _, metadata := range output.Contents { - keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/")) + if aws.Int64Value(metadata.Size) > 0 { + keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/")) + } } return !lastPage }) diff --git a/pkg/iac/terraform/state/enumerator/s3_test.go b/pkg/iac/terraform/state/enumerator/s3_test.go index 13610f06a..3b8c09e05 100644 --- a/pkg/iac/terraform/state/enumerator/s3_test.go +++ b/pkg/iac/terraform/state/enumerator/s3_test.go @@ -38,12 +38,15 @@ func TestS3Enumerator_Enumerate(t *testing.T) { Contents: []*s3.Object{ { Key: awssdk.String("a/nested/prefix/state1"), + Size: awssdk.Int64(5), }, { Key: awssdk.String("a/nested/prefix/state2"), + Size: awssdk.Int64(2), }, { Key: awssdk.String("a/nested/prefix/state3"), + Size: awssdk.Int64(1), }, }, }, false) @@ -51,12 +54,15 @@ func TestS3Enumerator_Enumerate(t *testing.T) { Contents: []*s3.Object{ { Key: awssdk.String("a/nested/prefix/state4"), + Size: awssdk.Int64(5), }, { Key: awssdk.String("a/nested/prefix/state5"), + Size: awssdk.Int64(5), }, { Key: awssdk.String("a/nested/prefix/state6"), + Size: awssdk.Int64(5), }, }, }, true) @@ -73,6 +79,48 @@ func TestS3Enumerator_Enumerate(t *testing.T) { "bucket-name/a/nested/prefix/state6", }, }, + { + name: "test that directories objects are filtered", + config: config.SupplierConfig{ + Path: "bucket-name/a/nested/prefix", + }, + mocks: func(client *mocks.FakeS3) { + input := &s3.ListObjectsV2Input{ + Bucket: awssdk.String("bucket-name"), + Prefix: awssdk.String("a/nested/prefix"), + } + client.On( + "ListObjectsV2Pages", + input, + mock.MatchedBy(func(callback func(res *s3.ListObjectsV2Output, lastPage bool) bool) bool { + callback(&s3.ListObjectsV2Output{ + Contents: []*s3.Object{ + { + Key: awssdk.String("a/nested/prefix/state1"), + Size: awssdk.Int64(0), + }, + { + Key: awssdk.String("a/nested/prefix/state2"), + Size: nil, + }, + { + Key: awssdk.String("a/nested/prefix/state3"), + Size: awssdk.Int64(-1), + }, + { + Key: awssdk.String("a/nested/prefix/state4"), + Size: awssdk.Int64(1), + }, + }, + }, true) + return true + }), + ).Return(nil) + }, + want: []string{ + "bucket-name/a/nested/prefix/state4", + }, + }, { name: "test when invalid config used", config: config.SupplierConfig{