diff --git a/pkg/iac/terraform/state/enumerator/s3.go b/pkg/iac/terraform/state/enumerator/s3.go index ae89f688..11751963 100644 --- a/pkg/iac/terraform/state/enumerator/s3.go +++ b/pkg/iac/terraform/state/enumerator/s3.go @@ -40,10 +40,7 @@ func (s *S3Enumerator) Enumerate() ([]string, error) { } bucket := bucketPath[0] prefix := strings.Join(bucketPath[1:], "/") - - if !HasMeta(prefix) { - prefix = filepath.Join(prefix, "*") - } + hasGlob := HasMeta(prefix) prefix, pattern, err := GlobS3(prefix) if err != nil { @@ -64,7 +61,7 @@ func (s *S3Enumerator) Enumerate() ([]string, error) { for _, metadata := range output.Contents { if aws.Int64Value(metadata.Size) > 0 { key := *metadata.Key - if match, _ := filepath.Match(filepath.Join(prefix, pattern), key); match { + if match, _ := filepath.Match(filepath.Join(prefix, pattern), key); !hasGlob || match { files = append(files, filepath.Join(bucket, key)) } } diff --git a/pkg/iac/terraform/state/enumerator/s3_test.go b/pkg/iac/terraform/state/enumerator/s3_test.go index c323864a..205d0fd2 100644 --- a/pkg/iac/terraform/state/enumerator/s3_test.go +++ b/pkg/iac/terraform/state/enumerator/s3_test.go @@ -57,11 +57,11 @@ func TestS3Enumerator_Enumerate(t *testing.T) { Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state5"), + Key: awssdk.String("a/nested/prefix/folder1/state5"), Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state6"), + Key: awssdk.String("a/nested/prefix/folder2/subfolder1/state6"), Size: awssdk.Int64(5), }, }, @@ -75,8 +75,8 @@ func TestS3Enumerator_Enumerate(t *testing.T) { "bucket-name/a/nested/prefix/state2", "bucket-name/a/nested/prefix/state3", "bucket-name/a/nested/prefix/state4", - "bucket-name/a/nested/prefix/state5", - "bucket-name/a/nested/prefix/state6", + "bucket-name/a/nested/prefix/folder1/state5", + "bucket-name/a/nested/prefix/folder2/subfolder1/state6", }, }, { @@ -96,26 +96,32 @@ func TestS3Enumerator_Enumerate(t *testing.T) { callback(&s3.ListObjectsV2Output{ Contents: []*s3.Object{ { - Key: awssdk.String("a/nested/prefix/1/state1.tfstate"), + Key: awssdk.String("a/nested/prefix/1/state1.tfstate"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/2/state2.tfstate"), + Key: awssdk.String("a/nested/prefix/2/state2.tfstate"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state3.tfstate"), + Key: awssdk.String("a/nested/prefix/state3.tfstate"), + Size: awssdk.Int64(5), }, }, }, false) callback(&s3.ListObjectsV2Output{ Contents: []*s3.Object{ { - Key: awssdk.String("a/nested/prefix/4/4/state4.tfstate"), + Key: awssdk.String("a/nested/prefix/4/4/state4.tfstate"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state5.state"), + Key: awssdk.String("a/nested/prefix/state5.state"), + Size: awssdk.Int64(5), }, { - Key: awssdk.String("a/nested/prefix/state6.tfstate.backup"), + Key: awssdk.String("a/nested/prefix/state6.tfstate.backup"), + Size: awssdk.Int64(5), }, }, }, true) @@ -123,9 +129,62 @@ func TestS3Enumerator_Enumerate(t *testing.T) { }), ).Return(nil) }, - want: []string{}, + want: nil, err: "** not supported for S3 pattern", }, + { + name: "test results with simple glob", + config: config.SupplierConfig{ + Path: "bucket-name/a/nested/prefix/*.tfstate", + }, + 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/1/state1.tfstate"), + Size: awssdk.Int64(5), + }, + { + Key: awssdk.String("a/nested/prefix/2/state2.tfstate"), + Size: awssdk.Int64(5), + }, + { + Key: awssdk.String("a/nested/prefix/state3.tfstate"), + Size: awssdk.Int64(5), + }, + }, + }, false) + callback(&s3.ListObjectsV2Output{ + Contents: []*s3.Object{ + { + Key: awssdk.String("a/nested/prefix/4/4/state4.tfstate"), + Size: awssdk.Int64(5), + }, + { + Key: awssdk.String("a/nested/prefix/state5.state"), + Size: awssdk.Int64(5), + }, + { + Key: awssdk.String("a/nested/prefix/state6.tfstate.backup"), + Size: awssdk.Int64(5), + }, + }, + }, true) + return true + }), + ).Return(nil) + }, + want: []string{"bucket-name/a/nested/prefix/state3.tfstate"}, + err: "", + }, { name: "test when invalid config used", config: config.SupplierConfig{ @@ -169,6 +228,7 @@ func TestS3Enumerator_Enumerate(t *testing.T) { got, err := s.Enumerate() if err != nil && err.Error() != tt.err { t.Fatalf("Expected error '%s', got '%s'", tt.err, err.Error()) + return } if err != nil && tt.err == "" { t.Fatalf("Expected error '%s' but got nil", tt.err)