From 1ee31b4679642245ff934e53b8bbe27e9e8bc4a0 Mon Sep 17 00:00:00 2001 From: Martin Guibert Date: Wed, 7 Apr 2021 11:01:16 +0200 Subject: [PATCH] Fix #409 - Ignore directory (size <= 0) when listing bucket object - Add test for empty directory --- pkg/iac/terraform/state/enumerator/s3.go | 5 +- pkg/iac/terraform/state/enumerator/s3_test.go | 60 +++++++++++++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/pkg/iac/terraform/state/enumerator/s3.go b/pkg/iac/terraform/state/enumerator/s3.go index 38850f3a..ff372453 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 13610f06..8152824e 100644 --- a/pkg/iac/terraform/state/enumerator/s3_test.go +++ b/pkg/iac/terraform/state/enumerator/s3_test.go @@ -37,26 +37,32 @@ func TestS3Enumerator_Enumerate(t *testing.T) { callback(&s3.ListObjectsV2Output{ Contents: []*s3.Object{ { - Key: awssdk.String("a/nested/prefix/state1"), + Key: awssdk.String("a/nested/prefix/state1"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state2"), + Key: awssdk.String("a/nested/prefix/state2"), + Size: awssdk.Int64(2), }, { - Key: awssdk.String("a/nested/prefix/state3"), + Key: awssdk.String("a/nested/prefix/state3"), + Size: awssdk.Int64(1), }, }, }, false) callback(&s3.ListObjectsV2Output{ Contents: []*s3.Object{ { - Key: awssdk.String("a/nested/prefix/state4"), + Key: awssdk.String("a/nested/prefix/state4"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state5"), + Key: awssdk.String("a/nested/prefix/state5"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state6"), + 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{