Skip to content

Commit

Permalink
Merge pull request #410 from cloudskiff/fix/ignore_s3_backend_directory
Browse files Browse the repository at this point in the history
Fix #409 do not list directory when using s3 as state backend
  • Loading branch information
eliecharra authored Apr 7, 2021
2 parents 07b7712 + 3075ed9 commit d81d0a9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pkg/iac/terraform/state/enumerator/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
})
Expand Down
60 changes: 54 additions & 6 deletions pkg/iac/terraform/state/enumerator/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down

0 comments on commit d81d0a9

Please sign in to comment.